Share UI between screens

The recording screen was reverted to a dummy in the process.
This commit is contained in:
Elias Projahn 2021-01-31 20:03:20 +01:00
parent 2d846a7b1a
commit 6abd450452
17 changed files with 555 additions and 977 deletions

View file

@ -13,4 +13,10 @@ pub use player_bar::*;
pub mod poe_list;
pub use poe_list::*;
pub mod screen;
pub use screen::*;
pub mod section;
pub use section::*;
mod indexed_list_model;

113
src/widgets/screen.rs Normal file
View file

@ -0,0 +1,113 @@
use gio::prelude::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
/// A general framework for screens. Screens have a header bar with at least
/// a button to go back and a scrollable content area that clamps its content.
pub struct Screen {
/// The actual GTK widget.
pub widget: gtk::Box,
/// The button to switch to the previous screen.
back_button: gtk::Button,
/// The title widget within the header bar.
window_title: libadwaita::WindowTitle,
/// The action menu.
menu: gio::Menu,
/// The entry for searching.
search_entry: gtk::SearchEntry,
/// The stack to switch to the loading page.
stack: gtk::Stack,
/// The box containing the content.
content_box: gtk::Box,
/// The actions for the menu.
actions: gio::SimpleActionGroup,
}
impl Screen {
/// Create a new screen.
pub fn new() -> Self {
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/screen.ui");
get_widget!(builder, gtk::Box, widget);
get_widget!(builder, gtk::Button, back_button);
get_widget!(builder, libadwaita::WindowTitle, window_title);
get_widget!(builder, gio::Menu, menu);
get_widget!(builder, gtk::ToggleButton, search_button);
get_widget!(builder, gtk::SearchEntry, search_entry);
get_widget!(builder, gtk::Stack, stack);
get_widget!(builder, gtk::Box, content_box);
let actions = gio::SimpleActionGroup::new();
widget.insert_action_group("widget", Some(&actions));
search_button.connect_toggled(clone!(@strong search_entry => move |search_button| {
if search_button.get_active() {
search_entry.grab_focus();
}
}));
Self {
widget,
back_button,
window_title,
menu,
search_entry,
stack,
content_box,
actions,
}
}
/// Set a closure to be called when the back button is pressed.
pub fn set_back_cb<F: Fn() + 'static>(&self, cb: F) {
self.back_button.connect_clicked(move |_| cb());
}
/// Show a title in the header bar.
pub fn set_title(&self, title: &str) {
self.window_title.set_title(Some(title));
}
/// Show a subtitle in the header bar.
pub fn set_subtitle(&self, subtitle: &str) {
self.window_title.set_subtitle(Some(subtitle));
}
/// Add a new item to the action menu and register a callback for it.
pub fn add_action<F: Fn() + 'static>(&self, label: &str, cb: F) {
let name = rand::random::<u64>().to_string();
let action = gio::SimpleAction::new(&name, None);
action.connect_activate(move |_, _| cb());
self.actions.add_action(&action);
self.menu.append(Some(label), Some(&format!("widget.{}", name)));
}
/// Set the closure to be called when the search string has changed.
pub fn set_search_cb<F: Fn() + 'static>(&self, cb: F) {
self.search_entry.connect_search_changed(move |_| cb());
}
/// Get the current search string.
pub fn get_search(&self) -> String {
self.search_entry.get_text().unwrap().to_string().to_lowercase()
}
/// Hide the loading page and switch to the content.
pub fn ready(&self) {
self.stack.set_visible_child_name("content");
}
/// Add content to the bottom of the content area.
pub fn add_content<W: IsA<gtk::Widget>>(&self, content: &W) {
self.content_box.append(content);
}
}

48
src/widgets/section.rs Normal file
View file

@ -0,0 +1,48 @@
use gtk::prelude::*;
use gtk_macros::get_widget;
/// A widget displaying a title, a framed child widget and, if needed, some
/// actions.
pub struct Section {
/// The actual GTK widget.
pub widget: gtk::Box,
/// The box containing the title and action buttons.
title_box: gtk::Box,
}
impl Section {
/// Create a new section.
pub fn new<W: IsA<gtk::Widget>>(title: &str, content: &W) -> Self {
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/section.ui");
get_widget!(builder, gtk::Box, widget);
get_widget!(builder, gtk::Box, title_box);
get_widget!(builder, gtk::Label, title_label);
get_widget!(builder, gtk::Frame, frame);
title_label.set_label(title);
frame.set_child(Some(content));
Self {
widget,
title_box,
}
}
/// Add an action button. This should by definition be something that is
/// doing something with the child widget that is applicable in all
/// situations where the widget is visible. The new button will be packed
/// to the end of the title box.
pub fn add_action<F: Fn() + 'static>(&self, icon_name: &str, cb: F) {
let button = gtk::ButtonBuilder::new()
.has_frame(false)
.valign(gtk::Align::Center)
.icon_name(icon_name)
.build();
button.connect_clicked(move |_| cb());
self.title_box.append(&button);
}
}