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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! Commands that control the scoped window.
use zng_app::{
event::{CommandHandle, CommandInfoExt, CommandNameExt, CommandScope, command},
hn,
shortcut::{CommandShortcutExt, shortcut},
view_process::VIEW_PROCESS,
window::WindowId,
};
use zng_layout::unit::{Dip, DipPoint, PxPoint, PxToDip as _};
use zng_view_api::window::{WindowCapability, WindowState};
use zng_wgt::{CommandIconExt as _, ICONS, wgt_fn};
use crate::{IME_EVENT, ImeArgs, WINDOWS, WINDOWS_SV, WindowInstanceState, WindowVars};
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 {
/// Setup command handlers, handles live in the WindowVars hooks.
pub fn init(id: WindowId, vars: &WindowVars) {
let state = vars.state();
let restore_state = vars.restore_state();
let s = state.get();
let c = WindowCommands {
maximize_handle: MAXIMIZE_CMD.scoped(id).on_event(
!matches!(s, WindowState::Maximized),
true,
false,
hn!(state, |args| {
args.propagation.stop();
state.set(WindowState::Maximized);
}),
),
minimize_handle: MINIMIZE_CMD.scoped(id).on_event(
!matches!(s, WindowState::Minimized),
true,
false,
hn!(state, |args| {
args.propagation.stop();
state.set(WindowState::Minimized);
}),
),
restore_handle: RESTORE_CMD.scoped(id).on_event(
!matches!(s, WindowState::Normal),
true,
false,
hn!(state, restore_state, |args| {
args.propagation.stop();
state.set(restore_state.get());
}),
),
_fullscreen_handle: FULLSCREEN_CMD.scoped(id).on_event(
true,
true,
false,
hn!(state, restore_state, |args| {
if let WindowState::Fullscreen = state.get() {
state.set(restore_state.get());
} else {
state.set(WindowState::Fullscreen);
}
}),
),
_exclusive_handle: EXCLUSIVE_FULLSCREEN_CMD.scoped(id).on_event(
true,
true,
false,
hn!(state, |args| {
if let WindowState::Exclusive = state.get() {
state.set(restore_state.get());
} else {
state.set(WindowState::Exclusive);
}
}),
),
_close_handle: CLOSE_CMD.scoped(id).on_event(
true,
true,
false,
hn!(|args| {
args.propagation.stop();
let _ = WINDOWS.close(id);
}),
),
};
state
.hook(move |a| {
let state = *a.value();
let c = &c; // hold all handles
c.restore_handle.enabled().set(state != WindowState::Normal);
c.maximize_handle.enabled().set(state != WindowState::Maximized);
c.minimize_handle.enabled().set(state != WindowState::Minimized);
true
})
.perm();
fn can_open_ctx_menu(state: WindowInstanceState) -> bool {
matches!(state, WindowInstanceState::Loaded { has_view: true })
&& VIEW_PROCESS.info().window.contains(WindowCapability::OPEN_TITLE_BAR_CONTEXT_MENU)
}
let handle = OPEN_TITLE_BAR_CONTEXT_MENU_CMD.scoped(id).on_event(
can_open_ctx_menu(vars.0.instance_state.get()),
true,
false,
hn!(|args| {
if let Some(w) = WINDOWS_SV.read().windows.get(&id)
&& let Some(vars) = &w.vars
&& let Some(r) = &w.root
&& let Some(v) = &r.view_window
{
args.propagation.stop();
let pos = if let Some(p) = args.param::<DipPoint>() {
*p
} else if let Some(p) = args.param::<PxPoint>() {
p.to_dip(vars.0.scale_factor.get())
} else {
DipPoint::splat(Dip::new(24))
};
let _ = v.open_title_bar_context_menu(pos);
}
}),
);
vars.0
.instance_state
.hook(move |a| {
handle.enabled().set(can_open_ctx_menu(a.value().clone()));
true
})
.perm();
let handle = CANCEL_IME_CMD.scoped(id).on_event(
false,
false,
false,
hn!(|args| {
let s = WINDOWS_SV.read();
if let Some(w) = s.windows.get(&id)
&& let Some(r) = &w.root
&& let Some(v) = &r.view_window
&& let Some(f) = s.focused.get()
&& f.window_id() == id
&& (matches!(args.scope, CommandScope::Window(w) if w == id)
|| matches!(args.scope, CommandScope::Widget(w) if w == f.widget_id()))
{
args.propagation.stop();
let _ = v.set_ime_area(None);
IME_EVENT.notify(ImeArgs::now(f, "", None));
}
}),
);
vars.0
.focused
.hook(move |a| {
handle.enabled().set(*a.value());
true
})
.perm();
let handle = DRAG_MOVE_RESIZE_CMD.scoped(id).on_event(
vars.0.resizable.get(),
true,
false,
hn!(|args| {
if let Some(w) = WINDOWS_SV.read().windows.get(&id)
&& let Some(r) = &w.root
&& let Some(v) = &r.view_window
{
args.propagation.stop();
let _ = match args.param::<crate::cmd::ResizeDirection>() {
Some(r) => v.drag_resize(*r),
None => v.drag_move(),
};
}
}),
);
vars.0
.resizable
.hook(move |a| {
handle.enabled().set(*a.value());
true
})
.perm();
}
}