mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-27 04:07:25 +01:00
Use new navigator for all screens
This commit is contained in:
parent
c72bc71432
commit
ee71a905e1
6 changed files with 156 additions and 226 deletions
|
|
@ -1,11 +1,10 @@
|
|||
use super::RecordingScreen;
|
||||
|
||||
use crate::backend::Backend;
|
||||
use crate::database::{Ensemble, Recording};
|
||||
use crate::editors::EnsembleEditor;
|
||||
use crate::navigator::NavigatorWindow;
|
||||
use crate::widgets::{List, Navigator, NavigatorScreen, Screen, Section};
|
||||
|
||||
use crate::navigator::{NavigatorWindow, NavigationHandle, Screen};
|
||||
use crate::widgets;
|
||||
use crate::widgets::{List, Section, Widget};
|
||||
use gettextrs::gettext;
|
||||
use glib::clone;
|
||||
use gtk::prelude::*;
|
||||
|
|
@ -15,61 +14,54 @@ use std::rc::Rc;
|
|||
|
||||
/// A screen for showing recordings with a ensemble.
|
||||
pub struct EnsembleScreen {
|
||||
backend: Rc<Backend>,
|
||||
handle: NavigationHandle<()>,
|
||||
ensemble: Ensemble,
|
||||
widget: Screen,
|
||||
widget: widgets::Screen,
|
||||
recording_list: Rc<List>,
|
||||
recordings: RefCell<Vec<Recording>>,
|
||||
navigator: RefCell<Option<Rc<Navigator>>>,
|
||||
}
|
||||
|
||||
impl EnsembleScreen {
|
||||
impl Screen<Ensemble, ()> for EnsembleScreen {
|
||||
/// Create a new ensemble screen for the specified ensemble and load the
|
||||
/// contents asynchronously.
|
||||
pub fn new(backend: Rc<Backend>, ensemble: Ensemble) -> Rc<Self> {
|
||||
let widget = Screen::new();
|
||||
fn new(ensemble: Ensemble, handle: NavigationHandle<()>) -> Rc<Self> {
|
||||
let widget = widgets::Screen::new();
|
||||
widget.set_title(&ensemble.name);
|
||||
|
||||
let recording_list = List::new();
|
||||
|
||||
let this = Rc::new(Self {
|
||||
backend,
|
||||
handle,
|
||||
ensemble,
|
||||
widget,
|
||||
recording_list,
|
||||
recordings: RefCell::new(Vec::new()),
|
||||
navigator: RefCell::new(None),
|
||||
});
|
||||
|
||||
this.widget.set_back_cb(clone!(@strong this => move || {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.pop();
|
||||
}
|
||||
this.widget.set_back_cb(clone!(@weak this => move || {
|
||||
this.handle.pop(None);
|
||||
}));
|
||||
|
||||
|
||||
this.widget.add_action(&gettext("Edit ensemble"), clone!(@strong this => move || {
|
||||
this.widget.add_action(&gettext("Edit ensemble"), clone!(@weak this => move || {
|
||||
spawn!(@clone this, async move {
|
||||
let window = NavigatorWindow::new(this.backend.clone());
|
||||
replace!(window.navigator, EnsembleEditor, None).await;
|
||||
let window = NavigatorWindow::new(this.handle.backend.clone());
|
||||
replace!(window.navigator, EnsembleEditor, Some(this.ensemble.clone())).await;
|
||||
});
|
||||
}));
|
||||
|
||||
this.widget.add_action(&gettext("Delete ensemble"), clone!(@strong this => move || {
|
||||
let context = glib::MainContext::default();
|
||||
let clone = this.clone();
|
||||
context.spawn_local(async move {
|
||||
clone.backend.db().delete_ensemble(&clone.ensemble.id).await.unwrap();
|
||||
clone.backend.library_changed();
|
||||
this.widget.add_action(&gettext("Delete ensemble"), clone!(@weak this => move || {
|
||||
spawn!(@clone this, async move {
|
||||
this.handle.backend.db().delete_ensemble(&this.ensemble.id).await.unwrap();
|
||||
this.handle.backend.library_changed();
|
||||
});
|
||||
}));
|
||||
|
||||
this.widget.set_search_cb(clone!(@strong this => move || {
|
||||
this.widget.set_search_cb(clone!(@weak this => move || {
|
||||
this.recording_list.invalidate_filter();
|
||||
}));
|
||||
|
||||
this.recording_list.set_make_widget_cb(clone!(@strong this => move |index| {
|
||||
this.recording_list.set_make_widget_cb(clone!(@weak this => move |index| {
|
||||
let recording = &this.recordings.borrow()[index];
|
||||
|
||||
let row = libadwaita::ActionRow::new();
|
||||
|
|
@ -78,17 +70,17 @@ impl EnsembleScreen {
|
|||
row.set_subtitle(Some(&recording.get_performers()));
|
||||
|
||||
let recording = recording.to_owned();
|
||||
row.connect_activated(clone!(@strong this => move |_| {
|
||||
let navigator = this.navigator.borrow().clone();
|
||||
if let Some(navigator) = navigator {
|
||||
navigator.push(RecordingScreen::new(this.backend.clone(), recording.clone()));
|
||||
}
|
||||
row.connect_activated(clone!(@weak this => move |_| {
|
||||
let recording = recording.clone();
|
||||
spawn!(@clone this, async move {
|
||||
push!(this.handle, RecordingScreen, recording.clone()).await;
|
||||
});
|
||||
}));
|
||||
|
||||
row.upcast()
|
||||
}));
|
||||
|
||||
this.recording_list.set_filter_cb(clone!(@strong this => move |index| {
|
||||
this.recording_list.set_filter_cb(clone!(@weak this => move |index| {
|
||||
let recording = &this.recordings.borrow()[index];
|
||||
let search = this.widget.get_search();
|
||||
let text = recording.work.get_title() + &recording.get_performers();
|
||||
|
|
@ -97,43 +89,32 @@ impl EnsembleScreen {
|
|||
|
||||
// Load the content asynchronously.
|
||||
|
||||
let context = glib::MainContext::default();
|
||||
let clone = Rc::clone(&this);
|
||||
|
||||
context.spawn_local(async move {
|
||||
let recordings = clone
|
||||
spawn!(@clone this, async move {
|
||||
let recordings = this.handle
|
||||
.backend
|
||||
.db()
|
||||
.get_recordings_for_ensemble(&clone.ensemble.id)
|
||||
.get_recordings_for_ensemble(&this.ensemble.id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if !recordings.is_empty() {
|
||||
let length = recordings.len();
|
||||
clone.recordings.replace(recordings);
|
||||
clone.recording_list.update(length);
|
||||
this.recordings.replace(recordings);
|
||||
this.recording_list.update(length);
|
||||
|
||||
let section = Section::new("Recordings", &clone.recording_list.widget);
|
||||
clone.widget.add_content(§ion.widget);
|
||||
let section = Section::new("Recordings", &this.recording_list.widget);
|
||||
this.widget.add_content(§ion.widget);
|
||||
}
|
||||
|
||||
clone.widget.ready();
|
||||
this.widget.ready();
|
||||
});
|
||||
|
||||
this
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigatorScreen for EnsembleScreen {
|
||||
fn attach_navigator(&self, navigator: Rc<Navigator>) {
|
||||
self.navigator.replace(Some(navigator));
|
||||
}
|
||||
|
||||
impl Widget for EnsembleScreen {
|
||||
fn get_widget(&self) -> gtk::Widget {
|
||||
self.widget.widget.clone().upcast()
|
||||
}
|
||||
|
||||
fn detach_navigator(&self) {
|
||||
self.navigator.replace(None);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue