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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! Font dialog widget.
use crate::core::{Color, Font, HorizontalAlignment, Rect, Size};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::{GenericSignal, Signal1};
use crate::tr;
use crate::impl_widget_property_hooks;
use crate::property_names_of;
use crate::widget::capability::coercion::expect_bool;
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
/// Font selection dialog.
///
/// Holds the font the user is choosing and signals the outcome. It does not
/// enumerate or preview installed fonts — the caller supplies candidate fonts
/// through [`FontDialog::set_current_font`].
pub struct FontDialog {
base: BaseWidget,
current_font: Font,
modal: bool,
/// Emitted with the new font on every change, including while the user is
/// still browsing, and again from [`FontDialog::accept`]. A slot that reads
/// it as "confirmed" will fire on unconfirmed selections; use `accepted` for
/// the commit point.
pub font_selected: Signal1<Font>,
/// Emitted by [`FontDialog::accept`], after `font_selected`. Carries no
/// payload.
pub accepted: GenericSignal,
/// Emitted by [`FontDialog::reject`]. The current font is **not** restored
/// to its pre-dialog value, so a caller that needs cancel semantics must
/// snapshot the original font itself.
pub rejected: GenericSignal,
}
impl FontDialog {
/// Creates a dialog with the default font and modality on.
///
/// `geometry` is in parent-relative logical pixels; the size hint is
/// 400x300.
pub fn new(geometry: Rect) -> Self {
Self {
base: BaseWidget::new(WidgetKind::FontDialog, geometry, "FontDialog"),
current_font: Font::default(),
modal: true,
font_selected: Signal1::new(),
accepted: GenericSignal::new(),
rejected: GenericSignal::new(),
}
}
/// Returns the font currently selected in the dialog.
pub fn current_font(&self) -> &Font {
&self.current_font
}
/// Replaces the selected font and emits `font_selected`.
///
/// Always signals and always requests a redraw, even for an unchanged
/// value, so a slot that calls back into this setter will recurse.
pub fn set_current_font(&mut self, font: Font) {
self.current_font = font.clone();
self.font_selected.emit(font);
self.base.request_redraw();
}
/// Confirms the dialog: emits `font_selected` with the current font, then
/// `accepted`, then hides.
///
/// Note the current font is reported twice in total — once from the
/// preceding `set_current_font` calls and once here — so a slot connected
/// to `font_selected` will see a duplicate for the final value.
pub fn accept(&mut self) {
self.font_selected.emit(self.current_font.clone());
self.accepted.emit();
self.hide();
}
/// Cancels the dialog: emits `rejected` and hides. The selected font is
/// left as-is; see [`FontDialog::rejected`].
pub fn reject(&mut self) {
self.rejected.emit();
self.hide();
}
/// Returns a copy of the selected font. Equivalent to cloning
/// [`FontDialog::current_font`]; kept for callers using the
/// `get_font`/`set_current_font` pairing.
pub fn get_font(&self) -> Font {
self.current_font.clone()
}
/// Returns whether the dialog is modal. Defaults to `true`.
///
/// Records the intent; enforcement is the modal stack in
/// [`crate::widget::runtime`] (`enter_modal` / `exit_modal`).
pub fn is_modal(&self) -> bool {
self.modal
}
/// Sets the modality intent and requests a redraw. See
/// [`FontDialog::is_modal`].
pub fn set_modal(&mut self, modal: bool) {
self.modal = modal;
self.base.request_redraw();
}
}
impl Widget for FontDialog {
fn base(&self) -> &BaseWidget {
&self.base
}
fn base_mut(&mut self) -> &mut BaseWidget {
&mut self.base
}
fn size_hint(&self) -> Size {
crate::core::Size::new(400, 300)
}
/// Reports this widget as the object that paints it.
///
/// `FontDialog` implements `Draw`, so `Some(self)` is total and cannot be
/// wrong.
fn as_draw_mut(&mut self) -> Option<&mut dyn crate::widget::Draw> {
Some(self)
}
impl_widget_property_hooks!();
}
/// `FontDialog`'s property contract.
///
/// Read semantics are carried over unchanged from the centralised
/// `access_read_dialog.in.rs` dispatch. `modal` is read-only: the old write layer
/// had no arm for this kind.
impl WidgetProperties for FontDialog {
fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
match name {
"modal" => Ok(CapabilityValue::Bool(self.is_modal())),
_ => base_property_get(self, name),
}
}
fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
match name {
"modal" => {
self.set_modal(expect_bool(value)?);
Ok(())
}
_ => base_property_set(self, name, value),
}
}
fn property_names(&self) -> &'static [&'static str] {
property_names_of!["modal", BASE_PROPERTY_NAMES]
}
/// Runs one of the commands `font_dialog` publishes.
///
/// `accept` and `reject` are the dialog's own commit/cancel actions and map
/// onto its real `accept` / `reject`; neither takes a payload. `set_current_font`
/// needs a font, so a bare invocation is reported as needing one rather than
/// being called unknown.
fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
match name {
"accept" => {
self.accept();
Ok(())
}
"reject" => {
self.reject();
Ok(())
}
"set_current_font" => Err(CapabilityAccessError::OutOfRange),
_ => Err(CapabilityAccessError::UnknownCommand),
}
}
}
impl EventHandler for FontDialog {
fn handle_event(&mut self, event: &Event) {
self.base.handle_event(event);
if !self.base.is_enabled() {
return;
}
if let Event::KeyPress { key, .. } = event {
if *key == 13 {
self.accept();
} else if *key == 27 {
self.reject();
}
}
}
}
impl Draw for FontDialog {
fn draw(&mut self, context: &mut RenderContext) {
let rect = self.geometry();
let style = self.style().clone();
// Chrome colours resolve explicit style first, then the theme's resolved style
// for this control, and only then fall back to a literal. The style step alone
// was not enough: `WidgetStyle` carries no title-bar or accent field, so the two
// most visible pixels of the dialog — the title band and the accept button —
// stayed a hardcoded blue in either appearance, and the render census reported
// the whole control as theme-blind.
//
// The theme read is a separate manager lock, taken and released inside
// `resolved_theme_style`, so it is not held across the draw — the global
// manager's mutex is not re-entrant.
let theme = crate::style::resolved_theme_style("font_dialog");
// The window fill and the accent are read as their own lock acquisition and copied
// out as values, so the guard is dropped before anything else touches the theme —
// the global manager's mutex is not re-entrant.
let (window_fill, accent) = {
let manager = crate::style::theme_manager();
match manager.current_theme() {
Some(active) => (active.colors.background, active.colors.primary),
None => (Color::WHITE, Color::rgb(0, 120, 215)),
}
};
let accent_ink = accent.contrast_color();
// A dialog is a `Surface`-role control and `Surface` resolves to the window's own
// fill, which would leave the frame invisible against the window. A resolved
// surface equal to the window fill is therefore re-derived one step toward the
// ink, the same distinction `Colors::input_background` draws for a field.
let ink = style
.text_color
.or_else(|| theme.as_ref().and_then(|t| t.text_color))
.unwrap_or(Color::BLACK);
let surface = match style
.background_color
.or_else(|| theme.as_ref().and_then(|t| t.background_color))
{
Some(resolved) if resolved != window_fill => resolved,
_ => window_fill.blend(&ink, 0.06),
};
let border = style
.border_color
.or_else(|| theme.as_ref().and_then(|t| t.border_color))
.unwrap_or(Color::rgb(160, 160, 160));
// The three list columns and the preview are field interiors: one step *away* from
// the dialog surface, so on a light theme darker and on a dark one lighter rather
// than the forced white both used to be.
let field = if surface.is_dark() {
surface.blend(&Color::WHITE, 0.08)
} else {
surface.blend(&Color::BLACK, 0.06)
};
context.fill_rect(Rect::new(rect.x, rect.y, rect.width, rect.height), surface);
context.draw_rect(Rect::new(rect.x, rect.y, rect.width, rect.height), border);
// Title bar: a separate region from the dialog surface, in the theme's accent
// rather than the literal blue it carried before. The label is fitted to the bar,
// so a truncating locale cannot run the title past the frame.
const TITLE_BAR_HEIGHT: u32 = 28;
context.fill_rect(Rect::new(rect.x, rect.y, rect.width, TITLE_BAR_HEIGHT), accent);
let title_font = Font::default();
let title_label = tr!("dialog.font.select_font");
let title_metrics = context.measure_text(&title_label, &title_font);
context.draw_text_fitted(
Rect::new(
rect.x + 8,
rect.y + ((TITLE_BAR_HEIGHT as i32 - title_metrics.height as i32) / 2).max(0),
rect.width.saturating_sub(16),
title_metrics.height.max(1),
),
&title_label,
&title_font,
accent_ink,
HorizontalAlignment::Left,
);
// The columns share the space left after the button row is reserved, so a third
// of an already-short dialog cannot reach below the buttons. `col_w` is derived
// from that clamped height rather than from the dialog's own, which is what made
// the last column's label leave the frame.
let btn_h = 28i32;
let button_top = (rect.y + rect.height as i32 - btn_h - 12).max(rect.y);
let col_area = (button_top - 46 - (rect.y + 38)).max(0) as u32;
let col_w = (rect.width / 3).saturating_sub(6);
let list_y = rect.y + 38;
let list_h = col_area.saturating_sub(28);
// Header labels live in a 12 px strip above the columns. That strip is narrower
// than a 14 px font's line box on purpose: the label's top edge is flush with the
// column's own top edge, and `draw_text_fitted` clamps to the box's left/right
// edges, which is the dimension that overflowed.
let header_h = 12u32;
// Family, Style, Size columns
let col_labels =
[tr!("dialog.font.font_family"), tr!("dialog.font.style"), tr!("dialog.font.size")];
for (i, label) in col_labels.iter().enumerate() {
let col_x = rect.x as f32 + 4.0 + i as f32 * (col_w as f32 + 4.0);
context.draw_text_fitted(
Rect::new(col_x as i32, list_y - 10, col_w, header_h),
label.as_str(),
&Font::default(),
ink,
HorizontalAlignment::Left,
);
context.fill_rect(Rect::new(col_x as i32, list_y, col_w, list_h), field);
context.draw_rect(Rect::new(col_x as i32, list_y, col_w, list_h), border);
}
// Preview area
let prev_y = list_y + list_h as i32 + 8;
let bw = rect.width.saturating_sub(8);
context.fill_rect(Rect::new(rect.x + 4, prev_y, bw, 36), field);
context.draw_rect(Rect::new(rect.x + 4, prev_y, bw, 36), border);
// The sample is fitted to the preview well, so a caller-selected font larger than
// the well is truncated there instead of marching out of the dialog.
let preview_font = self.current_font.clone();
let preview_metrics = context.measure_text("AaBbYyZz 0123", &preview_font);
context.draw_text_fitted(
Rect::new(
rect.x + 10,
prev_y + ((36 - preview_metrics.height as i32) / 2).max(0),
rect.width.saturating_sub(20),
preview_metrics.height.max(1),
),
"AaBbYyZz 0123",
&preview_font,
ink,
HorizontalAlignment::Left,
);
// OK/Cancel. Right-aligned inside the frame and floored at its left edge, so a
// control narrower than the two 80 px buttons keeps them on screen; the labels are
// centred in their buttons and fitted to them.
let btn_y = button_top;
const BTN_W: i32 = 80;
const BTN_STEP: i32 = 88;
let cancel_x = (rect.x + rect.width as i32 - BTN_STEP).max(rect.x);
let ok_x = (cancel_x - BTN_STEP).max(rect.x);
let ok_rect = Rect::new(ok_x, btn_y, BTN_W as u32, btn_h as u32);
context.fill_rect(ok_rect, accent);
context.draw_text_fitted(
ok_rect,
&tr!("dialog.ok"),
&Font::default(),
accent_ink,
HorizontalAlignment::Center,
);
let cancel_rect = Rect::new(cancel_x, btn_y, BTN_W as u32, btn_h as u32);
context.fill_rect(cancel_rect, surface.blend(&ink, 0.1));
context.draw_rect(cancel_rect, border);
context.draw_text_fitted(
cancel_rect,
&tr!("dialog.cancel"),
&Font::default(),
ink,
HorizontalAlignment::Center,
);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::event::Event;
use std::sync::{Arc, Mutex};
#[test]
fn set_current_font_emits_font_selected() {
let mut dialog = FontDialog::new(Rect::new(0, 0, 420, 320));
let seen = Arc::new(Mutex::new(Font::default()));
let seen_clone = Arc::clone(&seen);
dialog.font_selected.connect(move |font| {
if let Ok(mut f) = seen_clone.lock() {
*f = (*font).clone();
}
});
let target = Font::simple("Sans", 18.0);
dialog.set_current_font(target.clone());
assert_eq!(dialog.current_font(), &target);
assert_eq!(*seen.lock().expect("seen lock"), target);
}
#[test]
fn enter_accepts_and_escape_rejects() {
let mut dialog = FontDialog::new(Rect::new(0, 0, 420, 320));
let accepted = Arc::new(Mutex::new(0usize));
let rejected = Arc::new(Mutex::new(0usize));
let a = Arc::clone(&accepted);
dialog.accepted.connect(move || {
if let Ok(mut n) = a.lock() {
*n += 1;
}
});
let r = Arc::clone(&rejected);
dialog.rejected.connect(move || {
if let Ok(mut n) = r.lock() {
*n += 1;
}
});
dialog.show();
dialog.handle_event(&Event::key_press(13, 0));
assert_eq!(*accepted.lock().expect("accepted lock"), 1);
assert!(!dialog.is_visible());
dialog.show();
dialog.handle_event(&Event::key_press(27, 0));
assert_eq!(*rejected.lock().expect("rejected lock"), 1);
assert!(!dialog.is_visible());
}
}