[sushi/wip/cosimoc/no-clutter: 47/50] Add "Open With" as a general feature



commit a8fb5743711c1473b07f4a54978954526225f859
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sat Jun 15 18:52:14 2019 -0700

    Add "Open With" as a general feature
    
    Instead of having it only for certain viewers.
    We add a button on the titlebar for that purpose.

 src/ui/mainWindow.js | 40 ++++++++++++++++++++++++++++++++++++++++
 src/ui/utils.js      | 16 ----------------
 src/viewers/html.js  | 11 +----------
 src/viewers/text.js  | 10 +---------
 4 files changed, 42 insertions(+), 35 deletions(-)
---
diff --git a/src/ui/mainWindow.js b/src/ui/mainWindow.js
index afa9b71..a6d6cd2 100644
--- a/src/ui/mainWindow.js
+++ b/src/ui/mainWindow.js
@@ -78,6 +78,10 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
                                              decoration_layout: 'menu:close' });
         this.set_titlebar(this._titlebar);
 
+        this._openButton = new Gtk.Button();
+        this._openButton.connect('clicked', this._onFileOpenClicked.bind(this));
+        this._titlebar.pack_end(this._openButton);
+
         this.connect('delete-event', this._onDeleteEvent.bind(this));
         this.connect('destroy', this._onDestroy.bind(this));
         this.connect('key-press-event', this._onKeyPressEvent.bind(this));
@@ -290,6 +294,41 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
         return false;
     }
 
+    _updateTitlebar() {
+        try {
+            let appInfo = this.file.query_default_handler(null);
+            // TRANSLATORS: This is the display name of an application, e.g. "Open With Image Viewer"
+            this._openButton.set_label(_("Open With %s").format(appInfo.get_display_name()));
+        } catch (e) {
+            // This happens when running under flatpak, since we don't have direct access
+            // to the other applications
+            if (!e.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.NOT_SUPPORTED))
+                logError(e, `Failed to query default handler for ${this.file.get_uri()}`);
+            this._openButton.set_label(_("Open"));
+        }
+    }
+
+    _onFileOpenClicked() {
+        let ctx = this.get_display().get_app_launch_context();
+        ctx.set_timestamp(Gtk.get_current_event_time());
+        ctx.set_screen(this.get_screen());
+
+        // Ideally we would use gtk_show_uri_on_window() here, since it properly
+        // parents dialogs that may come from a flatpak portal over the window.
+        //
+        // Unfortunately we need to wait until the result of the launch before
+        // destroying our window, which gtk_show_uri_on_window() doesn't allow,
+        // so we use GIO directly.
+        Gio.AppInfo.launch_default_for_uri_async(this.file.get_uri(), ctx, null, (obj, result) => {
+            try {
+                Gio.AppInfo.launch_default_for_uri_finish(result);
+                this.destroy();
+            } catch (e) {
+                logError(e, `Failed to launch default handler for ${this.file.get_uri()}`);
+            }
+        });
+    }
+
     /**************************************************************************
      ************************ public methods **********************************
      **************************************************************************/
@@ -305,6 +344,7 @@ var MainWindow = GObject.registerClass(class MainWindow extends Gtk.Window {
 
     setFile(file) {
         this.file = file;
+        this._updateTitlebar();
         this._createRenderer(file);
     }
 
diff --git a/src/ui/utils.js b/src/ui/utils.js
index 77a3db9..e203c05 100644
--- a/src/ui/utils.js
+++ b/src/ui/utils.js
@@ -75,19 +75,3 @@ function createFullscreenButton(renderer) {
             button.icon_name = 'view-fullscreen-symbolic';
     });
 }
-
-function createOpenButton(file, mainWindow) {
-    return createToolButton('document-open-symbolic', function(widget) {
-        let timestamp = Gtk.get_current_event_time();
-        try {
-            Gtk.show_uri(widget.get_screen(),
-                         file.get_uri(),
-                         timestamp);
-
-            mainWindow.destroy();
-        } catch (e) {
-            log('Unable to execute the default application for ' +
-                file.get_uri() + ' : ' + e.toString());
-        }
-    });
-}
diff --git a/src/viewers/html.js b/src/viewers/html.js
index 119c753..2f10093 100644
--- a/src/viewers/html.js
+++ b/src/viewers/html.js
@@ -39,12 +39,9 @@ var Klass = GObject.registerClass({
                                          false)
     },
 }, class HTMLRenderer extends WebKit2.WebView {
-    _init(file, mainWindow) {
+    _init(file) {
         super._init();
 
-        this._mainWindow = mainWindow;
-        this._file = file;
-
         /* disable the default context menu of the web view */
         this.connect('context-menu',
                      function() {return true;});
@@ -60,12 +57,6 @@ var Klass = GObject.registerClass({
     populateToolbar(toolbar) {
         let toolbarZoom = Utils.createFullscreenButton(this);
         toolbar.add(toolbarZoom);
-
-        let separator = new Gtk.Separator({ orientation: Gtk.Orientation.VERTICAL });
-        toolbar.add(separator);
-
-        let toolbarRun = Utils.createOpenButton(this._file, this._mainWindow);
-        toolbar.add(toolbarRun);
     }
 });
 
diff --git a/src/viewers/text.js b/src/viewers/text.js
index 4189eec..bed0960 100644
--- a/src/viewers/text.js
+++ b/src/viewers/text.js
@@ -53,12 +53,9 @@ var Klass = GObject.registerClass({
                                          false)
     },
 }, class TextRenderer extends Gtk.ScrolledWindow {
-    _init(file, mainWindow) {
+    _init(file) {
         super._init();
 
-        this._mainWindow = mainWindow;
-        this._file = file;
-
         let textLoader = new Sushi.TextLoader();
         textLoader.connect('loaded', this._onBufferLoaded.bind(this));
         textLoader.uri = file.get_uri();
@@ -95,11 +92,6 @@ var Klass = GObject.registerClass({
     get moveOnClick() {
         return false;
     }
-
-    populateToolbar(toolbar) {
-        let toolbarRun = Utils.createOpenButton(this._file, this._mainWindow);
-        toolbar.add(toolbarRun);
-    }
 });
 
 // register for text/plain and let the mime handler call us for child types


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]