[gnome-2048] Use a menu for animations speed.



commit 1a513f622a7d1cec287ba9aee25e5c95f03dab4c
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Jan 26 09:03:12 2019 +0100

    Use a menu for animations speed.
    
    No need to expose internals to users.

 data/preferences.ui  | 35 +++++++++++++++++++++++------------
 src/application.vala | 42 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 63 insertions(+), 14 deletions(-)
---
diff --git a/data/preferences.ui b/data/preferences.ui
index b8e6c84..3548bee 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -17,13 +17,6 @@
 -->
 <interface>
   <requires lib="gtk+" version="3.12"/>
-  <object class="GtkAdjustment" id="animationsspeed">
-    <property name="lower">20</property>  <!-- also enforced in -->
-    <property name="upper">200</property> <!-- gschema.xml file -->
-    <property name="value">100</property>
-    <property name="step-increment">5</property>
-    <property name="page-increment">10</property>
-  </object>
   <object class="GtkDialog" id="preferencesdialog">
     <property name="border-width">12</property>
     <!-- Translators: title of the dialog that appears when the users clicks the "Preferences" entry in the 
hamburger menu -->
@@ -33,6 +26,7 @@
     <property name="type-hint">dialog</property>
     <property name="use-header-bar">1</property>
     <property name="modal">True</property>
+    <property name="destroy-with-parent">True</property>
     <child internal-child="vbox">
       <object class="GtkBox">
         <child>
@@ -53,12 +47,9 @@
               </packing>
             </child>
             <child>
-              <object class="GtkScale">
+              <object class="GtkMenuButton" id="animations-button">
                 <property name="visible">True</property>
-                <property name="adjustment">animationsspeed</property>
-                <property name="inverted">True</property>
-                <property name="round-digits">1</property>
-                <property name="draw-value">False</property>
+                <property name="menu-model">animations-menu</property>
               </object>
               <packing>
                 <property name="left-attach">1</property>
@@ -112,4 +103,24 @@
       </object>
     </child>
   </object>
+  <menu id="animations-menu">
+    <item>
+      <!-- Translators: on preferences window, entry of the menu for changing animations speed (with a 
mnemonic that appears pressing Alt)-->
+      <attribute name="label" translatable="yes">_Slow</attribute>
+      <attribute name="action">app.animations-speed</attribute>
+      <attribute name="target">250.0</attribute>
+    </item>
+    <item>
+      <!-- Translators: on preferences window, entry of the menu for changing animations speed (with a 
mnemonic that appears pressing Alt) -->
+      <attribute name="label" translatable="yes">_Normal</attribute>
+      <attribute name="action">app.animations-speed</attribute>
+      <attribute name="target">100.0</attribute>
+    </item>
+    <item>
+      <!-- Translators: on preferences window, entry of the menu for changing animations speed (with a 
mnemonic that appears pressing Alt) -->
+      <attribute name="label" translatable="yes">_Fast</attribute>
+      <attribute name="action">app.animations-speed</attribute>
+      <attribute name="target">40.0</attribute>
+    </item>
+  </menu>
 </interface>
diff --git a/src/application.vala b/src/application.vala
index 11e109f..aff1ef1 100644
--- a/src/application.vala
+++ b/src/application.vala
@@ -53,6 +53,7 @@ public class Application : Gtk.Application
         { "new-game",           new_game_cb             },
         { "toggle-new-game",    toggle_new_game_cb      },
         { "new-game-sized",     new_game_sized_cb, "s"  },  // no way to make it take an int8/int32
+        { "animations-speed",   _animations_speed, "s"  },  // idem, for a double
 
         { "quit",               quit_cb                 },
 
@@ -397,7 +398,8 @@ public class Application : Gtk.Application
     * * preferences dialog
     \*/
 
-    private Dialog _preferences_dialog;
+    private Dialog      _preferences_dialog;
+    private MenuButton  _animations_button;
 
     private bool _should_create_preferences_dialog = true;
     private inline void _create_preferences_dialog ()
@@ -405,6 +407,7 @@ public class Application : Gtk.Application
         Builder builder = new Builder.from_resource ("/org/gnome/gnome-2048/data/preferences.ui");
 
         _preferences_dialog = (Dialog) builder.get_object ("preferencesdialog");
+        _preferences_dialog.set_application (this); // else we cannot use "app." actions in the dialog
         _preferences_dialog.set_transient_for (_window);
 
         _preferences_dialog.response.connect ((response_id) => {
@@ -416,8 +419,43 @@ public class Application : Gtk.Application
             });
 
         _settings.bind ("do-congrat",       builder.get_object ("congratswitch"),   "active", 
GLib.SettingsBindFlags.DEFAULT);
-        _settings.bind ("animations-speed", builder.get_object ("animationsspeed"), "value",  
GLib.SettingsBindFlags.DEFAULT);
         _settings.bind ("allow-undo",       builder.get_object ("undoswitch"),      "active", 
GLib.SettingsBindFlags.DEFAULT);
+
+        _animations_button = (MenuButton) builder.get_object ("animations-button");
+        _settings.changed ["animations-speed"].connect (_set_animations_button_label);
+        _set_animations_button_label (_settings, "animations-speed");
+    }
+    private inline void _set_animations_button_label (GLib.Settings settings, string key_name)
+    {
+        double speed = settings.get_double (key_name);
+        string _animations_button_label;
+        _get_animations_button_label (ref speed, out _animations_button_label);
+        _animations_button.set_label (_animations_button_label);
+    }
+    private static inline void _get_animations_button_label (ref double speed, out string 
_animations_button_label)
+    {
+        if (speed == 100.0)
+            /* Translators: in the preferences dialog; possible label of the MenuButton to choose animation 
speed */
+            _animations_button_label = _("Normal");
+
+        else if (speed == 40.0)
+            /* Translators: in the preferences dialog; possible label of the MenuButton to choose animation 
speed */
+            _animations_button_label = _("Fast");
+
+        else if (speed == 250.0)
+            /* Translators: in the preferences dialog; possible label of the MenuButton to choose animation 
speed */
+            _animations_button_label = _("Slow");
+
+        else
+            /* Translators: in the preferences dialog; possible label of the MenuButton to choose animation 
speed */
+            _animations_button_label = _("Custom");
+    }
+
+    private inline void _animations_speed (SimpleAction action, Variant? variant)
+        requires (variant != null)
+    {
+        double speed = double.parse (((!) variant).get_string ());
+        _settings.set_double ("animations-speed", speed);
     }
 
     private inline void preferences_cb (/* SimpleAction action, Variant? variant */)


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