[gnome-mines/arnaudb/wip/gtk4: 74/85] Fix board ratio.




commit 9ce37806e27b5ee91373a63ecfae9508404264f0
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Wed Apr 15 23:50:58 2020 +0200

    Fix board ratio.
    
    That is not perfect, but good enough.

 src/gnome-mines.vala    | 33 ++---------------------
 src/interface.ui        |  7 +----
 src/minefield-view.vala | 72 +++++++++++++++++++++++++++++++++----------------
 3 files changed, 52 insertions(+), 60 deletions(-)
---
diff --git a/src/gnome-mines.vala b/src/gnome-mines.vala
index 9f594cc..55d3695 100644
--- a/src/gnome-mines.vala
+++ b/src/gnome-mines.vala
@@ -43,7 +43,6 @@ public class Mines : Gtk.Application
     private Box aspect_child;
     private Box buttons_box;
     private Box paused_box;
-    private ScrolledWindow scrolled;
     private Stack stack;
     private ThemeSelectorDialog theme_dialog;
 
@@ -60,9 +59,6 @@ public class Mines : Gtk.Application
     private bool window_is_fullscreen;
     private bool window_is_tiled;
 
-    /* true when the new game minefield draws once */
-    private bool first_draw = false;
-
     /* true when the user has requested the game to pause. */
     private bool pause_requested;
 
@@ -256,37 +252,12 @@ public class Mines : Gtk.Application
 
         minefield_view = new MinefieldView (settings);
 
-        /* Hook a resize on the first minefield draw so that the ratio
-           calculation in minefield_aspect.size-allocate runs one more time
-           with stable allocation sizes for the current minefield configutation */
-        minefield_view.draw.connect ((context, data) => {
-            if(!first_draw) {
-                minefield_aspect.queue_resize ();
-                minefield_view.queue_draw ();
-                first_draw = true;
-                return true;
-            };
-            return false;
-        });
-
         stack = (Stack) ui_builder.get_object ("stack");
 
-        scrolled = (ScrolledWindow) ui_builder.get_object ("scrolled");
-        scrolled.add (minefield_view);
-
         minefield_overlay = (Overlay) ui_builder.get_object ("minefield_overlay");
         minefield_aspect = (AspectFrame) ui_builder.get_object ("minefield_aspect");
-
-        minefield_aspect.size_allocate.connect ((allocation) => {
-             uint width = minefield_view.mine_size * minefield_view.minefield.width;
-             width += aspect_child.spacing;
-             width += buttons_box.get_allocated_width ();
-             float new_ratio = (float) width / (minefield_view.minefield.height * minefield_view.mine_size);
-             if (new_ratio != minefield_aspect.ratio) {
-                minefield_aspect.ratio = new_ratio;
-                first_draw = false;
-             };
-        });
+        minefield_aspect.obey_child = true;
+        minefield_overlay.add (minefield_view);
 
         paused_box = (Box) ui_builder.get_object ("paused_box");
         buttons_box = (Box) ui_builder.get_object ("buttons_box");
diff --git a/src/interface.ui b/src/interface.ui
index a84a67e..68968c9 100644
--- a/src/interface.ui
+++ b/src/interface.ui
@@ -246,12 +246,7 @@
                               <object class="GtkOverlay" id="minefield_overlay">
                                 <property name="can_focus">False</property>
                                 <child>
-                                  <object class="GtkScrolledWindow" id="scrolled">
-                                    <property name="can_focus">True</property>
-                                    <child>
-                                      <placeholder/>
-                                    </child>
-                                  </object>
+                                  <placeholder/>
                                 </child>
                               </object>
                             </child>
diff --git a/src/minefield-view.vala b/src/minefield-view.vala
index 947508a..3afd398 100644
--- a/src/minefield-view.vala
+++ b/src/minefield-view.vala
@@ -90,7 +90,7 @@ private class Position : Object
     }
 }
 
-public class MinefieldView : Gtk.Grid
+public class MinefieldView : Gtk.Widget
 {
     private Settings settings;
     private bool force_nolongpress;
@@ -142,23 +142,36 @@ public class MinefieldView : Gtk.Grid
 
     private Gtk.EventControllerKey key_controller;    // for keeping in memory
 
+    private Gtk.AspectFrame frame;
+    private Gtk.Grid grid;
+    private Gtk.BinLayout layout;
+
     construct
     {
         init_keyboard ();
-    }
 
-    public MinefieldView (Settings settings)
-    {
-        this.settings = settings;
-        this.force_nolongpress = false;
-        row_homogeneous = true;
-        row_spacing = 0;
-        column_homogeneous = true;
-        column_spacing = 0;
-        can_focus = true;
         hexpand = true;
         vexpand = true;
-        add_css_class ("minefield");
+
+        layout = new Gtk.BinLayout ();
+        set_layout_manager (layout);
+
+        frame = new Gtk.AspectFrame (/* label */ null, /* xalign */ 0.5f, /* yalign */ 0.5f, /* ratio */ 
1.0f, /* obey-child */ false);
+        frame.shadow_type = Gtk.ShadowType.NONE;
+        frame.insert_after (this, /* insert first */ null);
+
+        grid = new Gtk.Grid ();
+        frame.add (grid);
+
+        this.force_nolongpress = false;
+        grid.row_homogeneous = true;
+        grid.row_spacing = 0;
+        grid.column_homogeneous = true;
+        grid.column_spacing = 0;
+        grid.can_focus = true;
+        grid.hexpand = true;
+        grid.vexpand = true;
+        grid.add_css_class ("minefield");
 
         selected = new Position ();
         selected.set_x.connect ((x) => { return x; });
@@ -170,6 +183,17 @@ public class MinefieldView : Gtk.Grid
         keyboard_cursor.validate.connect ((x, y) => { return true; });
     }
 
+    public MinefieldView (Settings settings)
+    {
+        this.settings = settings;
+    }
+
+    protected override void destroy ()
+    {
+        frame.destroy ();
+        base.destroy ();
+    }
+
     private Minefield _minefield;
     public Minefield minefield
     {
@@ -181,10 +205,12 @@ public class MinefieldView : Gtk.Grid
                 SignalHandler.disconnect_by_func (_minefield, null, this);
             }
             _minefield = value;
+            frame.ratio = (float) (minefield.width) / (float) (minefield.height);
+
             remove_css_class ("explodedField");
             remove_css_class ("completedField");
             mines = new Tile[_minefield.width, _minefield.height];
-            forall ((child) => { remove (child); });
+            grid.forall ((child) => { grid.remove (child); });
             for (int i = 0; i < _minefield.width; i++)
             {
                 for (int j = 0; j < _minefield.height; j++)
@@ -193,7 +219,7 @@ public class MinefieldView : Gtk.Grid
                     mines[i,j].tile_pressed.connect (tile_pressed_cb);
                     mines[i,j].tile_released.connect (tile_released_cb);
                     mines[i,j].tile_long_pressed.connect (tile_long_pressed_cb);
-                    add (mines[i,j], i, j);
+                    add_tile (mines[i,j], i, j);
                 }
             }
             selected.is_set = false;
@@ -308,17 +334,17 @@ public class MinefieldView : Gtk.Grid
         add_css_class ("completedField");
     }
 
-    public override void measure (Gtk.Orientation orientation, int for_size, out int minimum, out int 
natural, out int minimum_baseline, out int natural_baseline)
-    {
-        if (orientation == Gtk.Orientation.HORIZONTAL)
-            minimum = natural = minefield != null ? (int) (minefield.width * minimum_size) : 0;
-        else
-            minimum = natural = minefield != null ? (int) (minefield.height * minimum_size) : 0;
-    }
+//    public override void measure (Gtk.Orientation orientation, int for_size, out int minimum, out int 
natural, out int minimum_baseline, out int natural_baseline)
+//    {
+//        if (orientation == Gtk.Orientation.HORIZONTAL)
+//            minimum = natural = minefield != null ? (int) (minefield.width * minimum_size) : 0;
+//        else
+//            minimum = natural = minefield != null ? (int) (minefield.height * minimum_size) : 0;
+//    }
 
-    public new void add (Gtk.Widget child, int i, int j)
+    public void add_tile (Gtk.Widget child, int i, int j)
     {
-        attach (child, i-1, j-1, 1, 1);
+        grid.attach (child, i-1, j-1, 1, 1);
         child.hexpand = true;
         child.vexpand = true;
     }


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