[gnome-taquin] Add restart shortcut.



commit b2a1878c6ee983f20f0d2655633241c41560b381
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Jan 23 14:16:41 2019 +0100

    Add restart shortcut.

 data/help-overlay.ui |  7 ++++
 src/game-view.vala   |  2 +-
 src/game-window.vala | 97 ++++++++++++++++++++++++++++++++--------------------
 src/taquin-main.vala |  2 +-
 4 files changed, 69 insertions(+), 39 deletions(-)
---
diff --git a/data/help-overlay.ui b/data/help-overlay.ui
index 71f4073..1a2d2ba 100644
--- a/data/help-overlay.ui
+++ b/data/help-overlay.ui
@@ -53,6 +53,13 @@
                 <property name="accelerator">&lt;Primary&gt;n</property>
               </object>
             </child>
+            <child>
+              <object class="GtkShortcutsShortcut">
+                <property name="visible">True</property>
+                <property name="title" translatable="yes" context="shortcut window">Restart game</property>
+                <property name="accelerator">&lt;Primary&gt;&lt;Shift&gt;n</property>
+              </object>
+            </child>
             <child>
               <object class="GtkShortcutsShortcut">
                 <property name="visible">True</property>
diff --git a/src/game-view.vala b/src/game-view.vala
index a2486e4..7e4cdb7 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -57,7 +57,7 @@ private class GameView : BaseView, AdaptativeWidget
             start_game_button = new Button.with_mnemonic (_("_Start Game"));
             ((!) start_game_button).get_style_context ().add_class ("start-game-button");
             ((!) start_game_button).halign = Align.CENTER;
-            ((!) start_game_button).set_action_name ("ui.start-game");
+            ((!) start_game_button).set_action_name ("ui.start-or-restart");
             /* Translators: when configuring a new game, tooltip text of the blue Start button */
             // start_game_button.set_tooltip_text (_("Start a new game as configured"));
             ((StyleContext) ((!) start_game_button).get_style_context ()).add_class ("suggested-action");
diff --git a/src/game-window.vala b/src/game-window.vala
index 88b1500..804074d 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -138,6 +138,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
     public void set_moves_count (uint moves_count)
     {
         headerbar.set_moves_count (ref moves_count);
+        hide_notification ();
     }
 
     public void set_subtitle (string? subtitle)
@@ -223,49 +224,37 @@ private class GameWindow : BaseWindow, AdaptativeWidget
 
     private const GLib.ActionEntry [] ui_action_entries =
     {
-        { "new-game", new_game_cb },
-        { "start-game", start_game_cb },
-
-        { "restart", restart_cb },
-        {    "undo",    undo_cb },
-        {    "redo",    redo_cb },
+        { "start-or-restart",   start_or_restart_cb },  // "Start new game" button or <Shift><Primary>n
+        { "new-game",           new_game_cb },          // "New game" button or <Shift>n
+        { "restart",            restart_cb },           // "Restart" menu entry; keep action to allow 
disabling menu entry
 
+        { "undo", undo_cb },
+        { "redo", redo_cb },
         { "hint", hint_cb }
     };
 
-    private void new_game_cb (/* SimpleAction action, Variant? variant */)
+    private void start_or_restart_cb (/* SimpleAction action, Variant? variant */)
     {
         if (game_view.is_in_in_window_mode ())
             return;
-        if (!game_view.game_content_visible_if_true ())
-            return;
 
-        wait ();
-
-        game_view.configure_transition (StackTransitionType.SLIDE_LEFT, 800);
-
-        headerbar.new_game ();
-        back_action_disabled = false;
-        show_new_game_screen ();
+        if (!game_view.game_content_visible_if_true ())
+            start_game ();
+        else if (restart_action.get_enabled ())
+            restart_game ();
+        else
+            /* Translator: during a game, if the user tries with <Shift><Ctrl>n to restart the game, while 
already on initial position */
+            show_notification (_("Already on initial position."));
     }
 
-    private void start_game_cb (/* SimpleAction action, Variant? variant */)
+    private void new_game_cb (/* SimpleAction action, Variant? variant */)
     {
         if (game_view.is_in_in_window_mode ())
             return;
-        if (game_view.game_content_visible_if_true ())
+        if (!game_view.game_content_visible_if_true ())
             return;
 
-        game_finished = false;
-
-        restart_action.set_enabled (false);
-           undo_action.set_enabled (false);
-           redo_action.set_enabled (false);
-
-        play ();        // FIXME lag (see in Taquin…)
-
-        game_view.configure_transition (StackTransitionType.SLIDE_DOWN, 1000);
-        show_view ();
+        new_game ();
     }
 
     private void restart_cb (/* SimpleAction action, Variant? variant */)
@@ -275,15 +264,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         if (!game_view.game_content_visible_if_true ())
             return;
 
-        game_finished = false;
-        hide_notification ();
-
-        if (headerbar.new_game_button_is_focus ())
-            game_view.show_game_content (/* grab focus */ true);
-        redo_action.set_enabled (true);
-        restart_action.set_enabled (false);
-
-        restart ();
+        restart_game ();
     }
 
     private void undo_cb (/* SimpleAction action, Variant? variant */)
@@ -327,4 +308,46 @@ private class GameWindow : BaseWindow, AdaptativeWidget
 
         hint ();
     }
+
+    /*\
+    * * actions helpers
+    \*/
+
+    private void start_game ()
+    {
+        game_finished = false;
+
+        restart_action.set_enabled (false);
+           undo_action.set_enabled (false);
+           redo_action.set_enabled (false);
+
+        play ();        // FIXME lag (see in Taquin…)
+
+        game_view.configure_transition (StackTransitionType.SLIDE_DOWN, 1000);
+        show_view ();
+    }
+
+    private void restart_game ()
+    {
+        game_finished = false;
+        hide_notification ();
+
+        if (headerbar.new_game_button_is_focus ())
+            game_view.show_game_content (/* grab focus */ true);
+        redo_action.set_enabled (true);
+        restart_action.set_enabled (false);
+
+        restart ();
+    }
+
+    private void new_game ()
+    {
+        wait ();
+
+        game_view.configure_transition (StackTransitionType.SLIDE_LEFT, 800);
+
+        headerbar.new_game ();
+        back_action_disabled = false;
+        show_new_game_screen ();
+    }
 }
diff --git a/src/taquin-main.vala b/src/taquin-main.vala
index 627599e..fb8435c 100644
--- a/src/taquin-main.vala
+++ b/src/taquin-main.vala
@@ -160,7 +160,7 @@ private class Taquin : Gtk.Application, BaseApplication
         set_accels_for_action ("base.copy",             {        "<Primary>c"       });
         set_accels_for_action ("base.copy-alt",         { "<Shift><Primary>c"       });
         set_accels_for_action ("ui.new-game",           {        "<Primary>n"       });
-        set_accels_for_action ("ui.start-game",         { "<Shift><Primary>n"       });
+        set_accels_for_action ("ui.start-or-restart",   { "<Shift><Primary>n"       });
         set_accels_for_action ("app.quit",              {        "<Primary>q",
                                                           "<Shift><Primary>q"       });
         set_accels_for_action ("base.paste",            {        "<Primary>v"       });


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