[five-or-more/arnaudb/code-improvements: 4/9] Do not share settings with View.



commit 91cdf540b21742af024bdc12223a0019a67bb16c
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri May 8 22:08:37 2020 +0200

    Do not share settings with View.

 src/view.vala   | 51 +++++++++++++++++++++++++++------------------------
 src/window.vala |  3 ++-
 2 files changed, 29 insertions(+), 25 deletions(-)
---
diff --git a/src/view.vala b/src/view.vala
index 4ecb126..3a7a1c2 100644
--- a/src/view.vala
+++ b/src/view.vala
@@ -27,7 +27,6 @@ private class View : DrawingArea
 {
     private const int MINIMUM_BOARD_SIZE = 256;
 
-    private GLib.Settings settings;
     private Game? game = null;
     private ThemeRenderer? theme = null;
     private StyleContext cs;
@@ -54,9 +53,34 @@ private class View : DrawingArea
     private EventControllerKey key_controller;          // for keeping in memory
     private GestureMultiPress click_controller;         // for keeping in memory
 
-    internal View (GLib.Settings settings, Game game, ThemeRenderer theme)
+    private const string default_background_color = "#7590AE";
+    private string _background_color = default_background_color;
+    public string background_color
+    {
+        internal get { return _background_color; }
+        internal set
+        {
+            Gdk.RGBA color = Gdk.RGBA ();
+            if (!color.parse (value))
+                _background_color = default_background_color;
+            else
+                _background_color = color.to_string ();
+
+            try
+            {
+                provider.load_from_data (".game-view { background-color: %s; }".printf (_background_color));
+            }
+            catch (Error e)
+            {
+                warning ("Failed to load CSS data to provider");
+                return;
+            }
+            queue_draw ();
+        }
+    }
+
+    internal View (Game game, ThemeRenderer theme)
     {
-        this.settings = settings;
         this.game = game;
         this.theme = theme;
 
@@ -68,12 +92,6 @@ private class View : DrawingArea
         cs.add_class ("game-view");
         cs.add_provider (provider, STYLE_PROVIDER_PRIORITY_USER);
 
-        set_background_color ();
-        settings.changed[FiveOrMoreApp.KEY_BACKGROUND_COLOR].connect (() => {
-            set_background_color ();
-            queue_draw ();
-        });
-
         set_size_request (MINIMUM_BOARD_SIZE, MINIMUM_BOARD_SIZE);
 
         board_rectangle = Gdk.Rectangle ();
@@ -118,21 +136,6 @@ private class View : DrawingArea
         queue_draw ();
     }
 
-    private void set_background_color ()
-    {
-        var color_str = settings.get_string (FiveOrMoreApp.KEY_BACKGROUND_COLOR);
-
-        try
-        {
-            provider.load_from_data (".game-view { background-color: %s; }".printf (color_str));
-        }
-        catch (Error e)
-        {
-            warning ("Failed to load CSS data to provider");
-            return;
-        }
-    }
-
     private void move_keyboard_cursor (int x, int y)
     {
         int prev_x = keyboard_cursor_x;
diff --git a/src/window.vala b/src/window.vala
index 6f4a816..720645c 100644
--- a/src/window.vala
+++ b/src/window.vala
@@ -105,7 +105,8 @@ private class GameWindow : ApplicationWindow
         game.notify["status-message"].connect ((s, p) => { set_status_message 
(status[game.status_message].printf(game.score)); });
         set_status_message (status[game.status_message]);
 
-        View game_view = new View (settings, game, theme);
+        View game_view = new View (game, theme);
+        settings.bind (FiveOrMoreApp.KEY_BACKGROUND_COLOR, game_view, "background-color", 
SettingsBindFlags.DEFAULT);
         grid_frame.add (game_view);
         game_view.show ();
 


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