[gnome-calculator/wip-gtk4-port] Refactoring model building out of the popover



commit 0079f038782493ae39203ffd673d6f742e18fcfa
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Oct 1 10:55:46 2021 +0300

    Refactoring model building out of the popover

 src/math-buttons.vala          | 25 ++++++++++++++++++++++-
 src/math-function-popover.vala | 46 ++++++++++++++++++------------------------
 2 files changed, 44 insertions(+), 27 deletions(-)
---
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index 470d8024..89bbfeeb 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -427,8 +427,15 @@ public class MathButtons : Gtk.Box
         if (menu_button != null)
             menu_button.popover = new MathVariablePopover (equation);
         menu_button = builder.get_object ("calc_function_button") as Gtk.MenuButton;
+
+        FunctionManager function_manager = FunctionManager.get_default_function_manager ();
         if (menu_button != null)
-            menu_button.popover = new MathFunctionPopover (equation);
+        {
+            var model = new ListStore(typeof(MathFunction));
+            MathFunctionPopover math_popover = new MathFunctionPopover (equation, model);
+            build_functions_model (model, math_popover, function_manager);
+            menu_button.popover = math_popover;
+        }
 
         if (mode == ButtonMode.PROGRAMMING)
         {
@@ -472,6 +479,22 @@ public class MathButtons : Gtk.Box
         return panel;
     }
 
+    private ListStore build_functions_model (ListStore model, MathFunctionPopover math_popover, 
FunctionManager function_manager)
+    {
+        var names = function_manager.get_names ();
+
+        for (var i = 0; names[i] != null; i++)
+        {
+            var function = function_manager[names[i]];
+            math_popover.item_added_cb (function);
+        }
+
+        function_manager.function_added.connect (math_popover.item_added_cb);
+        function_manager.function_edited.connect (math_popover.item_edited_cb);
+        function_manager.function_deleted.connect (math_popover.item_deleted_cb);
+        return model;
+    }
+
     private void converter_changed_cb ()
     {
         Unit from_unit, to_unit;
diff --git a/src/math-function-popover.vala b/src/math-function-popover.vala
index 80475bff..7d5ace17 100644
--- a/src/math-function-popover.vala
+++ b/src/math-function-popover.vala
@@ -30,35 +30,11 @@ public class MathFunctionPopover : Gtk.Popover
 
     private ListStore model;
 
-    public MathFunctionPopover (MathEquation equation)
+    public MathFunctionPopover (MathEquation equation, ListStore model)
     {
         this.equation = equation;
 
-        model = new ListStore(typeof(MathFunction));
-
-        FunctionManager function_manager = FunctionManager.get_default_function_manager ();
-        var names = function_manager.get_names ();
-
-        for (var i = 0; names[i] != null; i++)
-        {
-            var function = function_manager[names[i]];
-            model.insert_sorted (function, function_compare);
-        }
-
-        function_manager.function_added.connect ((function) => {
-            model.insert_sorted (function, function_compare);
-        });
-        function_manager.function_edited.connect ((function) => {
-            uint position;
-            if (model.find_with_equal_func (function, (a, b) => (function_compare (a,b) == 0), out position))
-                model.remove (position);
-            model.insert_sorted (function, function_compare);
-        });
-        function_manager.function_deleted.connect ((function) => {
-            uint position;
-            if (model.find_with_equal_func (function, (a, b) => (function_compare (a,b) == 0), out position))
-                model.remove (position);
-        });
+        this.model = model;
 
         function_list.bind_model (model, make_function_row);
 
@@ -66,6 +42,24 @@ public class MathFunctionPopover : Gtk.Popover
         add_arguments_button.set_increments (1, 1);
     }
 
+    public void item_added_cb (MathFunction function)
+    {
+        model.insert_sorted (function, function_compare);
+    }
+
+    public void item_edited_cb (MathFunction function)
+    {
+        item_deleted_cb (function);
+        item_added_cb (function);
+    }
+
+    public void item_deleted_cb (MathFunction function)
+    {
+        uint position;
+        if (model.find_with_equal_func (function, (a, b) => (function_compare (a,b) == 0), out position))
+            model.remove (position);
+    }
+
     [GtkCallback]
     private void insert_function_cb (Gtk.ListBoxRow row)
     {


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