Skip to content

Commit

Permalink
Add OnePhoto component
Browse files Browse the repository at this point in the history
  • Loading branch information
blissd committed Mar 28, 2024
1 parent 48989fc commit af00f18
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use self::{
all_photos::AllPhotosOutput,
month_photos::MonthPhotos,
year_photos::YearPhotos,
one_photo::OnePhoto,
one_photo::OnePhotoInput,
}
};

Expand All @@ -39,8 +41,8 @@ pub(super) struct App {
all_photos: Controller<AllPhotos>,
month_photos: Controller<MonthPhotos>,
year_photos: Controller<YearPhotos>,
one_photo: Controller<OnePhoto>,
picture_navigation_view: adw::NavigationView,
picture_view: gtk::Picture,
}

#[derive(Debug)]
Expand Down Expand Up @@ -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 = &gtk::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(),
},
},
}
Expand Down Expand Up @@ -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)
Expand All @@ -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!();
Expand Down Expand Up @@ -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");
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/app/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod about;
pub mod all_photos;
pub mod month_photos;
pub mod year_photos;
pub mod one_photo;
79 changes: 79 additions & 0 deletions src/app/components/one_photo.rs
Original file line number Diff line number Diff line change
@@ -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<RefCell<photos_core::Controller>>,
picture: gtk::Picture,
}

#[relm4::component(pub)]
impl SimpleComponent for OnePhoto {
type Init = Rc<RefCell<photos_core::Controller>>;
type Input = OnePhotoInput;
type Output = ();

view! {
adw::ToolbarView {
add_top_bar = &adw::HeaderBar,

#[wrap(Some)]
set_content = &gtk::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<Self>,
) -> ComponentParts<Self> {

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

0 comments on commit af00f18

Please sign in to comment.