diff --git a/src/advice.rs b/src/advice.rs index 14482c8..98bf31c 100644 --- a/src/advice.rs +++ b/src/advice.rs @@ -23,18 +23,24 @@ pub struct AdviceCard { #[do_not_track] metadata: Advice, texture: Option, + file: Option, } #[derive(Debug)] pub enum AdviceCardCmds { - GotTexture(gdk::Texture), + GotTexture(gio::File, gdk::Texture), Error(LibraryError), } +#[derive(Debug)] +pub enum AdviceCardInput { + OpenFile +} + #[relm4::factory(pub)] impl FactoryComponent for AdviceCard { type Init = Advice; - type Input = (); + type Input = AdviceCardInput; type Output = (); type CommandOutput = AdviceCardCmds; type ParentWidget = gtk::FlowBox; @@ -42,8 +48,8 @@ impl FactoryComponent for AdviceCard { /* TODO: * Action "View in Image viewer": * Triggers: - * - One click on "enlarge" button - * - Double click on "texture" itself + * - [x] One click on "enlarge" button + * - [ ] Double click on "texture" itself * (Is there a better way than this?) * Create temporary file, let application open this file... */ @@ -78,6 +84,8 @@ impl FactoryComponent for AdviceCard { set_icon_name: "four-arrows-pointing-outward-symbolic", set_margin_all: 8, + + connect_clicked => AdviceCardInput::OpenFile, }, #[wrap(Some)] @@ -94,6 +102,7 @@ impl FactoryComponent for AdviceCard { let _self = Self { metadata: value.clone(), texture: None, + file: None, tracker: 0, }; @@ -119,7 +128,7 @@ impl FactoryComponent for AdviceCard { file }; - let image = glycin::Loader::new(file) + let image = glycin::Loader::new(file.clone()) .load() .await .expect("Image decoding failed"); @@ -128,7 +137,7 @@ impl FactoryComponent for AdviceCard { .await .expect("Image frame decoding failed"); - AdviceCardCmds::GotTexture(frame.texture()) + AdviceCardCmds::GotTexture(file, frame.texture()) }); _self @@ -136,8 +145,21 @@ impl FactoryComponent for AdviceCard { fn update_cmd(&mut self, message: Self::CommandOutput, sender: FactorySender) { match message { - AdviceCardCmds::GotTexture(texture) => self.set_texture(Some(texture)), + AdviceCardCmds::GotTexture(file, texture) => { + self.set_texture(Some(texture)); + self.set_file(Some(file)); + }, AdviceCardCmds::Error(err) => todo!(), }; } + + fn update(&mut self, message: Self::Input, sender: FactorySender) { + match message { + AdviceCardInput::OpenFile => { + let file_launcher = gtk::FileLauncher::new(self.get_file().as_ref()); + file_launcher.set_always_ask(true); + file_launcher.launch(None::<>k::Window>, None::<&gio::Cancellable>, |_| {}); + }, + } + } }