[gnome-calculator/wip-gtk4-port] Use MathPopover as base for MathFunctionPopover



commit d3a33a3b6bac6e78aa2e5bbb8f81c5eaf3acb0f2
Author: Robert Roth <robert roth off gmail com>
Date:   Fri Oct 1 12:10:34 2021 +0300

    Use MathPopover as base for MathFunctionPopover

 lib/math-function.vala          | 10 +++++++++
 src/math-buttons.vala           |  8 +++----
 src/math-function-popover.vala  | 46 ++++++++++++++---------------------------
 src/ui/math-function-popover.ui |  2 +-
 4 files changed, 30 insertions(+), 36 deletions(-)
---
diff --git a/lib/math-function.vala b/lib/math-function.vala
index de3b44a3..4373d360 100644
--- a/lib/math-function.vala
+++ b/lib/math-function.vala
@@ -31,6 +31,16 @@ public class MathFunction : Object
         get { return _description; }
     }
 
+    public static int compare_func (MathFunction function1, MathFunction function2)
+    {
+        return strcmp (function1.name, function2.name);
+    }
+
+    public static bool equal_func (MathFunction function1, MathFunction function2)
+    {
+        return function1.name == function2.name;
+    }
+
     public MathFunction (string function_name, string[] arguments, string? expression, string? description)
     {
         _name = function_name;
diff --git a/src/math-buttons.vala b/src/math-buttons.vala
index 89bbfeeb..1648510d 100644
--- a/src/math-buttons.vala
+++ b/src/math-buttons.vala
@@ -479,7 +479,7 @@ public class MathButtons : Gtk.Box
         return panel;
     }
 
-    private ListStore build_functions_model (ListStore model, MathFunctionPopover math_popover, 
FunctionManager function_manager)
+    private ListStore build_functions_model (ListStore model, MathPopover<MathFunction> math_popover, 
FunctionManager function_manager)
     {
         var names = function_manager.get_names ();
 
@@ -489,9 +489,9 @@ public class MathButtons : Gtk.Box
             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);
+        function_manager.function_added.connect (f=>math_popover.item_added_cb(f as MathFunction));
+        function_manager.function_edited.connect (f=>math_popover.item_edited_cb(f as MathFunction));
+        function_manager.function_deleted.connect (f=>math_popover.item_deleted_cb(f as MathFunction));
         return model;
     }
 
diff --git a/src/math-function-popover.vala b/src/math-function-popover.vala
index 989e61c4..cea097a9 100644
--- a/src/math-function-popover.vala
+++ b/src/math-function-popover.vala
@@ -8,14 +8,16 @@
  * license.
  */
 
+class Something
+{
+
+}
 [GtkTemplate (ui = "/org/gnome/calculator/math-function-popover.ui")]
-public class MathFunctionPopover : Gtk.Popover
+public class MathFunctionPopover : MathPopover<MathFunction>
 {
     // Used to pretty print function arguments, e.g. f(x, y, z)
     private static string[] FUNCTION_ARGS = {"x","y","z","u","v","w","a","b","c","d"};
 
-    private MathEquation equation;
-
     [GtkChild]
     private unowned Gtk.ListBox function_list;
 
@@ -28,38 +30,16 @@ public class MathFunctionPopover : Gtk.Popover
     [GtkChild]
     private unowned Gtk.SpinButton add_arguments_button;
 
-    private ListStore model;
-
     public MathFunctionPopover (MathEquation equation, ListStore model)
     {
-        this.equation = equation;
+        base (equation, model, (a,b) => MathFunction.compare_func (a as MathFunction,b as MathFunction));
 
-        this.model = model;
-
-        function_list.bind_model (model, make_function_row);
+        function_list.bind_model (model, (item) => make_item_row(item as MathFunction));
 
         add_arguments_button.set_range (1, 10);
         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)
     {
@@ -130,9 +110,8 @@ public class MathFunctionPopover : Gtk.Popover
         function_manager.delete (function.name);
     }
 
-    private Gtk.Widget make_function_row (Object param)
+    protected override Gtk.Widget make_item_row (MathFunction function)
     {
-        MathFunction function = param as MathFunction;
         var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
 
         var expression = "(x)";
@@ -163,9 +142,14 @@ public class MathFunctionPopover : Gtk.Popover
         return hbox;
     }
 
-    private static int function_compare (Object function1, Object function2)
+
+    public override int get_item_index (MathFunction item)
     {
-        return strcmp ((function1 as MathFunction).name, (function2 as MathFunction).name);
+        uint position;
+        if (model.find_with_equal_func (item as Object, (a, b) => (MathFunction.equal_func(a as 
MathFunction, b as MathFunction)), out position))
+            return (int)position;
+        else
+            return -1;
     }
 
 }
diff --git a/src/ui/math-function-popover.ui b/src/ui/math-function-popover.ui
index 6142dd41..2e3bbf14 100644
--- a/src/ui/math-function-popover.ui
+++ b/src/ui/math-function-popover.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk+" version="3.16"/>
-  <template class="MathFunctionPopover" parent="GtkPopover">
+  <template class="MathFunctionPopover" parent="MathPopover">
     <property name="can_focus">False</property>
     <child>
       <object class="GtkBox" id="vbox">


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