[gnome-games] ui: Introduce CollectionThumbnail



commit 1a1657c7d0e59972771a50046ef93e56cd6a1284
Author: Neville <nevilleantony98 gmail com>
Date:   Fri Jun 19 23:50:17 2020 +0530

    ui: Introduce CollectionThumbnail
    
    This will be used to generate covers for collection based on the games
    it contains. It will automatically rearrange and update the cover when
    games are added and removed.

 data/org.gnome.Games.gresource.xml |  1 +
 data/ui/collection-thumbnail.ui    | 52 ++++++++++++++++++++++++++++
 src/meson.build                    |  1 +
 src/ui/collection-thumbnail.vala   | 71 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 125 insertions(+)
---
diff --git a/data/org.gnome.Games.gresource.xml b/data/org.gnome.Games.gresource.xml
index e8030e51..834d0051 100644
--- a/data/org.gnome.Games.gresource.xml
+++ b/data/org.gnome.Games.gresource.xml
@@ -14,6 +14,7 @@
     <file preprocess="xml-stripblanks">ui/checkmark-item.ui</file>
     <file preprocess="xml-stripblanks">ui/collection-empty.ui</file>
     <file preprocess="xml-stripblanks">ui/collection-icon-view.ui</file>
+    <file preprocess="xml-stripblanks">ui/collection-thumbnail.ui</file>
     <file preprocess="xml-stripblanks">ui/collection-view.ui</file>
     <file preprocess="xml-stripblanks">ui/collections-main-page.ui</file>
     <file preprocess="xml-stripblanks">ui/collections-page.ui</file>
diff --git a/data/ui/collection-thumbnail.ui b/data/ui/collection-thumbnail.ui
new file mode 100644
index 00000000..4979482c
--- /dev/null
+++ b/data/ui/collection-thumbnail.ui
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="3.24"/>
+  <template class="GamesCollectionThumbnail" parent="GtkBin">
+    <property name="visible">True</property>
+    <child>
+      <object class="GtkGrid" id="grid">
+        <property name="visible">True</property>
+        <property name="row-spacing">6</property>
+        <property name="column-spacing">6</property>
+        <property name="row-homogeneous">True</property>
+        <property name="column-homogeneous">True</property>
+        <child>
+          <object class="GtkEventBox">
+            <property name="visible">True</property>
+          </object>
+          <packing>
+            <property name="left-attach">0</property>
+            <property name="top-attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkEventBox">
+            <property name="visible">True</property>
+          </object>
+          <packing>
+            <property name="left-attach">1</property>
+            <property name="top-attach">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkEventBox">
+            <property name="visible">True</property>
+          </object>
+          <packing>
+            <property name="left-attach">0</property>
+            <property name="top-attach">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkEventBox">
+            <property name="visible">True</property>
+          </object>
+          <packing>
+            <property name="left-attach">1</property>
+            <property name="top-attach">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/meson.build b/src/meson.build
index 8a0c05ae..11d13259 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -112,6 +112,7 @@ vala_sources = [
   'ui/checkmark-item.vala',
   'ui/collection-empty.vala',
   'ui/collection-icon-view.vala',
+  'ui/collection-thumbnail.vala',
   'ui/collection-view.vala',
   'ui/collections-main-page.vala',
   'ui/collections-page.vala',
diff --git a/src/ui/collection-thumbnail.vala b/src/ui/collection-thumbnail.vala
new file mode 100644
index 00000000..72081f31
--- /dev/null
+++ b/src/ui/collection-thumbnail.vala
@@ -0,0 +1,71 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+[GtkTemplate (ui = "/org/gnome/Games/ui/collection-thumbnail.ui")]
+private class Games.CollectionThumbnail : Gtk.Bin {
+       // This should match the number of grid children in the template
+       const uint N_ROWS = 2;
+       const uint N_COLUMNS = 2;
+
+       [GtkChild]
+       private Gtk.Grid grid;
+
+       private ulong games_changed_id = 0;
+
+       private Collection _collection;
+       public Collection collection {
+               get { return _collection; }
+               set {
+                       if (games_changed_id > 0)
+                               collection.disconnect (games_changed_id);
+
+                       _collection = value;
+
+                       games_changed_id = collection.games_changed.connect (on_games_changed);
+               }
+       }
+
+       public void on_games_changed () {
+               var max_subcovers = N_ROWS * N_COLUMNS;
+
+               var children = grid.get_children ();
+               children.reverse ();
+
+               var pos = 0;
+               var game_model = collection.get_game_model ();
+               var n_games = game_model.get_n_items ();
+               foreach (var child in children) {
+                       var event_box = child as Gtk.Bin;
+
+                       if (pos < n_games) {
+                               var game = game_model.get_item (pos) as Game;
+                               var game_thumbnail = get_game_thumbnail (game);
+
+                               var current_thumbnail = event_box.get_child ();
+                               if (current_thumbnail != null)
+                                       event_box.remove (current_thumbnail);
+                               event_box.add (game_thumbnail);
+
+                               pos++;
+                               continue;
+                       }
+
+                       if (pos < max_subcovers) {
+                               var game_thumbnail = event_box.get_child ();
+                               if (game_thumbnail != null)
+                                       event_box.remove (game_thumbnail);
+                               pos++;
+                       }
+               }
+       }
+
+       private GameThumbnail? get_game_thumbnail (Game game) {
+               var game_thumbnail = new GameThumbnail ();
+               game_thumbnail.game = game;
+               game.replaced.connect ((new_game) => {
+                       game_thumbnail.game = new_game;
+               });
+               game_thumbnail.visible = true;
+
+               return game_thumbnail;
+       }
+}


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