From 29e89580d80122e5ea2e99cc82e9383403f4e911 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Mon, 1 Feb 2021 18:31:05 +0100 Subject: [PATCH] Add a common editor widget --- res/musicus.gresource.xml | 1 + res/ui/editor.ui | 125 ++++++++++++++++++++++++++++++++++ src/editors/person.rs | 78 ++++++++++++--------- src/meson.build | 3 + src/widgets/editor.rs | 85 +++++++++++++++++++++++ src/widgets/entry_row.rs | 44 ++++++++++++ src/widgets/mod.rs | 9 +++ src/widgets/upload_section.rs | 53 ++++++++++++++ 8 files changed, 364 insertions(+), 34 deletions(-) create mode 100644 res/ui/editor.ui create mode 100644 src/widgets/editor.rs create mode 100644 src/widgets/entry_row.rs create mode 100644 src/widgets/upload_section.rs diff --git a/res/musicus.gresource.xml b/res/musicus.gresource.xml index f1dbbbd..286a78b 100644 --- a/res/musicus.gresource.xml +++ b/res/musicus.gresource.xml @@ -1,6 +1,7 @@ + ui/editor.ui ui/ensemble_editor.ui ui/instrument_editor.ui ui/login_dialog.ui diff --git a/res/ui/editor.ui b/res/ui/editor.ui new file mode 100644 index 0000000..24d6973 --- /dev/null +++ b/res/ui/editor.ui @@ -0,0 +1,125 @@ + + + + + + crossfade + + + content + + + vertical + + + false + false + + + + + + Cancel + + + + + Save + + + + + + + + true + + + + + vertical + 12 + 12 + 36 + + + + + + + + + + + + + loading + + + vertical + + + false + false + + + Loading + + + + + + + true + true + center + center + 32 + 32 + true + + + + + + + + + error + + + vertical + + + false + false + + + Error + + + + + + + network-error-symbolic + Error + + + Try again + true + true + center + center + + + + + + + + + + diff --git a/src/editors/person.rs b/src/editors/person.rs index 9863397..a70051c 100644 --- a/src/editors/person.rs +++ b/src/editors/person.rs @@ -1,7 +1,9 @@ use crate::backend::Backend; -use crate::database::*; -use crate::widgets::{Navigator, NavigatorScreen}; +use crate::database::generate_id; +use crate::database::Person; +use crate::widgets::{Editor, EntryRow, Navigator, NavigatorScreen, Section, UploadSection}; use anyhow::Result; +use gettextrs::gettext; use glib::clone; use gtk::prelude::*; use gtk_macros::get_widget; @@ -11,12 +13,14 @@ use std::rc::Rc; /// A dialog for creating or editing a person. pub struct PersonEditor { backend: Rc, + + /// The ID of the person that is edited or a newly generated one. id: String, - widget: gtk::Stack, - info_bar: gtk::InfoBar, - first_name_entry: gtk::Entry, - last_name_entry: gtk::Entry, - upload_switch: gtk::Switch, + + editor: Editor, + first_name: EntryRow, + last_name: EntryRow, + upload: UploadSection, saved_cb: RefCell ()>>>, navigator: RefCell>>, } @@ -24,22 +28,29 @@ pub struct PersonEditor { impl PersonEditor { /// Create a new person editor and optionally initialize it. pub fn new(backend: Rc, person: Option) -> Rc { - // Create UI + let editor = Editor::new(); + editor.set_title("Person"); - let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/person_editor.ui"); + let list = gtk::ListBoxBuilder::new() + .selection_mode(gtk::SelectionMode::None) + .build(); - get_widget!(builder, gtk::Stack, widget); - get_widget!(builder, gtk::Button, back_button); - get_widget!(builder, gtk::Button, save_button); - get_widget!(builder, gtk::InfoBar, info_bar); - get_widget!(builder, gtk::Entry, first_name_entry); - get_widget!(builder, gtk::Entry, last_name_entry); - get_widget!(builder, gtk::Switch, upload_switch); + let first_name = EntryRow::new(&gettext("First name")); + let last_name = EntryRow::new(&gettext("Last name")); + + list.append(&first_name.widget); + list.append(&last_name.widget); + + let section = Section::new(&gettext("General"), &list); + let upload = UploadSection::new(); + + editor.add_content(§ion.widget); + editor.add_content(&upload.widget); let id = match person { Some(person) => { - first_name_entry.set_text(&person.first_name); - last_name_entry.set_text(&person.last_name); + first_name.set_text(&person.first_name); + last_name.set_text(&person.last_name); person.id } @@ -49,29 +60,28 @@ impl PersonEditor { let this = Rc::new(Self { backend, id, - widget, - info_bar, - first_name_entry, - last_name_entry, - upload_switch, + editor, + first_name, + last_name, + upload, saved_cb: RefCell::new(None), navigator: RefCell::new(None), }); // Connect signals and callbacks - back_button.connect_clicked(clone!(@strong this => move |_| { + this.editor.set_back_cb(clone!(@strong this => move || { let navigator = this.navigator.borrow().clone(); if let Some(navigator) = navigator { navigator.pop(); } })); - save_button.connect_clicked(clone!(@strong this => move |_| { + this.editor.set_save_cb(clone!(@strong this => move || { let context = glib::MainContext::default(); let clone = this.clone(); context.spawn_local(async move { - clone.widget.set_visible_child_name("loading"); + clone.editor.loading(); match clone.clone().save().await { Ok(_) => { let navigator = clone.navigator.borrow().clone(); @@ -79,9 +89,9 @@ impl PersonEditor { navigator.pop(); } } - Err(_) => { - clone.info_bar.set_revealed(true); - clone.widget.set_visible_child_name("content"); + Err(err) => { + let description = gettext!("Cause: {}", err); + clone.editor.error(&gettext("Failed to save person!"), &description); } } @@ -98,8 +108,8 @@ impl PersonEditor { /// Save the person and possibly upload it to the server. async fn save(self: Rc) -> Result<()> { - let first_name = self.first_name_entry.get_text().unwrap().to_string(); - let last_name = self.last_name_entry.get_text().unwrap().to_string(); + let first_name = self.first_name.get_text(); + let last_name = self.last_name.get_text(); let person = Person { id: self.id.clone(), @@ -107,8 +117,7 @@ impl PersonEditor { last_name, }; - let upload = self.upload_switch.get_active(); - if upload { + if self.upload.get_active() { self.backend.post_person(&person).await?; } @@ -129,10 +138,11 @@ impl NavigatorScreen for PersonEditor { } fn get_widget(&self) -> gtk::Widget { - self.widget.clone().upcast() + self.editor.widget.clone().upcast() } fn detach_navigator(&self) { self.navigator.replace(None); } } + diff --git a/src/meson.build b/src/meson.build index 6c97a64..88aa8da 100644 --- a/src/meson.build +++ b/src/meson.build @@ -90,6 +90,9 @@ sources = files( 'selectors/recording.rs', 'selectors/selector.rs', 'selectors/work.rs', + 'widgets/editor.rs', + 'widgets/entry_row.rs', + 'widgets/upload_section.rs', 'widgets/indexed_list_model.rs', 'widgets/list.rs', 'widgets/mod.rs', diff --git a/src/widgets/editor.rs b/src/widgets/editor.rs new file mode 100644 index 0000000..2844c1d --- /dev/null +++ b/src/widgets/editor.rs @@ -0,0 +1,85 @@ +use gio::prelude::*; +use glib::clone; +use gtk::prelude::*; +use gtk_macros::get_widget; + +/// Common UI elements for an editor. +pub struct Editor { + /// The actual GTK widget. + pub widget: gtk::Stack, + + /// The button to switch to the previous screen. + back_button: gtk::Button, + + /// The title widget within the header bar. + window_title: libadwaita::WindowTitle, + + /// The button to save the edited item. + save_button: gtk::Button, + + /// The box containing the content. + content_box: gtk::Box, + + /// The status page for the error screen. + status_page: libadwaita::StatusPage, +} + +impl Editor { + /// Create a new screen. + pub fn new() -> Self { + let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/editor.ui"); + + get_widget!(builder, gtk::Stack, widget); + get_widget!(builder, gtk::Button, back_button); + get_widget!(builder, libadwaita::WindowTitle, window_title); + get_widget!(builder, gtk::Button, save_button); + get_widget!(builder, gtk::Box, content_box); + get_widget!(builder, libadwaita::StatusPage, status_page); + get_widget!(builder, gtk::Button, try_again_button); + + try_again_button.connect_clicked(clone!(@strong widget => move |_| { + widget.set_visible_child_name("content"); + })); + + Self { + widget, + back_button, + window_title, + save_button, + content_box, + status_page, + } + } + + /// Set a closure to be called when the back button is pressed. + pub fn set_back_cb(&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)); + } + + pub fn set_save_cb(&self, cb: F) { + self.save_button.connect_clicked(move |_| cb()); + } + + /// Show a loading page. + pub fn loading(&self) { + self.widget.set_visible_child_name("loading"); + } + + /// Show an error page. The page contains a button to get back to the + /// actual editor. + pub fn error(&self, title: &str, description: &str) { + self.status_page.set_title(Some(title)); + self.status_page.set_description(Some(description)); + self.widget.set_visible_child_name("error"); + } + + /// Add content to the bottom of the content area. + pub fn add_content>(&self, content: &W) { + self.content_box.append(content); + } +} diff --git a/src/widgets/entry_row.rs b/src/widgets/entry_row.rs new file mode 100644 index 0000000..bab51fe --- /dev/null +++ b/src/widgets/entry_row.rs @@ -0,0 +1,44 @@ +use gtk::prelude::*; +use libadwaita::prelude::*; + +/// A list box row with an entry. +pub struct EntryRow { + /// The actual GTK widget. + pub widget: libadwaita::ActionRow, + + /// The managed entry. + entry: gtk::Entry, +} + +impl EntryRow { + /// Create a new entry row. + pub fn new(title: &str) -> Self { + let entry = gtk::EntryBuilder::new() + .hexpand(true) + .valign(gtk::Align::Center) + .build(); + + let widget = libadwaita::ActionRowBuilder::new() + .activatable(true) + .activatable_widget(&entry) + .title(title) + .build(); + + widget.add_suffix(&entry); + + Self { + widget, + entry, + } + } + + /// Set the text of the entry. + pub fn set_text(&self, text: &str) { + self.entry.set_text(text); + } + + /// Get the text that was entered by the user. + pub fn get_text(&self) -> String { + self.entry.get_text().unwrap().to_string() + } +} diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 7406ccc..bb85c93 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -1,3 +1,9 @@ +pub mod editor; +pub use editor::*; + +pub mod entry_row; +pub use entry_row::*; + pub mod list; pub use list::*; @@ -19,4 +25,7 @@ pub use screen::*; pub mod section; pub use section::*; +pub mod upload_section; +pub use upload_section::*; + mod indexed_list_model; diff --git a/src/widgets/upload_section.rs b/src/widgets/upload_section.rs new file mode 100644 index 0000000..411b72b --- /dev/null +++ b/src/widgets/upload_section.rs @@ -0,0 +1,53 @@ +use super::Section; + +use gettextrs::gettext; +use gtk::prelude::*; +use libadwaita::prelude::*; + +/// A section showing a switch to enable uploading an item. +pub struct UploadSection { + /// The GTK widget of the wrapped section. + pub widget: gtk::Box, + + /// The section itself. + section: Section, + + /// The upload switch. + switch: gtk::Switch, +} + +impl UploadSection { + /// Create a new upload section which will be initially switched on. + pub fn new() -> Self { + let list = gtk::ListBoxBuilder::new() + .selection_mode(gtk::SelectionMode::None) + .build(); + + let switch = gtk::SwitchBuilder::new() + .active(true) + .valign(gtk::Align::Center) + .build(); + + let row = libadwaita::ActionRowBuilder::new() + .title("Upload changes to the server") + .activatable(true) + .activatable_widget(&switch) + .build(); + + row.add_suffix(&switch); + list.append(&row); + + let section = Section::new(&gettext("Upload"), &list); + + Self { + widget: section.widget.clone(), + section, + switch, + } + } + + /// Return whether the user has enabled the upload switch. + pub fn get_active(&self) -> bool { + self.switch.get_active() + } +}