Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "Open in" submenu when running as Flatpak #1512

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ namespace Scratch {
public class Application : Gtk.Application {
public string data_home_folder_unsaved { get { return _data_home_folder_unsaved; } }
public string default_font { get; set; }
public bool is_running_in_flatpak { get; construct; }

private static string _data_home_folder_unsaved;
private static bool create_new_tab = false;
private static bool create_new_window = false;

private LocationJumpManager location_jump_manager;

const OptionEntry[] ENTRIES = {
Expand All @@ -48,7 +51,6 @@ namespace Scratch {
_data_home_folder_unsaved = Path.build_filename (
Environment.get_user_data_dir (), Constants.PROJECT_NAME, "unsaved"
);

}

construct {
Expand All @@ -60,6 +62,7 @@ namespace Scratch {
application_id += "." + Constants.BRANCH.replace ("/", ".").replace ("-", "_");
}

is_running_in_flatpak = FileUtils.test ("/.flatpak-info", FileTest.IS_REGULAR);
add_main_option_entries (ENTRIES);

// Init settings
Expand Down
17 changes: 2 additions & 15 deletions src/FolderManager/FileView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -419,20 +419,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane

private void action_launch_app_with_file_path (SimpleAction action, Variant? param) {
var params = param.get_strv ();
var path = params[0];
if (path == null || path == "") {
return;
}

var app_id = params[1];
if (app_id == null || app_id == "") {
return;
}

var app_info = new GLib.DesktopAppInfo (app_id);
var file = GLib.File.new_for_path (path);

Utils.launch_app_with_file (app_info, file);
Utils.launch_app_with_file (params[1], params[0]);
}

private void action_show_app_chooser (SimpleAction action, Variant? param) {
Expand All @@ -449,7 +436,7 @@ public class Scratch.FolderManager.FileView : Code.Widgets.SourceList, Code.Pane
if (dialog.run () == Gtk.ResponseType.OK) {
var app_info = dialog.get_app_info ();
if (app_info != null) {
Utils.launch_app_with_file (app_info, file);
Utils.launch_app_with_file (app_info.get_id (), path);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/FolderManager/ProjectFolderItem.vala
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ namespace Scratch.FolderManager {
}

public override Gtk.Menu? get_context_menu () {
GLib.FileInfo info = null;
unowned string? file_type = null;

string file_type = "";
try {
info = file.file.query_info (GLib.FileAttribute.STANDARD_CONTENT_TYPE, GLib.FileQueryInfoFlags.NONE);
file_type = info.get_content_type ();
var info = file.file.query_info (GLib.FileAttribute.STANDARD_CONTENT_TYPE, GLib.FileQueryInfoFlags.NONE);
if (info.has_attribute (GLib.FileAttribute.STANDARD_CONTENT_TYPE)) {
file_type = info.get_content_type ();
}
} catch (Error e) {
warning (e.message);
}
Expand Down
82 changes: 59 additions & 23 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -201,47 +201,83 @@ namespace Scratch.Utils {
return false;
}

public GLib.Menu create_executable_app_items_for_file (GLib.File file, string file_type) {
var external_apps = GLib.AppInfo.get_all_for_type (file_type);
var this_id = GLib.Application.get_default ().application_id + ".desktop";
public GLib.Menu? create_executable_app_items_for_file (GLib.File file, string file_type) {
var scratch_app = (Scratch.Application) (GLib.Application.get_default ());
var this_id = scratch_app.application_id + ".desktop";
var menu = new GLib.Menu ();

external_apps.sort ((a, b) => {
return a.get_name ().collate (b.get_name ());
});

foreach (AppInfo app_info in external_apps) {
string app_id = app_info.get_id ();
if (app_id == this_id) {
continue;
}

if (scratch_app.is_running_in_flatpak) {
var menu_item = new MenuItem (
app_info.get_name (),
///TRANSLATORS '%s' represents the quoted basename of a uri to be opened with the default app
_("Show '%s' with default app").printf (file.get_basename ()),
GLib.Action.print_detailed_name (
Scratch.FolderManager.FileView.ACTION_PREFIX
+ Scratch.FolderManager.FileView.ACTION_LAUNCH_APP_WITH_FILE_PATH,
new GLib.Variant.array (
GLib.VariantType.STRING,
{ file.get_path (), app_id }
{ file.get_path (), "" }
)
)
);
menu_item.set_icon (app_info.get_icon ());
menu.append_item (menu_item);
} else {
List<AppInfo> external_apps = null;
if (file_type == "") {
var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true);
external_apps.prepend (files_appinfo);
} else {
external_apps = GLib.AppInfo.get_all_for_type (file_type);
external_apps.sort ((a, b) => {
return a.get_name ().collate (b.get_name ());
});
}

foreach (AppInfo app_info in external_apps) {
string app_id = app_info.get_id ();
if (app_id == this_id) {
continue;
}

var menu_item = new MenuItem (
app_info.get_name (),
GLib.Action.print_detailed_name (
Scratch.FolderManager.FileView.ACTION_PREFIX
+ Scratch.FolderManager.FileView.ACTION_LAUNCH_APP_WITH_FILE_PATH,
new GLib.Variant.array (
GLib.VariantType.STRING,
{ file.get_path (), app_id }
)
)
);
menu_item.set_icon (app_info.get_icon ());
menu.append_item (menu_item);
}
}

return menu;
}

public void launch_app_with_file (AppInfo app_info, GLib.File file) {
var file_list = new List<GLib.File> ();
file_list.append (file);
public void launch_app_with_file (string app_id, string path) {
var scratch_app = (Scratch.Application) (GLib.Application.get_default ());
if (scratch_app.is_running_in_flatpak || app_id == "") {
var uri = Uri.join (UriFlags.NONE, "file", null, null, -1, path, null, null);

try {
app_info.launch (file_list, null);
} catch (Error e) {
warning (e.message);
try {
Gtk.show_uri_on_window (scratch_app.get_active_window (), uri, Gdk.CURRENT_TIME);
} catch (Error e) {
warning ("Error showing uri %s, %s", uri, e.message);
}
} else {
var app_info = new GLib.DesktopAppInfo (app_id);
var file = GLib.File.new_for_path (path);
var file_list = new List<GLib.File> ();
file_list.append (file);

try {
app_info.launch (file_list, null);
} catch (Error e) {
warning (e.message);
}
}
}

Expand Down