[iagno] Split out HistoryButton.



commit 1cdab90b67478496f8224e086bdc53e73fd9ec2a
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Dec 6 02:00:30 2019 +0100

    Split out HistoryButton.

 data/iagno.gresource.xml  |  1 +
 data/ui/game-headerbar.ui |  8 +----
 data/ui/history-button.ui | 32 +++++++++++++++++
 po/POTFILES.in            |  1 +
 po/POTFILES.skip          |  1 +
 src/game-headerbar.vala   | 62 ++++----------------------------
 src/history-button.vala   | 91 +++++++++++++++++++++++++++++++++++++++++++++++
 src/meson.build           |  1 +
 8 files changed, 135 insertions(+), 62 deletions(-)
---
diff --git a/data/iagno.gresource.xml b/data/iagno.gresource.xml
index 246be62..50f66b1 100644
--- a/data/iagno.gresource.xml
+++ b/data/iagno.gresource.xml
@@ -11,6 +11,7 @@
     <file compressed="true">base-window.css</file>
     <file preprocess="xml-stripblanks" compressed="true" alias="base-window.ui"             
ui/base-window.ui</file>
     <file preprocess="xml-stripblanks" compressed="true" alias="game-headerbar.ui"          
ui/game-headerbar.ui</file>
+    <file preprocess="xml-stripblanks" compressed="true" alias="history-button.ui"          
ui/history-button.ui</file>
     <file compressed="true">iagno.css</file>
     <file compressed="true">new-game-screen.css</file>
     <file preprocess="xml-stripblanks" compressed="true" alias="new-game-screen.ui"         
ui/new-game-screen.ui</file>
diff --git a/data/ui/game-headerbar.ui b/data/ui/game-headerbar.ui
index 8b8f94e..b1ee37a 100644
--- a/data/ui/game-headerbar.ui
+++ b/data/ui/game-headerbar.ui
@@ -64,14 +64,8 @@
       </object>
     </child>
     <child>
-      <object class="GtkMenuButton" id="history_button">
+      <object class="HistoryButton" 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>
-        <style>
-          <class name="history-button"/>
-        </style>
       </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..a6c56c4
--- /dev/null
+++ b/data/ui/history-button.ui
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  This file is part of GNOME Reversi, also known as Iagno.
+
+  Copyright 2012 Tiffany Antopolski and Robert Ancell
+  Copyright 2013, 2014, 2015 Michael Catanzaro
+  Copyright 2014, 2015, 2019 Arnaud Bonatti
+
+  GNOME Reversi 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 Reversi 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 Reversi.  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>
+  </template>
+</interface>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index fbd2197..78a0d3d 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -19,6 +19,7 @@ src/game-headerbar.vala
 src/game.vala
 src/game-view.vala
 src/game-window.vala
+src/history-button.vala
 src/iagno.vala
 src/new-game-screen.vala
 src/player.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index b8d2233..d0ccb55 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -6,6 +6,7 @@ src/game.c
 src/game-headerbar.c
 src/game-view.c
 src/game-window.c
+src/history-button.c
 src/iagno.c
 src/player.c
 src/reversi-view.c
diff --git a/src/game-headerbar.vala b/src/game-headerbar.vala
index bd5c77b..b5b7b2b 100644
--- a/src/game-headerbar.vala
+++ b/src/game-headerbar.vala
@@ -23,9 +23,9 @@ using Gtk;
 [GtkTemplate (ui = "/org/gnome/Reversi/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 = 
""; }
@@ -37,8 +37,6 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
 
     construct
     {
-        configure_history_button ();
-
         init_modes ();
 
         if (window_name != "")
@@ -79,6 +77,7 @@ 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);
     }
 
@@ -236,65 +235,18 @@ private class GameHeaderBar : BaseHeaderBar, AdaptativeWidget
     * * history menu
     \*/
 
-    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 
*/
-
-        history_button_new_game ();
-    }
-
     internal inline void update_history_button (bool finish_animation)
     {
-        history_button.set_menu_model (finish_animation ? finish_menu : history_menu);
+        history_button.update_menu (finish_animation);
     }
 
     internal inline void history_button_new_game ()
     {
-        set_history_button_label (Player.DARK);
-        update_history_button (/* final animation */ false);
+        history_button.new_game ();
     }
 
     internal void set_history_button_label (Player player)
     {
-        switch (player)
-        {
-            case Player.LIGHT:
-                    history_button.set_label (history_button_light_label);  break;
-            case Player.DARK:
-                    history_button.set_label (history_button_dark_label);   break;
-            case Player.NONE:
-                if (is_extra_thin)
-                    /* Translators: label of the game status button (in the headerbar, next to the hamburger 
button), at the end of the game; this string is for when the window is really small, so keep the string as 
small as possible (3~5 characters) */
-                    history_button.set_label (_("End!"));
-
-                else
-                    /* Translators: label of the game status button (in the headerbar, next to the hamburger 
button), at the end of the game, if the window is not too thin */
-                    history_button.set_label (_("Finished!"));              break;
-            default: assert_not_reached ();
-        }
-
-        Widget? history_label = history_button.get_child ();
-        if ((history_label != null)
-         && (!) history_label is Label)
-            ((Label) (!) history_label).set_ellipsize (Pango.EllipsizeMode.END);
-     // else assert_not_reached ();
+        history_button.update_label (player);
     }
 }
diff --git a/src/history-button.vala b/src/history-button.vala
new file mode 100644
index 0000000..704d1e4
--- /dev/null
+++ b/src/history-button.vala
@@ -0,0 +1,91 @@
+/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+   This file is part of GNOME Reversi, also known as Iagno.
+
+   Copyright 2019 Arnaud Bonatti
+
+   GNOME Reversi 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 Reversi 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 Reversi.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
+using Gtk;
+
+[GtkTemplate (ui = "/org/gnome/Reversi/ui/history-button.ui")]
+private class HistoryButton : MenuButton
+{
+    [CCode (notify = false)] internal bool is_extra_thin { private get; internal set; default = true; }
+
+    private GLib.Menu history_menu;
+    private GLib.Menu finish_menu;
+
+    private string history_button_light_label;
+    private string history_button_dark_label;
+
+    construct
+    {
+        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 
*/
+
+        new_game ();
+    }
+
+    /*\
+    * * internal calls
+    \*/
+
+    internal inline void update_menu (bool finish_animation)
+    {
+        set_menu_model (finish_animation ? finish_menu : history_menu);
+    }
+
+    internal inline void new_game ()
+    {
+        update_label (Player.DARK);
+        update_menu (/* final animation */ false);
+    }
+
+    internal void update_label (Player player)
+    {
+        switch (player)
+        {
+            case Player.LIGHT:
+                    set_label (history_button_light_label);  break;
+            case Player.DARK:
+                    set_label (history_button_dark_label);   break;
+            case Player.NONE:
+                if (is_extra_thin)
+                    /* Translators: label of the game status button (in the headerbar, next to the hamburger 
button), at the end of the game; this string is for when the window is really small, so keep the string as 
small as possible (3~5 characters) */
+                    set_label (_("End!"));
+
+                else
+                    /* Translators: label of the game status button (in the headerbar, next to the hamburger 
button), at the end of the game, if the window is not too thin */
+                    set_label (_("Finished!"));              break;
+            default: assert_not_reached ();
+        }
+
+        Widget? history_label = get_child ();
+        if (history_label != null && (!) history_label is Label)
+            ((Label) (!) history_label).set_ellipsize (Pango.EllipsizeMode.END);
+    }
+}
diff --git a/src/meson.build b/src/meson.build
index 578f4c1..4732e2d 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -77,6 +77,7 @@ executable(meson.project_name(),
         'game-view.vala',
         'game-window.vala',
         'game.vala',
+        'history-button.vala',
         'iagno.vala',
         'new-game-screen.vala',
         'night-light-monitor.vala',


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