glasskit 0.4.0

Native Windows UI with a glass look: tray icon and menu, glass popup, themed dialogs, and helpers for tray apps.
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
// Copyright (c) 2026 John Law
// SPDX-License-Identifier: MIT

//! A small modal window built from standard Windows controls, in the same colors as the popup.
//! The app places the controls and handles the events. `glasskit` knows nothing about the content.

use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::ffi::c_void;
use std::rc::Rc;

use crate::error::{Call, Result};
use windows::Win32::Foundation::{COLORREF, HWND, LPARAM, LRESULT, RECT, WPARAM};
use windows::Win32::Graphics::Direct2D::Common::{
    D2D_RECT_F, D2D1_ALPHA_MODE_IGNORE, D2D1_COLOR_F, D2D1_PIXEL_FORMAT,
};
use windows::Win32::Graphics::Direct2D::{
    D2D1_DRAW_TEXT_OPTIONS_CLIP, D2D1_FACTORY_TYPE_SINGLE_THREADED, D2D1_FEATURE_LEVEL_DEFAULT,
    D2D1_RENDER_TARGET_PROPERTIES, D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1_RENDER_TARGET_USAGE_NONE,
    D2D1_ROUNDED_RECT, D2D1CreateFactory, ID2D1DCRenderTarget, ID2D1Factory, ID2D1RenderTarget,
};
use windows::Win32::Graphics::DirectWrite::{
    DWRITE_FACTORY_TYPE_SHARED, DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STYLE_NORMAL,
    DWRITE_FONT_WEIGHT_NORMAL, DWRITE_MEASURING_MODE_NATURAL, DWRITE_PARAGRAPH_ALIGNMENT_CENTER,
    DWRITE_TEXT_ALIGNMENT_CENTER, DWRITE_TEXT_ALIGNMENT_LEADING, DWRITE_TRIMMING,
    DWRITE_TRIMMING_GRANULARITY_CHARACTER, DWRITE_WORD_WRAPPING_NO_WRAP, DWriteCreateFactory,
    IDWriteFactory, IDWriteTextFormat,
};
use windows::Win32::Graphics::Dwm::{
    DWMWA_CAPTION_COLOR, DWMWA_TEXT_COLOR, DWMWA_USE_IMMERSIVE_DARK_MODE,
};
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_B8G8R8A8_UNORM;
use windows::Win32::Graphics::Gdi::{
    BeginPaint, CLEARTYPE_QUALITY, CLIP_DEFAULT_PRECIS, CreateFontW, CreateSolidBrush,
    DEFAULT_CHARSET, DeleteObject, EndPaint, FW_NORMAL, FW_SEMIBOLD, FillRect, GetDC, GetTextFaceW,
    HBRUSH, HDC, HFONT, HGDIOBJ, InvalidateRect, OUT_DEFAULT_PRECIS, PAINTSTRUCT, ReleaseDC,
    SelectObject, SetBkColor, SetTextColor,
};
use windows::Win32::UI::Controls::{
    DRAWITEMSTRUCT, MEASUREITEMSTRUCT, ODS_COMBOBOXEDIT, ODS_DISABLED, ODS_FOCUS, ODS_NOFOCUSRECT,
    ODS_SELECTED, ODT_BUTTON, ODT_COMBOBOX, ODT_LISTBOX, SetWindowTheme, WM_MOUSELEAVE,
};
use windows::Win32::UI::HiDpi::AdjustWindowRectExForDpi;
use windows::Win32::UI::Input::KeyboardAndMouse::{
    EnableWindow, GetFocus, IsWindowEnabled, ReleaseCapture, SetCapture, SetFocus, VIRTUAL_KEY,
    VK_DOWN, VK_LEFT, VK_RIGHT, VK_UP,
};
use windows::Win32::UI::Shell::{DefSubclassProc, RemoveWindowSubclass, SetWindowSubclass};
use windows::Win32::UI::WindowsAndMessaging::{
    BS_OWNERDRAW, CB_ADDSTRING, CB_GETCURSEL, CB_GETDROPPEDSTATE, CB_GETLBTEXT, CB_GETLBTEXTLEN,
    CB_RESETCONTENT, CB_SETCURSEL, CBS_DROPDOWNLIST, CBS_HASSTRINGS, CBS_OWNERDRAWFIXED,
    CW_USEDEFAULT, CreateWindowExW, DLGC_WANTARROWS, DefWindowProcW, ES_AUTOHSCROLL,
    ES_AUTOVSCROLL, ES_MULTILINE, ES_NUMBER, ES_WANTRETURN, GetClientRect, GetDlgCtrlID,
    GetDlgItem, GetParent, GetWindowTextLengthW, GetWindowTextW, HMENU, IDC_ARROW, IDC_SIZEALL,
    LB_ADDSTRING, LB_DELETESTRING, LB_GETCURSEL, LB_GETTEXT, LB_GETTEXTLEN, LB_INSERTSTRING,
    LB_RESETCONTENT, LB_SETCURSEL, LBS_HASSTRINGS, LBS_NOINTEGRALHEIGHT, LBS_NOTIFY,
    LBS_OWNERDRAWFIXED, MB_ICONWARNING, MB_OK, MessageBoxW, SPI_GETWORKAREA, SWP_NOSIZE,
    SWP_NOZORDER, SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, SendMessageW, SetWindowPos, SetWindowTextW,
    SystemParametersInfoW, WINDOW_EX_STYLE, WINDOW_STYLE, WM_CAPTURECHANGED, WM_CLOSE, WM_COMMAND,
    WM_CTLCOLOREDIT, WM_CTLCOLORLISTBOX, WM_CTLCOLORSTATIC, WM_DRAWITEM, WM_ENABLE, WM_ERASEBKGND,
    WM_GETDLGCODE, WM_KEYDOWN, WM_KILLFOCUS, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MEASUREITEM,
    WM_MOUSEMOVE, WM_NCDESTROY, WM_PAINT, WM_SETFOCUS, WM_SETFONT, WS_CAPTION, WS_CHILD,
    WS_EX_APPWINDOW, WS_EX_CONTROLPARENT, WS_EX_DLGMODALFRAME, WS_OVERLAPPED, WS_SYSMENU,
    WS_TABSTOP, WS_VISIBLE, WS_VSCROLL,
};
use windows::core::{HSTRING, PCWSTR, w};
use windows_numerics::Vector2;

use crate::Ui;
use crate::events::DialogId;
use crate::native::{self, NativeWindow, state_of};
use crate::placement::{Area, Placement, SCREEN_MARGIN};
use crate::theme::{Theme, colorref, over, rgba};
use crate::ui::Notice;

mod paint;
mod picker;
mod types;

use paint::*;
use picker::*;
pub use types::*;

const CLASS_NAME: PCWSTR = w!("GlasskitDialog");

const PLACEMENT_CLASS: PCWSTR = w!("GlasskitPlacement");

const FIRST_ID: usize = 100;

const ID_OK: usize = 1;

const ID_CANCEL: usize = 2;

const SS_CENTERIMAGE: u32 = 0x200;

const BN_CLICKED: u32 = 0;

const SELCHANGE: u32 = 1;

const EN_CHANGE: u32 = 0x300;

/// The extra height of a closed drop-down list that holds its open list.
const DROP_DOWN_LIST_HEIGHT: i32 = 200;

const FONT_SIZE: f32 = 14.0;

const BUTTON_RADIUS: f32 = 6.0;

const CARD_RADIUS: f32 = 8.0;

const CHECK_BOX: f32 = 18.0;

const CHECK_RADIUS: f32 = 4.0;

const CHECK_GAP: f32 = 8.0;

const HEADING_INSET: i32 = 12;

const HEADING_TOP: i32 = 8;

const HEADING_HEIGHT: i32 = 20;

const FIELD_RADIUS: f32 = 4.0;

const FIELD_INSET_X: i32 = 8;

const FIELD_INSET_Y: i32 = 5;

const FIELD_LINE_HEIGHT: i32 = 20;

const LIST_INSET: i32 = 4;

const ITEM_HEIGHT: i32 = 24;

const ITEM_RADIUS: f32 = 4.0;

const ITEM_TEXT_INSET: f32 = 8.0;

const CHEVRON_WIDTH: f32 = 28.0;

/// The height of a closed drop-down at 100% display scale.
const DROP_DOWN_HEIGHT: i32 = 26;

/// Notification codes of edit boxes, lists, and drop-downs that change how a field looks.
const EN_SETFOCUS: u32 = 0x100;

const EN_KILLFOCUS: u32 = 0x200;

const LBN_SETFOCUS: u32 = 4;

const LBN_KILLFOCUS: u32 = 5;

const CBN_SETFOCUS: u32 = 3;

const CBN_KILLFOCUS: u32 = 4;

const CBN_DROPDOWN: u32 = 7;

const CBN_CLOSEUP: u32 = 8;

/// The faces to try, the first that Windows has is used. Segoe UI Variable is the popup font.
const FONT_FACES: [PCWSTR; 2] = [w!("Segoe UI Variable Text"), w!("Segoe UI")];

#[derive(Debug, Clone, Copy)]
enum Control<'a> {
    Label(&'a str),
    /// A card with a heading. Add it before the controls that it holds.
    Group(&'a str),
    Edit {
        multiline: bool,
        digits_only: bool,
    },
    DropDown,
    List,
    Check(&'a str),
    Button(&'a str),
    /// A picker for where a window shows on the primary monitor: a small copy of the work area
    /// with the window in it. The user drags the window, clicks a place, or uses the arrow keys.
    /// `window_width` and `window_height` are the window size at 100% display scale.
    Placement {
        window_width: i32,
        window_height: i32,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
    Static,
    Edit,
    DropDown,
    List,
    Check,
    Button,
    Placement,
}

/// What is behind a control: the window or a card.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Surface {
    Window,
    Card,
}

/// The window procedure only puts events in the queue. [`Dialog::run`] gives them to the handler
/// outside the window procedure, so the handler can change the controls safely.
struct Shared {
    id: u32,
    kinds: RefCell<Vec<Kind>>,
    surfaces: RefCell<Vec<Surface>>,
    checked: RefCell<Vec<bool>>,
    /// The cards in client pixels.
    cards: RefCell<Vec<RECT>>,
    /// For each control: the field that the dialog paints around it, in client pixels.
    fields: RefCell<Vec<Option<RECT>>>,
    /// For each control: the placement that a placement picker shows.
    placements: RefCell<Vec<Placement>>,
    /// For each control: the window size that a placement picker shows, at 100% scale.
    window_sizes: RefCell<Vec<(i32, i32)>>,
    /// While the user drags in a placement picker: the grab point in the window, in pixels.
    grab: Cell<Option<(f32, f32)>>,
    hover: Cell<Option<usize>>,
    events: RefCell<VecDeque<Event>>,
    muted: Cell<bool>,
    look: Look,
}

impl Shared {
    /// Queue an event for the app, and wake the event loop.
    fn push(&self, event: Event) {
        self.events.borrow_mut().push_back(event);
        crate::ui::notify(Notice::Dialog(self.id));
    }

    fn index_of(&self, child: HWND) -> Option<usize> {
        let id = unsafe { GetDlgCtrlID(child) } as usize;
        let index = id.checked_sub(FIRST_ID)?;
        (index < self.kinds.borrow().len()).then_some(index)
    }
}

/// A modal window with controls that the app places.
///
/// Make it with [`Dialog::new`], add the controls with the `add_` methods, then call
/// [`Dialog::run`]. The window closes when the `Dialog` is dropped.
pub struct Dialog {
    ui: Ui,
    id: DialogId,
    shown: bool,
    /// Declared before the fonts, so the window is gone before its fonts.
    window: NativeWindow<Shared>,
    font: Font,
    heading_font: Font,
    scale: f32,
    controls: Vec<HWND>,
}

/// A font that Windows deletes when the value is dropped.
struct Font(HFONT);

impl Drop for Font {
    fn drop(&mut self) {
        unsafe {
            let _ = DeleteObject(HGDIOBJ(self.0.0));
        }
    }
}

impl Dialog {
    /// `width` and `height` are the client size at 100% display scale.
    /// The colors follow the Windows light or dark app mode, as the popup does.
    pub fn new(ui: &Ui, title: &str, width: i32, height: i32) -> Result<Self> {
        let hinstance = ui.hinstance();
        unsafe {
            native::register_class(CLASS_NAME, Some(wndproc), hinstance, IDC_ARROW)?;
            native::register_class(
                PLACEMENT_CLASS,
                Some(placement_proc),
                hinstance,
                IDC_SIZEALL,
            )?;

            let dpi = crate::primary_monitor_dpi();
            let scale = dpi as f32 / 96.0;
            let theme = Theme::current();
            let style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU;
            let ex_style = WS_EX_DLGMODALFRAME | WS_EX_CONTROLPARENT | WS_EX_APPWINDOW;
            let mut frame = RECT {
                left: 0,
                top: 0,
                right: scaled(width, scale),
                bottom: scaled(height, scale),
            };
            AdjustWindowRectExForDpi(&mut frame, style, false, ex_style, dpi)
                .call("AdjustWindowRectExForDpi")?;
            let (outer_w, outer_h) = (frame.right - frame.left, frame.bottom - frame.top);

            let hwnd = CreateWindowExW(
                ex_style,
                CLASS_NAME,
                &HSTRING::from(title),
                style,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                outer_w,
                outer_h,
                None,
                None,
                Some(hinstance),
                None,
            )
            .call("CreateWindowExW(dialog)")?;

            let look = Look::new(theme, scale);
            native::set_dwm_attribute(
                hwnd,
                DWMWA_USE_IMMERSIVE_DARK_MODE,
                i32::from(theme.dark) as u32,
            );
            native::set_dwm_attribute(hwnd, DWMWA_CAPTION_COLOR, colorref(look.window).0);
            native::set_dwm_attribute(hwnd, DWMWA_TEXT_COLOR, look.text().0);

            let id = ui.new_id();
            let window = NativeWindow::attach(
                hwnd,
                Rc::new(Shared {
                    kinds: RefCell::new(Vec::new()),
                    surfaces: RefCell::new(Vec::new()),
                    checked: RefCell::new(Vec::new()),
                    cards: RefCell::new(Vec::new()),
                    fields: RefCell::new(Vec::new()),
                    placements: RefCell::new(Vec::new()),
                    window_sizes: RefCell::new(Vec::new()),
                    grab: Cell::new(None),
                    hover: Cell::new(None),
                    id,
                    events: RefCell::new(VecDeque::new()),
                    muted: Cell::new(false),
                    look,
                }),
            );

            let mut work = RECT::default();
            if SystemParametersInfoW(
                SPI_GETWORKAREA,
                0,
                Some(&mut work as *mut _ as *mut c_void),
                SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
            )
            .is_ok()
            {
                let x = work.left + ((work.right - work.left - outer_w) / 2).max(0);
                let y = work.top + ((work.bottom - work.top - outer_h) / 2).max(0);
                let _ = SetWindowPos(hwnd, None, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
            }

            Ok(Self {
                ui: ui.clone(),
                id: DialogId(id),
                shown: false,
                window,
                font: Font(ui_font(scale, FW_NORMAL.0 as i32)),
                heading_font: Font(ui_font(scale, FW_SEMIBOLD.0 as i32)),
                scale,
                controls: Vec::new(),
            })
        }
    }

    fn shared(&self) -> &Shared {
        self.window.state()
    }

    fn add(&mut self, control: Control, bounds: Bounds) -> Result<usize> {
        let s = |v: i32| scaled(v, self.scale);
        let bounds = match control {
            Control::Group(_) => {
                self.shared().cards.borrow_mut().push(RECT {
                    left: s(bounds.x),
                    top: s(bounds.y),
                    right: s(bounds.x + bounds.width),
                    bottom: s(bounds.y + bounds.height),
                });
                Bounds::new(
                    bounds.x + HEADING_INSET,
                    bounds.y + HEADING_TOP,
                    bounds.width - 2 * HEADING_INSET,
                    HEADING_HEIGHT,
                )
            }
            _ => bounds,
        };
        let child = WS_CHILD | WS_VISIBLE;
        let owner_drawn = child | WS_TABSTOP | WINDOW_STYLE(BS_OWNERDRAW as u32);
        let (class, text, kind, style, ex_style) = match control {
            Control::Label(text) | Control::Group(text) => (
                w!("STATIC"),
                text,
                Kind::Static,
                child | WINDOW_STYLE(SS_CENTERIMAGE),
                WINDOW_EX_STYLE(0),
            ),
            Control::Edit {
                multiline,
                digits_only,
            } => {
                let mut style = child | WS_TABSTOP;
                if multiline {
                    style |= WS_VSCROLL
                        | WINDOW_STYLE((ES_MULTILINE | ES_AUTOVSCROLL | ES_WANTRETURN) as u32);
                } else {
                    style |= WINDOW_STYLE(ES_AUTOHSCROLL as u32);
                }
                if digits_only {
                    style |= WINDOW_STYLE(ES_NUMBER as u32);
                }
                (w!("EDIT"), "", Kind::Edit, style, WINDOW_EX_STYLE(0))
            }
            Control::DropDown => (
                w!("COMBOBOX"),
                "",
                Kind::DropDown,
                child
                    | WS_TABSTOP
                    | WS_VSCROLL
                    | WINDOW_STYLE((CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED | CBS_HASSTRINGS) as u32),
                WINDOW_EX_STYLE(0),
            ),
            Control::List => (
                w!("LISTBOX"),
                "",
                Kind::List,
                child
                    | WS_TABSTOP
                    | WS_VSCROLL
                    | WINDOW_STYLE(
                        (LBS_NOTIFY | LBS_NOINTEGRALHEIGHT | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS)
                            as u32,
                    ),
                WINDOW_EX_STYLE(0),
            ),
            Control::Check(text) => (
                w!("BUTTON"),
                text,
                Kind::Check,
                owner_drawn,
                WINDOW_EX_STYLE(0),
            ),
            // The default button is not marked: Enter gives Event::Submit in any case.
            Control::Button(text) => (
                w!("BUTTON"),
                text,
                Kind::Button,
                owner_drawn,
                WINDOW_EX_STYLE(0),
            ),
            Control::Placement { .. } => (
                PLACEMENT_CLASS,
                "",
                Kind::Placement,
                child | WS_TABSTOP,
                WINDOW_EX_STYLE(0),
            ),
        };
        let rect = RECT {
            left: s(bounds.x),
            top: s(bounds.y),
            right: s(bounds.x + bounds.width),
            bottom: s(bounds.y + bounds.height),
        };
        let surface = if self
            .shared()
            .cards
            .borrow()
            .iter()
            .any(|c| contains(c, &rect))
        {
            Surface::Card
        } else {
            Surface::Window
        };
        // An edit box or a list sits without a frame in a field that the dialog paints.
        let inner = match (control, kind) {
            (
                Control::Edit {
                    multiline: false, ..
                },
                _,
            ) => {
                let line = s(FIELD_LINE_HEIGHT);
                let top = rect.top + ((rect.bottom - rect.top - line) / 2).max(0);
                RECT {
                    left: rect.left + s(FIELD_INSET_X),
                    top,
                    right: rect.right - s(FIELD_INSET_X),
                    bottom: top + line,
                }
            }
            (_, Kind::Edit) => inset(&rect, s(FIELD_INSET_X), s(FIELD_INSET_Y)),
            (_, Kind::List) => inset(&rect, s(LIST_INSET), s(LIST_INSET)),
            _ => rect,
        };
        let height = match kind {
            Kind::DropDown => s(bounds.height) + s(DROP_DOWN_LIST_HEIGHT),
            _ => inner.bottom - inner.top,
        };
        let index = self.controls.len();
        let id = FIRST_ID + index;
        {
            let shared = self.shared();
            shared.kinds.borrow_mut().push(kind);
            shared.surfaces.borrow_mut().push(surface);
            shared.checked.borrow_mut().push(false);
            shared.placements.borrow_mut().push(Placement::default());
            shared.window_sizes.borrow_mut().push(match control {
                Control::Placement {
                    window_width,
                    window_height,
                } => (window_width, window_height),
                _ => (0, 0),
            });
            shared
                .fields
                .borrow_mut()
                .push(matches!(kind, Kind::Edit | Kind::List).then_some(rect));
        }
        let hwnd = unsafe {
            CreateWindowExW(
                ex_style,
                class,
                &HSTRING::from(text),
                style,
                inner.left,
                inner.top,
                inner.right - inner.left,
                height,
                Some(self.window.hwnd()),
                Some(HMENU(id as *mut c_void)),
                None,
                None,
            )
        };
        let hwnd = match hwnd {
            Ok(hwnd) => hwnd,
            Err(err) => {
                let shared = self.shared();
                shared.kinds.borrow_mut().pop();
                shared.surfaces.borrow_mut().pop();
                shared.checked.borrow_mut().pop();
                shared.fields.borrow_mut().pop();
                shared.placements.borrow_mut().pop();
                shared.window_sizes.borrow_mut().pop();
                return Err(err).call("CreateWindowExW(control)");
            }
        };
        let font = match control {
            Control::Group(_) => self.heading_font.0,
            _ => self.font.0,
        };
        unsafe {
            SendMessageW(
                hwnd,
                WM_SETFONT,
                Some(WPARAM(font.0 as usize)),
                Some(LPARAM(1)),
            );
            if self.shared().look.theme.dark
                && matches!(kind, Kind::Edit | Kind::List | Kind::DropDown)
            {
                let _ = SetWindowTheme(hwnd, w!("DarkMode_Explorer"), PCWSTR::null());
            }
            if matches!(kind, Kind::Button | Kind::Check | Kind::DropDown) {
                let shared = self.shared() as *const Shared as usize;
                let _ = SetWindowSubclass(hwnd, Some(themed_control), index, shared);
            }
        }
        self.controls.push(hwnd);
        Ok(index)
    }

    /// Add a text that the user cannot change.
    pub fn add_label(&mut self, text: &str, bounds: Bounds) -> Result<Id<kind::Label>> {
        self.add(Control::Label(text), bounds).map(Id::new)
    }

    /// Add a card with a heading. Add it before the controls that go on it, and make it
    /// contain their bounds: the controls then take the card color.
    pub fn add_group(&mut self, heading: &str, bounds: Bounds) -> Result<Id<kind::Group>> {
        self.add(Control::Group(heading), bounds).map(Id::new)
    }

    /// Add an empty edit box.
    pub fn add_edit(&mut self, style: EditStyle, bounds: Bounds) -> Result<Id<kind::Edit>> {
        self.add(
            Control::Edit {
                multiline: style.multiline,
                digits_only: style.digits_only,
            },
            bounds,
        )
        .map(Id::new)
    }

    /// Add an empty drop-down list. `bounds` is the size of the closed list.
    pub fn add_drop_down(&mut self, bounds: Bounds) -> Result<Id<kind::DropDown>> {
        self.add(Control::DropDown, bounds).map(Id::new)
    }

    /// Add an empty list.
    pub fn add_list(&mut self, bounds: Bounds) -> Result<Id<kind::List>> {
        self.add(Control::List, bounds).map(Id::new)
    }

    /// Add a check box that is not checked.
    pub fn add_check(&mut self, text: &str, bounds: Bounds) -> Result<Id<kind::Check>> {
        self.add(Control::Check(text), bounds).map(Id::new)
    }

    /// Add a push button. A click gives [`Event::Clicked`].
    pub fn add_button(&mut self, text: &str, bounds: Bounds) -> Result<Id<kind::Button>> {
        self.add(Control::Button(text), bounds).map(Id::new)
    }

    /// Add a picker for where a window shows on the primary monitor: a small copy of the
    /// work area with the window in it. The user drags the window, clicks a place, or uses
    /// the arrow keys. `window_width` and `window_height` are the window size at 100% display
    /// scale. A change gives [`Event::Changed`].
    pub fn add_placement_picker(
        &mut self,
        window_width: i32,
        window_height: i32,
        bounds: Bounds,
    ) -> Result<Id<kind::PlacementPicker>> {
        self.add(
            Control::Placement {
                window_width,
                window_height,
            },
            bounds,
        )
        .map(Id::new)
    }

    fn control(&self, index: usize) -> HWND {
        self.controls[index]
    }

    fn kind(&self, index: usize) -> Kind {
        self.shared().kinds.borrow()[index]
    }

    fn send(&self, index: usize, msg: u32, wparam: usize, lparam: isize) -> isize {
        unsafe {
            SendMessageW(
                self.control(index),
                msg,
                Some(WPARAM(wparam)),
                Some(LPARAM(lparam)),
            )
            .0
        }
    }

    /// Run `change` without [`Event::Changed`] events.
    fn quietly<T>(&self, change: impl FnOnce() -> T) -> T {
        self.shared().muted.set(true);
        let result = change();
        self.shared().muted.set(false);
        result
    }

    /// The text of a label or an edit box.
    pub fn text<K: HasText>(&self, id: Id<K>) -> String {
        unsafe {
            let hwnd = self.control(id.index);
            let len = GetWindowTextLengthW(hwnd).max(0) as usize;
            let mut buf = vec![0u16; len + 1];
            let copied = GetWindowTextW(hwnd, &mut buf).max(0) as usize;
            String::from_utf16_lossy(&buf[..copied])
        }
    }

    /// Change the text of a label or an edit box. It does not give [`Event::Changed`].
    /// A multiline edit box needs `"\r\n"` between lines.
    pub fn set_text<K: HasText>(&self, id: Id<K>, text: &str) {
        self.quietly(|| unsafe {
            let _ = SetWindowTextW(self.control(id.index), &HSTRING::from(text));
        });
    }

    /// Replace all items of a list or a drop-down. Then no item is selected.
    pub fn set_items<K: HasItems, S: AsRef<str>>(&self, id: Id<K>, items: &[S]) {
        let (reset, add) = match self.kind(id.index) {
            Kind::DropDown => (CB_RESETCONTENT, CB_ADDSTRING),
            _ => (LB_RESETCONTENT, LB_ADDSTRING),
        };
        self.quietly(|| {
            self.send(id.index, reset, 0, 0);
            for item in items {
                let text = HSTRING::from(item.as_ref());
                self.send(id.index, add, 0, text.as_ptr() as isize);
            }
        });
    }

    /// Replace the text of one item of a list, and keep the selection.
    pub fn set_item(&self, id: Id<kind::List>, index: usize, text: &str) {
        let selected = self.selected(id);
        let text = HSTRING::from(text);
        self.quietly(|| {
            self.send(id.index, LB_DELETESTRING, index, 0);
            self.send(id.index, LB_INSERTSTRING, index, text.as_ptr() as isize);
        });
        self.set_selected(id, selected);
    }

    /// The selected item of a list or a drop-down, if one is selected.
    pub fn selected<K: HasItems>(&self, id: Id<K>) -> Option<usize> {
        let get = match self.kind(id.index) {
            Kind::DropDown => CB_GETCURSEL,
            _ => LB_GETCURSEL,
        };
        usize::try_from(self.send(id.index, get, 0, 0)).ok()
    }

    /// Select an item of a list or a drop-down, or none. It does not give [`Event::Changed`].
    pub fn set_selected<K: HasItems>(&self, id: Id<K>, index: Option<usize>) {
        let set = match self.kind(id.index) {
            Kind::DropDown => CB_SETCURSEL,
            _ => LB_SETCURSEL,
        };
        self.quietly(|| self.send(id.index, set, index.unwrap_or(usize::MAX), 0));
    }

    /// Whether a check box is checked.
    pub fn checked(&self, id: Id<kind::Check>) -> bool {
        self.shared().checked.borrow()[id.index]
    }

    /// Check or clear a check box. It does not give [`Event::Clicked`].
    pub fn set_checked(&self, id: Id<kind::Check>, checked: bool) {
        self.shared().checked.borrow_mut()[id.index] = checked;
        unsafe {
            let _ = InvalidateRect(Some(self.control(id.index)), None, false);
        }
    }

    /// The place that a placement picker shows.
    pub fn placement(&self, id: Id<kind::PlacementPicker>) -> Placement {
        self.shared().placements.borrow()[id.index]
    }

    /// Change the place that a placement picker shows. It does not give [`Event::Changed`].
    pub fn set_placement(&self, id: Id<kind::PlacementPicker>, placement: Placement) {
        self.shared().placements.borrow_mut()[id.index] = placement;
        unsafe {
            let _ = InvalidateRect(Some(self.control(id.index)), None, false);
        }
    }

    /// Let the user use a control, or show it in gray and ignore the user.
    pub fn set_enabled(&self, id: impl Into<AnyId>, enabled: bool) {
        unsafe {
            let _ = EnableWindow(self.control(id.into().0), enabled);
        }
    }

    /// Move the keyboard focus to a control.
    pub fn focus(&self, id: impl Into<AnyId>) {
        unsafe {
            let _ = SetFocus(Some(self.control(id.into().0)));
        }
    }

    /// Show a message that the user must close before the dialog continues.
    pub fn warn(&self, title: &str, message: &str) {
        unsafe {
            MessageBoxW(
                Some(self.window.hwnd()),
                &HSTRING::from(message),
                &HSTRING::from(title),
                MB_OK | MB_ICONWARNING,
            );
        }
    }

    /// The id that [`Event::Dialog`](crate::events::Event::Dialog) gives when this dialog has
    /// events.
    pub fn id(&self) -> DialogId {
        self.id
    }

    /// Show the dialog and return at once. When the user does something, the event loop gives
    /// [`Event::Dialog`](crate::events::Event::Dialog) with [`Dialog::id`]; then call
    /// [`Dialog::next_event`] until it gives `None`. The dialog stays until it is dropped, and
    /// other windows go on working while it shows.
    pub fn show(&mut self) {
        if self.shown {
            return;
        }
        self.shown = true;
        self.ui.add_dialog(self.window.hwnd());
        native::show_window(self.window.hwnd());
        if let Some(first) =
            (0..self.controls.len()).find(|&index| self.kind(index) != Kind::Static)
        {
            self.focus(AnyId(first));
        }
    }

    /// The next thing that the user did, or `None` when there is nothing more now.
    pub fn next_event(&mut self) -> Option<Event> {
        self.shared().events.borrow_mut().pop_front()
    }

    /// Show the dialog and give each event to `handler` until it returns [`Flow::Close`], with a
    /// message loop of its own. It is for an app without an
    /// [`EventLoop`](crate::events::EventLoop); with one, use [`Dialog::show`].
    pub fn run(&mut self, mut handler: impl FnMut(&mut Dialog, Event) -> Flow) -> Result<Outcome> {
        self.show();
        loop {
            while let Some(event) = self.next_event() {
                if handler(self, event) == Flow::Close {
                    return Ok(Outcome::Closed);
                }
            }
            if !self.ui.pump_wait()? {
                return Ok(Outcome::Quit);
            }
        }
    }
}

impl Drop for Dialog {
    fn drop(&mut self) {
        self.ui.remove_dialog(self.window.hwnd());
    }
}

fn scaled(value: i32, scale: f32) -> i32 {
    (value as f32 * scale).round() as i32
}

fn contains(outer: &RECT, inner: &RECT) -> bool {
    inner.left >= outer.left
        && inner.top >= outer.top
        && inner.right <= outer.right
        && inner.bottom <= outer.bottom
}

/// The popup font at 14 px, or Segoe UI if Windows has no other face.
fn ui_font(scale: f32, weight: i32) -> HFONT {
    let height = -(FONT_SIZE * scale).round() as i32;
    for face in FONT_FACES {
        let font = unsafe {
            CreateFontW(
                height,
                0,
                0,
                0,
                weight,
                0,
                0,
                0,
                DEFAULT_CHARSET,
                OUT_DEFAULT_PRECIS,
                CLIP_DEFAULT_PRECIS,
                CLEARTYPE_QUALITY,
                0,
                face,
            )
        };
        if font.is_invalid() || has_face(font, face) {
            return font;
        }
        unsafe {
            let _ = DeleteObject(HGDIOBJ(font.0));
        }
    }
    HFONT::default()
}

/// Windows gives a different font if it does not have the face that was asked for.
fn has_face(font: HFONT, face: PCWSTR) -> bool {
    unsafe {
        let hdc = GetDC(None);
        let old = SelectObject(hdc, HGDIOBJ(font.0));
        let mut buf = [0u16; 64];
        let len = GetTextFaceW(hdc, Some(&mut buf)).max(0) as usize;
        SelectObject(hdc, old);
        ReleaseDC(None, hdc);
        let wanted = face.as_wide();
        len > 0
            && buf[..len.min(buf.len())]
                .strip_suffix(&[0])
                .unwrap_or(&buf[..len])
                == wanted
    }
}

extern "system" fn wndproc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
    unsafe {
        let Some(shared) = state_of::<Shared>(hwnd) else {
            return DefWindowProcW(hwnd, msg, wparam, lparam);
        };
        match msg {
            WM_ERASEBKGND => {
                paint_background(shared, hwnd, HDC(wparam.0 as *mut c_void));
                LRESULT(1)
            }
            WM_CTLCOLORSTATIC | WM_CTLCOLOREDIT | WM_CTLCOLORLISTBOX => control_colors(
                shared,
                msg,
                HDC(wparam.0 as *mut c_void),
                HWND(lparam.0 as *mut c_void),
            ),
            WM_DRAWITEM => {
                let item = &*(lparam.0 as *const DRAWITEMSTRUCT);
                match item.CtlType {
                    ODT_BUTTON => paint_control(shared, item),
                    ODT_LISTBOX | ODT_COMBOBOX => paint_item(shared, item),
                    _ => {}
                }
                LRESULT(1)
            }
            WM_MEASUREITEM => {
                let item = &mut *(lparam.0 as *mut MEASUREITEMSTRUCT);
                let s = |v: i32| scaled(v, shared.look.scale);
                // The closed drop-down is the selection field and a 3-pixel frame on each side.
                item.itemHeight = if item.CtlType == ODT_COMBOBOX && item.itemID == u32::MAX {
                    (s(DROP_DOWN_HEIGHT) - 6).max(1) as u32
                } else {
                    s(ITEM_HEIGHT) as u32
                };
                LRESULT(1)
            }
            WM_LBUTTONDOWN => {
                // A click on the padding of a field gives the focus to the control in it.
                let (x, y) = native::point(lparam);
                let hit = shared.fields.borrow().iter().position(|f| {
                    f.is_some_and(|f| x >= f.left && x < f.right && y >= f.top && y < f.bottom)
                });
                if let Some(index) = hit
                    && let Ok(child) = GetDlgItem(Some(hwnd), (FIRST_ID + index) as i32)
                {
                    let _ = SetFocus(Some(child));
                }
                LRESULT(0)
            }
            WM_COMMAND => {
                let id = wparam.0 & 0xFFFF;
                let code = ((wparam.0 >> 16) & 0xFFFF) as u32;
                if id >= FIRST_ID {
                    let index = id - FIRST_ID;
                    let kind = shared.kinds.borrow().get(index).copied();
                    match (kind, code) {
                        (Some(Kind::Edit), EN_SETFOCUS | EN_KILLFOCUS)
                        | (Some(Kind::List), LBN_SETFOCUS | LBN_KILLFOCUS) => {
                            let field = shared.fields.borrow().get(index).copied().flatten();
                            if let Some(field) = field {
                                let _ = InvalidateRect(Some(hwnd), Some(&field), true);
                            }
                        }
                        (
                            Some(Kind::DropDown),
                            CBN_SETFOCUS | CBN_KILLFOCUS | CBN_DROPDOWN | CBN_CLOSEUP,
                        ) => {
                            let _ =
                                InvalidateRect(Some(HWND(lparam.0 as *mut c_void)), None, false);
                        }
                        _ => {}
                    }
                }
                let event = match id {
                    ID_OK => Some(Event::Submit),
                    ID_CANCEL => Some(Event::Close),
                    _ if id >= FIRST_ID => {
                        let index = id - FIRST_ID;
                        let kind = shared.kinds.borrow().get(index).copied();
                        let control = AnyId(index);
                        match (kind, code) {
                            (Some(Kind::Check), BN_CLICKED) => {
                                if let Some(checked) = shared.checked.borrow_mut().get_mut(index) {
                                    *checked = !*checked;
                                }
                                let _ = InvalidateRect(
                                    Some(HWND(lparam.0 as *mut c_void)),
                                    None,
                                    false,
                                );
                                Some(Event::Clicked(control))
                            }
                            (Some(Kind::Button), BN_CLICKED) => Some(Event::Clicked(control)),
                            (Some(Kind::Edit), EN_CHANGE)
                            | (Some(Kind::DropDown | Kind::List), SELCHANGE)
                                if !shared.muted.get() =>
                            {
                                Some(Event::Changed(control))
                            }
                            _ => None,
                        }
                    }
                    _ => None,
                };
                if let Some(event) = event {
                    shared.push(event);
                }
                LRESULT(0)
            }
            WM_CLOSE => {
                shared.push(Event::Close);
                LRESULT(0)
            }
            _ => DefWindowProcW(hwnd, msg, wparam, lparam),
        }
    }
}

fn inset(r: &RECT, dx: i32, dy: i32) -> RECT {
    RECT {
        left: r.left + dx,
        top: r.top + dy,
        right: (r.right - dx).max(r.left + dx),
        bottom: (r.bottom - dy).max(r.top + dy),
    }
}

/// The work area of the primary monitor, in pixels.
fn work_area() -> RECT {
    let mut work = RECT::default();
    let read = unsafe {
        SystemParametersInfoW(
            SPI_GETWORKAREA,
            0,
            Some(&mut work as *mut _ as *mut c_void),
            SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS(0),
        )
    };
    if read.is_err() || work.right <= work.left || work.bottom <= work.top {
        return RECT {
            left: 0,
            top: 0,
            right: 1920,
            bottom: 1080,
        };
    }
    work
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn snap_moves_near_values_to_edges_and_center() {
        assert_eq!(snap(0.02), 0.0);
        assert_eq!(snap(0.47), 0.5);
        assert_eq!(snap(0.97), 1.0);
        assert_eq!(snap(0.3), 0.3);
        assert_eq!(snap(0.55), 0.55);
    }
}