[iagno] Move Undo action in a menu.



commit 68c9954c74a97dae8623f987c0ffdb1b081d9db4
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Thu Feb 28 16:50:21 2019 +0100

    Move Undo action in a menu.

 data/ui/iagno.ui     |  26 +++++++++---
 src/game-view.vala   |   4 ++
 src/game-window.vala | 109 +++++++++++++++++++++++++++++++--------------------
 src/iagno.vala       |  19 +++++----
 4 files changed, 101 insertions(+), 57 deletions(-)
---
diff --git a/data/ui/iagno.ui b/data/ui/iagno.ui
index e20118d..c8de818 100644
--- a/data/ui/iagno.ui
+++ b/data/ui/iagno.ui
@@ -57,16 +57,11 @@
             <property name="halign">center</property>
             <property name="valign">center</property>
             <property name="action-name">ui.new-game</property>
+            <property name="can-default">True</property>
             <!-- Translators: during a game, tooltip text of the New Game button -->
             <property name="tooltip-text" translatable="yes">Start a new game</property>
           </object>
         </child>
-        <child>
-          <object class="GtkBox" id="controls_box">
-            <property name="visible">False</property>
-            <property name="orientation">horizontal</property>
-          </object>
-        </child>
         <child>
           <object class="GtkButton" id="back_button">
             <property name="visible">False</property>
@@ -89,6 +84,25 @@
             </child>
           </object>
         </child>
+        <child>
+          <object class="GtkMenuButton" id="history_button">
+            <property name="visible">False</property>
+            <property name="valign">center</property>
+            <property name="can-focus">True</property>
+            <property name="focus-on-click">False</property>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon-name">document-open-recent-symbolic</property> <!-- or 
content-loading-symbolic -->
+                <property name="icon-size">1</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="pack-type">end</property>
+            <property name="position">2</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkMenuButton" id="info_button">
             <property name="visible">True</property>
diff --git a/src/game-view.vala b/src/game-view.vala
index a82b07d..da22afc 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -829,6 +829,8 @@ private class GameView : Gtk.DrawingArea
     * * game complete
     \*/
 
+    internal signal void notify_final_animation (bool undo);
+
     private bool flip_final_result_now = false;  // the final animation is delayed until this is true
 
     /* set only when a game is finished */
@@ -854,6 +856,7 @@ private class GameView : Gtk.DrawingArea
                     if (!game.is_complete)  // in case an undo has been called
                         return Source.REMOVE;
 
+                    notify_final_animation (/* undoing */ false);
                     set_winner_and_loser_variables ();
                     flip_final_result_now = true;
                     for (uint8 x = 0; x < game_size; x++)
@@ -901,6 +904,7 @@ private class GameView : Gtk.DrawingArea
         if (!flip_final_result_now)
             return false;
 
+        notify_final_animation (/* undoing */ true);
         flip_final_result_now = false;
         update_squares ();
 
diff --git a/src/game-window.vala b/src/game-window.vala
index b7650a0..70ce7e0 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -49,7 +49,6 @@ private class GameWindow : ApplicationWindow
     [GtkChild] private Button back_button;
     [GtkChild] private Button unfullscreen_button;
 
-    [GtkChild] private Box controls_box;
     [GtkChild] private Box game_box;
     [GtkChild] private Box new_game_box;
     [GtkChild] private Box side_box;
@@ -117,51 +116,14 @@ private class GameWindow : ApplicationWindow
             start_game_button = _start_game_button;
         }
 
+        configure_history_button ();
+
         game_box.pack_start (view, true, true, 0);
         game_box.set_focus_child (view);            // TODO test if necessary; note: view could grab focus 
from application
         view.halign = Align.FILL;
         view.can_focus = true;
         view.show ();
 
-        /* add controls */
-        if (GameWindowFlags.SHOW_UNDO in flags)
-        {
-            Box history_box = new Box (Orientation.HORIZONTAL, 0);
-            history_box.get_style_context ().add_class ("linked");
-
-            Button undo_button = new Button.from_icon_name ("edit-undo-symbolic", Gtk.IconSize.BUTTON);
-            undo_button.action_name = "ui.undo";
-            /* Translators: during a game, tooltip text of the Undo button */
-            undo_button.set_tooltip_text (_("Undo your most recent move"));
-            undo_button.valign = Align.CENTER;
-            undo_button.show ();
-            history_box.pack_start (undo_button, true, true, 0);
-
-            /* if (GameWindowFlags.SHOW_REDO in flags)
-            {
-                Button redo_button = new Button.from_icon_name ("edit-redo-symbolic", Gtk.IconSize.BUTTON);
-                redo_button.action_name = "app.redo";
-                / Translators: during a game, tooltip text of the Redo button /
-                redo_button.set_tooltip_text (_("Redo your most recent undone move"));
-                redo_button.valign = Align.CENTER;
-                redo_button.show ();
-                history_box.pack_start (redo_button, true, true, 0);
-            } */
-
-            history_box.show ();
-            controls_box.pack_start (history_box, true, true, 0);
-        }
-        /* if (GameWindowFlags.SHOW_HINT in flags)
-        {
-            Button hint_button = new Button.from_icon_name ("dialog-question-symbolic", Gtk.IconSize.BUTTON);
-            hint_button.action_name = "app.hint";
-            / Translators: during a game, tooltip text of the Hint button /
-            hint_button.set_tooltip_text (_("Receive a hint for your next move"));
-            hint_button.valign = Align.CENTER;
-            hint_button.show ();
-            controls_box.pack_start (hint_button, true, true, 0);
-        } */
-
         /* start or not */
         if (start_now)
             show_view ();
@@ -287,7 +249,11 @@ private class GameWindow : ApplicationWindow
     internal void finish_game ()
     {
         game_finished = true;
-        new_game_button.grab_focus ();
+        if (!history_button.active)
+            new_game_button.grab_focus ();
+        else
+            new_game_button.grab_default ();
+        set_history_button_label (Player.NONE);
     }
 
     /* internal void about ()
@@ -304,8 +270,8 @@ private class GameWindow : ApplicationWindow
         headerbar.set_subtitle (null);      // TODO save / restore?
 
         stack.set_visible_child_name ("start-box");
-        controls_box.hide ();
         new_game_button.hide ();
+        history_button.hide ();
 
         if (!game_finished && back_button.visible)
             back_button.grab_focus ();
@@ -318,7 +284,7 @@ private class GameWindow : ApplicationWindow
         stack.set_visible_child_name ("frame");
         back_button.hide ();        // TODO transition?
         new_game_button.show ();
-        controls_box.show ();
+        history_button.show ();
 
         if (game_finished)
             new_game_button.grab_focus ();
@@ -358,6 +324,8 @@ private class GameWindow : ApplicationWindow
         undo_action.set_enabled (false);
      // redo_action.set_enabled (false);
 
+        history_button_new_game ();
+
         play ();        // FIXME lag (see in Taquin…)
 
         stack.set_transition_type (StackTransitionType.SLIDE_DOWN);
@@ -432,4 +400,59 @@ private class GameWindow : ApplicationWindow
     {
         info_button.active = !info_button.active;
     }
+
+    /*\
+    * * history menu
+    \*/
+
+    [GtkChild] private MenuButton history_button;
+
+    private GLib.Menu history_menu;
+    private GLib.Menu finish_menu;
+
+    private string history_button_light_label;
+    private string history_button_dark_label;
+
+    private void configure_history_button ()
+    {
+        history_menu = new GLib.Menu ();
+        /* Translators: history menu entry (with a mnemonic that appears pressing Alt) */
+        history_menu.append (_("_Undo last move"), "ui.undo");
+        history_menu.freeze ();
+
+        finish_menu = new GLib.Menu ();
+        /* Translators: history menu entry, when game is finished, after final animation; undoes the 
animation (with a mnemonic that appears pressing Alt) */
+        finish_menu.append (_("_Show final board"), "ui.undo");
+        finish_menu.freeze ();
+
+        bool dir_is_ltr = get_locale_direction () == TextDirection.LTR;
+        history_button_light_label = dir_is_ltr ? "‎⮚ ⚪" : /* yes */ "‏⮘ ⚪";    /* both have an LTR/RTL mark 
*/
+        history_button_dark_label  = dir_is_ltr ? "‎⮚ ⚫" : /* yes */ "‏⮘ ⚫";    /* both have an LTR/RTL mark 
*/
+
+        ((GameView) view).notify_final_animation.connect ((undoing) => { update_history_button (!undoing); 
});
+
+        history_button_new_game ();
+    }
+
+    private inline void update_history_button (bool finish_animation)
+    {
+        history_button.set_menu_model (finish_animation ? finish_menu : history_menu);
+    }
+
+    private inline void history_button_new_game ()
+    {
+        set_history_button_label (Player.DARK);
+        update_history_button (/* final animation */ false);
+    }
+
+    internal void set_history_button_label (Player player)
+    {
+        switch (player)
+        {
+            case Player.LIGHT:  history_button.set_label (history_button_light_label);  return;
+            case Player.DARK:   history_button.set_label (history_button_dark_label);   return;
+            case Player.NONE:   history_button.set_label (_("Finished!"));              return;
+            default: assert_not_reached ();
+        }
+    }
 }
diff --git a/src/iagno.vala b/src/iagno.vala
index 4e0b786..96cdccf 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -497,8 +497,7 @@ private class Iagno : Gtk.Application
         }
 
         update_ui ();
-        play_sound (Sound.FLIP);
-        view.update_scoreboard ();
+        update_scoreboard ();
     }
 
     private void turn_ended_cb (bool undoing, bool no_draw)
@@ -520,9 +519,7 @@ private class Iagno : Gtk.Application
     private void prepare_move ()
         requires (game_is_set)
     {
-        /* for the move that just ended */
-        play_sound (Sound.FLIP);
-        view.update_scoreboard ();
+        update_scoreboard ();
 
         /*
          * Get the computer to move after a delay, so it looks like it's
@@ -536,9 +533,7 @@ private class Iagno : Gtk.Application
     private void pass ()
         requires (game_is_set)
     {
-        /* for the move that just ended */
-        play_sound (Sound.FLIP);
-        view.update_scoreboard ();
+        update_scoreboard ();
 
         if (!game.pass ())
             assert_not_reached ();
@@ -600,6 +595,14 @@ private class Iagno : Gtk.Application
         window.clear_subtitle ();
     }
 
+    private void update_scoreboard ()
+    {
+        /* for the move that just ended */
+        play_sound (Sound.FLIP);
+        view.update_scoreboard ();
+        window.set_history_button_label (game.current_color);
+    }
+
     /*\
     * * Sound
     \*/


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