feat: dynamic image measures based on screen size (see #27)

This commit is contained in:
Bnyro 2023-09-14 19:16:33 +02:00
parent 8e9cde2f97
commit 5d47f33661
2 changed files with 22 additions and 2 deletions

View File

@ -27,6 +27,7 @@ pub struct PostPage {
#[allow(dead_code)] #[allow(dead_code)]
create_comment_dialog: Controller<EditorDialog>, create_comment_dialog: Controller<EditorDialog>,
voting_row: Controller<VotingRowModel>, voting_row: Controller<VotingRowModel>,
thumbnail_height: i32,
} }
#[derive(Debug)] #[derive(Debug)]
@ -65,7 +66,8 @@ impl SimpleComponent for PostPage {
#[local_ref] #[local_ref]
image -> gtk::Box { image -> gtk::Box {
set_height_request: 400, #[watch]
set_height_request: model.thumbnail_height,
set_margin_bottom: 20, set_margin_bottom: 20,
set_margin_top: 20, set_margin_top: 20,
#[watch] #[watch]
@ -218,6 +220,7 @@ impl SimpleComponent for PostPage {
let voting_row = VotingRowModel::builder() let voting_row = VotingRowModel::builder()
.launch(VotingStats::default()) .launch(VotingStats::default())
.detach(); .detach();
let model = PostPage { let model = PostPage {
info: init, info: init,
image, image,
@ -226,6 +229,7 @@ impl SimpleComponent for PostPage {
community_avatar, community_avatar,
create_comment_dialog: dialog, create_comment_dialog: dialog,
voting_row, voting_row,
thumbnail_height: 400,
}; };
let image = model.image.widget(); let image = model.image.widget();
@ -241,6 +245,13 @@ impl SimpleComponent for PostPage {
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) { fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
match message { match message {
PostPageInput::UpdatePost(post) => { PostPageInput::UpdatePost(post) => {
match relm4::main_application().active_window() {
Some(window) => {
self.thumbnail_height = window.allocated_width() / 2;
}
None => unreachable!(),
}
self.info = post.clone(); self.info = post.clone();
self.image self.image

View File

@ -15,6 +15,7 @@ pub struct PostRow {
community_image: Controller<WebImage>, community_image: Controller<WebImage>,
thumbnail: Controller<WebImage>, thumbnail: Controller<WebImage>,
voting_row: Controller<VotingRowModel>, voting_row: Controller<VotingRowModel>,
image_size: i32,
} }
#[derive(Debug)] #[derive(Debug)]
@ -49,7 +50,7 @@ impl FactoryComponent for PostRow {
#[local_ref] #[local_ref]
thumbnail -> gtk::Box { thumbnail -> gtk::Box {
set_visible: self.post.post.thumbnail_url.is_some(), set_visible: self.post.post.thumbnail_url.is_some(),
set_size_request: (180, 180), set_size_request: (self.image_size, self.image_size),
set_margin_start: 10, set_margin_start: 10,
set_margin_end: 10, set_margin_end: 10,
set_hexpand: false, set_hexpand: false,
@ -168,6 +169,7 @@ impl FactoryComponent for PostRow {
community_image, community_image,
voting_row, voting_row,
thumbnail, thumbnail,
image_size: 1500,
} }
} }
@ -178,6 +180,13 @@ impl FactoryComponent for PostRow {
_returned_widget: &<Self::ParentWidget as relm4::factory::FactoryView>::ReturnedWidget, _returned_widget: &<Self::ParentWidget as relm4::factory::FactoryView>::ReturnedWidget,
sender: FactorySender<Self>, sender: FactorySender<Self>,
) -> Self::Widgets { ) -> Self::Widgets {
match root.widget_ref().toplevel_window() {
Some(window) => {
self.image_size = window.allocated_width() / 8;
}
None => unreachable!(),
}
println!("size {}", self.image_size);
let thumbnail = self.thumbnail.widget(); let thumbnail = self.thumbnail.widget();
let author_image = self.author_image.widget(); let author_image = self.author_image.widget();
let community_image = self.community_image.widget(); let community_image = self.community_image.widget();