[iagno] Manage History button from application.



commit 9a87023aa3dd0ce0e0885eda65f024419ef0c362
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Dec 6 14:09:53 2019 +0100

    Manage History button from application.

 data/ui/game-headerbar.ui |  9 ---------
 src/game-headerbar.vala   | 46 +++++++++++++++++++---------------------------
 src/game-window.vala      | 13 ++-----------
 src/history-button.vala   | 16 +++++++++++++---
 src/iagno.vala            | 11 ++++++++++-
 5 files changed, 44 insertions(+), 51 deletions(-)
---
diff --git a/data/ui/game-headerbar.ui b/data/ui/game-headerbar.ui
index b1ee37a..57af371 100644
--- a/data/ui/game-headerbar.ui
+++ b/data/ui/game-headerbar.ui
@@ -63,14 +63,5 @@
         </child>
       </object>
     </child>
-    <child>
-      <object class="HistoryButton" id="history_button">
-        <property name="visible">False</property>
-      </object>
-      <packing>
-        <property name="pack-type">end</property>
-        <property name="position">5</property>
-      </packing>
-    </child>
   </template>
 </interface>
diff --git a/src/game-headerbar.vala b/src/game-headerbar.vala
index b5b7b2b..d1735f3 100644
--- a/src/game-headerbar.vala
+++ b/src/game-headerbar.vala
@@ -23,7 +23,6 @@ using Gtk;
 [GtkTemplate (ui = "/org/gnome/Reversi/ui/game-headerbar.ui")]
 private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
 {
-    [GtkChild] private HistoryButton    history_button;
     [GtkChild] private Button           new_game_button;
     [GtkChild] private Button           back_button;
 
@@ -35,18 +34,24 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
     [CCode (notify = false)] public bool show_redo { private get; protected construct; default = false; }
     [CCode (notify = false)] public bool show_hint { private get; protected construct; default = false; }    
// TODO something
 
+    [CCode (notify = false)] public Widget? game_widget { private get; protected construct; default = null; }
+
     construct
     {
         init_modes ();
 
         if (window_name != "")
             window_has_name = true;
+
+        if (game_widget != null)
+            pack_end ((!) game_widget);
     }
 
     internal GameHeaderBar (string              _window_name,
                             string              _about_action_label,
                             GameWindowFlags     flags,
                             GLib.Menu?          _appearance_menu,
+                            Widget?             _game_widget,
                             NightLightMonitor   _night_light_monitor)
     {
         Object (about_action_label:     _about_action_label,
@@ -58,6 +63,7 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
                 show_redo:              GameWindowFlags.SHOW_REDO in flags,
                 show_undo:              GameWindowFlags.SHOW_UNDO in flags,
                 appearance_menu:        _appearance_menu,
+                game_widget:            _game_widget,
                 window_name:            _window_name);
     }
 
@@ -70,6 +76,9 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
     {
         base.set_window_size (new_size);
 
+        if (game_widget != null)
+            ((AdaptativeWidget) (!) game_widget).set_window_size (new_size);
+
         if (!window_has_name)
             return;
 
@@ -77,7 +86,6 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
         if (_is_extra_thin == is_extra_thin)
             return;
         is_extra_thin = _is_extra_thin;
-        history_button.is_extra_thin = is_extra_thin;
         set_default_widgets_default_states (this);
     }
 
@@ -107,7 +115,8 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
         current_view_is_new_game_screen = true;
 
      // new_game_button.hide ();
-        history_button.hide ();
+        if (game_widget != null)
+            ((!) game_widget).hide ();
 
         if (!game_finished && back_button.visible)
         {
@@ -124,7 +133,8 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
 
         back_button.hide ();        // TODO transition?
         new_game_button.show ();    // TODO transition?
-        history_button.show ();
+        if (game_widget != null)
+            ((!) game_widget).show ();
 
         if (game_finished)
         {
@@ -159,11 +169,10 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
 
     internal void finish_game ()
     {
-        if (!history_button.active)
+        if (game_widget != null && ((!) game_widget) is MenuButton && !((MenuButton) (!) game_widget).active)
             new_game_button.grab_focus ();
         else
             new_game_button.grab_default ();    // FIXME: grab_focus, but without closing the popover...
-        set_history_button_label (Player.NONE);
     }
 
     internal void update_title (string new_title)
@@ -219,34 +228,17 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
             }
             else
             {
-                real_this.history_button.show ();
+                if (real_this.game_widget != null)
+                    ((!) real_this.game_widget).show ();
                 real_this.new_game_button.show ();
             }
         }
         else
         {
             real_this.back_button.hide ();
-            real_this.history_button.hide ();
+            if (real_this.game_widget != null)
+                ((!) real_this.game_widget).hide ();
             real_this.new_game_button.hide ();
         }
     }
-
-    /*\
-    * * history menu
-    \*/
-
-    internal inline void update_history_button (bool finish_animation)
-    {
-        history_button.update_menu (finish_animation);
-    }
-
-    internal inline void history_button_new_game ()
-    {
-        history_button.new_game ();
-    }
-
-    internal void set_history_button_label (Player player)
-    {
-        history_button.update_label (player);
-    }
 }
diff --git a/src/game-window.vala b/src/game-window.vala
index 714619e..a16d416 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -40,9 +40,9 @@ private class GameWindow : BaseWindow, AdaptativeWidget
     private GameView        game_view;
     private Box             new_game_screen;
 
-    internal GameWindow (string? css_resource, string name, string about_action_label, bool start_now, 
GameWindowFlags flags, Box _new_game_screen, Widget view_content, GLib.Menu? appearance_menu, 
NightLightMonitor night_light_monitor)
+    internal GameWindow (string? css_resource, string name, string about_action_label, bool start_now, 
GameWindowFlags flags, Box _new_game_screen, Widget view_content, GLib.Menu? appearance_menu, Widget? 
game_widget, NightLightMonitor night_light_monitor)
     {
-        GameHeaderBar _headerbar = new GameHeaderBar (name, about_action_label, flags, appearance_menu, 
night_light_monitor);
+        GameHeaderBar _headerbar = new GameHeaderBar (name, about_action_label, flags, appearance_menu, 
game_widget, night_light_monitor);
         GameView      _game_view = new GameView (flags, _new_game_screen, view_content);
 
         Object (nta_headerbar               : (NightTimeAwareHeaderBar) _headerbar,
@@ -56,8 +56,6 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         game_view = _game_view;
         new_game_screen = _new_game_screen;
 
-        ((ReversiView) view_content).notify_final_animation.connect ((undoing) => { 
headerbar.update_history_button (!undoing); });
-
         /* CSS */
         if (css_resource != null)
         {
@@ -259,11 +257,6 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         headerbar.set_subtitle (null);
     }
 
-    internal void set_history_button_label (Player player)
-    {
-        headerbar.set_history_button_label (player);
-    }
-
     internal void update_title (string game_name)
     {
         headerbar.update_title (game_name);
@@ -297,8 +290,6 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         undo_action.set_enabled (false);
      // redo_action.set_enabled (false);
 
-        headerbar.history_button_new_game ();
-
         play ();        // FIXME lag (see in Taquin…)
 
         game_view.configure_transition (StackTransitionType.SLIDE_DOWN, 1000);
diff --git a/src/history-button.vala b/src/history-button.vala
index 704d1e4..b79c192 100644
--- a/src/history-button.vala
+++ b/src/history-button.vala
@@ -21,10 +21,8 @@
 using Gtk;
 
 [GtkTemplate (ui = "/org/gnome/Reversi/ui/history-button.ui")]
-private class HistoryButton : MenuButton
+private class HistoryButton : MenuButton, AdaptativeWidget
 {
-    [CCode (notify = false)] internal bool is_extra_thin { private get; internal set; default = true; }
-
     private GLib.Menu history_menu;
     private GLib.Menu finish_menu;
 
@@ -50,6 +48,16 @@ private class HistoryButton : MenuButton
         new_game ();
     }
 
+    private bool is_extra_thin = true;
+    protected override void set_window_size (AdaptativeWidget.WindowSize new_size)
+    {
+        bool _is_extra_thin = AdaptativeWidget.WindowSize.is_extra_thin (new_size);
+        if (_is_extra_thin == is_extra_thin)
+            return;
+        is_extra_thin = _is_extra_thin;
+        update_label (last_player);
+    }
+
     /*\
     * * internal calls
     \*/
@@ -65,8 +73,10 @@ private class HistoryButton : MenuButton
         update_menu (/* final animation */ false);
     }
 
+    private Player last_player = Player.NONE;
     internal void update_label (Player player)
     {
+        last_player = player;
         switch (player)
         {
             case Player.LIGHT:
diff --git a/src/iagno.vala b/src/iagno.vala
index 3f7464b..80a767f 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -51,6 +51,7 @@ private class Iagno : Gtk.Application, BaseApplication
     private GameWindow window;
     private ReversiView view;
     private NewGameScreen new_game_screen;
+    private HistoryButton history_button;
 
     /* Computer player (if there is one) */
     internal ComputerPlayer? computer { internal get; private set; default = null; }
@@ -388,6 +389,10 @@ private class Iagno : Gtk.Application, BaseApplication
         appearance_menu.append_section (null, section);
         appearance_menu.freeze ();
 
+        history_button = new HistoryButton ();
+        view.notify_final_animation.connect ((undoing) => { history_button.update_menu (!undoing); });
+        history_button.show ();
+
         /* Window */
         init_night_mode ();
         window = new GameWindow ("/org/gnome/Reversi/ui/iagno.css",
@@ -403,6 +408,7 @@ private class Iagno : Gtk.Application, BaseApplication
                                  (Box) new_game_screen,
                                  view,
                                  appearance_menu,
+                                 history_button,
                                  night_light_monitor);
 
         window.play.connect (start_game);
@@ -674,6 +680,8 @@ private class Iagno : Gtk.Application, BaseApplication
         game.turn_ended.connect (turn_ended_cb);
         view.game = game;
 
+        history_button.new_game ();
+
         if (two_players)
             computer = null;
         else
@@ -816,6 +824,7 @@ private class Iagno : Gtk.Application, BaseApplication
         requires (game_is_set)
     {
         window.finish_game ();
+        history_button.update_label (Player.NONE);
 
         if ((!game.reverse && game.n_light_tiles > game.n_dark_tiles)
          || ( game.reverse && game.n_light_tiles < game.n_dark_tiles))
@@ -872,7 +881,7 @@ private class Iagno : Gtk.Application, BaseApplication
     {
         /* for the move that just ended */
         play_sound (Sound.FLIP);
-        window.set_history_button_label (game.current_color);
+        history_button.update_label (game.current_color);
     }
 
     private void set_window_title ()


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