1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! Commands that control the scoped window.

use zng_app::{
    event::{command, CommandHandle, CommandInfoExt, CommandNameExt},
    shortcut::{shortcut, CommandShortcutExt},
    update::EventUpdate,
    window::{WindowId, WINDOW},
};
use zng_var::Var;
use zng_view_api::window::WindowState;
use zng_wgt::{wgt_fn, CommandIconExt as _, ICONS};

use crate::{WindowVars, WINDOWS};

pub use zng_view_api::window::ResizeDirection;

command! {
    /// Represents the window **close** action.
    pub static CLOSE_CMD = {
        l10n!: true,
        name: "Close",
        info: "Close the window",
        shortcut: [shortcut!(ALT+F4), shortcut!(CTRL+'W')],
        icon: wgt_fn!(|_| ICONS.get(["window-close", "close"])),
    };

    /// Represents the window **minimize** action.
    pub static MINIMIZE_CMD = {
        l10n!: true,
        name: "Minimize",
        info: "Minimize the window",
        icon: wgt_fn!(|_| ICONS.get(["window-minimize"])),
    };

    /// Represents the window **maximize** action.
    pub static MAXIMIZE_CMD = {
        l10n!: true,
        name: "Maximize",
        info: "Maximize the window",
        icon: wgt_fn!(|_| ICONS.get(["window-maximize"])),
    };

    /// Represents the window **toggle fullscreen** action.
    ///
    /// # Behavior
    ///
    /// This command is about the *windowed* fullscreen state ([`WindowState::Fullscreen`]),
    /// use the [`EXCLUSIVE_FULLSCREEN_CMD`] to toggle *exclusive* video mode fullscreen.
    pub static FULLSCREEN_CMD = {
        l10n!: true,
        name: "Fullscreen",
        info: "Toggle fullscreen mode on the window",
        shortcut: {
            let a = if cfg!(target_os = "macos") {
                shortcut!(CTRL|SHIFT+'F')
            } else {
                shortcut!(F11)
            };
            [a, shortcut!(ZoomToggle)]
        },
        icon: wgt_fn!(|_| ICONS.get(["window-windowed-fullscreen", "window-fullscreen", "fullscreen"])),
    };

    /// Represents the window **toggle fullscreen** action.
    ///
    /// # Behavior
    ///
    /// This command is about the *exclusive* fullscreen state ([`WindowState::Exclusive`]),
    /// use the [`FULLSCREEN_CMD`] to toggle *windowed* fullscreen.
    pub static EXCLUSIVE_FULLSCREEN_CMD = {
        l10n!: true,
        name: "Exclusive Fullscreen",
        info: "Toggle exclusive fullscreen mode on the window",
        icon: wgt_fn!(|_| ICONS.get(["window-exclusive-fullscreen", "window-fullscreen", "fullscreen"])),
    };

    /// Represents the window **restore** action.
    ///
    /// Restores the window to its previous non-minimized state or normal state.
    pub static RESTORE_CMD = {
        l10n!: true,
        name: "Restore",
        info: "Restores the window to its previous non-minimized state or normal state",
        icon: wgt_fn!(|_| ICONS.get(["window-restore"])),
    };

    /// Represents the **close IME** action.
    ///
    /// If any IME preview is active close it without committing.
    pub static CANCEL_IME_CMD;

    /// Represents the window **drag-move** and **drag-resize** actions.
    ///
    /// There's no guarantee that this will work unless the left mouse button was pressed immediately before this command is called.
    ///
    /// # Parameter
    ///
    /// If this command is called without parameter the window will drag-move, if it is called with a [`ResizeDirection`] the
    /// window will drag-resize.
    pub static DRAG_MOVE_RESIZE_CMD;

    /// Represents the window **open title bar context menu** action.
    ///
    /// # Parameter
    ///
    /// This command supports an optional parameter, it can be a [`DipPoint`] or [`PxPoint`] that defines
    /// the menu position.
    ///
    /// [`DipPoint`]: zng_layout::unit::DipPoint
    /// [`PxPoint`]: zng_layout::unit::PxPoint
    pub static OPEN_TITLE_BAR_CONTEXT_MENU_CMD;
}

pub(super) struct WindowCommands {
    maximize_handle: CommandHandle,
    minimize_handle: CommandHandle,
    restore_handle: CommandHandle,

    fullscreen_handle: CommandHandle,
    exclusive_handle: CommandHandle,

    close_handle: CommandHandle,
}
impl WindowCommands {
    pub fn new(window_id: WindowId) -> Self {
        WindowCommands {
            maximize_handle: MAXIMIZE_CMD.scoped(window_id).subscribe(false),
            minimize_handle: MINIMIZE_CMD.scoped(window_id).subscribe(false),
            restore_handle: RESTORE_CMD.scoped(window_id).subscribe(false),
            fullscreen_handle: FULLSCREEN_CMD.scoped(window_id).subscribe(true),
            exclusive_handle: EXCLUSIVE_FULLSCREEN_CMD.scoped(window_id).subscribe(true),
            close_handle: CLOSE_CMD.scoped(window_id).subscribe(true),
        }
    }

    pub fn event(&mut self, window_vars: &WindowVars, update: &EventUpdate) {
        let scope = WINDOW.id();
        if let Some(args) = MAXIMIZE_CMD.scoped(scope).on(update) {
            args.handle_enabled(&self.maximize_handle, |_| {
                window_vars.state().set(WindowState::Maximized);
            });
        } else if let Some(args) = MINIMIZE_CMD.scoped(scope).on(update) {
            args.handle_enabled(&self.minimize_handle, |_| {
                window_vars.state().set(WindowState::Minimized);
            });
        } else if let Some(args) = RESTORE_CMD.scoped(scope).on(update) {
            args.handle_enabled(&self.restore_handle, |_| {
                window_vars.state().set(window_vars.restore_state().get());
            });
        } else if let Some(args) = CLOSE_CMD.scoped(scope).on(update) {
            args.handle_enabled(&self.close_handle, |_| {
                let _ = WINDOWS.close(scope);
            });
        } else if let Some(args) = FULLSCREEN_CMD.scoped(scope).on(update) {
            args.handle_enabled(&self.fullscreen_handle, |_| {
                if let WindowState::Fullscreen = window_vars.state().get() {
                    window_vars.state().set(window_vars.restore_state().get());
                } else {
                    window_vars.state().set(WindowState::Fullscreen);
                }
            });
        } else if let Some(args) = EXCLUSIVE_FULLSCREEN_CMD.scoped(scope).on(update) {
            args.handle_enabled(&self.exclusive_handle, |_| {
                if let WindowState::Exclusive = window_vars.state().get() {
                    window_vars.state().set(window_vars.restore_state().get());
                } else {
                    window_vars.state().set(WindowState::Exclusive);
                }
            });
        }
    }

    pub fn init(&mut self, window_vars: &WindowVars) {
        self.update_state(window_vars.state().get());
    }

    pub fn update(&mut self, window_vars: &WindowVars) {
        if let Some(state) = window_vars.state().get_new() {
            self.update_state(state);
        }
    }

    fn update_state(&mut self, state: WindowState) {
        self.restore_handle.set_enabled(state != WindowState::Normal);
        self.maximize_handle.set_enabled(state != WindowState::Maximized);
        self.minimize_handle.set_enabled(state != WindowState::Minimized);
    }
}