From 89762892b2866667958359574ed188a96c46d855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Sun, 5 Feb 2023 08:50:09 -0800 Subject: [PATCH] TorrentListBox: make placeholder internal --- src/MainWindow.vala | 64 ++++----------------------------- src/Widgets/TorrentListBox.vala | 50 ++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 60 deletions(-) diff --git a/src/MainWindow.vala b/src/MainWindow.vala index 106260b..53dd395 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -21,7 +21,6 @@ public class Torrential.MainWindow : Gtk.ApplicationWindow { private Gtk.Button magnet_button; - private Gtk.Stack stack; private Granite.Toast toast; private Widgets.MultiInfoBar infobar; private Widgets.TorrentListBox list_box; @@ -33,13 +32,13 @@ public class Torrential.MainWindow : Gtk.ApplicationWindow { public TorrentManager torrent_manager; private FileMonitor download_monitor; - private const string ACTION_GROUP_PREFIX_NAME = "tor"; - private const string ACTION_GROUP_PREFIX = ACTION_GROUP_PREFIX_NAME + "."; + public const string ACTION_GROUP_PREFIX_NAME = "tor"; + public const string ACTION_GROUP_PREFIX = ACTION_GROUP_PREFIX_NAME + "."; private const string ACTION_FILTER = "action-filter"; - private const string ACTION_PREFERENCES = "preferences"; + public const string ACTION_PREFERENCES = "preferences"; private const string ACTION_QUIT = "quit"; - private const string ACTION_OPEN = "open"; + public const string ACTION_OPEN = "open"; private const string ACTION_OPEN_MAGNET = "open-magnet"; private const string ACTION_OPEN_COMPLETED_TORRENT = "show-torrent"; private const string ACTION_SHOW_WINDOW = "show-window"; @@ -110,44 +109,9 @@ public class Torrential.MainWindow : Gtk.ApplicationWindow { child = list_box }; - var welcome_screen = new Granite.Placeholder (_("No Torrents Added")) { - description = _("Add a torrent file to begin downloading.") - }; - - var open_button = welcome_screen.append_button ( - new ThemedIcon ("folder"), - _("Open Torrent"), - _("Open a torrent file from your computer.") - ); - open_button.action_name = ACTION_GROUP_PREFIX + ACTION_OPEN; - - var preferences_button = welcome_screen.append_button ( - new ThemedIcon ("open-menu"), - _("Preferences"), - _("Set download folder and other preferences.") - ); - preferences_button.action_name = ACTION_GROUP_PREFIX + ACTION_PREFERENCES; - - var no_results_alertview = new Granite.Placeholder (_("No Search Results")) { - description = _("Try changing search terms"), - icon = new ThemedIcon ("edit-find-symbolic") - }; - - var empty_category_alertview = new Granite.Placeholder (_("No Torrents Here")) { - description = _("Try a different category"), - icon = new ThemedIcon ("edit-find-symbolic") - }; - - stack = new Gtk.Stack (); - stack.add_named (welcome_screen, "welcome"); - stack.add_named (list_box_scroll, "main"); - stack.add_named (no_results_alertview, "no_results"); - stack.add_named (empty_category_alertview, "empty_category"); - stack.visible_child_name = "welcome"; - var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0); box.append (infobar); - box.append (stack); + box.append (list_box_scroll); toast = new Granite.Toast (""); @@ -210,7 +174,6 @@ public class Torrential.MainWindow : Gtk.ApplicationWindow { if (torrents.size == 0) { search_entry.sensitive = false; ((SimpleAction) actions.lookup_action (ACTION_FILTER)).set_enabled (false); - stack.visible_child_name = "welcome"; } } @@ -290,27 +253,12 @@ public class Torrential.MainWindow : Gtk.ApplicationWindow { } private void update_view () { - if (search_entry.text != "") { - list_box.filter (Widgets.TorrentListBox.FilterType.SEARCH, search_entry.text); - if (!list_box.has_visible_children ()) { - stack.visible_child_name = "no_results"; - } else { - stack.visible_child_name = "main"; - } - return; - } - - if (!list_box.has_visible_children ()) { - stack.visible_child_name = "empty_category"; - } else { - stack.visible_child_name = "main"; - } + list_box.filter (Widgets.TorrentListBox.FilterType.SEARCH, search_entry.text); } private void enable_main_view () { search_entry.sensitive = true; ((SimpleAction) actions.lookup_action (ACTION_FILTER)).set_enabled (true); - stack.visible_child_name = "main"; } private void on_link_copied () { diff --git a/src/Widgets/TorrentListBox.vala b/src/Widgets/TorrentListBox.vala index 0eb8a7c..9f1e002 100644 --- a/src/Widgets/TorrentListBox.vala +++ b/src/Widgets/TorrentListBox.vala @@ -44,6 +44,9 @@ public class Torrential.Widgets.TorrentListBox : Gtk.Box { private const string ACTION_OPEN = "action-open"; private Gtk.ListBox listbox; + private Gtk.Stack stack; + private Granite.Placeholder search_placeholder; + private Granite.Placeholder welcome_placeholder; public TorrentListBox (Gee.ArrayList torrents) { Object (torrents: torrents); @@ -56,6 +59,33 @@ public class Torrential.Widgets.TorrentListBox : Gtk.Box { var key_controller = new Gtk.EventControllerKey (); + welcome_placeholder = new Granite.Placeholder (_("No Torrents Added")) { + description = _("Add a torrent file to begin downloading.") + }; + + var open_button = welcome_placeholder.append_button ( + new ThemedIcon ("folder"), + _("Open Torrent"), + _("Open a torrent file from your computer.") + ); + open_button.action_name = MainWindow.ACTION_GROUP_PREFIX + MainWindow.ACTION_OPEN; + + var preferences_button = welcome_placeholder.append_button ( + new ThemedIcon ("open-menu"), + _("Preferences"), + _("Set download folder and other preferences.") + ); + preferences_button.action_name = MainWindow.ACTION_GROUP_PREFIX + MainWindow.ACTION_PREFERENCES; + + search_placeholder = new Granite.Placeholder ("") { + icon = new ThemedIcon ("edit-find-symbolic") + }; + + stack = new Gtk.Stack (); + stack.add_child (welcome_placeholder); + stack.add_child (search_placeholder); + stack.visible_child = welcome_placeholder; + listbox = new Gtk.ListBox () { activate_on_single_click = false, hexpand = true, @@ -67,6 +97,7 @@ public class Torrential.Widgets.TorrentListBox : Gtk.Box { listbox.add_controller (secondary_click_gesture); listbox.row_activated.connect (on_row_activated); listbox.set_sort_func (sort); + listbox.set_placeholder (stack); append (listbox); @@ -146,7 +177,9 @@ public class Torrential.Widgets.TorrentListBox : Gtk.Box { public void update () { var child = listbox.get_first_child (); while (child != null) { - ((TorrentListRow) child).update (); + if (child is TorrentListRow) { + ((TorrentListRow) child).update (); + } child = child.get_next_sibling (); } @@ -241,6 +274,19 @@ public class Torrential.Widgets.TorrentListBox : Gtk.Box { } public void filter (FilterType filter, string? search_term) { + if (has_visible_children ()) { + stack.visible_child = search_placeholder; + if (search_term != null && search_term != "") { + search_placeholder.title = _("No Search Results"); + search_placeholder.description = _("Try changing search terms"); + } else { + search_placeholder.title = _("No Torrents Here"); + search_placeholder.description = _("Try a different category"); + } + } else { + stack.visible_child = welcome_placeholder; + } + switch (filter) { case FilterType.ALL: listbox.set_filter_func (null); @@ -273,7 +319,7 @@ public class Torrential.Widgets.TorrentListBox : Gtk.Box { public bool has_visible_children () { var child = listbox.get_first_child (); while (child != null) { - if (child.get_child_visible ()) { + if (child is TorrentListRow && child.visible) { return true; }