Skip to content

Commit 872e5ff

Browse files
committed
Use GtkSelection
1 parent b9f6b9a commit 872e5ff

1 file changed

Lines changed: 23 additions & 26 deletions

File tree

src/Views/Wallpaper.vala

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
2323
private static GLib.Settings gnome_background_settings;
2424

2525
private Gtk.ScrolledWindow wallpaper_scrolled_window;
26-
private Gtk.FlowBox wallpaper_view;
2726
private Gtk.Overlay view_overlay;
2827
private Gtk.ComboBoxText combo;
2928
private Gtk.ColorButton color_button;
3029

3130
private GLib.ListStore wallpaper_model;
31+
private Gtk.SingleSelection wallpaper_selection;
3232
private WallpaperContainer active_wallpaper = null;
3333
private SolidColorContainer solid_color = null;
3434
private WallpaperContainer wallpaper_for_removal = null;
@@ -56,14 +56,16 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
5656

5757
wallpaper_model = new GLib.ListStore (typeof (WallpaperContainer));
5858

59-
wallpaper_view = new Gtk.FlowBox () {
59+
wallpaper_selection = new Gtk.SingleSelection (wallpaper_model);
60+
61+
var wallpaper_view = new Gtk.FlowBox () {
6062
activate_on_single_click = true,
6163
homogeneous = true,
6264
selection_mode = SINGLE,
6365
min_children_per_line = 3,
6466
max_children_per_line = 5
6567
};
66-
wallpaper_view.bind_model (wallpaper_model, create_widget_func);
68+
wallpaper_view.bind_model (wallpaper_selection, create_widget_func);
6769
wallpaper_view.add_css_class (Granite.STYLE_CLASS_VIEW);
6870
wallpaper_view.child_activated.connect (update_checked_wallpaper);
6971
wallpaper_view.add_controller (drop_target);
@@ -195,7 +197,7 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
195197
}
196198

197199
private void update_checked_wallpaper (Gtk.FlowBox box, Gtk.FlowBoxChild child) {
198-
var children = (WallpaperContainer) wallpaper_view.get_selected_children ().data;
200+
var children = (WallpaperContainer) wallpaper_selection.get_selected_item ();
199201

200202
if (!(children is SolidColorContainer)) {
201203
current_wallpaper_path = children.uri;
@@ -228,9 +230,8 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
228230
set_combo_disabled_if_necessary ();
229231
create_solid_color_container (color_button.rgba.to_string ());
230232

231-
wallpaper_model.insert_sorted (solid_color, wallpapers_sort_function);
232-
233-
wallpaper_view.select_child (solid_color);
233+
var pos = wallpaper_model.insert_sorted (solid_color, wallpapers_sort_function);
234+
wallpaper_selection.set_selected (pos);
234235

235236
if (active_wallpaper != null) {
236237
active_wallpaper.checked = false;
@@ -251,17 +252,17 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
251252
if (active_wallpaper == solid_color) {
252253
active_wallpaper.checked = false;
253254

254-
var child = wallpaper_view.get_first_child ();
255-
while (child != null) {
255+
var child = wallpaper_selection.get_item (0);
256+
for (var pos = 0; child != null; pos++) {
256257
var container = (WallpaperContainer) child;
257258
if (container.uri == current_wallpaper_path) {
258259
container.checked = true;
259-
wallpaper_view.select_child (container);
260+
wallpaper_selection.set_selected (pos);
260261
active_wallpaper = container;
261262
break;
262263
}
263264

264-
child = child.get_next_sibling ();
265+
child = wallpaper_selection.get_item (pos);
265266
}
266267
}
267268
} else {
@@ -337,9 +338,12 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
337338
finished = true;
338339

339340
if (gnome_background_settings.get_string ("picture-options") == "none") {
340-
wallpaper_view.select_child (solid_color);
341-
solid_color.checked = true;
342-
active_wallpaper = solid_color;
341+
uint pos = -1;
342+
if (wallpaper_model.find (solid_color, out pos)) {
343+
wallpaper_selection.set_selected (pos);
344+
solid_color.checked = true;
345+
active_wallpaper = solid_color;
346+
}
343347
}
344348

345349
if (active_wallpaper != null) {
@@ -356,14 +360,9 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
356360
}
357361

358362
private void create_solid_color_container (string color) {
359-
if (solid_color != null) {
360-
wallpaper_view.unselect_child (solid_color);
361-
362-
uint pos = -1;
363-
if (wallpaper_model.find (solid_color, out pos)) {
364-
wallpaper_model.remove (pos);
365-
}
366-
363+
uint pos = -1;
364+
if (solid_color != null && wallpaper_model.find (solid_color, out pos)) {
365+
wallpaper_model.remove (pos);
367366
solid_color.destroy ();
368367
}
369368

@@ -402,7 +401,7 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
402401
var thumb_path = info.get_attribute_as_string (FileAttribute.THUMBNAIL_PATH);
403402
var thumb_valid = info.get_attribute_boolean (FileAttribute.THUMBNAIL_IS_VALID);
404403
var wallpaper = new WallpaperContainer (uri, thumb_path, thumb_valid);
405-
wallpaper_model.insert_sorted (wallpaper, wallpapers_sort_function);
404+
var pos = wallpaper_model.insert_sorted (wallpaper, wallpapers_sort_function);
406405

407406
wallpaper.trash.connect (() => {
408407
send_undo_toast ();
@@ -411,16 +410,14 @@ public class PantheonShell.Wallpaper : Switchboard.SettingsPage {
411410

412411
// Select the wallpaper if it is the current wallpaper
413412
if (current_wallpaper_path.has_suffix (uri) && gnome_background_settings.get_string ("picture-options") != "none") {
414-
this.wallpaper_view.select_child (wallpaper);
413+
wallpaper_selection.set_selected (pos);
415414
// Set the widget activated without activating it
416415
wallpaper.checked = true;
417416
active_wallpaper = wallpaper;
418417
}
419418
} catch (Error e) {
420419
critical ("Unable to add wallpaper: %s", e.message);
421420
}
422-
423-
wallpaper_view.invalidate_sort ();
424421
}
425422

426423
public void cancel_thumbnail_generation () {

0 commit comments

Comments
 (0)