Rewrite using Relm4

Yes, this is a monster commit but at this stage I'm the only one working
on this project anyway. Further commits will follow Best Practises™
again.
This commit is contained in:
Reinout Meliesie 2026-01-21 13:20:47 +01:00
commit 4d4b7eb1c7
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
43 changed files with 2159 additions and 1248 deletions

71
src/ui/components/app.rs Normal file
View file

@ -0,0 +1,71 @@
use gtk4::prelude::GtkWindowExt;
use libadwaita::ApplicationWindow;
use libadwaita::prelude::AdwApplicationWindowExt;
use relm4::{
Component, ComponentController, ComponentParts, ComponentSender, Controller, component,
};
use crate::persist::data_manager::{DataManager, DataManagerError};
use crate::ui::components::media_type_switcher::MediaTypeSwitcher;
pub struct App {
media_type_switcher: Option<Controller<MediaTypeSwitcher>>,
}
#[derive(Debug)]
pub enum AppCmdOutput {
DataManagerReady,
DataManagerFailed(DataManagerError),
}
#[component(pub)]
impl Component for App {
type Input = ();
type Output = ();
type Init = ();
type CommandOutput = AppCmdOutput;
view! {
ApplicationWindow {
set_title: Some("Zoödex"),
#[watch]
set_content: model.media_type_switcher.as_ref().map(Controller::widget),
}
}
fn init(_init: (), root: ApplicationWindow, sender: ComponentSender<App>) -> ComponentParts<App> {
sender.oneshot_command(async {
match DataManager::init().await {
Ok(_) => AppCmdOutput::DataManagerReady,
Err(error) => AppCmdOutput::DataManagerFailed(error),
}
});
let model = App {
media_type_switcher: None,
};
let widgets = view_output!();
ComponentParts { model, widgets }
}
fn update_cmd(
&mut self,
message: AppCmdOutput,
_sender: ComponentSender<App>,
_root: &ApplicationWindow,
) {
match message {
AppCmdOutput::DataManagerReady => {
// Only launch child component after data manager is ready.
self.media_type_switcher = Some(MediaTypeSwitcher::builder().launch(()).detach());
}
AppCmdOutput::DataManagerFailed(error) => {
// TODO: Show error in GUI.
println!("Error occurred in data manager initialization: {:?}", error);
}
}
}
}