[gnome-calculator] Let the window handle window-specific actions



commit feae89e30ad2edcb8f5907fd2c8ab1fcc77d62de
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Tue Feb 4 13:12:51 2014 -0600

    Let the window handle window-specific actions
    
    Pull copy/cut/paste/undo/redo and mode changing out of application scope
    and into window scope. Remove mode switching from the app menu and put
    it into a gear menu instead. The gear menu is on the left instead of the
    right because this looks far better in Basic Mode.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=714991

 data/menu.ui              |   47 +++++++++++++-------------
 src/gnome-calculator.vala |   79 ++------------------------------------------
 src/math-window.vala      |   77 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 98 deletions(-)
---
diff --git a/data/menu.ui b/data/menu.ui
index 48943b4..821cc65 100644
--- a/data/menu.ui
+++ b/data/menu.ui
@@ -3,29 +3,6 @@
   <!-- interface-requires gtk+ 3.0 -->
   <menu id="appmenu">
     <section>
-      <attribute name="label" translatable="yes">Mode</attribute>
-      <item>
-        <attribute name="label" translatable="yes">Basic</attribute>
-        <attribute name="action">app.mode</attribute>
-        <attribute name="target">basic</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">Advanced</attribute>
-        <attribute name="action">app.mode</attribute>
-        <attribute name="target">advanced</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">Financial</attribute>
-        <attribute name="action">app.mode</attribute>
-        <attribute name="target">financial</attribute>
-      </item>
-      <item>
-        <attribute name="label" translatable="yes">Programming</attribute>
-        <attribute name="action">app.mode</attribute>
-        <attribute name="target">programming</attribute>
-      </item>
-    </section>
-    <section>
       <item>
         <attribute name="label" translatable="yes">Preferences</attribute>
         <attribute name="action">app.preferences</attribute>
@@ -48,4 +25,28 @@
       </item>
     </section>
   </menu>
+  <menu id="window-menu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">Basic Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">basic</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Advanced Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">advanced</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Financial Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">financial</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">Programming Mode</attribute>
+        <attribute name="action">win.mode</attribute>
+        <attribute name="target">programming</attribute>
+      </item>
+    </section>
+  </menu>
 </interface>
diff --git a/src/gnome-calculator.vala b/src/gnome-calculator.vala
index f0a036b..63a052e 100644
--- a/src/gnome-calculator.vala
+++ b/src/gnome-calculator.vala
@@ -19,11 +19,6 @@ public class Calculator : Gtk.Application
 
     private const ActionEntry[] app_entries =
     {
-        { "copy", copy_cb, null, null, null },
-        { "paste", paste_cb, null, null, null },
-        { "undo", undo_cb, null, null, null },
-        { "redo", redo_cb, null, null, null },
-        { "mode", mode_changed_cb, "s", "\"basic\"", null },
         { "preferences", show_preferences_cb, null, null, null },
         { "help", help_cb, null, null, null },
         { "about", about_cb, null, null, null },
@@ -71,8 +66,6 @@ public class Calculator : Gtk.Application
         var buttons = window.buttons;
         buttons.programming_base = number_base;
         buttons.mode = button_mode; // FIXME: We load the basic buttons even if we immediately switch to the 
next type
-        buttons.notify["mode"].connect ((pspec) => { mode_cb (); });
-        mode_cb ();
 
         var builder = new Gtk.Builder ();
         try
@@ -87,10 +80,10 @@ public class Calculator : Gtk.Application
         var menu = builder.get_object ("appmenu") as MenuModel;
         set_app_menu (menu);
 
-        add_accelerator ("<control>C", "app.copy", null);
-        add_accelerator ("<control>V", "app.paste", null);
-        add_accelerator ("<control>Z", "app.undo", null);
-        add_accelerator ("<control><shift>Z", "app.redo", null);
+        add_accelerator ("<control>C", "win.copy", null);
+        add_accelerator ("<control>V", "win.paste", null);
+        add_accelerator ("<control>Z", "win.undo", null);
+        add_accelerator ("<control><shift>Z", "win.redo", null);
     }
 
     protected override void activate ()
@@ -132,70 +125,6 @@ public class Calculator : Gtk.Application
         settings.set_int ("base", buttons.programming_base);
     }
 
-    private void mode_cb ()
-    {
-        var buttons = window.buttons;
-        var state = "basic";
-        switch (buttons.mode)
-        {
-        default:
-        case ButtonMode.BASIC:
-            state = "basic";
-            //FIXME: Should it revert to decimal mode? equation.number_format = NumberFormat.DECIMAL;
-            break;
-
-        case ButtonMode.ADVANCED:
-            state = "advanced";
-            break;
-
-        case ButtonMode.FINANCIAL:
-            state = "financial";
-            break;
-
-        case ButtonMode.PROGRAMMING:
-            state = "programming";
-            break;
-        }
-
-        var action = lookup_action ("mode") as SimpleAction;
-        action.set_state (new Variant.string (state));
-    }
-
-    private void copy_cb ()
-    {
-        window.equation.copy ();
-    }
-
-    private void paste_cb ()
-    {
-        window.equation.paste ();
-    }
-
-    private void undo_cb ()
-    {
-        window.equation.undo ();
-    }
-
-    private void redo_cb ()
-    {
-        window.equation.redo ();
-    }
-
-    private void mode_changed_cb (SimpleAction action, Variant? parameter)
-    {
-        var mode = ButtonMode.BASIC;
-        var mode_str = parameter.get_string (null);
-        if (mode_str == "basic")
-            mode = ButtonMode.BASIC;
-        else if (mode_str == "advanced")
-            mode = ButtonMode.ADVANCED;
-        else if (mode_str == "financial")
-            mode = ButtonMode.FINANCIAL;
-        else if (mode_str == "programming")
-            mode = ButtonMode.PROGRAMMING;
-        window.buttons.mode = mode;
-    }
-
     private void show_preferences_cb ()
     {
         if (preferences_dialog == null)
diff --git a/src/math-window.vala b/src/math-window.vala
index f63f570..a380bf0 100644
--- a/src/math-window.vala
+++ b/src/math-window.vala
@@ -23,6 +23,15 @@ public class MathWindow : Gtk.ApplicationWindow
 
     private Gtk.HeaderBar headerbar;
 
+    private const ActionEntry[] window_entries =
+    {
+        { "copy", copy_cb, null, null, null },
+        { "paste", paste_cb, null, null, null },
+        { "undo", undo_cb, null, null, null },
+        { "redo", redo_cb, null, null, null },
+        { "mode", mode_cb, "s", "\"basic\"", null },
+    };
+
     public MathWindow (Gtk.Application app, MathEquation equation)
     {
         Object (application: app);
@@ -33,8 +42,30 @@ public class MathWindow : Gtk.ApplicationWindow
         role = "gnome-calculator";
         resizable = false;
 
+        add_action_entries (window_entries, this);
+
+        var builder = new Gtk.Builder ();
+        try
+        {
+            builder.add_from_resource ("/org/gnome/calculator/menu.ui");
+        }
+        catch (Error e)
+        {
+            error ("Error loading menu UI: %s", e.message);
+        }
+
+        var menu = builder.get_object ("window-menu") as MenuModel;
+
+        var gear_menu_button = new Gtk.MenuButton ();
+        gear_menu_button.valign = Gtk.Align.CENTER;
+        gear_menu_button.image = new Gtk.Image.from_icon_name ("emblem-system-symbolic", 
Gtk.IconSize.BUTTON);
+        gear_menu_button.menu_model = menu;
+        gear_menu_button.get_style_context ().add_class ("image-button");
+        gear_menu_button.show ();
+
         headerbar = new Gtk.HeaderBar ();
         headerbar.show_close_button = true;
+        headerbar.pack_start (gear_menu_button);
         headerbar.show ();
         set_titlebar (headerbar);
 
@@ -69,23 +100,29 @@ public class MathWindow : Gtk.ApplicationWindow
 
     private void mode_changed_cb ()
     {
+        var action = (SimpleAction) lookup_action ("mode");
+
         switch (buttons.mode)
         {
         default:
         case ButtonMode.BASIC:
             headerbar.set_title (_("Basic Mode"));
+            action.set_state (new Variant.string ("basic"));
             break;
 
         case ButtonMode.ADVANCED:
             headerbar.set_title (_("Advanced Mode"));
+            action.set_state (new Variant.string ("advanced"));
             break;
 
         case ButtonMode.FINANCIAL:
             headerbar.set_title (_("Financial Mode"));
+            action.set_state (new Variant.string ("financial"));
             break;
 
         case ButtonMode.PROGRAMMING:
             headerbar.set_title (_("Programming Mode"));
+            action.set_state (new Variant.string ("programming"));
             break;
         }
     }
@@ -147,4 +184,44 @@ public class MathWindow : Gtk.ApplicationWindow
         else
             right_aligned = false;
     }
+
+    private void copy_cb ()
+    {
+        equation.copy ();
+    }
+
+    private void paste_cb ()
+    {
+        equation.paste ();
+    }
+
+    private void undo_cb ()
+    {
+        equation.undo ();
+    }
+
+    private void redo_cb ()
+    {
+        equation.redo ();
+    }
+
+    private void mode_cb (SimpleAction action, Variant? parameter)
+        requires (parameter != null)
+        requires (parameter.is_of_type (VariantType.STRING))
+    {
+        var mode = ButtonMode.BASIC;
+        var mode_str = parameter.get_string (null);
+
+        if (mode_str == "basic")
+            mode = ButtonMode.BASIC;
+        else if (mode_str == "advanced")
+            mode = ButtonMode.ADVANCED;
+        else if (mode_str == "financial")
+            mode = ButtonMode.FINANCIAL;
+        else if (mode_str == "programming")
+            mode = ButtonMode.PROGRAMMING;
+        else assert_not_reached ();
+
+        buttons.mode = mode;
+    }
 }


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