From af00f189a372dcc6d9b47f91f7aea0bf861d086b Mon Sep 17 00:00:00 2001 From: David Bliss Date: Thu, 28 Mar 2024 16:20:26 +0000 Subject: [PATCH] Add OnePhoto component --- src/app.rs | 37 ++++++--------- src/app/components/mod.rs | 1 + src/app/components/one_photo.rs | 79 +++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 src/app/components/one_photo.rs diff --git a/src/app.rs b/src/app.rs index a05184d6..1b8ca8f0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -30,6 +30,8 @@ use self::{ all_photos::AllPhotosOutput, month_photos::MonthPhotos, year_photos::YearPhotos, + one_photo::OnePhoto, + one_photo::OnePhotoInput, } }; @@ -39,8 +41,8 @@ pub(super) struct App { all_photos: Controller, month_photos: Controller, year_photos: Controller, + one_photo: Controller, picture_navigation_view: adw::NavigationView, - picture_view: gtk::Picture, } #[derive(Debug)] @@ -156,20 +158,8 @@ impl SimpleComponent for App { adw::NavigationPage { set_tag: Some("picture"), - adw::ToolbarView { - add_top_bar = &adw::HeaderBar, - - #[wrap(Some)] - set_content = >k::Box { - set_orientation: gtk::Orientation::Vertical, - - #[local_ref] - picture_view -> gtk::Picture { - set_can_shrink: true, - set_valign: gtk::Align::Center, - } - } - }, + // one_photo is the full-screen display of a given photo + model.one_photo.widget(), }, }, } @@ -226,6 +216,7 @@ impl SimpleComponent for App { let month_photos = MonthPhotos::builder().launch(controller.clone()).detach(); let year_photos = YearPhotos::builder().launch(controller.clone()).detach(); + let one_photo = OnePhoto::builder().launch(controller.clone()).detach(); let about_dialog = AboutDialog::builder() .transient_for(&root) @@ -242,8 +233,8 @@ impl SimpleComponent for App { all_photos, month_photos, year_photos, + one_photo, picture_navigation_view: picture_navigation_view.clone(), - picture_view: picture_view.clone(), }; let widgets = view_output!(); @@ -277,14 +268,12 @@ impl SimpleComponent for App { match message { AppMsg::Quit => main_application().quit(), AppMsg::ViewPhoto(picture_id) => { - println!("Showing photo for {}", picture_id); - let result = self.controller.borrow_mut().get(picture_id); - if let Ok(Some(pic)) = result { - self.picture_view.set_filename(Some(pic.path)); - self.picture_navigation_view.push_by_tag("picture"); - } else { - println!("Failed loading {}: {:?}", picture_id, result); - } + println!("Forward Showing photo for {}", picture_id); + // Send message to OnePhoto to show image + self.one_photo.emit(OnePhotoInput::ViewPhoto(picture_id)); + + // Display navigation page for viewing an individual photo. + self.picture_navigation_view.push_by_tag("picture"); } } } diff --git a/src/app/components/mod.rs b/src/app/components/mod.rs index b0924a22..034fc18c 100644 --- a/src/app/components/mod.rs +++ b/src/app/components/mod.rs @@ -6,3 +6,4 @@ pub mod about; pub mod all_photos; pub mod month_photos; pub mod year_photos; +pub mod one_photo; diff --git a/src/app/components/one_photo.rs b/src/app/components/one_photo.rs new file mode 100644 index 00000000..4e77b47e --- /dev/null +++ b/src/app/components/one_photo.rs @@ -0,0 +1,79 @@ +// SPDX-FileCopyrightText: © 2024 David Bliss +// +// SPDX-License-Identifier: GPL-3.0-or-later + +use gtk::prelude::{ OrientableExt}; +use photos_core::repo::PictureId; +use relm4::gtk; +use relm4::gtk::prelude::WidgetExt; +use relm4::*; +use std::cell::RefCell; +use std::rc::Rc; + +#[derive(Debug)] +pub enum OnePhotoInput { + ViewPhoto(PictureId), +} + +#[derive(Debug)] +pub struct OnePhoto { + controller: Rc>, + picture: gtk::Picture, +} + +#[relm4::component(pub)] +impl SimpleComponent for OnePhoto { + type Init = Rc>; + type Input = OnePhotoInput; + type Output = (); + + view! { + adw::ToolbarView { + add_top_bar = &adw::HeaderBar, + + #[wrap(Some)] + set_content = >k::Box { + set_orientation: gtk::Orientation::Vertical, + + #[local_ref] + picture -> gtk::Picture { + set_can_shrink: true, + set_valign: gtk::Align::Center, + } + } + } + } + + fn init( + controller: Self::Init, + _root: Self::Root, + _sender: ComponentSender, + ) -> ComponentParts { + + let picture = gtk::Picture::new(); + + let widgets = view_output!(); + + let model = OnePhoto { + controller, + picture, + }; + + ComponentParts { model, widgets } + } + + fn update(&mut self, msg: Self::Input, sender: ComponentSender) { + match msg { + OnePhotoInput::ViewPhoto(picture_id) => { + println!("Showing photo for {}", picture_id); + let result = self.controller.borrow_mut().get(picture_id); + if let Ok(Some(pic)) = result { + self.picture.set_filename(Some(pic.path)); + //self.picture_navigation_view.push_by_tag("picture"); + } else { + println!("Failed loading {}: {:?}", picture_id, result); + } + } + } + } +}