[gnome-taquin] Split out HistoryButton.



commit 394b3a0ee428f33780ad23ac48e77232ba5fd934
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Dec 7 14:30:08 2019 +0100

    Split out HistoryButton.

 data/ui/game-headerbar.ui |  14 +---
 data/ui/history-button.ui |  37 +++++++++++
 po/POTFILES.in            |   1 +
 po/POTFILES.skip          |   1 +
 src/game-headerbar.vala   | 119 ++--------------------------------
 src/game-window.vala      |   3 -
 src/history-button.vala   | 159 ++++++++++++++++++++++++++++++++++++++++++++++
 src/meson.build           |   1 +
 src/taquin-main.vala      |   3 +-
 src/taquin.gresource.xml  |   1 +
 10 files changed, 208 insertions(+), 131 deletions(-)
---
diff --git a/data/ui/game-headerbar.ui b/data/ui/game-headerbar.ui
index ec444c5..d82eada 100644
--- a/data/ui/game-headerbar.ui
+++ b/data/ui/game-headerbar.ui
@@ -20,20 +20,8 @@
   <template class="GameHeaderBar" parent="BaseHeaderBar">
     <property name="show-close-button">True</property>
     <child>
-      <object class="GtkMenuButton" id="history_button">
+      <object class="HistoryButton" id="history_button">
         <property name="visible">True</property>
-        <property name="valign">center</property>
-        <property name="focus-on-click">False</property>
-        <property name="label">0</property>
-        <property name="sensitive">False</property>
-        <child internal-child="accessible">
-          <object class="AtkObject">
-            <!-- Translators: accessible name of the history menubutton -->
-            <property name="AtkObject::accessible-name" translatable="yes">History</property>
-            <!-- Translators: accessible description of the history menubutton -->
-            <property name="AtkObject::accessible-description" translatable="yes">Game history 
menu</property>
-          </object>
-        </child>
       </object>
       <packing>
         <property name="pack-type">end</property>
diff --git a/data/ui/history-button.ui b/data/ui/history-button.ui
new file mode 100644
index 0000000..2c6e232
--- /dev/null
+++ b/data/ui/history-button.ui
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  This file is part of GNOME Taquin
+
+  GNOME Taquin is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  GNOME Taquin is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with GNOME Taquin.  If not, see <https://www.gnu.org/licenses/>.
+-->
+<interface>
+  <requires lib="gtk+" version="3.12"/>
+  <template class="HistoryButton" parent="GtkMenuButton">
+    <property name="valign">center</property>
+    <property name="can-focus">True</property>
+    <property name="focus-on-click">False</property>
+    <style>
+      <class name="history-button"/>
+    </style>
+
+    <child internal-child="accessible">
+      <object class="AtkObject">
+        <!-- Translators: accessible name of the history menubutton -->
+        <property name="AtkObject::accessible-name" translatable="yes">History</property>
+        <!-- Translators: accessible description of the history menubutton -->
+        <property name="AtkObject::accessible-description" translatable="yes">Game history menu</property>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 53a32f7..de771e8 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -13,6 +13,7 @@ src/base-headerbar.vala
 src/game-headerbar.vala
 src/game-view.vala
 src/game-window.vala
+src/history-button.vala
 src/new-game-screen.vala
 src/taquin-game.vala
 src/taquin-main.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index 77d6c96..62094ca 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -4,6 +4,7 @@ src/base-headerbar.c
 src/game-headerbar.c
 src/game-view.c
 src/game-window.c
+src/history-button.c
 src/new-game-screen.c
 src/taquin-game.c
 src/taquin-main.c
diff --git a/src/game-headerbar.vala b/src/game-headerbar.vala
index f1f0d98..8ab01d8 100644
--- a/src/game-headerbar.vala
+++ b/src/game-headerbar.vala
@@ -23,22 +23,17 @@ using Gtk;
 [GtkTemplate (ui = "/org/gnome/Taquin/ui/game-headerbar.ui")]
 private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
 {
-    [GtkChild] private MenuButton   history_button;
-    [GtkChild] private Button       new_game_button;
-    [GtkChild] private Button       back_button;
+    [GtkChild] private HistoryButton    history_button;
+    [GtkChild] private Button           new_game_button;
+    [GtkChild] private Button           back_button;
 
     [CCode (notify = false)] public bool window_has_name { private get; protected construct    ; default = 
false; }
     [CCode (notify = false)] public string window_name   { private get; protected construct set; default = 
""; }
 
     [CCode (notify = false)] public bool has_sound { private get; protected construct; default = false; }
-    [CCode (notify = false)] public bool show_undo { private get; protected construct; default = false; }
-    [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
 
     construct
     {
-        generate_moves_menu ();
-
         init_modes ();
 
         if (window_name != "")
@@ -56,9 +51,6 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
                 has_keyboard_shortcuts: GameWindowFlags.SHORTCUTS in flags,
                 has_sound:              GameWindowFlags.HAS_SOUND in flags,
                 has_help:               GameWindowFlags.SHOW_HELP in flags, // TODO rename show_help
-                show_hint:              GameWindowFlags.SHOW_HINT in flags,
-                show_redo:              GameWindowFlags.SHOW_REDO in flags,
-                show_undo:              GameWindowFlags.SHOW_UNDO in flags,
                 appearance_menu:        _appearance_menu,
                 window_name:            _window_name);
     }
@@ -143,9 +135,7 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
     {
         back_button.show ();
         new_game_button.hide ();        // TODO transition?
-        best_score = 0;
-        last_moves_count = 0;
-        generate_moves_menu ();
+        history_button.new_game ();
     }
 
     /*\
@@ -157,12 +147,9 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
         new_game_button.grab_focus ();
     }
 
-    private uint last_moves_count = 0;
     internal void set_moves_count (ref uint moves_count)
     {
-        history_button.set_label (get_moves_count_string (ref moves_count));
-        history_button.set_sensitive ((moves_count != 0) || (best_score != 0));
-        last_moves_count = moves_count;
+        history_button.set_moves_count (ref moves_count);
     }
 
     internal void update_title (string new_title)
@@ -233,102 +220,8 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
     * * moves menu
     \*/
 
-    private uint best_score = 0;
     internal void save_best_score (out string best_score_string)
     {
-        get_best_score_string (ref best_score, ref last_moves_count, out best_score_string);
-
-        if ((best_score == 0) || (last_moves_count < best_score))
-            best_score = last_moves_count;
-        generate_moves_menu ();
-    }
-    private static inline void get_best_score_string (ref uint best_score, ref uint last_moves_count, out 
string best_score_string)
-    {
-        if (best_score == 0)
-        {
-            best_score_string = usual_best_score_string;
-            return;
-        }
-
-        if (last_moves_count < best_score)
-        {
-            /* Translators: in-window notification; on both games, if the user solved the puzzle more than 
one time */
-            best_score_string =    _("Bravo! You improved your best score!");
-            if (best_score_string != "Bravo! You improved your best score!")
-                return;
-        }
-        else if (last_moves_count == best_score)
-        {
-            /* Translators: in-window notification; on both games, if the user solved the puzzle more than 
one time */
-            best_score_string =    _("Bravo! You equalized your best score.");
-            if (best_score_string != "Bravo! You equalized your best score.")
-                return;
-        }
-        else
-        {
-            /* Translators: in-window notification; on both games, if the user solved the puzzle more than 
one time */
-            best_score_string =    _("Bravo! You finished the game again.");
-            if (best_score_string != "Bravo! You finished the game again.")
-                return;
-        }
-
-        if (usual_best_score_string_untranslated != usual_best_score_string)
-            best_score_string = usual_best_score_string;
-    }
-    /* Translators: in-window notification; on both games, if the user solves the puzzle the first time */
-    private const string usual_best_score_string              = _("Bravo! You finished the game!");
-    private const string usual_best_score_string_untranslated =   "Bravo! You finished the game!" ;
-
-    private void generate_moves_menu ()
-    {
-        GLib.Menu menu = new GLib.Menu ();
-        generate_undo_actions_section (ref menu, show_undo, show_redo);
-        if (best_score != 0)
-            generate_best_score_section (ref best_score, ref menu);
-        menu.freeze ();
-        history_button.set_menu_model (menu);
-    }
-
-    private static inline void generate_undo_actions_section (ref GLib.Menu menu, bool show_undo, bool 
show_redo)
-    {
-        GLib.Menu section = new GLib.Menu ();
-
-        if (show_undo)
-        {
-            /* Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) */
-            section.append (_("_Undo"), "ui.undo");
-
-         // if (show_redo)
-         // /* Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) */
-         //     section.append (_("_Redo"), "ui.redo");
-        }
-
-
-        /* Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) */
-        section.append (_("_Restart"), "ui.restart");
-
-        section.freeze ();
-        menu.append_section (null, section);
-    }
-
-    private static inline void generate_best_score_section (ref uint best_score, ref GLib.Menu menu)
-    {
-        GLib.Menu section = new GLib.Menu ();
-
-        /* Translators: during a game that has already been finished (and possibly restarted), entry in the 
menu of the moves button */
-        section.append (_("Best score: %s").printf (get_moves_count_string (ref best_score)), null);
-
-        section.freeze ();
-        menu.append_section (null, section);
-    }
-
-    private static string get_moves_count_string (ref uint moves_count)
-    {
-        string moves_count_string;
-        if (moves_count != uint.MAX)
-            moves_count_string = moves_count.to_string ();
-        else
-            moves_count_string = "∞";
-        return moves_count_string;
+        history_button.save_best_score (out best_score_string);
     }
 }
diff --git a/src/game-window.vala b/src/game-window.vala
index 5e4c430..8fae7ac 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -25,9 +25,6 @@ private enum GameWindowFlags {
     HAS_SOUND,
     SHORTCUTS,
     SHOW_HELP,
-    SHOW_HINT,
-    SHOW_REDO,
-    SHOW_UNDO,
     SHOW_START_BUTTON;
 }
 
diff --git a/src/history-button.vala b/src/history-button.vala
new file mode 100644
index 0000000..7e816a0
--- /dev/null
+++ b/src/history-button.vala
@@ -0,0 +1,159 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
+
+   This file is part of a GNOME game
+
+   Copyright (C) 2019 – Arnaud Bonatti <arnaud bonatti gmail com>
+
+   This application is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This application is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along
+   with this application.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
+using Gtk;
+
+[GtkTemplate (ui = "/org/gnome/Taquin/ui/history-button.ui")]
+private class HistoryButton : MenuButton
+{
+    construct
+    {
+        update_state (/* label */ "0", /* sensitive */ false);
+        generate_moves_menu ();
+    }
+
+    /*\
+    * * utilities
+    \*/
+
+    private void update_state (string label, bool sensitive)
+    {
+        set_label (label);
+        set_sensitive (sensitive);
+    }
+
+    private static string get_moves_count_string (ref uint moves_count)
+    {
+        string moves_count_string;
+        if (moves_count != uint.MAX)
+            moves_count_string = moves_count.to_string ();
+        else
+            moves_count_string = "∞";
+        return moves_count_string;
+    }
+
+    /*\
+    * * menu generation
+    \*/
+
+    private void generate_moves_menu ()
+    {
+        GLib.Menu menu = new GLib.Menu ();
+        generate_undo_actions_section (ref menu);
+        if (best_score != 0)
+            generate_best_score_section (ref best_score, ref menu);
+        menu.freeze ();
+        set_menu_model (menu);
+    }
+
+    private static inline void generate_undo_actions_section (ref GLib.Menu menu)
+    {
+        GLib.Menu section = new GLib.Menu ();
+        /* Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) */
+        section.append (_("_Undo"), "ui.undo");
+
+        /* Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) */
+        section.append (_("_Restart"), "ui.restart");
+
+        section.freeze ();
+        menu.append_section (null, section);
+    }
+
+    private static inline void generate_best_score_section (ref uint best_score, ref GLib.Menu menu)
+    {
+        GLib.Menu section = new GLib.Menu ();
+
+        /* Translators: during a game that has already been finished (and possibly restarted), entry in the 
menu of the moves button */
+        section.append (_("Best score: %s").printf (get_moves_count_string (ref best_score)), null);
+
+        section.freeze ();
+        menu.append_section (null, section);
+    }
+
+    /*\
+    * * moves menu
+    \*/
+
+    private uint best_score = 0;
+    internal void save_best_score (out string best_score_string)
+    {
+        get_best_score_string (ref best_score, ref last_moves_count, out best_score_string);
+
+        if ((best_score == 0) || (last_moves_count < best_score))
+            best_score = last_moves_count;
+        generate_moves_menu ();
+    }
+
+    private static inline void get_best_score_string (ref uint best_score, ref uint last_moves_count, out 
string best_score_string)
+    {
+        if (best_score == 0)
+        {
+            best_score_string = usual_best_score_string;
+            return;
+        }
+
+        if (last_moves_count < best_score)
+        {
+            /* Translators: in-window notification; on both games, if the user solved the puzzle more than 
one time */
+            best_score_string =    _("Bravo! You improved your best score!");
+            if (best_score_string != "Bravo! You improved your best score!")
+                return;
+        }
+        else if (last_moves_count == best_score)
+        {
+            /* Translators: in-window notification; on both games, if the user solved the puzzle more than 
one time */
+            best_score_string =    _("Bravo! You equalized your best score.");
+            if (best_score_string != "Bravo! You equalized your best score.")
+                return;
+        }
+        else
+        {
+            /* Translators: in-window notification; on both games, if the user solved the puzzle more than 
one time */
+            best_score_string =    _("Bravo! You finished the game again.");
+            if (best_score_string != "Bravo! You finished the game again.")
+                return;
+        }
+
+        if (usual_best_score_string_untranslated != usual_best_score_string)
+            best_score_string = usual_best_score_string;
+    }
+    /* Translators: in-window notification; on both games, if the user solves the puzzle the first time */
+    private const string usual_best_score_string              = _("Bravo! You finished the game!");
+    private const string usual_best_score_string_untranslated =   "Bravo! You finished the game!" ;
+
+    /*\
+    * * some internal calls
+    \*/
+
+    internal void new_game ()
+    {
+        best_score = 0;
+        last_moves_count = 0;
+        generate_moves_menu ();
+    }
+
+    private uint last_moves_count = 0;
+    internal void set_moves_count (ref uint moves_count)
+    {
+        update_state (/* label     */ get_moves_count_string (ref moves_count),
+                      /* sensitive */ (moves_count != 0) || (best_score != 0));
+        last_moves_count = moves_count;
+    }
+}
diff --git a/src/meson.build b/src/meson.build
index af8dec8..2811963 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -23,6 +23,7 @@ executable(meson.project_name(),[
         'game-headerbar.vala',
         'game-view.vala',
         'game-window.vala',
+        'history-button.vala',
         'new-game-screen.vala',
         'night-light-monitor.vala',
         'notifications-revealer.vala',
diff --git a/src/taquin-main.vala b/src/taquin-main.vala
index bc150a1..2c941b1 100644
--- a/src/taquin-main.vala
+++ b/src/taquin-main.vala
@@ -192,8 +192,7 @@ private class Taquin : Gtk.Application, BaseApplication
                                  GameWindowFlags.SHOW_START_BUTTON
                                  | GameWindowFlags.HAS_SOUND
                                  | GameWindowFlags.SHORTCUTS
-                                 | GameWindowFlags.SHOW_HELP
-                                 | GameWindowFlags.SHOW_UNDO,
+                                 | GameWindowFlags.SHOW_HELP,
                                  (Box) new_game_screen,
                                  view,
                                  null,  // appearance menu
diff --git a/src/taquin.gresource.xml b/src/taquin.gresource.xml
index 8e03495..16ab120 100644
--- a/src/taquin.gresource.xml
+++ b/src/taquin.gresource.xml
@@ -11,6 +11,7 @@
     <file alias="base-window.css">../data/base-window.css</file>
     <file preprocess="xml-stripblanks" alias="base-window.ui">../data/ui/base-window.ui</file>
     <file preprocess="xml-stripblanks" alias="game-headerbar.ui">../data/ui/game-headerbar.ui</file>
+    <file preprocess="xml-stripblanks" alias="history-button.ui">../data/ui/history-button.ui</file>
     <file alias="new-game-screen.css">../data/new-game-screen.css</file>
     <file preprocess="xml-stripblanks" alias="new-game-screen.ui">../data/ui/new-game-screen.ui</file>
     <file preprocess="xml-stripblanks" 
alias="notifications-revealer.ui">../data/ui/notifications-revealer.ui</file>


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