[gnome-calculator: 1/2] math-preferences: Derive from HdyPreferencesWindow




commit f196b762ed411b79b286bc5a915c3c84cd2e5d5e
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Fri Mar 19 13:37:08 2021 +0100

    math-preferences: Derive from HdyPreferencesWindow
    
    This makes it look better and more in line with the other GNOME
    applications, and simplifies the code a bit.

 src/math-preferences.vala  | 104 ++++++++++++++------
 src/ui/math-preferences.ui | 229 +++++++++------------------------------------
 2 files changed, 121 insertions(+), 212 deletions(-)
---
diff --git a/src/math-preferences.vala b/src/math-preferences.vala
index bc8704f2..10c5b245 100644
--- a/src/math-preferences.vala
+++ b/src/math-preferences.vala
@@ -8,16 +8,47 @@
  * license.
  */
 [GtkTemplate (ui = "/org/gnome/calculator/math-preferences.ui")]
-public class MathPreferencesDialog : Gtk.Dialog
+public class MathPreferencesDialog : Hdy.PreferencesWindow
 {
+    private struct ComboEntry {
+        string name;
+        uint val;
+    }
+
+    private ComboEntry[] entries_angle_units = {
+        { _("Radians"), 0 },
+        { _("Degrees"), 1 },
+        { _("Gradians"), 2 }
+    };
+
+    private ComboEntry[] entries_word_size = {
+        // Translators: Word size combo: 8 bit
+        { _("8-bit"), 8 },
+        // Translators: Word size combo: 16 bit
+        { _("16-bit"), 16 },
+        // Translators: Word size combo: 32 bit
+        { _("32-bit"), 32 },
+        // Translators: Word size combo: 64 bit
+        { _("64-bit"), 64 }
+    };
+
+    private ComboEntry[] entries_refresh_interval = {
+        // Translators: Refresh interval combo: never
+        { _("never"), 0 },
+        // Translators: Refresh interval combo: daily
+        { _("daily"), 86400 },
+        // Translators: Refresh interval combo: weekly
+        { _("weekly"), 604800 }
+    };
+
     public MathEquation equation { private get; construct; }
 
     [GtkChild]
-    private unowned Gtk.ComboBoxText combo_angle_units;
+    private unowned Hdy.ComboRow row_angle_units;
     [GtkChild]
-    private unowned Gtk.ComboBoxText combo_refresh_interval;
+    private unowned Hdy.ComboRow row_word_size;
     [GtkChild]
-    private unowned Gtk.ComboBoxText combo_word_size;
+    private unowned Hdy.ComboRow row_refresh_interval;
     [GtkChild]
     private unowned Gtk.SpinButton spinbutton_decimals;
     [GtkChild]
@@ -36,12 +67,16 @@ public class MathPreferencesDialog : Gtk.Dialog
     {
         settings = new Settings ("org.gnome.calculator");
 
+        populate_combo_row (row_angle_units, entries_angle_units);
+        populate_combo_row (row_word_size, entries_word_size);
+        populate_combo_row (row_refresh_interval, entries_refresh_interval);
+
         spinbutton_decimals.value_changed.connect (() => { equation.accuracy = 
spinbutton_decimals.get_value_as_int (); });
         switch_trailing_zeroes.state_set.connect ((state) => { equation.show_trailing_zeroes = state; });
         switch_thousands_separators.state_set.connect ((state) => { equation.show_thousands_separators = 
state; });
-        combo_angle_units.changed.connect (combo_angle_units_changed_cb);
-        combo_word_size.changed.connect (combo_word_size_changed_cb);
-        combo_refresh_interval.changed.connect (combo_refresh_interval_changed_cb);
+        row_angle_units.notify["selected-index"].connect (row_angle_units_changed_cb);
+        row_word_size.notify["selected-index"].connect (row_word_size_changed_cb);
+        row_refresh_interval.notify["selected-index"].connect (row_refresh_interval_changed_cb);
 
         spinbutton_decimals.set_value (equation.accuracy);
         equation.notify["accuracy"].connect ((pspec) => { spinbutton_decimals.set_value (equation.accuracy); 
});
@@ -52,51 +87,64 @@ public class MathPreferencesDialog : Gtk.Dialog
         switch_trailing_zeroes.set_active (equation.show_trailing_zeroes);
         equation.notify["show-trailing_zeroes"].connect (() => { switch_trailing_zeroes.set_active 
(equation.show_trailing_zeroes); });
 
-        set_combo_box_from_int (combo_word_size, equation.word_size);
-        equation.notify["word-size"].connect ((pspec) => { set_combo_box_from_int (combo_word_size, 
equation.word_size); });
+        set_combo_row_from_int (row_word_size, entries_word_size, equation.word_size);
+        equation.notify["word-size"].connect ((pspec) => { set_combo_row_from_int (row_word_size, 
entries_word_size, equation.word_size); });
 
-        set_combo_box_from_int (combo_angle_units, equation.angle_units);
-        equation.notify["angle-units"].connect ((pspec) => { set_combo_box_from_int (combo_angle_units, 
equation.angle_units); });
+        set_combo_row_from_int (row_angle_units, entries_angle_units, equation.angle_units);
+        equation.notify["angle-units"].connect ((pspec) => { set_combo_row_from_int (row_angle_units, 
entries_angle_units, equation.angle_units); });
 
-        set_combo_box_from_int (combo_refresh_interval, settings.get_int ("refresh-interval"));
+        set_combo_row_from_int (row_refresh_interval, entries_refresh_interval, settings.get_int 
("refresh-interval"));
     }
 
+    private void populate_combo_row (Hdy.ComboRow row, ComboEntry[] entries) {
+        var list_store = new ListStore (typeof (Hdy.ValueObject));
+
+        foreach (var e in entries) {
+            var val = Value (typeof (string));
+            val.set_string (e.name);
+
+            var value_object = new Hdy.ValueObject (val);
+            value_object.set_data ("value", e.val.to_pointer ());
+            list_store.append (value_object);
+        }
 
-    private void combo_angle_units_changed_cb (Gtk.ComboBox combo)
+        row.bind_name_model (list_store, (o) => {
+            return (o is Hdy.ValueObject) ? ((Hdy.ValueObject) o).dup_string () : null;
+        });
+    }
+
+    private void row_angle_units_changed_cb ()
     {
-        string active_id = combo.get_active_id ();
-        AngleUnit value = (AngleUnit) int.parse (active_id);
+        AngleUnit value = (AngleUnit) entries_angle_units[row_angle_units.selected_index].val;
         equation.angle_units = value;
     }
 
-    private void combo_word_size_changed_cb (Gtk.ComboBox combo)
+    private void row_word_size_changed_cb ()
     {
-        string active_id = combo.get_active_id ();
-        int value = int.parse (active_id);
+        int value = (int) entries_word_size[row_word_size.selected_index].val;
         equation.word_size = value;
     }
 
-    private void combo_refresh_interval_changed_cb (Gtk.ComboBox combo)
+    private void row_refresh_interval_changed_cb ()
     {
-        string active_id = combo.get_active_id ();
-        int value = int.parse (active_id);
+        int value = (int) entries_refresh_interval[row_refresh_interval.selected_index].val;
         settings.set_int ("refresh-interval", value);
         CurrencyManager.get_default ().refresh_interval = value;
     }
 
-    protected override void response (int id)
-    {
-        hide ();
-    }
-
     protected override bool delete_event (Gdk.EventAny event)
     {
         hide ();
         return true;
     }
 
-    private void set_combo_box_from_int (Gtk.ComboBox combo, int value)
+    private void set_combo_row_from_int (Hdy.ComboRow row, ComboEntry[] entries, int value)
     {
-        combo.active_id = value.to_string ();
+        for (int i = 0; i < entries.length; i++) {
+            if (entries[i].val == value) {
+                row.selected_index = i;
+                break;
+            }
+        }
     }
 }
diff --git a/src/ui/math-preferences.ui b/src/ui/math-preferences.ui
index 0eb3ec2e..8331d839 100644
--- a/src/ui/math-preferences.ui
+++ b/src/ui/math-preferences.ui
@@ -7,224 +7,85 @@
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
   </object>
-  <template class="MathPreferencesDialog" parent="GtkDialog">
-    <property name="can_focus">False</property>
-    <property name="border_width">8</property>
-    <property name="resizable">False</property>
-    <property name="destroy_with_parent">True</property>
-    <property name="type_hint">dialog</property>
-    <property name="gravity">center</property>
-    <child type="titlebar">
-      <object class="GtkHeaderBar" id="headerbar">
+  <template class="MathPreferencesDialog" parent="HdyPreferencesWindow">
+    <property name="search_enabled">False</property>
+    <property name="default_height">440</property>
+    <child>
+      <object class="HdyPreferencesPage">
         <property name="visible">True</property>
-        <property name="can_focus">False</property>
-        <property name="title" translatable="yes">Preferences</property>
-        <property name="show_close_button">True</property>
-        <property name="decoration_layout">:close</property>
         <child>
-          <placeholder/>
-        </child>
-      </object>
-    </child>
-    <child internal-child="vbox">
-      <object class="GtkBox" id="preferences_container">
-        <property name="can_focus">False</property>
-        <property name="orientation">vertical</property>
-        <property name="spacing">2</property>
-        <child internal-child="action_area">
-          <object class="GtkButtonBox">
-            <property name="can_focus">False</property>
-            <property name="layout_style">end</property>
-            <child>
-              <placeholder/>
-            </child>
-            <child>
-              <placeholder/>
-            </child>
-          </object>
-          <packing>
-            <property name="expand">False</property>
-            <property name="fill">False</property>
-            <property name="position">0</property>
-          </packing>
-        </child>
-        <child>
-          <object class="GtkGrid" id="preferences_grid">
+          <object class="HdyPreferencesGroup">
             <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <property name="border_width">5</property>
-            <property name="row_spacing">12</property>
-            <property name="column_spacing">6</property>
             <child>
-              <object class="GtkLabel" id="label_decimals_number">
+              <object class="HdyActionRow">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="halign">start</property>
-                <property name="label" translatable="yes">Number of _decimals</property>
+                <property name="title" translatable="yes">Number of _decimals</property>
                 <property name="use_underline">True</property>
-                <property name="mnemonic_widget">spinbutton_decimals</property>
+                <!-- <property name="mnemonic_widget">spinbutton_decimals</property> -->
+                <child>
+                  <object class="GtkSpinButton" id="spinbutton_decimals">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="valign">center</property>
+                    <property name="adjustment">adjustment_decimals</property>
+                    <property name="numeric">True</property>
+                    <property name="value">9</property>
+                  </object>
+                </child>
               </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">0</property>
-              </packing>
             </child>
             <child>
-              <object class="GtkLabel" id="label_trailing_zeroes">
+              <object class="HdyActionRow">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="halign">start</property>
-                <property name="label" translatable="yes">Trailing _zeroes</property>
+                <property name="title" translatable="yes">Trailing _zeroes</property>
                 <property name="use_underline">True</property>
+                <property name="activatable_widget">switch_trailing_zeroes</property>
+                <child>
+                  <object class="GtkSwitch" id="switch_trailing_zeroes">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="valign">center</property>
+                  </object>
+                </child>
               </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">1</property>
-              </packing>
             </child>
             <child>
-              <object class="GtkLabel" id="label_thousands_separator">
+              <object class="HdyActionRow">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="halign">start</property>
-                <property name="label" translatable="yes">_Thousands separators</property>
+                <property name="title" translatable="yes">_Thousands separators</property>
                 <property name="use_underline">True</property>
-                <property name="mnemonic_widget">switch_thousands_separators</property>
+                <property name="activatable_widget">switch_thousands_separators</property>
+                <child>
+                  <object class="GtkSwitch" id="switch_thousands_separators">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="valign">center</property>
+                  </object>
+                </child>
               </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">2</property>
-              </packing>
             </child>
             <child>
-              <object class="GtkLabel" id="label_angle_units">
+              <object class="HdyComboRow" id="row_angle_units">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="halign">start</property>
-                <property name="label" translatable="yes">_Angle units</property>
+                <property name="title" translatable="yes">_Angle units</property>
                 <property name="use_underline">True</property>
-                <property name="mnemonic_widget">combo_angle_units</property>
               </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">3</property>
-              </packing>
             </child>
             <child>
-              <object class="GtkLabel" id="label_word_size">
+              <object class="HdyComboRow" id="row_word_size">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="halign">start</property>
-                <property name="label" translatable="yes">Word _size</property>
+                <property name="title" translatable="yes">Word _size</property>
                 <property name="use_underline">True</property>
-                <property name="mnemonic_widget">combo_word_size</property>
               </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">4</property>
-              </packing>
             </child>
             <child>
-              <object class="GtkLabel" id="label_exchange_refresh">
+              <object class="HdyComboRow" id="row_refresh_interval">
                 <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="halign">start</property>
-                <property name="label" translatable="yes">E_xchange rate refresh interval</property>
+                <property name="title" translatable="yes">E_xchange rate refresh interval</property>
                 <property name="use_underline">True</property>
-                <property name="mnemonic_widget">combo_refresh_interval</property>
-              </object>
-              <packing>
-                <property name="left_attach">0</property>
-                <property name="top_attach">5</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkSpinButton" id="spinbutton_decimals">
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="adjustment">adjustment_decimals</property>
-                <property name="numeric">True</property>
-                <property name="value">9</property>
-              </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">0</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkSwitch" id="switch_trailing_zeroes">
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="halign">end</property>
-              </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkSwitch" id="switch_thousands_separators">
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="halign">end</property>
-              </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">2</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkComboBoxText" id="combo_angle_units">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <items>
-                  <item id="0" translatable="yes">Radians</item>
-                  <item id="1" translatable="yes">Degrees</item>
-                  <item id="2" translatable="yes">Gradians</item>
-                </items>
-              </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">3</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkComboBoxText" id="combo_word_size">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <items>
-                  <item id="8" translatable="yes" comments="Word size combo: 8 bit">8-bit</item>
-                  <item id="16" translatable="yes" comments="Word size combo: 16 bit">16-bit</item>
-                  <item id="32" translatable="yes" comments="Word size combo: 32 bit">32-bit</item>
-                  <item id="64" translatable="yes" comments="Word size combo: 64 bit">64-bit</item>
-                </items>
-              </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">4</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkComboBoxText" id="combo_refresh_interval">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <items>
-                  <item id="0" translatable="yes" comments="Refresh interval combo: never">never</item>
-                  <item id="86400" translatable="yes" comments="Refresh interval combo: daily">daily</item>
-                  <item id="604800" translatable="yes" comments="Refresh interval combo: 
weekly">weekly</item>
-                </items>
               </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">5</property>
-              </packing>
             </child>
           </object>
-          <packing>
-            <property name="expand">True</property>
-            <property name="fill">True</property>
-            <property name="position">1</property>
-          </packing>
         </child>
       </object>
     </child>


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