Skip to content

Latest commit

 

History

History
58 lines (44 loc) · 1.92 KB

closing_the_window_on_demand.md

File metadata and controls

58 lines (44 loc) · 1.92 KB

Closing The Window On Demand

This tutorial follows the previous tutorial. We use the close function in window module to close the window. This is also done by returning the Command obtained by the close function.

Similar to the resize function, the close function also needs an ID of the window. We pass window::Id::MAIN for the ID.

use iced::{
    executor,
    widget::{button, row},
    window, Application, Command, Settings,
};

fn main() -> iced::Result {
    MyApp::run(Settings::default())
}

#[derive(Debug, Clone)]
enum MyAppMessage {
    CloseWindow,
}

struct MyApp;

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, Command::none())
    }

    fn title(&self) -> String {
        String::from("My App")
    }

    fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
        match message {
            MyAppMessage::CloseWindow => window::close(window::Id::MAIN),
        }
    }

    fn view(&self) -> iced::Element<Self::Message> {
        row![button("Close window").on_press(MyAppMessage::CloseWindow),].into()
    }
}

Closing the window on demand

➡️ Next: On Pressed/Released Of Some Widgets

📘 Back: Table of contents