We can use functions provided in window module to change the window after it is initialized. For example, to resize the window. These functions return Command, which can be used as the return value in update method. Developers might be interested in other Commands in window module.
The resize function needs an ID of the window we are going to resize. Internally, Iced reserves window::Id::MAIN for the first window spawned.
use iced::{
executor,
widget::{button, row, text_input},
window, Application, Command, Settings, Size,
};
fn main() -> iced::Result {
MyApp::run(Settings::default())
}
#[derive(Debug, Clone)]
enum MyAppMessage {
UpdateWidth(String),
UpdateHeight(String),
ResizeWindow,
}
struct MyApp {
width: String,
height: String,
}
impl Application for MyApp {
type Executor = executor::Default;
type Message = MyAppMessage;
type Theme = iced::Theme;
type Flags = ();
fn new(_flags: Self::Flags) -> (Self, iced::Command<Self::Message>) {
(
Self {
width: "1024".into(),
height: "768".into(),
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("My App")
}
fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
match message {
MyAppMessage::UpdateWidth(w) => self.width = w,
MyAppMessage::UpdateHeight(h) => self.height = h,
MyAppMessage::ResizeWindow => {
return window::resize(
iced::window::Id::MAIN,
Size::new(self.width.parse().unwrap(), self.height.parse().unwrap()),
)
}
}
Command::none()
}
fn view(&self) -> iced::Element<Self::Message> {
row![
text_input("Width", &self.width).on_input(MyAppMessage::UpdateWidth),
text_input("Height", &self.height).on_input(MyAppMessage::UpdateHeight),
button("Resize window").on_press(MyAppMessage::ResizeWindow),
]
.into()
}
}
➡️ Next: Closing The Window On Demand
📘 Back: Table of contents