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
// Copyright (c) 2026 John Law
// SPDX-License-Identifier: MIT
//! Native Windows UI with a glass look: a tray icon and menu, a frosted-glass popup, themed
//! dialogs, and helpers for tray apps.
//!
//! - [`events`]: [`EventLoop`], the main loop of the app. It sleeps until a menu command, the
//! end of a popup, a change in a dialog, a deadline, or an unlock of the session.
//! - [`tray`]: an icon in the notification area with a menu.
//! - [`popup`]: a frosted-glass popup with a question and choice buttons. It fades in without
//! taking the keyboard focus, can show whether a choice was correct, and can dismiss itself.
//! - [`dialog`]: a small modal window made of standard controls, in the same colors.
//! - [`layout`]: rows and columns that place the controls of a dialog.
//! - [`placement`]: where a window shows, as fractions of the free space of the screen.
//! - [`autostart`], [`presence`], [`process`], [`single_instance`], and [`show_error`]: small
//! helpers that tray apps often need.
//!
//! All windows follow the Windows light or dark app mode and the display scale.
//!
//! # Threads
//!
//! Make one [`Ui`] on the thread that shows the windows, and make all windows on that thread.
//! [`Ui`] is not `Send`, so the compiler stops a window from going to another thread. The
//! thread must run [`EventLoop::next`], which handles the window messages of all windows of
//! the thread. An app without a tray can use [`ChoicePopup::ask`] and [`Dialog::run`]
//! instead: each of them has a message loop of its own and returns when its window ends.
//!
//! # Example
//!
//! A tray app that asks a question each minute and on "Ask now". The same program is in
//! `examples/quiz.rs`.
//!
//! ```no_run
//! use std::time::{Duration, SystemTime};
//!
//! use glasskit::Ui;
//! use glasskit::events::{Event, EventLoop};
//! use glasskit::placement::Placement;
//! use glasskit::popup::{ChoicePopup, Mark, Prompt};
//! use glasskit::tray::{Icon, MenuItem, Tray};
//!
//! #[derive(Debug, Clone, PartialEq, Eq)]
//! enum Command {
//! AskNow,
//! }
//!
//! fn main() -> glasskit::Result<()> {
//! let ui = Ui::new()?;
//! let mut events = EventLoop::new(&ui)?;
//! let size = Icon::tray_size();
//! let icon = Icon::from_argb(size, vec![0xFF2E_86DE; (size * size) as usize])?;
//! let mut tray = Tray::new(&mut events, "Quiz", &icon)?;
//! tray.set_menu(&[
//! MenuItem::command(Command::AskNow, "Ask now"),
//! MenuItem::Separator,
//! MenuItem::Quit("Quit".into()),
//! ]);
//! let mut popup = ChoicePopup::new(&ui, Placement::default())?;
//! let mut next = SystemTime::now() + Duration::from_secs(60);
//! loop {
//! match events.next(Some(next))? {
//! Event::Quit => return Ok(()),
//! Event::Deadline | Event::Command(Command::AskNow) => {
//! next = SystemTime::now() + Duration::from_secs(60);
//! if !popup.is_showing() {
//! let prompt = Prompt::new("What is 2 + 2?", ["3", "4", "5"])
//! .marks(|clicked| answer_marks(clicked, 1))
//! .auto_dismiss(Some(Duration::from_secs(30)));
//! popup.show(&prompt)?;
//! }
//! }
//! Event::Popup(id) if id == popup.id() => {
//! if let Some(reply) = popup.take_reply() {
//! println!("{:?}", reply.response);
//! }
//! }
//! _ => {}
//! }
//! }
//! }
//!
//! /// Mark the correct answer good and a wrong click bad.
//! fn answer_marks(clicked: usize, correct: usize) -> Vec<(usize, Mark)> {
//! let mut marks = vec![(correct, Mark::Good)];
//! if clicked != correct {
//! marks.push((clicked, Mark::Bad));
//! }
//! marks
//! }
//! ```
//!
//! [`EventLoop`]: events::EventLoop
//! [`EventLoop::next`]: events::EventLoop::next
//! [`ChoicePopup::ask`]: popup::ChoicePopup::ask
//! [`Dialog::run`]: dialog::Dialog::run
compile_error!;
/// Compiles the example of the README as a doc test.
;
pub use ;
pub use Ui;
use crateCall;
use ;
use ;
use CreateMutexW;
use UI;
use UI;
use HSTRING;
/// Register a window class. A class stays registered until the process ends, so a second
/// registration of the same class is not an error.
pub
/// The system DPI stays at its sign-in value. Read the DPI of the monitor each time a window
/// opens, so a change of the display scale applies to the next window.
pub
/// Show an error in a message box, and wait until the user closes it.
/// It needs no [`Ui`], so it also works before the setup or after a failed setup.
/// Proof that this process is the only one with a name in the user session.
/// The name is free again when the guard is dropped or the process ends.
;
/// Take `name` for this process in the user session. It gives `None` if another process
/// has the name, for example a second start of the same app.