[dconf-editor] Add a "Copy current path" option.



commit d235687988ee0afee15744a8d08d7dc5404a6d7c
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Dec 3 11:57:19 2015 +0100

    Add a "Copy current path" option.

 editor/dconf-editor.ui       |   29 ++++++++++++++++++--
 editor/dconf-editor.vala     |   50 +++++++++++++++++++++++++++++++++++
 editor/dconf-window.vala     |   21 ++++++++++-----
 editor/key-list-box-row.vala |   60 ++++++++---------------------------------
 4 files changed, 102 insertions(+), 58 deletions(-)
---
diff --git a/editor/dconf-editor.ui b/editor/dconf-editor.ui
index d4eaabf..cb9a0d7 100644
--- a/editor/dconf-editor.ui
+++ b/editor/dconf-editor.ui
@@ -17,10 +17,33 @@
         <property name="show-close-button">True</property>
         <property name="title" translatable="yes">dconf Editor</property>
         <child>
+          <object class="GtkMenuButton" id="info_button">
+            <property name="visible">True</property>
+            <signal name="clicked" handler="on_menu_button_clicked"/>
+            <style>
+              <class name="image-button"/>
+            </style>
+            <child internal-child="accessible">
+              <object class="AtkObject">
+                <property name="AtkObject::accessible-name" translatable="yes">Actions</property> <!-- TODO 
1/3 Informations -->
+                <property name="AtkObject::accessible-description" translatable="yes">Current view 
actions</property> <!-- TODO 2/3 About the current view -->
+              </object>
+            </child>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon-name">open-menu-symbolic</property> <!-- TODO 3/3 
dialog-information-symbolic -->
+                <property name="icon-size">1</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack-type">end</property>
+          </packing>
+        </child>
+        <child>
           <object class="GtkToggleButton" id="search_button">
             <property name="visible">True</property>
-            <property name="valign">center</property>
-            <property name="sensitive">True</property>
             <property name="active" bind-source="search_bar" bind-property="search-mode-enabled" 
bind-flags="bidirectional|sync-create"/>
             <!-- <accelerator key="F" signal="toggled" modifiers="GDK_CONTROL_MASK"/> TODO -->
             <style>
@@ -48,7 +71,7 @@
           <object class="Bookmarks" id="bookmarks_button">
             <property name="visible">True</property>
             <property name="schema-id">ca.desrt.dconf-editor.Settings</property>
-            <signal name="clicked" handler="on_bookmarks_button_clicked"/>
+            <signal name="clicked" handler="on_menu_button_clicked"/>
             <signal name="bookmark_activated" handler="scroll_to_path"/>
             <style>
               <class name="image-button"/> <!-- TODO https://bugzilla.gnome.org/show_bug.cgi?id=756731 -->
diff --git a/editor/dconf-editor.vala b/editor/dconf-editor.vala
index 3d17ae1..ae3b9bb 100644
--- a/editor/dconf-editor.vala
+++ b/editor/dconf-editor.vala
@@ -25,6 +25,10 @@ class ConfigurationEditor : Gtk.Application
 
     private const GLib.ActionEntry [] action_entries =
     {
+        // generic
+        { "copy", copy_cb, "s" },   // TODO is that really the good way to do things? (see Taquin)
+
+        // app-menu
         { "about", about_cb },
         { "quit", quit_cb }
     };
@@ -80,6 +84,52 @@ class ConfigurationEditor : Gtk.Application
     }
 
     /*\
+    * * Copy action
+    \*/
+
+    private Notification notification = new Notification (_("Copied to clipboard"));
+    private bool notification_active = false;
+    private uint notification_number;
+
+    private void copy_cb (SimpleAction action, Variant? gvariant)
+    {
+        if (gvariant == null)
+            return;
+        copy (((!) gvariant).get_string ());
+    }
+
+    public void copy (string text)
+    {
+        // clipboard
+        Gdk.Display? display = Gdk.Display.get_default ();
+        if (display == null)
+            return;
+
+        Gtk.Clipboard clipboard = Gtk.Clipboard.get_default ((!) display);
+        clipboard.set_text (text, text.length);
+
+        // notification
+        if (notification_active == true)
+        {
+            Source.remove (notification_number);    // FIXME doesn't work [as expected], timeout runs until 
its end and withdraws the notification then
+            notification_active = false;
+        }
+
+        notification_number = Timeout.add_seconds (30, () => {
+                if (notification_active == false)
+                    return Source.CONTINUE;
+                withdraw_notification ("copy");
+                notification_active = false;
+                return Source.REMOVE;
+            });
+        notification_active = true;
+
+        notification.set_body (text);
+        withdraw_notification ("copy");             // TODO report bug: Shell cancels previous notification 
of the same name, instead of replacing it
+        send_notification ("copy", notification);
+    }
+
+    /*\
     * * App-menu callbacks
     \*/
 
diff --git a/editor/dconf-window.vala b/editor/dconf-window.vala
index 517e91d..836ded6 100644
--- a/editor/dconf-window.vala
+++ b/editor/dconf-window.vala
@@ -38,6 +38,8 @@ class DConfWindow : ApplicationWindow
     [GtkChild] private SearchEntry search_entry;
     [GtkChild] private Button search_next_button;
 
+    [GtkChild] private MenuButton info_button;
+
     public DConfWindow ()
     {
         set_default_size (settings.get_int ("window-width"), settings.get_int ("window-height"));
@@ -135,6 +137,10 @@ class DConfWindow : ApplicationWindow
             key_model = model.get_directory (iter).key_model;
             current_path = model.get_directory (iter).full_name;
             bookmarks_button.current_path = current_path;
+
+            GLib.Menu menu = new GLib.Menu ();
+            menu.append (_("Copy current path"), "app.copy('" + current_path + "')");   // TODO protection 
against some chars in text? 1/2
+            info_button.set_menu_model ((MenuModel) menu);
         }
 
         key_list_box.bind_model (key_model, new_list_box_row);
@@ -153,7 +159,6 @@ class DConfWindow : ApplicationWindow
                 if (dir.full_name == full_name)
                 {
                     select_dir (iter);
-                    bookmarks_button.current_path = full_name;
                     return true;
                 }
             }
@@ -173,7 +178,7 @@ class DConfWindow : ApplicationWindow
     {
         if (((Key) item).has_schema)
         {
-            KeyListBoxRowEditable key_list_box_row = new KeyListBoxRowEditable ((Window) this, 
(GSettingsKey) item);
+            KeyListBoxRowEditable key_list_box_row = new KeyListBoxRowEditable ((GSettingsKey) item);
             key_list_box_row.button_press_event.connect (on_button_pressed);
             key_list_box_row.show_dialog.connect (() => {
                     KeyEditor key_editor = new KeyEditor ((GSettingsKey) item);
@@ -184,7 +189,7 @@ class DConfWindow : ApplicationWindow
         }
         else
         {
-            KeyListBoxRowEditableNoSchema key_list_box_row = new KeyListBoxRowEditableNoSchema ((Window) 
this, (DConfKey) item);
+            KeyListBoxRowEditableNoSchema key_list_box_row = new KeyListBoxRowEditableNoSchema ((DConfKey) 
item);
             key_list_box_row.button_press_event.connect (on_button_pressed);
             key_list_box_row.show_dialog.connect (() => {
                     KeyEditorNoSchema key_editor = new KeyEditorNoSchema ((DConfKey) item);
@@ -226,26 +231,28 @@ class DConfWindow : ApplicationWindow
                 case "f":
                     if (bookmarks_button.active)
                         bookmarks_button.active = false;
+                    if (info_button.active)
+                        info_button.active = false;
                     search_bar.set_search_mode (!search_bar.get_search_mode ());
                     return true;
                 case "c":
                     ListBoxRow? selected_row = (ListBoxRow) key_list_box.get_selected_row ();
-                    if (selected_row != null)
-                        ((KeyListBoxRow) ((!) selected_row).get_child ()).copy_text ();
+                    ConfigurationEditor application = (ConfigurationEditor) get_application ();
+                    application.copy (selected_row == null ? current_path : ((KeyListBoxRow) ((!) 
selected_row).get_child ()).get_text ());
                     return true;
                 default:
                     break;  // TODO report bug for making <ctrl>v work?
             }
         }
 
-        if (bookmarks_button.active)        // TODO open bug
+        if (bookmarks_button.active || info_button.active)      // TODO open bug about modal popovers and 
search_bar
             return false;
 
         return search_bar.handle_event (event);
     }
 
     [GtkCallback]
-    private void on_bookmarks_button_clicked ()
+    private void on_menu_button_clicked ()
     {
         search_bar.set_search_mode (false);
     }
diff --git a/editor/key-list-box-row.vala b/editor/key-list-box-row.vala
index 06a6fa6..6d50772 100644
--- a/editor/key-list-box-row.vala
+++ b/editor/key-list-box-row.vala
@@ -20,11 +20,6 @@ using Gtk;
 [GtkTemplate (ui = "/ca/desrt/dconf-editor/ui/key-list-box-row.ui")]
 private abstract class KeyListBoxRow : EventBox
 {
-    protected Window window { get; set; }
-    protected Notification notification = new Notification (_("Copied to clipboard"));
-    protected bool notification_active = false;
-    protected uint notification_number;
-
     [GtkChild] protected Label key_name_label;
     [GtkChild] protected Label key_value_label;
     [GtkChild] protected Label key_info_label;
@@ -34,6 +29,8 @@ private abstract class KeyListBoxRow : EventBox
     protected ContextPopover? nullable_popover;
     protected virtual bool generate_popover (ContextPopover popover) { return false; }      // no popover 
should be created
 
+    public abstract string get_text ();
+
     public override bool button_press_event (Gdk.EventButton event)     // list_box_row selection is done 
elsewhere
     {
         if (event.button == Gdk.BUTTON_SECONDARY)
@@ -55,41 +52,6 @@ private abstract class KeyListBoxRow : EventBox
         return false;
     }
 
-    protected abstract string get_text ();
-    public void copy_text ()
-    {
-        string text = get_text ();
-
-        // clipboard
-        Gdk.Display? display = Gdk.Display.get_default ();
-        if (display == null)
-            return;
-
-        Clipboard clipboard = Clipboard.get_default ((!) display);
-        clipboard.set_text (text, text.length);
-
-        // notification
-        GLib.Application application = window.get_application ();   // TODO better; but "of course", after 
the window is added to the application...
-        if (notification_active == true)
-        {
-            Source.remove (notification_number);  // FIXME doesn't work [as expected], the timeout runs 
until its end, and withdraws the notification then
-            notification_active = false;
-        }
-
-        notification_number = Timeout.add_seconds (30, () => {
-                if (notification_active == false)
-                    return Source.CONTINUE;
-                application.withdraw_notification ("copy");
-                notification_active = false;
-                return Source.REMOVE;
-            });
-        notification_active = true;
-
-        notification.set_body (text);
-        application.withdraw_notification ("copy");     // TODO report bug: Shell cancels previous 
notification of the same name, instead of replacing it
-        application.send_notification ("copy", notification);
-    }
-
     protected static string cool_text_value (Key key)   // TODO better
     {
         return Key.cool_text_value_from_variant (key.value, key.type_string);
@@ -100,9 +62,8 @@ private class KeyListBoxRowEditableNoSchema : KeyListBoxRow
 {
     public DConfKey key { get; private set; }
 
-    public KeyListBoxRowEditableNoSchema (Window _window, DConfKey _key)
+    public KeyListBoxRowEditableNoSchema (DConfKey _key)
     {
-        this.window = _window;
         this.key = _key;
 
         Pango.AttrList attr_list = new Pango.AttrList ();
@@ -125,7 +86,7 @@ private class KeyListBoxRowEditableNoSchema : KeyListBoxRow
     protected override bool generate_popover (ContextPopover popover)
     {
         popover.new_action ("customize", () => { show_dialog (); });
-        popover.new_action ("copy", () => { copy_text (); });
+        popover.new_copy_action (get_text ());
 
         if (key.type_string == "b" || key.type_string == "mb")
         {
@@ -144,9 +105,8 @@ private class KeyListBoxRowEditable : KeyListBoxRow
 
     private Pango.AttrList attr_list = new Pango.AttrList ();
 
-    public KeyListBoxRowEditable (Window _window, GSettingsKey _key)
+    public KeyListBoxRowEditable (GSettingsKey _key)
     {
-        this.window = _window;
         this.key = _key;
 
         key_value_label.set_attributes (attr_list);
@@ -165,7 +125,7 @@ private class KeyListBoxRowEditable : KeyListBoxRow
     protected override bool generate_popover (ContextPopover popover)
     {
         popover.new_action ("customize", () => { show_dialog (); });
-        popover.new_action ("copy", () => { copy_text (); });
+        popover.new_copy_action (get_text ());
 
         if (key.type_string == "b" || key.type_string == "<enum>" || key.type_string == "mb")
         {
@@ -236,8 +196,6 @@ private class ContextPopover : Popover
         {
             /* Translators: "open key-editor dialog" action in the right-click menu on the list of keys */
             case "customize":   text = _("Customize…");     break;
-            /* Translators: "copy to clipboard" action in the right-click menu on the list of keys */
-            case "copy":        text = _("Copy");           break;
             /* Translators: "reset key value" action in the right-click menu on the list of keys */
             case "default1":    text = _("Set to default"); break;
             /* Translators: "reset key value" option of a multi-choice list in the right-click menu on the 
list of keys */
@@ -252,6 +210,12 @@ private class ContextPopover : Popover
         current_section.append (text, current_group_prefix + "." + action_action);
     }
 
+    public void new_copy_action (string text)
+    {
+        /* Translators: "copy to clipboard" action in the right-click menu on the list of keys */
+        current_section.append (_("Copy"), "app.copy('" + text + "')");   // TODO protection against some 
chars in text? 2/2
+    }
+
     public void new_section (bool draw_line = true)
     {
         current_group_prefix += "a";


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