Use new navigator for all screens

This commit is contained in:
Elias Projahn 2021-02-03 16:22:18 +01:00
parent c72bc71432
commit ee71a905e1
6 changed files with 156 additions and 226 deletions

View file

@ -99,7 +99,7 @@ impl Navigator {
.vexpand(true) .vexpand(true)
.build(); .build();
widget.add_child(empty_screen); widget.add_named(empty_screen, Some("empty_screen"));
let this = Rc::new(Self { let this = Rc::new(Self {
widget, widget,
@ -193,6 +193,8 @@ impl Navigator {
let widget = screen.get_widget(); let widget = screen.get_widget();
self.widget.set_visible_child(&widget); self.widget.set_visible_child(&widget);
} else { } else {
self.widget.set_visible_child_name("empty_screen");
if let Some(cb) = &*self.back_cb.borrow() { if let Some(cb) = &*self.back_cb.borrow() {
cb() cb()
} }

View file

@ -1,11 +1,10 @@
use super::RecordingScreen; use super::RecordingScreen;
use crate::backend::Backend; use crate::backend::Backend;
use crate::database::{Ensemble, Recording}; use crate::database::{Ensemble, Recording};
use crate::editors::EnsembleEditor; use crate::editors::EnsembleEditor;
use crate::navigator::NavigatorWindow; use crate::navigator::{NavigatorWindow, NavigationHandle, Screen};
use crate::widgets::{List, Navigator, NavigatorScreen, Screen, Section}; use crate::widgets;
use crate::widgets::{List, Section, Widget};
use gettextrs::gettext; use gettextrs::gettext;
use glib::clone; use glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
@ -15,61 +14,54 @@ use std::rc::Rc;
/// A screen for showing recordings with a ensemble. /// A screen for showing recordings with a ensemble.
pub struct EnsembleScreen { pub struct EnsembleScreen {
backend: Rc<Backend>, handle: NavigationHandle<()>,
ensemble: Ensemble, ensemble: Ensemble,
widget: Screen, widget: widgets::Screen,
recording_list: Rc<List>, recording_list: Rc<List>,
recordings: RefCell<Vec<Recording>>, 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 /// Create a new ensemble screen for the specified ensemble and load the
/// contents asynchronously. /// contents asynchronously.
pub fn new(backend: Rc<Backend>, ensemble: Ensemble) -> Rc<Self> { fn new(ensemble: Ensemble, handle: NavigationHandle<()>) -> Rc<Self> {
let widget = Screen::new(); let widget = widgets::Screen::new();
widget.set_title(&ensemble.name); widget.set_title(&ensemble.name);
let recording_list = List::new(); let recording_list = List::new();
let this = Rc::new(Self { let this = Rc::new(Self {
backend, handle,
ensemble, ensemble,
widget, widget,
recording_list, recording_list,
recordings: RefCell::new(Vec::new()), recordings: RefCell::new(Vec::new()),
navigator: RefCell::new(None),
}); });
this.widget.set_back_cb(clone!(@strong this => move || { this.widget.set_back_cb(clone!(@weak this => move || {
let navigator = this.navigator.borrow().clone(); this.handle.pop(None);
if let Some(navigator) = navigator {
navigator.pop();
}
})); }));
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 { spawn!(@clone this, async move {
let window = NavigatorWindow::new(this.backend.clone()); let window = NavigatorWindow::new(this.handle.backend.clone());
replace!(window.navigator, EnsembleEditor, None).await; replace!(window.navigator, EnsembleEditor, Some(this.ensemble.clone())).await;
}); });
})); }));
this.widget.add_action(&gettext("Delete ensemble"), clone!(@strong this => move || { this.widget.add_action(&gettext("Delete ensemble"), clone!(@weak this => move || {
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = this.clone(); this.handle.backend.db().delete_ensemble(&this.ensemble.id).await.unwrap();
context.spawn_local(async move { this.handle.backend.library_changed();
clone.backend.db().delete_ensemble(&clone.ensemble.id).await.unwrap();
clone.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.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 recording = &this.recordings.borrow()[index];
let row = libadwaita::ActionRow::new(); let row = libadwaita::ActionRow::new();
@ -78,17 +70,17 @@ impl EnsembleScreen {
row.set_subtitle(Some(&recording.get_performers())); row.set_subtitle(Some(&recording.get_performers()));
let recording = recording.to_owned(); let recording = recording.to_owned();
row.connect_activated(clone!(@strong this => move |_| { row.connect_activated(clone!(@weak this => move |_| {
let navigator = this.navigator.borrow().clone(); let recording = recording.clone();
if let Some(navigator) = navigator { spawn!(@clone this, async move {
navigator.push(RecordingScreen::new(this.backend.clone(), recording.clone())); push!(this.handle, RecordingScreen, recording.clone()).await;
} });
})); }));
row.upcast() 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 recording = &this.recordings.borrow()[index];
let search = this.widget.get_search(); let search = this.widget.get_search();
let text = recording.work.get_title() + &recording.get_performers(); let text = recording.work.get_title() + &recording.get_performers();
@ -97,43 +89,32 @@ impl EnsembleScreen {
// Load the content asynchronously. // Load the content asynchronously.
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = Rc::clone(&this); let recordings = this.handle
context.spawn_local(async move {
let recordings = clone
.backend .backend
.db() .db()
.get_recordings_for_ensemble(&clone.ensemble.id) .get_recordings_for_ensemble(&this.ensemble.id)
.await .await
.unwrap(); .unwrap();
if !recordings.is_empty() { if !recordings.is_empty() {
let length = recordings.len(); let length = recordings.len();
clone.recordings.replace(recordings); this.recordings.replace(recordings);
clone.recording_list.update(length); this.recording_list.update(length);
let section = Section::new("Recordings", &clone.recording_list.widget); let section = Section::new("Recordings", &this.recording_list.widget);
clone.widget.add_content(&section.widget); this.widget.add_content(&section.widget);
} }
clone.widget.ready(); this.widget.ready();
}); });
this this
} }
} }
impl NavigatorScreen for EnsembleScreen { impl Widget for EnsembleScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
}
fn get_widget(&self) -> gtk::Widget { fn get_widget(&self) -> gtk::Widget {
self.widget.widget.clone().upcast() self.widget.widget.clone().upcast()
} }
fn detach_navigator(&self) {
self.navigator.replace(None);
}
} }

View file

@ -1,11 +1,10 @@
use super::{WorkScreen, RecordingScreen}; use super::{WorkScreen, RecordingScreen};
use crate::backend::Backend; use crate::backend::Backend;
use crate::database::{Person, Recording, Work}; use crate::database::{Person, Recording, Work};
use crate::editors::PersonEditor; use crate::editors::PersonEditor;
use crate::navigator::NavigatorWindow; use crate::navigator::{NavigatorWindow, NavigationHandle, Screen};
use crate::widgets::{List, Navigator, NavigatorScreen, Screen, Section}; use crate::widgets;
use crate::widgets::{List, Section, Widget};
use gettextrs::gettext; use gettextrs::gettext;
use glib::clone; use glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
@ -15,67 +14,60 @@ use std::rc::Rc;
/// A screen for showing works by and recordings with a person. /// A screen for showing works by and recordings with a person.
pub struct PersonScreen { pub struct PersonScreen {
backend: Rc<Backend>, handle: NavigationHandle<()>,
person: Person, person: Person,
widget: Screen, widget: widgets::Screen,
work_list: Rc<List>, work_list: Rc<List>,
recording_list: Rc<List>, recording_list: Rc<List>,
works: RefCell<Vec<Work>>, works: RefCell<Vec<Work>>,
recordings: RefCell<Vec<Recording>>, recordings: RefCell<Vec<Recording>>,
navigator: RefCell<Option<Rc<Navigator>>>,
} }
impl PersonScreen { impl Screen<Person, ()> for PersonScreen {
/// Create a new person screen for the specified person and load the /// Create a new person screen for the specified person and load the
/// contents asynchronously. /// contents asynchronously.
pub fn new(backend: Rc<Backend>, person: Person) -> Rc<Self> { fn new(person: Person, handle: NavigationHandle<()>) -> Rc<Self> {
let widget = Screen::new(); let widget = widgets::Screen::new();
widget.set_title(&person.name_fl()); widget.set_title(&person.name_fl());
let work_list = List::new(); let work_list = List::new();
let recording_list = List::new(); let recording_list = List::new();
let this = Rc::new(Self { let this = Rc::new(Self {
backend, handle,
person, person,
widget, widget,
work_list, work_list,
recording_list, recording_list,
works: RefCell::new(Vec::new()), works: RefCell::new(Vec::new()),
recordings: RefCell::new(Vec::new()), recordings: RefCell::new(Vec::new()),
navigator: RefCell::new(None),
}); });
this.widget.set_back_cb(clone!(@strong this => move || { this.widget.set_back_cb(clone!(@weak this => move || {
let navigator = this.navigator.borrow().clone(); this.handle.pop(None);
if let Some(navigator) = navigator {
navigator.pop();
}
})); }));
this.widget.add_action(&gettext("Edit person"), clone!(@strong this => move || { this.widget.add_action(&gettext("Edit person"), clone!(@weak this => move || {
spawn!(@clone this, async move { spawn!(@clone this, async move {
let window = NavigatorWindow::new(this.backend.clone()); let window = NavigatorWindow::new(this.handle.backend.clone());
replace!(window.navigator, PersonEditor, None).await; replace!(window.navigator, PersonEditor, Some(this.person.clone())).await;
}); });
})); }));
this.widget.add_action(&gettext("Delete person"), clone!(@strong this => move || { this.widget.add_action(&gettext("Delete person"), clone!(@weak this => move || {
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = this.clone(); this.handle.backend.db().delete_person(&this.person.id).await.unwrap();
context.spawn_local(async move { this.handle.backend.library_changed();
clone.backend.db().delete_person(&clone.person.id).await.unwrap();
clone.backend.library_changed();
}); });
})); }));
this.widget.set_search_cb(clone!(@strong this => move || { this.widget.set_search_cb(clone!(@weak this => move || {
this.work_list.invalidate_filter(); this.work_list.invalidate_filter();
this.recording_list.invalidate_filter(); this.recording_list.invalidate_filter();
})); }));
this.work_list.set_make_widget_cb(clone!(@strong this => move |index| { this.work_list.set_make_widget_cb(clone!(@weak this => move |index| {
let work = &this.works.borrow()[index]; let work = &this.works.borrow()[index];
let row = libadwaita::ActionRow::new(); let row = libadwaita::ActionRow::new();
@ -83,24 +75,24 @@ impl PersonScreen {
row.set_title(Some(&work.title)); row.set_title(Some(&work.title));
let work = work.to_owned(); let work = work.to_owned();
row.connect_activated(clone!(@strong this => move |_| { row.connect_activated(clone!(@weak this => move |_| {
let navigator = this.navigator.borrow().clone(); let work = work.clone();
if let Some(navigator) = navigator { spawn!(@clone this, async move {
navigator.push(WorkScreen::new(this.backend.clone(), work.clone())); push!(this.handle, WorkScreen, work.clone()).await;
} });
})); }));
row.upcast() row.upcast()
})); }));
this.work_list.set_filter_cb(clone!(@strong this => move |index| { this.work_list.set_filter_cb(clone!(@weak this => move |index| {
let work = &this.works.borrow()[index]; let work = &this.works.borrow()[index];
let search = this.widget.get_search(); let search = this.widget.get_search();
let title = work.title.to_lowercase(); let title = work.title.to_lowercase();
search.is_empty() || title.contains(&search) search.is_empty() || title.contains(&search)
})); }));
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 recording = &this.recordings.borrow()[index];
let row = libadwaita::ActionRow::new(); let row = libadwaita::ActionRow::new();
@ -109,17 +101,17 @@ impl PersonScreen {
row.set_subtitle(Some(&recording.get_performers())); row.set_subtitle(Some(&recording.get_performers()));
let recording = recording.to_owned(); let recording = recording.to_owned();
row.connect_activated(clone!(@strong this => move |_| { row.connect_activated(clone!(@weak this => move |_| {
let navigator = this.navigator.borrow().clone(); let recording = recording.clone();
if let Some(navigator) = navigator { spawn!(@clone this, async move {
navigator.push(RecordingScreen::new(this.backend.clone(), recording.clone())); push!(this.handle, RecordingScreen, recording.clone()).await;
} });
})); }));
row.upcast() 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 recording = &this.recordings.borrow()[index];
let search = this.widget.get_search(); let search = this.widget.get_search();
let text = recording.work.get_title() + &recording.get_performers(); let text = recording.work.get_title() + &recording.get_performers();
@ -128,59 +120,48 @@ impl PersonScreen {
// Load the content asynchronously. // Load the content asynchronously.
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = Rc::clone(&this); let works = this.handle
context.spawn_local(async move {
let works = clone
.backend .backend
.db() .db()
.get_works(&clone.person.id) .get_works(&this.person.id)
.await .await
.unwrap(); .unwrap();
let recordings = clone let recordings = this.handle
.backend .backend
.db() .db()
.get_recordings_for_person(&clone.person.id) .get_recordings_for_person(&this.person.id)
.await .await
.unwrap(); .unwrap();
if !works.is_empty() { if !works.is_empty() {
let length = works.len(); let length = works.len();
clone.works.replace(works); this.works.replace(works);
clone.work_list.update(length); this.work_list.update(length);
let section = Section::new("Works", &clone.work_list.widget); let section = Section::new("Works", &this.work_list.widget);
clone.widget.add_content(&section.widget); this.widget.add_content(&section.widget);
} }
if !recordings.is_empty() { if !recordings.is_empty() {
let length = recordings.len(); let length = recordings.len();
clone.recordings.replace(recordings); this.recordings.replace(recordings);
clone.recording_list.update(length); this.recording_list.update(length);
let section = Section::new("Recordings", &clone.recording_list.widget); let section = Section::new("Recordings", &this.recording_list.widget);
clone.widget.add_content(&section.widget); this.widget.add_content(&section.widget);
} }
clone.widget.ready(); this.widget.ready();
}); });
this this
} }
} }
impl NavigatorScreen for PersonScreen { impl Widget for PersonScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
}
fn get_widget(&self) -> gtk::Widget { fn get_widget(&self) -> gtk::Widget {
self.widget.widget.clone().upcast() self.widget.widget.clone().upcast()
} }
fn detach_navigator(&self) {
self.navigator.replace(None);
}
} }

View file

@ -1,9 +1,9 @@
use crate::backend::Backend; use crate::backend::Backend;
use crate::database::Recording; use crate::database::Recording;
use crate::editors::RecordingEditor; use crate::editors::RecordingEditor;
use crate::navigator::NavigatorWindow; use crate::navigator::{NavigatorWindow, NavigationHandle, Screen};
use crate::widgets::{List, Navigator, NavigatorScreen, Screen, Section}; use crate::widgets;
use crate::widgets::{List, Section, Widget};
use gettextrs::gettext; use gettextrs::gettext;
use glib::clone; use glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
@ -13,58 +13,51 @@ use std::rc::Rc;
/// A screen for showing a recording. /// A screen for showing a recording.
pub struct RecordingScreen { pub struct RecordingScreen {
backend: Rc<Backend>, handle: NavigationHandle<()>,
recording: Recording, recording: Recording,
widget: Screen, widget: widgets::Screen,
track_list: Rc<List>, track_list: Rc<List>,
recordings: RefCell<Vec<Recording>>, recordings: RefCell<Vec<Recording>>,
navigator: RefCell<Option<Rc<Navigator>>>,
} }
impl RecordingScreen { impl Screen<Recording, ()> for RecordingScreen {
/// Create a new recording screen for the specified recording and load the /// Create a new recording screen for the specified recording and load the
/// contents asynchronously. /// contents asynchronously.
pub fn new(backend: Rc<Backend>, recording: Recording) -> Rc<Self> { fn new(recording: Recording, handle: NavigationHandle<()>) -> Rc<Self> {
let widget = Screen::new(); let widget = widgets::Screen::new();
widget.set_title(&recording.work.get_title()); widget.set_title(&recording.work.get_title());
widget.set_subtitle(&recording.get_performers()); widget.set_subtitle(&recording.get_performers());
let track_list = List::new(); let track_list = List::new();
let this = Rc::new(Self { let this = Rc::new(Self {
backend, handle,
recording, recording,
widget, widget,
track_list, track_list,
recordings: RefCell::new(Vec::new()), recordings: RefCell::new(Vec::new()),
navigator: RefCell::new(None),
}); });
this.widget.set_back_cb(clone!(@strong this => move || { this.widget.set_back_cb(clone!(@weak this => move || {
let navigator = this.navigator.borrow().clone(); this.handle.pop(None);
if let Some(navigator) = navigator {
navigator.pop();
}
})); }));
this.widget.add_action(&gettext("Edit recording"), clone!(@strong this => move || { this.widget.add_action(&gettext("Edit recording"), clone!(@weak this => move || {
spawn!(@clone this, async move { spawn!(@clone this, async move {
let window = NavigatorWindow::new(this.backend.clone()); let window = NavigatorWindow::new(this.handle.backend.clone());
replace!(window.navigator, RecordingEditor, None).await; replace!(window.navigator, RecordingEditor, Some(this.recording.clone())).await;
}); });
})); }));
this.widget.add_action(&gettext("Delete recording"), clone!(@strong this => move || { this.widget.add_action(&gettext("Delete recording"), clone!(@weak this => move || {
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = this.clone(); this.handle.backend.db().delete_recording(&this.recording.id).await.unwrap();
context.spawn_local(async move { this.handle.backend.library_changed();
clone.backend.db().delete_recording(&clone.recording.id).await.unwrap();
clone.backend.library_changed();
}); });
})); }));
this.widget.set_search_cb(clone!(@strong this => move || { this.widget.set_search_cb(clone!(@weak this => move || {
this.track_list.invalidate_filter(); this.track_list.invalidate_filter();
})); }));
@ -72,7 +65,7 @@ impl RecordingScreen {
// this.track_list.set_make_widget_cb(clone!(@strong this => move |index| { // this.track_list.set_make_widget_cb(clone!(@strong this => move |index| {
// })); // }));
this.track_list.set_filter_cb(clone!(@strong this => move |index| { this.track_list.set_filter_cb(clone!(@weak this => move |index| {
// TODO: Implement. // TODO: Implement.
// search.is_empty() || text.to_lowercase().contains(&search) // search.is_empty() || text.to_lowercase().contains(&search)
true true
@ -80,29 +73,18 @@ impl RecordingScreen {
// Load the content asynchronously. // Load the content asynchronously.
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = Rc::clone(&this);
context.spawn_local(async move {
// TODO: Implement. // TODO: Implement.
clone.widget.ready(); this.widget.ready();
}); });
this this
} }
} }
impl NavigatorScreen for RecordingScreen { impl Widget for RecordingScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
}
fn get_widget(&self) -> gtk::Widget { fn get_widget(&self) -> gtk::Widget {
self.widget.widget.clone().upcast() self.widget.widget.clone().upcast()
} }
fn detach_navigator(&self) {
self.navigator.replace(None);
}
} }

View file

@ -1,11 +1,10 @@
use super::RecordingScreen; use super::RecordingScreen;
use crate::backend::Backend; use crate::backend::Backend;
use crate::database::{Work, Recording}; use crate::database::{Work, Recording};
use crate::editors::WorkEditor; use crate::editors::WorkEditor;
use crate::navigator::NavigatorWindow; use crate::navigator::{NavigatorWindow, NavigationHandle, Screen};
use crate::widgets::{List, Navigator, NavigatorScreen, Screen, Section}; use crate::widgets;
use crate::widgets::{List, Section, Widget};
use gettextrs::gettext; use gettextrs::gettext;
use glib::clone; use glib::clone;
use gtk::prelude::*; use gtk::prelude::*;
@ -15,62 +14,55 @@ use std::rc::Rc;
/// A screen for showing recordings of a work. /// A screen for showing recordings of a work.
pub struct WorkScreen { pub struct WorkScreen {
backend: Rc<Backend>, handle: NavigationHandle<()>,
work: Work, work: Work,
widget: Screen, widget: widgets::Screen,
recording_list: Rc<List>, recording_list: Rc<List>,
recordings: RefCell<Vec<Recording>>, recordings: RefCell<Vec<Recording>>,
navigator: RefCell<Option<Rc<Navigator>>>,
} }
impl WorkScreen { impl Screen<Work, ()> for WorkScreen {
/// Create a new work screen for the specified work and load the /// Create a new work screen for the specified work and load the
/// contents asynchronously. /// contents asynchronously.
pub fn new(backend: Rc<Backend>, work: Work) -> Rc<Self> { fn new(work: Work, handle: NavigationHandle<()>) -> Rc<Self> {
let widget = Screen::new(); let widget = widgets::Screen::new();
widget.set_title(&work.title); widget.set_title(&work.title);
widget.set_subtitle(&work.composer.name_fl()); widget.set_subtitle(&work.composer.name_fl());
let recording_list = List::new(); let recording_list = List::new();
let this = Rc::new(Self { let this = Rc::new(Self {
backend, handle,
work, work,
widget, widget,
recording_list, recording_list,
recordings: RefCell::new(Vec::new()), recordings: RefCell::new(Vec::new()),
navigator: RefCell::new(None),
}); });
this.widget.set_back_cb(clone!(@strong this => move || { this.widget.set_back_cb(clone!(@weak this => move || {
let navigator = this.navigator.borrow().clone(); this.handle.pop(None);
if let Some(navigator) = navigator {
navigator.pop();
}
})); }));
this.widget.add_action(&gettext("Edit work"), clone!(@strong this => move || { this.widget.add_action(&gettext("Edit work"), clone!(@weak this => move || {
spawn!(@clone this, async move { spawn!(@clone this, async move {
let window = NavigatorWindow::new(this.backend.clone()); let window = NavigatorWindow::new(this.handle.backend.clone());
replace!(window.navigator, WorkEditor, None).await; replace!(window.navigator, WorkEditor, Some(this.work.clone())).await;
}); });
})); }));
this.widget.add_action(&gettext("Delete work"), clone!(@strong this => move || { this.widget.add_action(&gettext("Delete work"), clone!(@weak this => move || {
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = this.clone(); this.handle.backend.db().delete_work(&this.work.id).await.unwrap();
context.spawn_local(async move { this.handle.backend.library_changed();
clone.backend.db().delete_work(&clone.work.id).await.unwrap();
clone.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.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 recording = &this.recordings.borrow()[index];
let row = libadwaita::ActionRow::new(); let row = libadwaita::ActionRow::new();
@ -79,17 +71,17 @@ impl WorkScreen {
row.set_subtitle(Some(&recording.get_performers())); row.set_subtitle(Some(&recording.get_performers()));
let recording = recording.to_owned(); let recording = recording.to_owned();
row.connect_activated(clone!(@strong this => move |_| { row.connect_activated(clone!(@weak this => move |_| {
let navigator = this.navigator.borrow().clone(); let recording = recording.clone();
if let Some(navigator) = navigator { spawn!(@clone this, async move {
navigator.push(RecordingScreen::new(this.backend.clone(), recording.clone())); push!(this.handle, RecordingScreen, recording.clone()).await;
} });
})); }));
row.upcast() 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 recording = &this.recordings.borrow()[index];
let search = this.widget.get_search(); let search = this.widget.get_search();
let text = recording.work.get_title() + &recording.get_performers(); let text = recording.work.get_title() + &recording.get_performers();
@ -98,43 +90,32 @@ impl WorkScreen {
// Load the content asynchronously. // Load the content asynchronously.
let context = glib::MainContext::default(); spawn!(@clone this, async move {
let clone = Rc::clone(&this); let recordings = this.handle
context.spawn_local(async move {
let recordings = clone
.backend .backend
.db() .db()
.get_recordings_for_work(&clone.work.id) .get_recordings_for_work(&this.work.id)
.await .await
.unwrap(); .unwrap();
if !recordings.is_empty() { if !recordings.is_empty() {
let length = recordings.len(); let length = recordings.len();
clone.recordings.replace(recordings); this.recordings.replace(recordings);
clone.recording_list.update(length); this.recording_list.update(length);
let section = Section::new("Recordings", &clone.recording_list.widget); let section = Section::new("Recordings", &this.recording_list.widget);
clone.widget.add_content(&section.widget); this.widget.add_content(&section.widget);
} }
clone.widget.ready(); this.widget.ready();
}); });
this this
} }
} }
impl NavigatorScreen for WorkScreen { impl Widget for WorkScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
}
fn get_widget(&self) -> gtk::Widget { fn get_widget(&self) -> gtk::Widget {
self.widget.widget.clone().upcast() self.widget.widget.clone().upcast()
} }
fn detach_navigator(&self) {
self.navigator.replace(None);
}
} }

View file

@ -4,7 +4,7 @@ use crate::import::SourceSelector;
use crate::preferences::Preferences; use crate::preferences::Preferences;
use crate::screens::*; use crate::screens::*;
use crate::widgets::*; use crate::widgets::*;
use crate::navigator::NavigatorWindow; use crate::navigator::{Navigator, NavigatorWindow};
use futures::prelude::*; use futures::prelude::*;
use gettextrs::gettext; use gettextrs::gettext;
use gio::prelude::*; use gio::prelude::*;
@ -44,7 +44,7 @@ impl Window {
stack.add_named(&player_screen.widget, Some("player_screen")); stack.add_named(&player_screen.widget, Some("player_screen"));
let poe_list = PoeList::new(backend.clone()); let poe_list = PoeList::new(backend.clone());
let navigator = Navigator::new(&window, &empty_screen); let navigator = Navigator::new(backend.clone(), &window, &empty_screen);
navigator.set_back_cb(clone!(@strong leaflet, @strong sidebar_box => move || { navigator.set_back_cb(clone!(@strong leaflet, @strong sidebar_box => move || {
leaflet.set_visible_child(&sidebar_box); leaflet.set_visible_child(&sidebar_box);
})); }));
@ -178,14 +178,17 @@ impl Window {
.poe_list .poe_list
.set_selected_cb(clone!(@strong result => move |poe| { .set_selected_cb(clone!(@strong result => move |poe| {
result.leaflet.set_visible_child(&result.navigator.widget); result.leaflet.set_visible_child(&result.navigator.widget);
let poe = poe.to_owned();
spawn!(@clone result, async move {
match poe { match poe {
PersonOrEnsemble::Person(person) => { PersonOrEnsemble::Person(person) => {
result.navigator.clone().replace(PersonScreen::new(result.backend.clone(), person.clone())); replace!(result.navigator, PersonScreen, person.clone()).await;
} }
PersonOrEnsemble::Ensemble(ensemble) => { PersonOrEnsemble::Ensemble(ensemble) => {
result.navigator.clone().replace(EnsembleScreen::new(result.backend.clone(), ensemble.clone())); replace!(result.navigator, EnsembleScreen, ensemble.clone()).await;
} }
} }
});
})); }));
result result