denise-win32 0.31.0

Windows child-window control for Denise, for a panel inside an existing Win32 application.
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
// No console window in a release build. A Rust binary defaults to the console
// subsystem, so Windows allocates one and it appears behind the app; this switches
// the PE header so it never does. Debug builds keep it, because that is where a
// panic message or a startup error is worth reading — with the windows subsystem
// there is nowhere for stderr to go.
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

//! A Denise panel as a child control in an ordinary Win32 window.
//!
//! ```text
//! cargo run -p denise-win32 --example embed
//! ```
//!
//! The window, the message loop and the timer belong to the host. Denise owns one
//! child `HWND` inside it and nothing else — which is the shape a real host uses:
//! the control goes in a dialog next to the buttons it already has.
//!
//! # It is a diagnostic, not just a demo
//!
//! Three lines under the panel report what actually arrived: the last key
//! position with its modifiers, the last committed character with its codepoint,
//! and the last pointer event with the damage the frame produced. That is the
//! same trick `denise-evdev`'s `keys` example plays on Linux, and it is what
//! turned "æøå does not work" into a fixed AltGr bug in M4 — a panel that merely
//! *looks* right tells you nothing about which layer is lying.
//!
//! What to try, in rough order of how likely it is to be broken:
//!
//! 1. **Tab.** It should move focus between the field and the buttons. If the
//!    control is in a dialog and Tab never arrives, `WM_GETDLGCODE` is the
//!    suspect.
//! 2. **AltGr.** On a Norwegian layout `AltGr+2` is `@`. The key line should say
//!    `AltRight` and the text line `U+0040`. If it says `AltLeft`, the extended
//!    bit is being lost.
//! 3. **`æ ø å`.** Key positions `Quote`, `Semicolon`, `BracketLeft` on a
//!    Norwegian keyboard; the characters come separately, from `WM_CHAR`.
//! 4. **Press a button and drag off it before releasing.** It must un-press. If
//!    it stays lit, `SetCapture` is not working.
//! 5. **Resize the window.** The damage line should show small rectangles once
//!    it settles, not the whole client area every frame.
//! 6. **Move it to a display with a different DPI**, if there is one.
//!
//! Compile-checked for `x86_64-pc-windows-msvc` and not yet run; see the crate
//! documentation for what that does and does not cover.

#[cfg(not(windows))]
fn main() {
    eprintln!("this example needs Windows");
}

#[cfg(windows)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
    app::run()
}

#[cfg(windows)]
mod app {
    use std::time::Instant;

    use denise::{ElementState, InputEvent, Modifiers, Radius, Rect, Role, Size, Surface, Theme};
    use denise_ui::widgets::{Align, Button, Label, Panel, TextInput};
    use denise_ui::{NodeId, Ui};
    use denise_win32::{ControlDelegate, DeniseControl, DibSurface};
    use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, RECT, WPARAM};
    use windows::Win32::System::LibraryLoader::GetModuleHandleW;
    use windows::Win32::UI::WindowsAndMessaging::{
        AdjustWindowRect, CW_USEDEFAULT, CreateWindowExW, DefWindowProcW, DispatchMessageW,
        GetMessageW, MSG, PostQuitMessage, RegisterClassExW, SW_SHOW, SWP_NOZORDER, SetTimer,
        SetWindowPos, ShowWindow, TranslateMessage, WINDOW_EX_STYLE, WM_DESTROY, WM_SIZE, WM_TIMER,
        WNDCLASSEXW, WS_OVERLAPPEDWINDOW,
    };
    use windows::core::w;

    const WIDTH: i32 = 520;
    const HEIGHT: i32 = 400;
    /// The heartbeat, in milliseconds. Fixed rather than rescheduled from
    /// `next_wake_ms`: an update with nothing to do is one empty event list and no
    /// invalidation, which is cheaper than the code to be precise about a caret.
    const TICK_MS: u32 = 20;
    const TIMER_ID: usize = 1;

    /// Where the host keeps the control between messages. A real application
    /// would put it in its own document or dialog structure; a single-window
    /// example has nowhere better.
    static mut CONTROL: Option<DeniseControl> = None;

    #[derive(Clone, Debug, PartialEq, Eq)]
    enum Msg {
        Save,
        Reset,
    }

    struct Demo {
        ui: Ui<Msg>,
        status: NodeId,
        keys: NodeId,
        text: NodeId,
        pointer: NodeId,
        name: NodeId,
        started: Instant,
        saves: u32,
        /// Rectangles the last painted frame damaged, so the line can say whether
        /// the incremental path is doing anything.
        last_damage: usize,
    }

    impl Demo {
        fn new(size: Size) -> Self {
            let mut ui: Ui<Msg> = Ui::new(size, Theme::DARK);
            // Windows draws a pointer already. Said once, it sticks.
            ui.show_cursor(false);

            let root = ui.root();
            let card = ui
                .add(
                    root,
                    Panel::default().with_radius(Radius::Box),
                    Rect::new(28, 28, size.width as i32 - 56, size.height as i32 - 56),
                )
                .expect("card");

            ui.add(
                card,
                Label::new("Operatørpanel").with_size(22),
                Rect::new(24, 20, 380, 30),
            )
            .expect("title");

            let name = ui
                .add(
                    card,
                    TextInput::<Msg>::new()
                        .with_placeholder("Ola Nordmann")
                        .with_submit(Msg::Save),
                    Rect::new(24, 70, 380, 40),
                )
                .expect("name");

            ui.add(
                card,
                Button::new("Lagre", Msg::Save),
                Rect::new(24, 130, 180, 44),
            )
            .expect("save");

            ui.add(
                card,
                Button::new("Nullstill", Msg::Reset).with_role(Role::Neutral),
                Rect::new(220, 130, 180, 44),
            )
            .expect("reset");

            let status = ui
                .add(
                    card,
                    Label::new("Tab, Enter, click.")
                        .with_role(Role::Base300)
                        .with_align(Align::Center, Align::Center),
                    Rect::new(24, 186, 380, 22),
                )
                .expect("status");

            // The diagnostic. Monospace-ish and small, because it is meant to be
            // read while something else has focus.
            let mut line = |y: i32, text: &str| {
                ui.add(
                    card,
                    Label::new(text).with_role(Role::Base300).with_size(13),
                    Rect::new(24, y, 380, 18),
                )
                .expect("diagnostic line")
            };
            let keys = line(214, "key   -");
            let text = line(234, "text  -");
            let pointer = line(254, "mouse -");

            ui.focus(Some(name));

            Self {
                ui,
                status,
                keys,
                text,
                pointer,
                name,
                started: Instant::now(),
                saves: 0,
                last_damage: 0,
            }
        }

        fn set(&mut self, node: NodeId, text: String) {
            if let Some(label) = self.ui.widget_mut::<Label>(node) {
                // `update` rather than `set_text`: an unchanged reading should not
                // cost a repaint, and holding a key down sends the same line many
                // times a second.
                label.update(&text);
            }
        }

        /// Records what arrived, so the panel reports the input rather than only
        /// reacting to it.
        fn describe(&mut self, events: &[InputEvent]) {
            for event in events {
                match event {
                    InputEvent::Key {
                        code,
                        state,
                        repeat,
                        modifiers,
                    } => {
                        let mut held = String::new();
                        for (bit, name) in [
                            (Modifiers::SHIFT, "shift"),
                            (Modifiers::CTRL, "ctrl"),
                            (Modifiers::ALT, "alt"),
                            (Modifiers::SUPER, "win"),
                        ] {
                            if modifiers.contains(bit) {
                                held.push(' ');
                                held.push_str(name);
                            }
                        }
                        let updown = if state == &ElementState::Down {
                            "v"
                        } else {
                            "^"
                        };
                        let rep = if *repeat { " (repeat)" } else { "" };
                        self.set(self.keys, format!("key   {updown} {code:?}{held}{rep}"));
                    }
                    InputEvent::Text { ch } => {
                        self.set(self.text, format!("text  {ch:?}  U+{:04X}", *ch as u32));
                    }
                    InputEvent::PointerMoved { position } => {
                        let damage = self.last_damage;
                        self.set(
                            self.pointer,
                            format!("mouse {},{}   damage {damage}", position.x, position.y),
                        );
                    }
                    InputEvent::PointerScroll { delta_y, .. } => {
                        self.set(self.pointer, format!("wheel {delta_y:+.0}"));
                    }
                    InputEvent::PointerLeft => {
                        self.set(self.pointer, "mouse left the control".to_owned());
                    }
                    _ => {}
                }
            }
        }
    }

    impl ControlDelegate for Demo {
        fn update(
            &mut self,
            surface: &mut DibSurface,
            events: &[InputEvent],
            damage: &mut Vec<Rect>,
        ) {
            if surface.size() != self.ui.size() {
                self.ui = Demo::new(surface.size()).ui;
                self.ui.invalidate_all();
            }

            self.describe(events);
            self.ui.handle(events);
            self.ui.tick(self.started.elapsed().as_millis() as u64);

            let messages: Vec<Msg> = self.ui.drain_messages().collect();
            for message in messages {
                let text = match message {
                    Msg::Save => {
                        self.saves += 1;
                        let who = self
                            .ui
                            .widget::<TextInput<Msg>>(self.name)
                            .map(|f| f.text().to_owned())
                            .unwrap_or_default();
                        format!(
                            "Lagret {} ({})",
                            self.saves,
                            if who.is_empty() { "ingen" } else { &who }
                        )
                    }
                    Msg::Reset => {
                        if let Some(field) = self.ui.widget_mut::<TextInput<Msg>>(self.name) {
                            field.clear();
                        }
                        "Nullstilt.".to_owned()
                    }
                };
                if let Some(label) = self.ui.widget_mut::<Label>(self.status) {
                    label.set_text(text);
                }
            }

            // An idle panel does nothing at all, and Windows is told nothing, so
            // nothing composites.
            if !self.ui.needs_paint() {
                return;
            }

            match surface.acquire() {
                Ok(mut frame) => {
                    self.ui.paint(&mut frame);
                    drop(frame);
                    damage.extend_from_slice(self.ui.damage());
                    self.last_damage = self.ui.damage().len();
                    self.ui.presented();
                }
                Err(e) => eprintln!("acquire: {e}"),
            }
        }

        fn next_wake_ms(&self) -> Option<u64> {
            self.ui.next_wake_ms()
        }
    }

    pub fn run() -> Result<(), Box<dyn std::error::Error>> {
        // SAFETY: a null module name asks for this process's handle.
        let instance = unsafe { GetModuleHandleW(None) }?;

        let class = WNDCLASSEXW {
            cbSize: size_of::<WNDCLASSEXW>() as u32,
            lpfnWndProc: Some(host_proc),
            hInstance: instance.into(),
            lpszClassName: w!("Denise.Example.Host"),
            ..Default::default()
        };
        // SAFETY: `class` is fully initialised and `host_proc` has the required
        // signature.
        if unsafe { RegisterClassExW(&class) } == 0 {
            return Err("could not register the host class".into());
        }

        // `CreateWindowEx` takes the *window* size, not the client size, so
        // asking for 520x400 gets a client area smaller by the title bar and the
        // borders — and the panel inside would be quietly cut off at the bottom.
        // This is what the API is for.
        let mut wanted = RECT {
            left: 0,
            top: 0,
            right: WIDTH,
            bottom: HEIGHT,
        };
        // SAFETY: `wanted` is a live local and the style matches the one below.
        unsafe { AdjustWindowRect(&mut wanted, WS_OVERLAPPEDWINDOW, false) }?;

        // SAFETY: the class was just registered and every argument is valid.
        let window = unsafe {
            CreateWindowExW(
                WINDOW_EX_STYLE::default(),
                w!("Denise.Example.Host"),
                w!("Denise — embedded control"),
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                wanted.right - wanted.left,
                wanted.bottom - wanted.top,
                None,
                None,
                Some(instance.into()),
                None,
            )
        }?;

        let control = DeniseControl::new(
            window,
            Rect::new(0, 0, WIDTH, HEIGHT),
            1.0,
            Box::new(Demo::new(Size::new(WIDTH as u32, HEIGHT as u32))),
        )?;
        // SAFETY: single-threaded, and nothing reads this before it is written.
        unsafe { CONTROL = Some(control) };
        control.update();

        // SAFETY: `window` is live and the timer id is this window's own.
        unsafe { SetTimer(Some(window), TIMER_ID, TICK_MS, None) };
        // SAFETY: `window` is live.
        unsafe {
            let _ = ShowWindow(window, SW_SHOW);
        }

        let mut message = MSG::default();
        // SAFETY: the standard message loop. `GetMessageW` returns 0 on WM_QUIT
        // and -1 on error, both of which end it.
        while unsafe { GetMessageW(&mut message, None, 0, 0) }.as_bool() {
            // SAFETY: `message` was just filled by `GetMessageW`. `TranslateMessage`
            // is what turns WM_KEYDOWN into WM_CHAR, so a panel without it can be
            // navigated and not typed into.
            unsafe {
                let _ = TranslateMessage(&message);
                DispatchMessageW(&message);
            }
        }
        Ok(())
    }

    extern "system" fn host_proc(
        hwnd: HWND,
        message: u32,
        wparam: WPARAM,
        lparam: LPARAM,
    ) -> LRESULT {
        match message {
            WM_SIZE => {
                let width = (lparam.0 & 0xFFFF) as i32;
                let height = ((lparam.0 >> 16) & 0xFFFF) as i32;
                // SAFETY: single-threaded; the control outlives the window.
                if let Some(control) = unsafe { CONTROL } {
                    // SAFETY: the control's window is live for as long as this one.
                    unsafe {
                        let _ =
                            SetWindowPos(control.hwnd(), None, 0, 0, width, height, SWP_NOZORDER);
                    }
                }
                LRESULT(0)
            }
            WM_TIMER => {
                // SAFETY: as above.
                if let Some(control) = unsafe { CONTROL } {
                    control.update();
                }
                LRESULT(0)
            }
            WM_DESTROY => {
                // SAFETY: ends the message loop above.
                unsafe { PostQuitMessage(0) };
                LRESULT(0)
            }
            // SAFETY: the standard fallback, valid for any window and message.
            _ => unsafe { DefWindowProcW(hwnd, message, wparam, lparam) },
        }
    }
}