[gnome-calculator/wip-gtk4-port] More refactoring of common popover code



commit b1b9ca8965e972e93d7ec76d4fb2c2b79b434248
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Oct 1 17:34:15 2021 +0300

    More refactoring of common popover code

 src/math-function-popover.vala | 64 +++++++++++++++++-------------------------
 src/math-popover.vala          | 46 +++++++++++++++++++++++++++++-
 src/math-variable-popover.vala | 38 +++++++++----------------
 3 files changed, 84 insertions(+), 64 deletions(-)
---
diff --git a/src/math-function-popover.vala b/src/math-function-popover.vala
index 678a19e2..f495b24c 100644
--- a/src/math-function-popover.vala
+++ b/src/math-function-popover.vala
@@ -38,6 +38,24 @@ public class MathFunctionPopover : MathPopover<MathFunction>
 
         add_arguments_button.set_range (1, 10);
         add_arguments_button.set_increments (1, 1);
+        item_edited.connect (function_edited_cb);
+        item_deleted.connect (function_deleted_cb);
+    }
+
+    private void function_edited_cb (MathFunction function)
+    {
+        var function_to_edit = "%s(%s)=%s@%s".printf (function.name,
+                                                      string.joinv (";", function.arguments),
+                                                      function.expression,
+                                                      function.description);
+        equation.clear ();
+        equation.insert (function_to_edit);
+    }
+
+    private void function_deleted_cb (MathFunction function)
+    {
+        var function_manager = FunctionManager.get_default_function_manager ();
+        function_manager.delete (function.name);
     }
 
     [GtkCallback]
@@ -91,58 +109,26 @@ public class MathFunctionPopover : MathPopover<MathFunction>
         equation.insert (name);
     }
 
-    private void save_function_cb (Gtk.Widget widget)
+    protected override bool is_deletable (MathFunction function)
     {
-        var function = widget.get_data<MathFunction> ("function");
-        var function_to_edit = "%s(%s)=%s@%s".printf (function.name,
-                                                      string.joinv (";", function.arguments),
-                                                      function.expression,
-                                                      function.description);
-        equation.clear ();
-        equation.insert (function_to_edit);
+        return function.is_custom_function ();
     }
 
-    private void delete_function_cb (Gtk.Widget widget)
+    protected override bool is_editable (MathFunction function)
     {
-        var function = widget.get_data<MathFunction> ("function");
-
-        var function_manager = FunctionManager.get_default_function_manager ();
-        function_manager.delete (function.name);
+        return function.is_custom_function ();
     }
 
-    protected override Gtk.Widget make_item_row (MathFunction function)
+    protected override string get_item_text (MathFunction function)
     {
-        var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
-
         var expression = "(x)";
         if (function.is_custom_function ())
             expression = "(%s)".printf (string.joinv (";", function.arguments));
 
-        var label = new Gtk.Label ("<b>%s</b>%s".printf (function.name, expression));
-        label.set_margin_start (6);
-        label.set_use_markup (true);
-        label.halign = Gtk.Align.START;
-        hbox.pack_start (label, true, true, 0);
-
-        if (function.is_custom_function ())
-        {
-            var button = new Gtk.Button.from_icon_name ("document-edit-symbolic");
-            button.get_style_context ().add_class ("flat");
-            button.set_data<MathFunction> ("function", function);
-            button.clicked.connect (save_function_cb);
-            hbox.pack_start (button, false, true, 0);
-
-            button = new Gtk.Button.from_icon_name ("list-remove-symbolic");
-            button.get_style_context ().add_class ("flat");
-            button.set_data<MathFunction> ("function", function);
-            button.clicked.connect (delete_function_cb);
-            hbox.pack_start (button, false, true, 0);
-        }
-        hbox.show_all ();
-        return hbox;
+        string text = "<b>%s</b>%s".printf (function.name, expression);
+        return text;
     }
 
-
     protected override int get_item_index (MathFunction item)
     {
         uint position;
diff --git a/src/math-popover.vala b/src/math-popover.vala
index 1fc3a5f9..aba349c1 100644
--- a/src/math-popover.vala
+++ b/src/math-popover.vala
@@ -43,6 +43,50 @@ public abstract class MathPopover<T> : Gtk.Popover
 
     protected abstract int get_item_index (T item);
 
-    protected abstract Gtk.Widget make_item_row (T item);
+    protected abstract bool is_deletable (T item);
+    protected abstract bool is_editable (T item);
+    protected abstract string get_item_text (T item);
 
+    public signal void item_edited(T item);
+    public signal void item_deleted(T item);
+
+    protected Gtk.Widget make_item_row (T item)
+    {
+        var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
+
+        var label = new Gtk.Label (get_item_text (item));
+        label.set_margin_start (6);
+        label.set_use_markup (true);
+        label.halign = Gtk.Align.START;
+        hbox.pack_start (label, true, true, 0);
+
+        if (is_editable (item))
+        {
+            var button = new Gtk.Button.from_icon_name ("document-edit-symbolic");
+            button.get_style_context ().add_class ("flat");
+            button.set_data<Object> ("object", item as Object);
+            button.clicked.connect (save_function_cb);
+            hbox.pack_start (button, false, true, 0);
+        }
+        if (is_deletable (item))
+        {
+            var button = new Gtk.Button.from_icon_name ("list-remove-symbolic");
+            button.get_style_context ().add_class ("flat");
+            button.set_data<Object> ("object", item as Object);
+            button.clicked.connect (delete_function_cb);
+            hbox.pack_start (button, false, true, 0);
+        }
+        hbox.show_all ();
+        return hbox;
+    }
+
+    private void save_function_cb (Gtk.Widget widget)
+    {
+        item_edited((T)widget.get_data<Object> ("object"));
+    }
+
+    private void delete_function_cb (Gtk.Widget widget)
+    {
+        item_deleted((T)widget.get_data<Object> ("object"));
+    }
 }
diff --git a/src/math-variable-popover.vala b/src/math-variable-popover.vala
index e7359958..2318934d 100644
--- a/src/math-variable-popover.vala
+++ b/src/math-variable-popover.vala
@@ -48,7 +48,7 @@ public class MathVariablePopover : MathPopover<MathVariable>
 
         variable_list.bind_model (model, (variable) => make_item_row (variable as MathVariable));
         equation.history_signal.connect (this.handler);
-
+        item_deleted.connect (delete_variable_cb);
     }
 
     protected override int get_item_index (MathVariable item)
@@ -98,16 +98,23 @@ public class MathVariablePopover : MathPopover<MathVariable>
         variable_name_entry.set_text ("");
     }
 
-    private void delete_variable_cb (Gtk.Widget widget)
+    private void delete_variable_cb (MathVariable variable)
+    {
+        equation.variables.delete (variable.name);
+    }
+
+    protected override bool is_deletable (MathVariable variable)
     {
-        var name = widget.get_data<string> ("variable_name");
-        equation.variables.delete (name);
+        return !(variable.name in RESERVED_VARIABLE_NAMES);
     }
 
-    protected override Gtk.Widget make_item_row (MathVariable variable)
+    protected override bool is_editable (MathVariable variable)
     {
-        var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
+        return false;
+    }
 
+    protected override string get_item_text (MathVariable variable)
+    {
         string text;
         if (variable.value != null)
         {
@@ -116,24 +123,7 @@ public class MathVariablePopover : MathPopover<MathVariable>
         }
         else
             text = "<b>%s</b>".printf (variable.name);
-
-        var label = new Gtk.Label (text);
-        label.set_margin_start (6);
-        label.set_use_markup (true);
-        label.halign = Gtk.Align.START;
-        hbox.pack_start (label, true, true, 0);
-
-        if (!(variable.name in RESERVED_VARIABLE_NAMES))
-        {
-            var button = new Gtk.Button.from_icon_name ("list-remove-symbolic");
-            button.get_style_context ().add_class ("flat");
-            button.set_data<string> ("variable_name", variable.name);
-            button.clicked.connect (delete_variable_cb);
-            hbox.pack_start (button, false, true, 0);
-        }
-
-        hbox.show_all ();
-        return hbox;
+        return text;
     }
 
 }


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