diff --git a/src/ui/sheet_edit_dialog.rs b/src/ui/sheet_edit_dialog.rs new file mode 100644 index 0000000..a580e18 --- /dev/null +++ b/src/ui/sheet_edit_dialog.rs @@ -0,0 +1,176 @@ +use std::borrow::BorrowMut; + +use gtk::prelude::*; +use relm4::prelude::*; + +use crate::sheet::Sheet; + +pub struct SheetEditDialogModel { + hidden: bool, + sheet: Option, + sheet_name: String, + sheet_composer: String, +} + +#[derive(Debug)] +pub enum SheetEditDialogInput { + Show(Sheet), + Accept, + Cancel, +} + +#[derive(Debug)] +pub enum SheetEditDialogOutput { + SheetEdited(Sheet), +} + +#[relm4::component(pub)] +impl SimpleComponent for SheetEditDialogModel { + type Init = (); + type Input = SheetEditDialogInput; + type Output = SheetEditDialogOutput; + + view! { + gtk::Window { + set_modal: true, + #[watch] + set_visible: !model.hidden, + set_title: Some("Edit sheet"), + // set_secondary_text: Some("All unsaved changes will be lost"), + // add_button: ("Confirm", gtk::ResponseType::Accept), + // add_button: ("Cancel", gtk::ResponseType::Cancel), + // connect_response[sender] => move |_, resp| { + // sender.input(if resp == gtk::ResponseType::Accept { + // SheetEditDialogInput::Accept + // } else { + // SheetEditDialogInput::Cancel + // }) + // }, + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_margin_all : 5, + gtk::Label { + set_text: "Sheet name" + }, + gtk::Entry { + #[watch] + set_text: &model.sheet_name, + }, + gtk::Label { + set_text: "Sheet composer" + }, + gtk::Entry { + #[watch] + set_text : &model.sheet_composer, + }, + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_margin_top:5, + gtk::Button { + set_label : "Confirm", + connect_clicked[sender] => move |_| sender.input(SheetEditDialogInput::Accept) + }, + gtk::Button { + set_label: "Cancel", + connect_clicked[sender] => move |_| sender.input(SheetEditDialogInput::Cancel) + } + } + } + } + // gtk::MessageDialog { + // set_modal: true, + // #[watch] + // set_visible: !model.hidden, + // set_text: Some("Edit sheet"), + // // set_secondary_text: Some("All unsaved changes will be lost"), + // add_button: ("Confirm", gtk::ResponseType::Accept), + // add_button: ("Cancel", gtk::ResponseType::Cancel), + // connect_response[sender] => move |_, resp| { + // sender.input(if resp == gtk::ResponseType::Accept { + // SheetEditDialogInput::Accept + // } else { + // SheetEditDialogInput::Cancel + // }) + // }, + // } + } + + fn init( + _params: Self::Init, + root: &Self::Root, + sender: ComponentSender, + ) -> ComponentParts { + let model = SheetEditDialogModel { + hidden: true, + sheet: None, + sheet_name: String::new(), + sheet_composer: String::new(), + }; + let widgets = view_output!(); + ComponentParts { model, widgets } + } + + fn update(&mut self, msg: Self::Input, sender: ComponentSender) { + match msg { + SheetEditDialogInput::Show(sheet) => { + self.hidden = false; + self.sheet_name = sheet.pdf.get_name().to_string(); + match &sheet.kind { + crate::sheet::SheetKind::Sheet { + name, + composer_id, + first_page, + book_id, + } => { + self.sheet_name = name.to_string(); + self.sheet_composer = composer_id.to_string(); + } + crate::sheet::SheetKind::Orphan => { + self.sheet_name = sheet.pdf.get_name().to_string(); + self.sheet_composer = String::new(); + } + crate::sheet::SheetKind::Book { + name, + composer_id, + sheet_ids, + } => todo!("Cannot yet handle books!"), + }; + + self.sheet = Some(sheet); + } + SheetEditDialogInput::Accept => { + self.hidden = true; + if let Some(mut sheet) = self.sheet.take() { + match sheet.kind.borrow_mut() { + crate::sheet::SheetKind::Sheet { + name, + composer_id, + first_page, + book_id, + } => { + todo!("Do something!!!"); + // name = "hello world"; + // name = &mut self.sheet_name.clone(); + // composer_id = 0; + } + crate::sheet::SheetKind::Orphan => { + todo!("Create Sheet"); + } + crate::sheet::SheetKind::Book { + name, + composer_id, + sheet_ids, + } => todo!(), + }; + sender + .output(SheetEditDialogOutput::SheetEdited(sheet)) + .unwrap(); + } + } + SheetEditDialogInput::Cancel => { + self.hidden = true; + self.sheet = None; + } + } + } +}