[gnome-documents/wip/lokdocview-rebase: 8/9] lokview: Add "Copy to the Clipboard" functionality



commit ed5e7090a99eed984bf3f37204e9e47c523e6668
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Dec 10 13:34:18 2015 +0100

    lokview: Add "Copy to the Clipboard" functionality
    
    https://bugzilla.gnome.org/show_bug.cgi?id=753686

 src/lokview.js |   43 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 43 insertions(+), 0 deletions(-)
---
diff --git a/src/lokview.js b/src/lokview.js
index 2ad2ac4..69f232b 100644
--- a/src/lokview.js
+++ b/src/lokview.js
@@ -113,6 +113,11 @@ const LOKView = new Lang.Class({
         this.add_named(this._sw, 'view');
         this._createView();
 
+        // create context menu
+        let model = this._getPreviewContextMenu();
+        this._previewContextMenu = Gtk.Menu.new_from_model(model);
+        this._previewContextMenu.attach_to_widget(this._sw, null);
+
         this.show_all();
 
         this._zoomIn = Application.application.lookup_action('zoom-in');
@@ -129,6 +134,9 @@ const LOKView = new Lang.Class({
                 this.view.set_zoom(zoomLevel / 2);
             }));
 
+        this._copy = Application.application.lookup_action('copy');
+        let copyId = this._copy.connect('activate', Lang.bind(this, this._onCopyActivated));
+
         Application.documentManager.connect('load-started',
                                             Lang.bind(this, this._onLoadStarted));
         Application.documentManager.connect('load-error',
@@ -138,9 +146,18 @@ const LOKView = new Lang.Class({
            function() {
                this._zoomIn.disconnect(zoomInId);
                this._zoomOut.disconnect(zoomOutId);
+               this._copy.disconnect(copyId);
            }));
     },
 
+    _onCopyActivated: function() {
+        let [selectedText, mimeType] = this.view.copy_selection('text/plain;charset=utf-8');
+        let display = Gdk.Display.get_default();
+        let clipboard = Gtk.Clipboard.get_default(display);
+
+        clipboard.set_text(selectedText, selectedText.length);
+    },
+
     _onLoadStarted: function(manager, doc) {
         if (doc.viewType != Documents.ViewType.LOK)
             return;
@@ -149,6 +166,7 @@ const LOKView = new Lang.Class({
         let file = Gio.File.new_for_uri (doc.uri);
         let location = file.get_path();
         this._doc = doc;
+        this._copy.enabled = false;
         this.view.open_document(location, "{}", null, Lang.bind(this, this.open_document_cb));
         this._progressBar.show();
     },
@@ -179,6 +197,7 @@ const LOKView = new Lang.Class({
             return;
         this.view.reset_view();
         this.set_visible_child_full('view', Gtk.StackTransitionType.NONE);
+        this._copy.enabled = false;
     },
 
     _createView: function() {
@@ -186,17 +205,41 @@ const LOKView = new Lang.Class({
             this.view = LOKDocView.View.new(null, null, null);
             this._sw.add(this.view);
             this.view.show();
+            this.view.connect('button-press-event', Lang.bind(this, this._onButtonPressEvent));
             this.view.connect('load-changed', Lang.bind(this, this._onProgressChanged));
+            this.view.connect('text-selection', Lang.bind(this, this._onTextSelection));
         }
 
         this._navControls = new LOKViewNavControls(this, this._overlay);
         this.set_visible_child_full('view', Gtk.StackTransitionType.NONE);
     },
 
+    _getPreviewContextMenu: function() {
+        let builder = new Gtk.Builder();
+        builder.add_from_resource('/org/gnome/Documents/ui/preview-context-menu.ui');
+        return builder.get_object('preview-context-menu');
+    },
+
+    _onButtonPressEvent: function(widget, event) {
+        let button = event.get_button()[1];
+
+        if (button == 3) {
+            let time = event.get_time();
+            this._previewContextMenu.popup(null, null, null, button, time);
+            return true;
+        }
+
+        return false;
+   },
+
     _onProgressChanged: function() {
         this._progressBar.fraction = this.view.load_progress;
     },
 
+    _onTextSelection: function(hasSelection) {
+        this._copy.enabled = hasSelection;
+    },
+
     _setError: function(primary, secondary) {
         this._errorBox.update(primary, secondary);
         this.set_visible_child_name('error');


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