Skip to content

changing_the_window_dynamically.rs change to preset to monitor size. #24

@jpercyasnet

Description

@jpercyasnet

I added the following code; so I could preset the window based on monitor size.
enum Message {
UpdateWidth(String),
UpdateHeight(String),
ResizeWindow,
PrintSize(Option),
}

struct MyApp {
width: String,
height: String,
mwidth: f32,
mheight: f32,
}
fn new() -> (Self, Task) {
(
Self {
width: "1024".into(),
height: "768".into(),
mheight: 768.0,
mwidth: 1024.0,
},
window::latest().and_then(window::monitor_size)
.map(Message::PrintSize),
)
Message::PrintSize(sizei) => {
if let Some(sizex) = sizei {
println!(" good size = {:?}", sizex);
self.mwidth = sizex.width;
self.mheight = sizex.height;
if self.mwidth > 1920.0 {
self.mwidth = 1900.0;
}
if self.mheight > 1080.0 {
self.mheight = 1070.0;
}
} else {
println!(" bad size = {:?}", sizei);
self.mwidth = 1024.0;
self.mheight = 768.0;
}
self.width = format!("{}",self.mwidth);
self.height = format!("{}", self.mheight);
return window::latest().and_then(Self::resize_window(self.mwidth, self.mheight));
}
Message::ResizeWindow => {
let width = self.width.parse::().unwrap_or(self.mwidth);
let height = self.height.parse::().unwrap_or(self.mheight);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions