I have an Application implementation which creates a Container::new(Image::new("some_image_ex.jpg")) in iced. I am trying to find a way to create the window so that the window will be the height of the image unless the height is greater than the screen size.
Example: The user opens an image that creates a container that is 1000px by 2000px and the user's screen resolution is 1920x1080, I would like to set the Container's height to 1080 (and then scale the width of the image appropriately).
fn view(&mut self) -> Element<Message> {
use image::{GenericImageView, ImageFormat};
let im = image::open(&std::path::Path::new("resources/2.jpg")).unwrap();
let (width, height) = im.dimensions();
let img = Image::new("resources/2.jpg")
.height(Length::Fill)
.width(Length::Fill);
// .width should be smaller than the screens max height if height > max height
Container::new(img)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
Not sure about Iced exactly, but for Winit (which I baselessly assume you're using), you can go through the Window to get a MonitorHandle, which will tell the resolution of the monitor and it's DPI scaling factor (on some systems). But I think in general, you'll have some trouble getting a good size, because it's hard to account for how the user is using the screen unless you actually do a fullscreen transition. E.g., Windows has a task bar, and if you don't account for that, your window will probably be too big if you use the screen resolution.
What I usually do is have a setting that the user can just configure as a max window size, and pick a sane default. I would be curious to hear if there's actually something better to do.
Thanks for your response, it's given me some things to think about. I have noticed that other software behaves in the way I've described and do account for the taskbar (on win10). I guess I will keep looking.