Skip to content

Commit

Permalink
Fix "Open in" for Flatpak running
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Wootten authored and Jeremy Wootten committed Jan 20, 2025
1 parent 43ab8a1 commit d680ede
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
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 @@ -408,20 +408,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 @@ -438,7 +425,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
78 changes: 56 additions & 22 deletions src/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -201,46 +201,80 @@ namespace Scratch.Utils {
return false;
}

public GLib.Menu create_executable_app_items_for_file (GLib.File file, string file_type) {
List<AppInfo> external_apps = GLib.AppInfo.get_all_for_type (file_type);
var files_appinfo = AppInfo.get_default_for_type ("inode/directory", true);
external_apps.prepend (files_appinfo);

string 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 ();

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);
}

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

0 comments on commit d680ede

Please sign in to comment.