[iagno] Only load image on theme change.



commit 6565003d72fe687833ecc9e5d0368bd51f3dfc56
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri Jan 17 18:06:49 2020 +0100

    Only load image on theme change.

 src/reversi-view.vala  | 36 ++++--------------------------------
 src/theme-manager.vala | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 33 deletions(-)
---
diff --git a/src/reversi-view.vala b/src/reversi-view.vala
index cdca655..3e64759 100644
--- a/src/reversi-view.vala
+++ b/src/reversi-view.vala
@@ -312,7 +312,10 @@ private class ReversiView : Gtk.DrawingArea
         surface = new Cairo.Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, tile_size * 8,
                                                                                           tile_size * 4);
         context = new Cairo.Context (surface);
-        load_image (context, tile_size * 8, tile_size * 4);
+        Rsvg.DimensionData size = theme_manager.tileset_handle.get_dimensions ();
+        context.scale ((double) tile_size * 8.0 / (double) size.width,
+                       (double) tile_size * 4.0 / (double) size.height);
+        theme_manager.tileset_handle.render_cairo (context);
         tiles_pattern = new Cairo.Pattern.for_surface (surface);
 
         // noise pattern
@@ -700,37 +703,6 @@ private class ReversiView : Gtk.DrawingArea
         cr.arc (x1, y1, radius_arc,  Math.PI, -HALF_PI);
     }
 
-    private void load_image (Cairo.Context c, int width, int height)
-     // requires (theme_manager.pieces_file != "")
-    {
-        try
-        {
-            Rsvg.Handle h = new Rsvg.Handle.from_file (theme_manager.pieces_file);
-
-            Cairo.Matrix m = Cairo.Matrix.identity ();
-            m.scale ((double) width / h.width, (double) height / h.height);
-            c.set_matrix (m);
-            h.render_cairo (c);
-
-            return;
-        }
-        catch (Error e)
-        {
-            /* Fall through and try loading as a pixbuf */
-        }
-
-        try
-        {
-            Gdk.Pixbuf p = new Gdk.Pixbuf.from_file_at_scale (theme_manager.pieces_file, width, height, 
false);
-            Gdk.cairo_set_source_pixbuf (c, p, 0.0, 0.0);
-            c.paint ();
-        }
-        catch (Error e)
-        {
-            warning ("Failed to load theme image %s: %s", theme_manager.pieces_file, e.message);
-        }
-    }
-
     private void highlight_tile (Cairo.Context cr, uint8 x, uint8 y, uint8 intensity, bool soft_highlight)
     {
         if (soft_highlight)
diff --git a/src/theme-manager.vala b/src/theme-manager.vala
index 9ae179c..7e0ed56 100644
--- a/src/theme-manager.vala
+++ b/src/theme-manager.vala
@@ -93,7 +93,7 @@ private class ThemeManager : Object
     * * theme
     \*/
 
-    [CCode (notify = false)] internal string pieces_file            { internal get; private set; default = 
""; }
+    private string pieces_file = "";
 
     [CCode (notify = false)] internal double background_red         { internal get; private set; default = 
0.2; }
     [CCode (notify = false)] internal double background_green       { internal get; private set; default = 
0.6; }
@@ -142,6 +142,7 @@ private class ThemeManager : Object
             pieces_file = Path.build_filename (svg_path, key.get_string ("Pieces", "File"));
             if (Path.get_dirname (pieces_file) != svg_path)
                 pieces_file = Path.build_filename (svg_path, "black_and_white.svg");
+            load_handle ();
 
             background_red       = key.get_double  ("Background", "Red");
             background_green     = key.get_double  ("Background", "Green");
@@ -187,4 +188,32 @@ private class ThemeManager : Object
             warning ("Errors when loading theme: %s", e.message);
         }
     }
+
+    /*\
+    * * loading handle
+    \*/
+
+    private bool handle_loaded = false;
+    [CCode (notify = false)] internal Rsvg.Handle tileset_handle { internal get { if (!handle_loaded) 
assert_not_reached (); return _tileset_handle; }}
+
+    private Rsvg.Handle _tileset_handle;
+
+    private string old_pieces_file = "";
+    private inline void load_handle ()
+    {
+        if (handle_loaded && old_pieces_file == pieces_file)
+            return;
+
+        try
+        {
+            _tileset_handle = new Rsvg.Handle.from_file (pieces_file);
+        }
+        catch (Error e)
+        {
+            assert_not_reached ();
+        }
+
+        old_pieces_file = pieces_file;
+        handle_loaded = true;
+    }
 }


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