[five-or-more/arnaudb/wip/gtk4: 1/5] Use an AspectFrame.



commit c801725557f089f8543a30be514cf6ec86b33523
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Mar 27 15:40:32 2020 +0100

    Use an AspectFrame.

 data/five-or-more.ui | 11 ++++++++++-
 src/view.vala        |  5 +++++
 src/window.vala      | 14 +++++++++++---
 3 files changed, 26 insertions(+), 4 deletions(-)
---
diff --git a/data/five-or-more.ui b/data/five-or-more.ui
index 5606ade..4651c46 100644
--- a/data/five-or-more.ui
+++ b/data/five-or-more.ui
@@ -90,7 +90,16 @@
       </object>
     </child>
     <child>
-      <object class="GamesGridFrame" id="grid_frame"/>
+      <object class="GtkAspectFrame" id="grid_frame">
+        <property name="obey-child">False</property>
+        <property name="ratio">1.0</property>
+        <property name="xalign">0.5</property>
+        <property name="yalign">0.5</property>
+        <property name="margin-top">10</property>
+        <property name="margin-start">10</property>
+        <property name="margin-end">10</property>
+        <property name="margin-bottom">10</property>
+      </object>
     </child>
   </template>
 </interface>
diff --git a/src/view.vala b/src/view.vala
index 8f673af..417a003 100644
--- a/src/view.vala
+++ b/src/view.vala
@@ -326,6 +326,8 @@ private class View : DrawingArea
         piece_size = (width - 1) / game.n_cols;
         board_rectangle.width  = piece_size * game.n_cols;
         board_rectangle.height = piece_size * game.n_rows;
+        board_rectangle.x = (width  - board_rectangle.width)  / 2;
+        board_rectangle.y = (height - board_rectangle.height) / 2;
     }
 
     private inline void on_size_allocate (int width, int height)
@@ -427,9 +429,12 @@ private class View : DrawingArea
             return;
 
         fill_background (cr);
+        cr.save ();
+        cr.translate (board_rectangle.x, board_rectangle.y);
         draw_gridlines (cr);
         draw_shapes (cr);
         draw_cursor_box (cr);
         draw_path (cr);
+        cr.restore ();
     }
 }
diff --git a/src/window.vala b/src/window.vala
index d003953..7a44411 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -36,7 +36,7 @@ private class GameWindow : ApplicationWindow
     private MenuButton primary_menu_button;
 
     [GtkChild]
-    private Games.GridFrame grid_frame;
+    private AspectFrame grid_frame;
 
     public GLib.Settings settings { private get; protected construct; }
     private bool window_tiled;
@@ -58,6 +58,7 @@ private class GameWindow : ApplicationWindow
     construct
     {
         game = new Game (settings);
+        update_ratio ((BoardSize)settings.get_int ("size"));
         theme = new ThemeRenderer (settings);
 
         size_allocate.connect (on_size_allocate);
@@ -70,8 +71,6 @@ private class GameWindow : ApplicationWindow
         NextPiecesWidget next_pieces_widget = new NextPiecesWidget (settings, game, theme);
         preview_hbox.add (next_pieces_widget);
 
-        grid_frame.set (game.n_cols, game.n_rows);
-        game.board.board_changed.connect (() => { grid_frame.set (game.n_cols, game.n_rows); });
         game.notify["score"].connect ((s, p) => { set_status_message 
(status[StatusMessage.NONE].printf(game.score)); });
         game.notify["status-message"].connect ((s, p) => { set_status_message 
(status[game.status_message].printf(game.score)); });
         set_status_message (status[game.status_message]);
@@ -185,10 +184,19 @@ private class GameWindow : ApplicationWindow
             }
         } else {
             settings.set_int (FiveOrMoreApp.KEY_SIZE, size);
+            update_ratio (size);
         }
 
     }
 
+    private void update_ratio (BoardSize size)
+    {
+        if (size == BoardSize.LARGE)
+            grid_frame.ratio = 4.0f/3.0f;
+        else
+            grid_frame.ratio = 1.0f;
+    }
+
     private Games.Scores.Category? create_category_from_key (string key)
     {
         string? name = category_name_from_key (key);


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