[iagno/arnaudb/wip/gtk4: 33/54] Try to fix HistoryButton.



commit eabf636f4c3274f27a9aac618bf7fc6e07cde45b
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Feb 19 03:05:41 2020 +0100

    Try to fix HistoryButton.

 data/ui/history-button.ui |  2 +-
 src/history-button.vala   | 23 ++++++++++++++++-------
 src/iagno.vala            |  8 ++++----
 3 files changed, 21 insertions(+), 12 deletions(-)
---
diff --git a/data/ui/history-button.ui b/data/ui/history-button.ui
index c207830..6dfb1a5 100644
--- a/data/ui/history-button.ui
+++ b/data/ui/history-button.ui
@@ -21,7 +21,7 @@
 -->
 <interface>
   <requires lib="gtk+" version="3.96"/>
-  <template class="HistoryButton" parent="GtkMenuButton">
+  <template class="HistoryButton" parent="GtkToggleButton">
     <property name="valign">center</property>
     <property name="can-focus">True</property>
     <property name="focus-on-click">False</property>
diff --git a/src/history-button.vala b/src/history-button.vala
index 5b45703..296e37c 100644
--- a/src/history-button.vala
+++ b/src/history-button.vala
@@ -21,13 +21,28 @@
 using Gtk;
 
 [GtkTemplate (ui = "/org/gnome/Reversi/ui/history-button.ui")]
-private class HistoryButton : MenuButton, AdaptativeWidget
+private class HistoryButton : ToggleButton, AdaptativeWidget
 {
     [CCode (notify = false)] public ThemeManager theme_manager { private get; protected construct; }
 
+    ulong toggled_handler = 0;
+    [CCode (notify = false)] public MenuModel menu_model
+    {
+        internal construct set
+        {
+            if (toggled_handler != 0)
+                disconnect (toggled_handler);
+            popover = new PopoverMenu.from_model (this, value);
+            popover.set_autohide (false);
+            toggled_handler = toggled.connect ((_this) => { if (_this.get_active ()) ((HistoryButton) 
_this).popover.popup (); else ((HistoryButton) _this).popover.popdown (); }); // toggled is run-first
+        }
+    }
+
     [GtkChild] private Stack stack;
     [GtkChild] private DrawingArea drawing;
 
+    private PopoverMenu popover;
+
     internal HistoryButton (GLib.Menu menu, ThemeManager theme_manager)
     {
         Object (menu_model: menu, theme_manager: theme_manager);
@@ -62,11 +77,6 @@ private class HistoryButton : MenuButton, AdaptativeWidget
         }
     }
 
-    internal inline void update_menu (GLib.Menu menu)
-    {
-        set_menu_model (menu);
-    }
-
     /*\
     * * drawing
     \*/
@@ -122,7 +132,6 @@ private class HistoryButton : MenuButton, AdaptativeWidget
 
         draw_arrow (cr);
         draw_piece (cr);
-        return;
     }
 
     private const double arrow_margin_top = 3.0;
diff --git a/src/iagno.vala b/src/iagno.vala
index 79f3d03..fc1adef 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -416,8 +416,8 @@ private class Iagno : Gtk.Application, BaseApplication
         history_button_1 = new HistoryButton (history_menu, theme_manager);
         history_button_2 = new HistoryButton (history_menu, theme_manager);
         view.notify_final_animation.connect ((undoing) => {
-                history_button_1.update_menu (undoing ? history_menu : finish_menu);
-                history_button_2.update_menu (undoing ? history_menu : finish_menu);
+                history_button_1.menu_model = undoing ? history_menu : finish_menu;
+                history_button_2.menu_model = undoing ? history_menu : finish_menu;
             });
         history_button_1.show ();
         history_button_2.show ();
@@ -765,8 +765,8 @@ private class Iagno : Gtk.Application, BaseApplication
 
         history_button_1.set_player (Player.DARK);
         history_button_2.set_player (Player.DARK);
-        history_button_1.update_menu (history_menu);
-        history_button_2.update_menu (history_menu);
+        history_button_1.menu_model = history_menu;
+        history_button_2.menu_model = history_menu;
 
         if (two_players)
             computer = null;


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