Skip to content

Commit

Permalink
Leaflet → NavigationView (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Jun 5, 2024
1 parent 39ef8a9 commit 4c1a9f7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 96 deletions.
2 changes: 0 additions & 2 deletions src/Services/PlaybackManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,6 @@ public class Audience.PlaybackManager : Object {

playbin.uri = uri;

((Gtk.Application) Application.get_default ()).active_window.title = get_title (uri);

/* Set progress before subtitle uri else it gets reset to zero */
if (from_beginning) {
seek (0);
Expand Down
18 changes: 13 additions & 5 deletions src/Widgets/HeaderBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Audience.HeaderBar : Gtk.Box {

construct {
var navigation_button = new Gtk.Button.with_label ("") {
action_name = Window.ACTION_PREFIX + Window.ACTION_BACK,
valign = Gtk.Align.CENTER
};
navigation_button.get_style_context ().add_class (Granite.STYLE_CLASS_BACK_BUTTON);
Expand All @@ -34,8 +35,12 @@ public class Audience.HeaderBar : Gtk.Box {
tooltip_text = _("Unfullscreen")
};

var title_label = new Gtk.Label ("");
title_label.add_css_class (Granite.STYLE_CLASS_TITLE_LABEL);

header_bar = new Gtk.HeaderBar () {
show_title_buttons = true,
title_widget = title_label,
hexpand = true
};
header_bar.pack_start (navigation_button);
Expand All @@ -45,10 +50,15 @@ public class Audience.HeaderBar : Gtk.Box {
append (header_bar);

map.connect (() => {
var adjacent_page_name = ((Window) get_root ()).get_adjacent_page_name ();
if (adjacent_page_name != null) {
var current_page = (Adw.NavigationPage) get_ancestor (typeof (Adw.NavigationPage));
var navigation_view = (Adw.NavigationView) get_ancestor (typeof (Adw.NavigationView));

current_page.bind_property ("title", title_label, "label", SYNC_CREATE);

var previous_page = navigation_view.get_previous_page (current_page);
if (previous_page != null) {
navigation_button.visible = true;
navigation_button.label = adjacent_page_name;
navigation_button.label = previous_page.title;
} else {
navigation_button.visible = false;
}
Expand All @@ -58,8 +68,6 @@ public class Audience.HeaderBar : Gtk.Box {

unmap.connect (() => binding.unbind ());

navigation_button.clicked.connect (() => ((Adw.Leaflet) get_ancestor (typeof (Adw.Leaflet))).navigate (BACK));

unfullscreen_button.clicked.connect (() => ((Window) get_root ()).unfullscreen ());
}
}
11 changes: 7 additions & 4 deletions src/Widgets/Library/EpisodesPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

public class Audience.EpisodesPage : Gtk.Box {
public class Audience.EpisodesPage : Adw.NavigationPage {
private Gtk.Picture poster;
private Gtk.FilterListModel filter_model;
private Gtk.SearchEntry search_entry;
Expand Down Expand Up @@ -91,9 +91,12 @@ public class Audience.EpisodesPage : Gtk.Box {
grid.attach (scrolled_window, 1, 1);
grid.attach (alert_view, 1, 1);

orientation = VERTICAL;
append (header_bar);
append (grid);
var box = new Gtk.Box (VERTICAL, 0);
box.append (header_bar);
box.append (grid);

child = box;
title = _("Episodes");

factory.setup.connect ((obj) => {
var item = (Gtk.ListItem) obj;
Expand Down
5 changes: 3 additions & 2 deletions src/Widgets/Library/LibraryPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

public class Audience.LibraryPage : Gtk.Box {
public class Audience.LibraryPage : Adw.NavigationPage {
public signal void show_episodes (Objects.MediaItem item, bool setup_only = false);

private Audience.Services.LibraryManager manager;
Expand Down Expand Up @@ -76,7 +76,8 @@ public class Audience.LibraryPage : Gtk.Box {
};
toolbarview.add_top_bar (header_bar);

append (toolbarview);
child = toolbarview;
title = _("Library");

factory.setup.connect ((obj) => {
var item = (Gtk.ListItem) obj;
Expand Down
8 changes: 6 additions & 2 deletions src/Widgets/Player/PlayerPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Audience {
"asc"
};

public class PlayerPage : Gtk.Box {
public class PlayerPage : Adw.NavigationPage {
private Audience.Widgets.BottomBar bottom_bar;
private Adw.ToolbarView toolbarview;

Expand Down Expand Up @@ -57,7 +57,7 @@ namespace Audience {
toolbarview.add_top_bar (header_bar);
toolbarview.add_bottom_bar (bottom_bar);

append (toolbarview);
child = toolbarview;

map.connect (() => update_actions_enabled (true));

Expand Down Expand Up @@ -95,6 +95,10 @@ namespace Audience {
var play_pause_action = Application.get_default ().lookup_action (Audience.App.ACTION_PLAY_PAUSE);
((SimpleAction) play_pause_action).activate (null);
});

playback_manager.uri_changed.connect ((uri) => {
title = Audience.get_title (uri);
});
}

private void update_actions_enabled (bool enabled) {
Expand Down
34 changes: 19 additions & 15 deletions src/Widgets/WelcomePage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,32 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

public class Audience.WelcomePage : Granite.Placeholder {
public class Audience.WelcomePage : Adw.NavigationPage {
private string current_video;
private Gtk.Button replay_button;
private Gtk.Image replay_button_image;
private Gtk.Label replay_button_title;
private Gtk.Label replay_button_description;

public WelcomePage () {
Object (
title: _("No Videos Open"),
description: _("Select a source to begin playing.")
);
}

construct {
hexpand = true;
vexpand = true;
var placeholder = new Granite.Placeholder (_("No Videos Open")) {
description = _("Select a source to begin playing."),
hexpand = true,
vexpand = true
};

var open_button = placeholder.append_button (new ThemedIcon ("document-open"), _("Open file"), _("Open a saved file."));
replay_button = placeholder.append_button (new ThemedIcon ("media-playlist-repeat"), _("Replay last video"), "");
var disk_button = placeholder.append_button (new ThemedIcon ("media-cdrom"), _("Play from Disc"), _("Watch a DVD or open a file from disc"));
var library_button = placeholder.append_button (new ThemedIcon ("folder-videos"), _("Browse Library"), _("Watch a movie from your library"));

var box = new Gtk.Box (VERTICAL, 0);
box.append (new HeaderBar ());
box.append (placeholder);
box.add_css_class (Granite.STYLE_CLASS_VIEW);

var open_button = append_button (new ThemedIcon ("document-open"), _("Open file"), _("Open a saved file."));
replay_button = append_button (new ThemedIcon ("media-playlist-repeat"), _("Replay last video"), "");
var disk_button = append_button (new ThemedIcon ("media-cdrom"), _("Play from Disc"), _("Watch a DVD or open a file from disc"));
var library_button = append_button (new ThemedIcon ("folder-videos"), _("Browse Library"), _("Watch a movie from your library"));
child = box;
title = _("Home");

//A hacky way to update the labels and icon of the replay button
var replay_button_grid = (Gtk.Grid)replay_button.child;
Expand Down Expand Up @@ -98,7 +102,7 @@ public class Audience.WelcomePage : Granite.Placeholder {
if (current_video != "") {
var last_file = File.new_for_uri (current_video);
if (last_file.query_exists ()) {
replay_button_description.label = get_title (last_file.get_basename ());
replay_button_description.label = Audience.get_title (last_file.get_basename ());

show_replay_button = true;
}
Expand Down
92 changes: 26 additions & 66 deletions src/Window.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
*/

public class Audience.Window : Gtk.ApplicationWindow {
private Adw.Leaflet leaflet;
private Adw.NavigationView navigation_view;
private Granite.Toast app_notification;
private EpisodesPage episodes_page;
private LibraryPage library_page;
private Gtk.Box welcome_page_box;
private PlayerPage player_page;
private WelcomePage welcome_page;

public enum NavigationPage { WELCOME, LIBRARY, EPISODES }

Expand All @@ -53,8 +53,6 @@ public class Audience.Window : Gtk.ApplicationWindow {
};

static construct {
action_accelerators[ACTION_BACK] = "<Alt>Left";
action_accelerators[ACTION_BACK] = "Back";
action_accelerators[ACTION_FULLSCREEN] = "F";
action_accelerators[ACTION_FULLSCREEN] = "F11";
action_accelerators[ACTION_OPEN_FILE] = "<Control>O";
Expand All @@ -77,37 +75,19 @@ public class Audience.Window : Gtk.ApplicationWindow {

library_page = LibraryPage.get_instance ();

library_page.show_episodes.connect ((item, setup_only) => {
episodes_page.set_show (item);
if (!setup_only) {
leaflet.append (episodes_page);
leaflet.visible_child = episodes_page;

title = item.title;
}
});

var welcome_page = new WelcomePage ();

welcome_page_box = new Gtk.Box (VERTICAL, 0);
welcome_page_box.append (new HeaderBar ());
welcome_page_box.append (welcome_page);
welcome_page_box.add_css_class (Granite.STYLE_CLASS_VIEW);
welcome_page = new WelcomePage ();

player_page = new PlayerPage ();

episodes_page = new EpisodesPage ();

leaflet = new Adw.Leaflet () {
can_navigate_back = true,
can_unfold = false
};
leaflet.append (welcome_page_box);
navigation_view = new Adw.NavigationView ();
navigation_view.add (welcome_page);

app_notification = new Granite.Toast ("");

var overlay = new Gtk.Overlay () {
child = leaflet
child = navigation_view
};
overlay.add_overlay (app_notification);

Expand Down Expand Up @@ -135,7 +115,14 @@ public class Audience.Window : Gtk.ApplicationWindow {
app_notification.send_notification ();
});

leaflet.notify["visible-child"].connect (() => {
library_page.show_episodes.connect ((item, setup_only) => {
episodes_page.set_show (item);
if (!setup_only) {
navigation_view.push (episodes_page);
}
});

navigation_view.notify["visible-page"].connect (() => {
update_navigation ();
});

Expand Down Expand Up @@ -165,7 +152,7 @@ public class Audience.Window : Gtk.ApplicationWindow {
key_controller.key_released.connect (handle_key_press);

var drop_target = new Gtk.DropTarget (typeof (Gdk.FileList), COPY);
leaflet.add_controller (drop_target);
navigation_view.add_controller (drop_target);
drop_target.drop.connect ((val) => {
if (val.type () != typeof (Gdk.FileList)) {
return false;
Expand All @@ -184,7 +171,7 @@ public class Audience.Window : Gtk.ApplicationWindow {
}

private void action_back () {
leaflet.navigate (Adw.NavigationDirection.BACK);
navigation_view.pop ();
}

private void action_fullscreen () {
Expand All @@ -204,9 +191,9 @@ public class Audience.Window : Gtk.ApplicationWindow {
}

private void action_search () {
if (leaflet.visible_child == library_page) {
if (navigation_view.visible_page == library_page) {
library_page.search ();
} else if (leaflet.visible_child == episodes_page) {
} else if (navigation_view.visible_page == episodes_page) {
episodes_page.search ();
} else {
Gdk.Display.get_default ().beep ();
Expand All @@ -219,10 +206,6 @@ public class Audience.Window : Gtk.ApplicationWindow {
*/
if (!is_sandboxed ()) {
Audience.Services.LibraryManager.get_instance ().undo_delete_item ();

if (leaflet.visible_child != episodes_page) {
leaflet.visible_child = library_page;
}
}
}

Expand Down Expand Up @@ -251,7 +234,7 @@ public class Audience.Window : Gtk.ApplicationWindow {
}
}

if (leaflet.visible_child == player_page) {
if (navigation_view.visible_page == player_page) {
if (match_keycode (Gdk.Key.space, keycode) || match_keycode (Gdk.Key.p, keycode)) {
var play_pause_action = Application.get_default ().lookup_action (Audience.App.ACTION_PLAY_PAUSE);
((SimpleAction) play_pause_action).activate (null);
Expand Down Expand Up @@ -284,7 +267,7 @@ public class Audience.Window : Gtk.ApplicationWindow {
default:
break;
}
} else if (leaflet.visible_child == welcome_page_box) {
} else if (navigation_view.visible_page == welcome_page) {
bool ctrl_pressed = CONTROL_MASK in state;
if (match_keycode (Gdk.Key.p, keycode) || match_keycode (Gdk.Key.space, keycode)) {
resume_last_videos ();
Expand Down Expand Up @@ -333,8 +316,7 @@ public class Audience.Window : Gtk.ApplicationWindow {
}

public void show_library () {
leaflet.append (library_page);
leaflet.visible_child = library_page;
navigation_view.push (library_page);
}

public void run_open_file (bool clear_playlist = false, bool force_play = true) {
Expand Down Expand Up @@ -402,49 +384,27 @@ public class Audience.Window : Gtk.ApplicationWindow {
}

private void on_player_ended () {
leaflet.navigate (Adw.NavigationDirection.BACK);
navigation_view.pop ();
}

public void play_file (string uri, NavigationPage origin, bool from_beginning = true) {
leaflet.append (player_page);
leaflet.visible_child = player_page;
navigation_view.push (player_page);

PlaybackManager.get_default ().play_file (uri, from_beginning);
}

public string? get_adjacent_page_name () {
var previous_child = leaflet.get_adjacent_child (Adw.NavigationDirection.BACK);
if (previous_child == episodes_page) {
return _("Episodes");
} else if (previous_child == library_page) {
return _("Library");
} else if (previous_child == welcome_page_box) {
return _("Back");
} else {
return null;
}
}

private void update_navigation () {
int64 position = PlaybackManager.get_default ().position;
if (position > 0) {
settings.set_int64 ("last-stopped", position);
}

var play_pause_action = Application.get_default ().lookup_action (Audience.App.ACTION_PLAY_PAUSE);
((SimpleAction) play_pause_action).set_state (false);

if (leaflet.visible_child == welcome_page_box) {
title = _("Videos");
} else if (leaflet.visible_child == library_page) {
title = _("Library");
} else if (leaflet.visible_child == player_page) {
if (navigation_view.visible_page == player_page) {
((SimpleAction) play_pause_action).set_state (true);
}

var next_child = leaflet.get_adjacent_child (Adw.NavigationDirection.FORWARD);
if (next_child != null) {
leaflet.remove (next_child);
} else {
((SimpleAction) play_pause_action).set_state (false);
}
}
}

0 comments on commit 4c1a9f7

Please sign in to comment.