wiard 0.8.0

Window handling library for Windows in Rust
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use crate::*;
use std::cell::Cell;
use tokio::sync::oneshot;
use std::sync::Arc;
use windows::Win32::{Foundation::LPARAM, UI::WindowsAndMessaging::*};

/// An event when a window request to draw.
#[derive(Clone, Debug)]
pub struct Draw {
    pub invalidate_rect: PhysicalRect<i32>,
}

/// An event when window moved.
#[derive(Clone, Debug)]
pub struct Moved {
    pub position: ScreenPosition<i32>,
}

/// An moving edge of window when resizing.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ResizingEdge {
    Left,
    Right,
    Top,
    Bottom,
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
}

/// An event when resizing a window;
#[derive(Clone, Debug)]
pub struct Resizing {
    pub size: PhysicalSize<u32>,
    pub edge: ResizingEdge,
}

/// An event when resized or restored a window from maximized.
#[derive(Clone, Debug)]
pub struct Resized {
    pub size: PhysicalSize<u32>,
}

/// An event when a mouse button pressed or released.
#[derive(Clone, Debug)]
pub struct MouseInput {
    pub button: MouseButton,
    pub button_state: ButtonState,
    pub mouse_state: MouseState,
}

/// An event when a mouse cursor moved.
#[derive(Clone, Debug)]
pub struct CursorMoved {
    pub mouse_state: MouseState,
}

/// An event when a mouse cursor entered a window.
#[derive(Clone, Debug)]
pub struct CursorEntered {
    pub mouse_state: MouseState,
}

/// An event when a mouse cursor left a window.
#[derive(Debug)]
pub struct CursorLeft {
    pub position: PhysicalPosition<i32>,
}

// An event when a mouse wheel is rotated.
#[derive(Clone, Debug)]
pub struct MouseWheel {
    pub axis: MouseWheelAxis,
    pub distance: i32,
    pub mouse_state: MouseState,
}

/// An event when keyboard is input.
#[derive(Clone, Debug)]
pub struct KeyInput {
    pub key_code: KeyCode,
    pub key_state: KeyState,
    pub prev_pressed: bool,
}

impl KeyInput {
    #[inline]
    pub fn is(&self, key_code: impl Into<KeyCode>, key_state: KeyState) -> bool {
        self.key_code == key_code.into() && self.key_state == key_state
    }
}

/// An event that receive a keyboard input as the charcter code.
#[derive(Clone, Debug)]
pub struct CharInput {
    pub c: char,
}

/// An event of beginning IME composition.
///
/// When this event is dropped, this event send an IME candidate window position to the window.
/// Therefore, UiThread wait until this event is dropped.
///
#[derive(Debug)]
pub struct ImeBeginComposition {
    position: Cell<PhysicalPosition<i32>>,
    dpi: i32,
    tx: Option<oneshot::Sender<PhysicalPosition<i32>>>,
}

impl ImeBeginComposition {
    pub(crate) fn new(dpi: i32, tx: oneshot::Sender<PhysicalPosition<i32>>) -> Self {
        Self {
            position: Cell::new(PhysicalPosition::new(0, 0)),
            dpi,
            tx: Some(tx),
        }
    }

    #[inline]
    pub fn set_position(
        &self,
        position: impl ToPhysical<i32, Output<i32> = PhysicalPosition<i32>>,
    ) {
        self.position.set(position.to_physical(self.dpi));
    }
}

impl std::ops::Drop for ImeBeginComposition {
    #[inline]
    fn drop(&mut self) {
        self.tx.take().unwrap().send(self.position.get()).ok();
    }
}

/// An event when IME composition is updated.
#[derive(Clone, Debug)]
pub struct ImeUpdateComposition {
    pub chars: Vec<char>,
    pub clauses: Vec<ime::Clause>,
    pub cursor_position: usize,
}

/// An event when IME composition is finished.
#[derive(Clone, Debug)]
pub struct ImeEndComposition {
    pub result: Option<String>,
}

/// An event when IME candidate list is updated.
#[derive(Clone, Debug)]
pub struct ImeUpdateCandidateList {
    pub selection: usize,
    pub items: Vec<String>,
}

/// An event of maximized a window.
#[derive(Clone, Debug)]
pub struct Maximized {
    pub size: PhysicalSize<u32>,
}

/// An event of restored a window from minimized.
#[derive(Clone, Debug)]
pub struct Restored {
    pub size: PhysicalSize<u32>,
}

/// An event of changed DPI.
#[derive(Clone, Debug)]
pub struct DpiChanged {
    pub new_dpi: u32,
}

/// Values which can return from WM_NCHITTEST
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(u32)]
pub enum NcHitTestValue {
    Border = HTBORDER,
    Bottom = HTBOTTOM,
    BottomLeft = HTBOTTOMLEFT,
    BottomRight = HTBOTTOMRIGHT,
    Left = HTLEFT,
    Right = HTRIGHT,
    Top = HTTOP,
    TopLeft = HTTOPLEFT,
    TopRight = HTTOPRIGHT,
    Caption = HTCAPTION,
    Client = HTCLIENT,
    Size = HTSIZE,
    Help = HTHELP,
    HScroll = HTHSCROLL,
    VScroll = HTVSCROLL,
    Menu = HTMENU,
    MaxButton = HTMAXBUTTON,
    MinButton = HTMINBUTTON,
    CloseButton = HTCLOSE,
    SysMenu = HTSYSMENU,
    Error = HTERROR as u32,
    Transparent = HTTRANSPARENT as u32,
}

/// An event of non client area hit test.
///
/// UiThread wait until this event is dropped.
///
#[derive(Debug)]
pub struct NcHitTest {
    pub position: PhysicalPosition<i32>,
    value: Cell<Option<NcHitTestValue>>,
    tx: Option<oneshot::Sender<Option<NcHitTestValue>>>,
}

impl NcHitTest {
    pub(crate) fn new(lparam: LPARAM, tx: oneshot::Sender<Option<NcHitTestValue>>) -> Self {
        let position = lparam_to_point(lparam);
        Self {
            position,
            value: Cell::new(None),
            tx: Some(tx),
        }
    }

    #[inline]
    pub fn get(&self) -> Option<NcHitTestValue> {
        self.value.get()
    }

    #[inline]
    pub fn set(&self, value: Option<NcHitTestValue>) {
        self.value.set(value);
    }
}

impl std::ops::Drop for NcHitTest {
    fn drop(&mut self) {
        self.tx.take().unwrap().send(self.value.get()).ok();
    }
}

/// An event of occurred at the notify icon.
#[derive(Clone, Debug)]
pub struct NotifyIcon {
    pub id: super::NotifyIcon,
    pub event: NotifyIconEvent,
}

impl PartialEq<super::NotifyIcon> for NotifyIcon {
    #[inline]
    fn eq(&self, other: &super::NotifyIcon) -> bool {
        &self.id == other
    }
}

impl PartialEq<NotifyIcon> for super::NotifyIcon {
    #[inline]
    fn eq(&self, other: &NotifyIcon) -> bool {
        self == &other.id
    }
}

/// An event of pushed the menu item.
#[derive(Clone, Debug)]
pub struct MenuCommand {
    pub index: usize,
    pub handle: MenuHandle,
}

/// An event that requests to show context menu.
#[derive(Clone, Debug)]
pub struct ContextMenu {
    pub clicked_window: WindowHandle,
    pub position: ScreenPosition<i32>,
}

/// An event of changed the color mode.
#[derive(Clone, Debug)]
pub struct ColorModeChanged {
    pub current: ColorModeState,
    pub previous: ColorModeState,
}

/// An event of entered the dragging item.
///
/// UiThread wait until this event is dropped.
///
#[derive(Debug)]
pub struct DragEnter {
    pub position: PhysicalPosition<i32>,
    pub modifier_keys: ModifierKey,
    pub data: Arc<drag_drop::Data>,
    pub effect: drag_drop::Effect,
    pub(crate) tx: Option<oneshot::Sender<drag_drop::Effect>>,
}

impl std::ops::Drop for DragEnter {
    fn drop(&mut self) {
        let tx = self.tx.take().unwrap();
        tx.send(self.effect).ok();
    }
}

/// An event of moved the dragging item.
///
/// UiThread wait until this event is dropped.
///
#[derive(Debug)]
pub struct DragOver {
    pub position: PhysicalPosition<i32>,
    pub modifier_keys: ModifierKey,
    pub data: Arc<drag_drop::Data>,
    pub effect: drag_drop::Effect,
    pub(crate) tx: Option<oneshot::Sender<drag_drop::Effect>>,
}

impl std::ops::Drop for DragOver {
    fn drop(&mut self) {
        let tx = self.tx.take().unwrap();
        tx.send(self.effect).ok();
    }
}

/// An event of dropped the item.
///
/// UiThread wait until this event is dropped.
///
#[derive(Debug)]
pub struct Drop {
    pub position: PhysicalPosition<i32>,
    pub modifier_keys: ModifierKey,
    pub data: Arc<drag_drop::Data>,
    pub effect: drag_drop::Effect,
    pub(crate) tx: Option<oneshot::Sender<drag_drop::Effect>>,
}

impl std::ops::Drop for Drop {
    fn drop(&mut self) {
        let tx = self.tx.take().unwrap();
        tx.send(self.effect).ok();
    }
}

/// An event of request to close the window.
///
/// This event is called when the window is set `false` to [`auto_close()`].
///
/// [`auto_close()`]: ../struct.WindowBuilder.html#method.auto_close
///
#[derive(Clone, Debug)]
pub struct CloseRequest {
    handle: WindowHandle,
}

impl CloseRequest {
    pub(crate) fn new(handle: WindowHandle) -> Self {
        Self { handle }
    }

    #[inline]
    pub fn destroy(&self) {
        let handle = self.handle;
        UiThread::send_task(move || unsafe {
            DestroyWindow(handle.as_hwnd()).ok();
        });
    }
}

/// An event which defined by an user.
#[derive(Clone, Debug)]
pub struct App {
    pub index: u32,
    pub value0: usize,
    pub value1: isize,
}

impl App {
    #[inline]
    pub fn new(index: u32, value0: usize, value1: isize) -> Self {
        Self {
            index,
            value0,
            value1,
        }
    }
}

/// Other window messages
#[derive(Clone, Debug)]
pub struct Other {
    pub msg: u32,
    pub wparam: usize,
    pub lparam: isize,
}

/// Represents a event.
#[derive(Debug)]
#[non_exhaustive]
pub enum Event {
    /// An event when a window was active.
    Activated,
    /// An event when a window was inactive.
    Inactivated,
    /// An event when a window request to draw.
    Draw(Draw),
    /// An event when a window moved.
    Moved(Moved),
    /// An event when start to resize a window.
    EnterResizing,
    /// An event when resizing a window.
    Resizing(Resizing),
    /// An event when resized or restored a window from maximized.
    Resized(Resized),
    /// An event when a mouse button pressed or released.
    MouseInput(MouseInput),
    /// An event when a mouse cursor moved.
    CursorMoved(CursorMoved),
    /// An event when a mouse cursor entered a window.
    CursorEntered(CursorEntered),
    /// An event when a mouse cursor left a window.
    CursorLeft(CursorLeft),
    /// An event when a mouse wheel is rotated.
    MouseWheel(MouseWheel),
    /// An event when inputed using a keyboard.
    KeyInput(KeyInput),
    /// An event that received a keyboard input as the charactor.
    CharInput(CharInput),
    /// An event when an IME composition begin.
    ///
    /// **UiThread wait until this event value is dropped.**
    ImeBeginComposition(ImeBeginComposition),
    /// An event when an IME composition updated.
    ImeUpdateComposition(ImeUpdateComposition),
    /// An event when an IME composition is finished.
    ImeEndComposition(ImeEndComposition),
    /// An event when an IME candidate list opened.
    ImeBeginCandidateList,
    /// An event when an IME candidate list updated.
    ImeUpdateCandidateList(ImeUpdateCandidateList),
    /// An event when an IME candidate list closed.
    ImeEndCandidateList,
    /// An event when pushed a menu item.
    MenuCommand(MenuCommand),
    /// An event that requests to show a context menu.
    ContextMenu(ContextMenu),
    /// An event when a window minimized.
    Minizmized,
    /// An event when a window maximized.
    Maximized(Maximized),
    /// An event when a window restored from minimized.
    Restored(Restored),
    /// An event when a display scaling changed.
    DpiChanged(DpiChanged),
    /// An event of non client area hit test.
    ///
    /// **UiThread wait until this event value is dropped.**
    NcHitTest(NcHitTest),
    /// An event when a notify icon occurred.
    NotifyIcon(NotifyIcon),
    /// An event when a color mode changed.
    ColorModeChanged(ColorModeChanged),
    /// An event when a dragging item was entered on the window.
    ///
    /// **UiThread wait until this event value is dropped.**
    DragEnter(DragEnter),
    /// An event when a dragging item was moved on the window.
    ///
    /// **UiThread wait until this event value is dropped.**
    DragOver(DragOver),
    /// An event when a dragging item was left on the window.
    DragLeave,
    /// An event when a item was dropped on the window.
    ///
    /// **UiThread wait until this event value is dropped.**
    Drop(Drop),
    /// An event of requested to close the window.
    ///
    /// This event is called when the window is set `false` to [`auto_close()`].
    ///
    /// When destroying the window, call [`destroy()`] in this event value.
    ///
    /// [`auto_close()`]: ../struct.WindowBuilder.html#method.auto_close
    /// [`destroy()`]: ./event/struct.CloseRequest.html#method.destroy
    CloseRequest(CloseRequest),
    /// An event when the window closed.
    Closed,
    /// An event which defined by user.
    App(App),
    /// This event have raw window message values.
    Other(Other),
}