[gnome-games] ui: Introduce CollectionsMainPage



commit e8b5896922f09111f6553314b6de26f56821ba39
Author: Neville <nevilleantony98 gmail com>
Date:   Fri Jul 10 18:47:45 2020 +0530

    ui: Introduce CollectionsMainPage
    
    This will be used to display available collections in CollectionsPage
    in the upcoming commits

 data/org.gnome.Games.gresource.xml |   1 +
 data/ui/collections-main-page.ui   |  41 ++++++++++
 src/meson.build                    |   1 +
 src/ui/collections-main-page.vala  | 164 +++++++++++++++++++++++++++++++++++++
 4 files changed, 207 insertions(+)
---
diff --git a/data/org.gnome.Games.gresource.xml b/data/org.gnome.Games.gresource.xml
index 8cf2e639..0487f908 100644
--- a/data/org.gnome.Games.gresource.xml
+++ b/data/org.gnome.Games.gresource.xml
@@ -15,6 +15,7 @@
     <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-view.ui</file>
+    <file preprocess="xml-stripblanks">ui/collections-main-page.ui</file>
     <file preprocess="xml-stripblanks">ui/display-view.ui</file>
     <file preprocess="xml-stripblanks">ui/empty-collection.ui</file>
     <file preprocess="xml-stripblanks">ui/empty-search.ui</file>
diff --git a/data/ui/collections-main-page.ui b/data/ui/collections-main-page.ui
new file mode 100644
index 00000000..205f3e2f
--- /dev/null
+++ b/data/ui/collections-main-page.ui
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="3.24"/>
+  <template class="GamesCollectionsMainPage" parent="GtkBin">
+    <property name="visible">True</property>
+    <signal name="map" after="yes" handler="on_map"/>
+    <signal name="unmap" after="no" handler="on_unmap"/>
+    <signal name="size-allocate" after="no" handler="on_size_allocate"/>
+    <child>
+      <object class="GtkScrolledWindow" id="scrolled_window">
+        <property name="visible">True</property>
+        <property name="can-focus">True</property>
+        <property name="hscrollbar-policy">never</property>
+        <style>
+          <class name="content-view"/>
+        </style>
+        <child>
+          <object class="GtkFlowBox" id="flow_box">
+            <property name="visible">True</property>
+            <property name="can-focus">True</property>
+            <property name="halign">center</property>
+            <property name="valign">start</property>
+            <property name="margin-start">28</property>
+            <property name="margin-end">28</property>
+            <property name="margin-top">21</property>
+            <property name="margin-bottom">21</property>
+            <property name="homogeneous">True</property>
+            <property name="column-spacing">14</property>
+            <property name="row-spacing">14</property>
+            <property name="selection-mode">single</property>
+            <signal name="child-activated" handler="on_child_activated"/>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+  <object class="GamesGamepadBrowse" id="gamepad_browse">
+    <signal name="browse" handler="on_gamepad_browse"/>
+    <signal name="accept" handler="on_gamepad_accept"/>
+  </object>
+</interface>
diff --git a/src/meson.build b/src/meson.build
index 85e88625..60bd5971 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -112,6 +112,7 @@ vala_sources = [
   'ui/collection-empty.vala',
   'ui/collection-icon-view.vala',
   'ui/collection-view.vala',
+  'ui/collections-main-page.vala',
   'ui/display-bin.vala',
   'ui/display-view.vala',
   'ui/empty-collection.vala',
diff --git a/src/ui/collections-main-page.vala b/src/ui/collections-main-page.vala
new file mode 100644
index 00000000..3c84b461
--- /dev/null
+++ b/src/ui/collections-main-page.vala
@@ -0,0 +1,164 @@
+// This file is part of GNOME Games. License: GPL-3.0+.
+
+[GtkTemplate (ui = "/org/gnome/Games/ui/collections-main-page.ui")]
+private class Games.CollectionsMainPage : Gtk.Bin {
+       public signal void collection_activated (Collection collection);
+       public signal void gamepad_accepted ();
+
+       private Binding window_active_binding;
+       private bool _is_active;
+       public bool is_active {
+               get { return _is_active; }
+               set {
+                       if (_is_active == value)
+                               return;
+
+                       _is_active = value;
+
+                       if (!_is_active)
+                               gamepad_browse.cancel_cursor_movement ();
+               }
+       }
+
+       private CollectionModel _collection_model;
+       public CollectionModel collection_model {
+               get { return _collection_model; }
+               set {
+                       _collection_model = value;
+                       flow_box.bind_model (collection_model, add_collection);
+               }
+       }
+
+       [GtkChild]
+       private Gtk.FlowBox flow_box;
+       [GtkChild]
+       private GamepadBrowse gamepad_browse;
+       [GtkChild]
+       private Gtk.ScrolledWindow scrolled_window;
+
+       static construct {
+               set_css_name ("gamescollectionsmainpage");
+       }
+
+       construct {
+               flow_box.max_children_per_line = uint.MAX;
+       }
+
+       public bool has_collection_selected () {
+               foreach (var child in flow_box.get_selected_children ())
+                       if (child.get_mapped ())
+                               return true;
+
+               return false;
+       }
+
+       public bool select_default_collection (Gtk.DirectionType direction) {
+               Gtk.FlowBoxChild? child;
+               for (int i = 0; (child = flow_box.get_child_at_index (i)) != null; i++) {
+                       if (child.get_mapped ()) {
+                               flow_box.select_child (child);
+                               // This is needed to start moving the cursor with the gamepad only.
+                               child.focus (direction);
+
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
+       private Gtk.Widget add_collection (Object item) {
+               return new CollectionIconView (item as Collection);
+       }
+
+       public bool gamepad_button_press_event (Manette.Event event) {
+               if (!get_mapped ())
+                       return false;
+
+               return gamepad_browse.gamepad_button_press_event (event);
+       }
+
+       public bool gamepad_button_release_event (Manette.Event event) {
+               if (!get_mapped ())
+                       return false;
+
+               return gamepad_browse.gamepad_button_release_event (event);
+       }
+
+       public bool gamepad_absolute_axis_event (Manette.Event event) {
+               if (!get_mapped ())
+                       return false;
+
+               return gamepad_browse.gamepad_absolute_axis_event (event);
+       }
+
+       public void reset_scroll_position () {
+               var adjustment = scrolled_window.get_vadjustment ();
+               adjustment.value = 0;
+       }
+
+       [GtkCallback]
+       public void on_map () {
+               window_active_binding = null;
+               is_active = false;
+
+               var window = get_ancestor (typeof (Gtk.Window));
+               if (window == null)
+                       return;
+
+               window_active_binding = window.bind_property ("is-active", this, "is-active", 
BindingFlags.SYNC_CREATE);
+       }
+
+       [GtkCallback]
+       public void on_unmap () {
+               window_active_binding = null;
+               is_active = false;
+       }
+
+       [GtkCallback]
+       private bool on_gamepad_browse (Gtk.DirectionType direction) {
+               if (!has_collection_selected ())
+                       // This is needed to start moving the cursor with the gamepad only.
+                       return select_default_collection (direction);
+
+               switch (direction) {
+               case Gtk.DirectionType.UP:
+                       return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, -1);
+               case Gtk.DirectionType.DOWN:
+                       return flow_box.move_cursor (Gtk.MovementStep.DISPLAY_LINES, 1);
+               case Gtk.DirectionType.LEFT:
+                       return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, -1);
+               case Gtk.DirectionType.RIGHT:
+                       return flow_box.move_cursor (Gtk.MovementStep.VISUAL_POSITIONS, 1);
+               default:
+                       return false;
+               }
+       }
+
+       [GtkCallback]
+       private bool on_gamepad_accept () {
+               flow_box.activate_cursor_child ();
+               gamepad_accepted ();
+
+               return true;
+       }
+
+       [GtkCallback]
+       private void on_child_activated (Gtk.FlowBoxChild child) {
+               var collection_icon_view = child as CollectionIconView;
+               if (collection_icon_view != null)
+                       collection_activated (collection_icon_view.collection);
+       }
+
+       [GtkCallback]
+       private void on_size_allocate (Gtk.Allocation allocation) {
+               // If the window's width is less than half the width of a 1920×1080
+               // screen, display the game thumbnails at half the size to see more of
+               // them rather than a few huge thumbnails, making Games more usable on
+               // small screens.
+               if (allocation.width < 960)
+                       get_style_context ().remove_class ("large");
+               else
+                       get_style_context ().add_class ("large");
+       }
+}


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