Skip to content

Commit

Permalink
TorrentListBox: make placeholder internal (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Feb 6, 2023
1 parent d5df98c commit 2e22a8b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 60 deletions.
64 changes: 6 additions & 58 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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 ("");

Expand Down Expand Up @@ -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";
}
}

Expand Down Expand Up @@ -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 () {
Expand Down
50 changes: 48 additions & 2 deletions src/Widgets/TorrentListBox.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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<Torrent> torrents) {
Object (torrents: torrents);
Expand All @@ -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,
Expand All @@ -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);

Expand Down Expand Up @@ -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 ();
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 2e22a8b

Please sign in to comment.