Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.81 KB

closing_the_window_on_demand.md

File metadata and controls

54 lines (41 loc) · 1.81 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::{
    widget::{button, row},
    window, Element, Task,
};

fn main() -> iced::Result {
    iced::application("closing the window on demand", MyApp::update, MyApp::view).run()
}

struct MyApp {} 

impl Default for MyApp {
    fn default() -> Self {
        MyApp::new().0
    }
}

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

impl MyApp {
    fn new() -> (Self, Task<Message>) {
        (Self {}, Task::none())
    }

    fn update(&mut self, message: Message) -> Task<Message> {
        match message {
            Message::CloseWindow => return window::get_oldest().and_then(window::close),
        }
    }

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

Closing the window on demand

➡️ Next: On Pressed/Released Of Some Widgets

📘 Back: Table of contents