[gnome-boxes/polish-snapshot-preferences: 4/6] preferences, snapshot-list: Map "selected" state to current snapshot




commit 7a646b41235ed3721dbcaabe2472a5462cfc870b
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Jan 4 15:55:28 2022 +0100

    preferences, snapshot-list: Map "selected" state to current snapshot
    
    The only selected row in the snapshot list is the current snapshot.

 data/ui/preferences/snapshot-list-row.ui |  1 +
 data/ui/preferences/snapshots-page.ui    |  2 +-
 src/preferences/snapshot-list-row.vala   |  5 +++--
 src/preferences/snapshots-page.vala      | 35 ++++++++++++++++++++++++++++----
 4 files changed, 36 insertions(+), 7 deletions(-)
---
diff --git a/data/ui/preferences/snapshot-list-row.ui b/data/ui/preferences/snapshot-list-row.ui
index cf729b78..7daf18da 100644
--- a/data/ui/preferences/snapshot-list-row.ui
+++ b/data/ui/preferences/snapshot-list-row.ui
@@ -21,6 +21,7 @@
 
   <template class="BoxesSnapshotListRow" parent="HdyActionRow">
     <property name="visible">True</property>
+    <property name="activatable">False</property>
     <property name="can_focus">True</property>
 
     <child>
diff --git a/data/ui/preferences/snapshots-page.ui b/data/ui/preferences/snapshots-page.ui
index bdb48f08..196ff9a8 100644
--- a/data/ui/preferences/snapshots-page.ui
+++ b/data/ui/preferences/snapshots-page.ui
@@ -22,7 +22,7 @@
                 <child>
                   <object class="GtkListBox" id="listbox">
                     <property name="visible">True</property>
-                    <property name="selection-mode">none</property>
+                    <property name="selection-mode">single</property>
                     <signal name="add" handler="update_snapshot_stack_page"/>
                     <signal name="remove" handler="update_snapshot_stack_page"/>
                     <style>
diff --git a/src/preferences/snapshot-list-row.vala b/src/preferences/snapshot-list-row.vala
index c32ccb2b..a9574608 100644
--- a/src/preferences/snapshot-list-row.vala
+++ b/src/preferences/snapshot-list-row.vala
@@ -3,6 +3,7 @@
 [GtkTemplate (ui = "/org/gnome/Boxes/ui/preferences/snapshot-list-row.ui")]
 private class Boxes.SnapshotListRow : Hdy.ActionRow {
     public signal void deletion_requested (Boxes.PreferencesToast toast);
+    public signal void is_current ();
 
     public GVir.DomainSnapshot snapshot;
     public string activity_message { get; set; default = ""; }
@@ -36,8 +37,6 @@
 
             this.parent_container = parent;
         });
-        this.selectable = false;
-        this.activatable = false;
     }
 
     public SnapshotListRow (GVir.DomainSnapshot snapshot,
@@ -98,6 +97,8 @@ private void revert_to_activated (GLib.SimpleAction action, GLib.Variant? v) {
             try {
                 snapshot.revert_to_async.end (res);
                 parent_container.queue_draw ();
+
+                is_current ();
             } catch (GLib.Error e) {
                 warning (e.message);
                 machine.window.notificationbar.display_error (_("Failed to apply snapshot"));
diff --git a/src/preferences/snapshots-page.vala b/src/preferences/snapshots-page.vala
index 89a5442a..9bfea776 100644
--- a/src/preferences/snapshots-page.vala
+++ b/src/preferences/snapshots-page.vala
@@ -63,21 +63,48 @@ private async void fetch_snapshots () {
                                                         fetch_snapshots_cancellable);
             var snapshots =  machine.domain.get_snapshots ();
             foreach (var snapshot in snapshots) {
-                add_snapshot_row (snapshot);
+                listbox.add (create_snapshot_row (snapshot));
             }
         } catch (GLib.Error e) {
             warning ("Could not fetch snapshots: %s", e.message);
         }
     }
 
-    private void add_snapshot_row (GVir.DomainSnapshot snapshot) {
+    private SnapshotListRow create_snapshot_row (GVir.DomainSnapshot snapshot) {
         var row = new SnapshotListRow (snapshot, machine);
         row.notify["activity-message"].connect (row_activity_changed);
         row.deletion_requested.connect (on_row_deleted);
+        row.is_current.connect (select_row);
 
-        listbox.add (row);
+        try {
+            bool is_current = false;
+            if (snapshot.get_is_current (0, out is_current)) {
+                row.selectable = is_current;
+                if (is_current)
+                    select_row (row);
+            }
+        } catch (GLib.Error error) {
+            warning ("Failed to determine whether '%s' is the current snapshot: %s",
+                     snapshot.get_name (), error.message);
+        }
+
+        return row;
+    }
+
+    private void select_row (SnapshotListRow selected_row) {
+        // We want only the last created snapshot to be selectable
+        foreach (var child in listbox.get_children ()) {
+            if (child is SnapshotListRow) {
+                var row = child as SnapshotListRow;
+                row.selectable = false;
+            }
+        }
+
+        selected_row.selectable = true;
+        listbox.select_row (selected_row);
     }
 
+
     private void on_row_deleted (Boxes.PreferencesToast new_toast) {
         if (toast != null) {
             toast.dismiss ();
@@ -117,7 +144,7 @@ private async void create_snapshot () {
 
         try {
             var new_snapshot = yield machine.create_snapshot ();
-            add_snapshot_row (new_snapshot);
+            listbox.add (create_snapshot_row (new_snapshot));
         } catch (GLib.Error e) {
             var msg = _("Failed to create snapshot of %s").printf (machine.name);
             machine.window.notificationbar.display_error (msg);


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