First try in implementing sheet edit dialog

This commit is contained in:
Julian Mutter 2024-05-25 23:10:47 +02:00
parent 2ffd0bbbc3
commit 9692b1a825

176
src/ui/sheet_edit_dialog.rs Normal file
View File

@ -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>,
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<Self>,
) -> ComponentParts<Self> {
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<Self>) {
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;
}
}
}
}