[five-or-more/gsoc-vala-port: 23/29] Use CssProvider for background



commit e695fcd57a1fea192f406987f726478142059c25
Author: Ruxandra Simion <ruxandra simion93 gmail com>
Date:   Sun Aug 5 21:29:41 2018 +0300

    Use CssProvider for background

 data/five-or-more-vala.ui |  3 +++
 src-vala/view.vala        | 22 ++++++++++++++++++----
 src-vala/window.vala      |  7 ++++---
 3 files changed, 25 insertions(+), 7 deletions(-)
---
diff --git a/data/five-or-more-vala.ui b/data/five-or-more-vala.ui
index e882839..2eac945 100644
--- a/data/five-or-more-vala.ui
+++ b/data/five-or-more-vala.ui
@@ -72,6 +72,9 @@
         <property name="orientation">horizontal</property>
         <property name="spacing">12</property>
         <child>
+          <object class="GamesGridFrame" id="grid_frame">
+            <property name="visible">True</property>
+          </object>
           <object class="GtkButton" id="new_game_button">
                 <property name="visible">True</property>
                 <property name="use_underline">True</property>
diff --git a/src-vala/view.vala b/src-vala/view.vala
index 41b5be8..f08a151 100644
--- a/src-vala/view.vala
+++ b/src-vala/view.vala
@@ -6,6 +6,7 @@ public class View : Gtk.DrawingArea
     private Game? game = null;
     private ThemeRenderer? theme = null;
     private Gdk.RGBA background_color;
+    private Gtk.StyleContext cs;
 
     private Gdk.Rectangle board_rectangle;
 
@@ -83,6 +84,21 @@ public class View : Gtk.DrawingArea
     {
         var color_str = settings.get_string (FiveOrMoreApp.KEY_BACKGROUND_COLOR);
         background_color.parse (color_str);
+
+        cs = this.get_style_context ();
+        var provider = new Gtk.CssProvider ();
+        try
+        {
+            provider.load_from_data (".game-view { background-color: %s; }".printf 
(background_color.to_string ()));
+        }
+        catch (Error e)
+        {
+            warning ("Failed to load CSS data to provider");
+            return;
+        }
+
+        cs.add_class ("game-view");
+        cs.add_provider (provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);
     }
 
     public override bool button_press_event (Gdk.EventButton event)
@@ -177,14 +193,12 @@ public class View : Gtk.DrawingArea
 
     private void fill_background (Cairo.Context cr)
     {
-        Gdk.cairo_set_source_rgba (cr, background_color);
-        Gdk.cairo_rectangle (cr, board_rectangle);
-        cr.fill ();
+        cs.render_background (cr, board_rectangle.x, board_rectangle.y, board_rectangle.width, 
board_rectangle.height);
     }
 
     private void draw_gridlines (Cairo.Context cr)
     {
-        Gdk.RGBA grid_color = this.get_style_context ().get_color (Gtk.StateFlags.NORMAL);
+        Gdk.RGBA grid_color = cs.get_color (Gtk.StateFlags.NORMAL);
         Gdk.cairo_set_source_rgba (cr, grid_color);
         cr.set_line_width (2.0);
 
diff --git a/src-vala/window.vala b/src-vala/window.vala
index 0bff205..33866e1 100644
--- a/src-vala/window.vala
+++ b/src-vala/window.vala
@@ -11,10 +11,11 @@ public class GameWindow : Gtk.ApplicationWindow
     private Gtk.Box hbox;
 
     [GtkChild]
-    private Gtk.Label scorelabel;
-
     private Games.GridFrame grid_frame;
 
+    [GtkChild]
+    private Gtk.Label scorelabel;
+
     private Game? game = null;
     private ThemeRenderer? theme = null;
 
@@ -38,7 +39,7 @@ public class GameWindow : Gtk.ApplicationWindow
         next_pieces_widget.realize ();
         next_pieces_widget.show ();
 
-        grid_frame = new Games.GridFrame (game.n_cols, game.n_rows);
+        grid_frame.set (game.n_cols, game.n_rows);
         /* it depends on which one changes last, so it is better to call them both */
         game.notify["n-cols"].connect ((s, p) => { grid_frame.set (game.n_cols, game.n_rows); });
         game.notify["n-rows"].connect ((s, p) => { grid_frame.set (game.n_cols, game.n_rows); });


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