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
//! Direct2D / DirectWrite rasteriser for [`crate::Switch`].
//!
//! Painting is delegated to the shared
//! [`crate::primitives::switch::native_surface_paint::paint`] function —
//! see that fn's doc for details. The `Backend`-trait entry point
//! [`super::backend::WinBackend::draw_switch`] calls the shared `paint`
//! directly with `self.current_theme`, ensuring the live theme is used
//! for all painting. This module carries only [`win_switch_layout`],
//! which provides backend-specific geometry calculations without
//! performing painting.
//!
//! Only compiled on `target_os = "windows"` — see `super::mod`'s
//! `#[cfg(target_os = "windows")] mod switch;` and `backend.rs`'s
//! module docs for why the rest of this repo's `--features win` compile
//! gate stays meaningful without a Windows host.
use crate::event::Rect;
use crate::primitives::switch::{Switch, SwitchLayout};
/// Compute a [`Switch`]'s layout without painting — the twin of
/// `crate::Backend::draw_switch`'s paint path. A `Switch` has no
/// internal sub-regions, so this maps straight through to
/// [`Switch::layout`], the same way `win_button_layout` wraps
/// [`crate::primitives::button::Button`]'s layout.
pub fn win_switch_layout(rect: Rect, switch: &Switch) -> SwitchLayout {
switch.layout(rect)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::event::{Key, NamedKey, Point};
use crate::primitives::switch::{SwitchEvent, SwitchHit};
use crate::theme::Theme;
use crate::types::{Modifiers, WidgetId};
use crate::win::testing::HeadlessSurface;
use crate::win::text::DWrite;
use windows::Win32::Graphics::Direct2D::Common::D2D1_COLOR_F;
const W: u32 = 40;
const H: u32 = 16;
fn mk_switch() -> Switch {
Switch::new(WidgetId::new("settings:wrap"))
}
/// Paint `switch` via the shared
/// [`crate::primitives::switch::native_surface_paint::paint`]
/// through a [`super::super::surface::D2dSurface`] over `surface`'s
/// headless target — the same adapter `WinBackend::draw_switch`
/// uses, exercised here directly.
fn paint(
surface: &HeadlessSurface,
dwrite: &DWrite,
rect: Rect,
switch: &Switch,
theme: &Theme,
) -> SwitchLayout {
surface
.paint(|target| {
// Clear to opaque black so anti-aliased edges blend
// with a known background, not undefined DIB content.
//
// SAFETY: `target` is the live render target
// `HeadlessSurface::paint` passes in, mid-`BeginDraw`/
// `EndDraw`; the `D2D1_COLOR_F` is a plain stack literal
// borrowed only for the duration of this call.
unsafe {
target.Clear(Some(&D2D1_COLOR_F {
r: 0.0,
g: 0.0,
b: 0.0,
a: 1.0,
}));
}
let mut raw = super::super::surface::D2dSurface {
target,
dwrite: Some(dwrite),
};
crate::primitives::switch::native_surface_paint::paint(
switch, &mut raw, theme, rect,
);
})
.map(|_| win_switch_layout(rect, switch))
.expect("paint switch")
}
#[test]
fn on_value_fills_accent_track() {
let surface = HeadlessSurface::new(W, H).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let mut switch = mk_switch();
switch.value = true;
let rect = Rect::new(0.0, 0.0, W as f32, H as f32);
let theme = Theme::default();
paint(&surface, &dwrite, rect, &switch, &theme);
// Probe the track's far (unoccupied-by-thumb) end: "on" parks the
// thumb at the right edge, so the left edge still shows the
// track's own fill colour.
let px = surface.pixel_at(2, H / 2);
assert_eq!(
(px.r, px.g, px.b),
(theme.accent_bg.r, theme.accent_bg.g, theme.accent_bg.b)
);
}
#[test]
fn off_value_fills_surface_track() {
let surface = HeadlessSurface::new(W, H).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let switch = mk_switch();
let rect = Rect::new(0.0, 0.0, W as f32, H as f32);
let theme = Theme::default();
paint(&surface, &dwrite, rect, &switch, &theme);
// "off" parks the thumb at the left edge, so probe the right end
// for the track's own fill colour. Use H/4 inset from the right
// edge to stay clear of the anti-aliased rounded cap and border
// stroke.
let px = surface.pixel_at(W - H / 4, H / 2);
assert_eq!(
(px.r, px.g, px.b),
(theme.surface_bg.r, theme.surface_bg.g, theme.surface_bg.b)
);
}
#[test]
fn disabled_switch_fills_surface_track_regardless_of_value() {
let surface = HeadlessSurface::new(W, H).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let mut switch = mk_switch();
switch.value = true;
switch.disabled = true;
let rect = Rect::new(0.0, 0.0, W as f32, H as f32);
let theme = Theme::default();
paint(&surface, &dwrite, rect, &switch, &theme);
let px = surface.pixel_at(2, H / 2);
assert_eq!(
(px.r, px.g, px.b),
(theme.surface_bg.r, theme.surface_bg.g, theme.surface_bg.b)
);
}
/// Regression guard at a non-zero origin — layout helpers must
/// return coords in the same frame across backends.
fn hit_test_round_trip_at(x: f32, y: f32) {
let rect = Rect::new(x, y, 20.0, 10.0);
let layout = win_switch_layout(rect, &mk_switch());
let inside = layout.hit_test(Point::new(x + 2.0, y + 2.0));
assert_eq!(inside, SwitchHit::Body);
let outside = layout.hit_test(Point::new(x + 400.0, y));
assert_eq!(outside, SwitchHit::Outside);
}
#[test]
fn hit_test_round_trip() {
hit_test_round_trip_at(0.0, 0.0);
}
#[test]
fn hit_test_round_trip_at_nonzero_origin() {
hit_test_round_trip_at(6.0, 11.0);
}
#[test]
fn click_toggles_after_painting() {
let surface = HeadlessSurface::new(W, H).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let switch = mk_switch();
let rect = Rect::new(0.0, 0.0, W as f32, H as f32);
let theme = Theme::default();
let layout = paint(&surface, &dwrite, rect, &switch, &theme);
assert_eq!(layout.hit_test(Point::new(2.0, 2.0)), SwitchHit::Body);
assert_eq!(
switch.click(),
Some(SwitchEvent::Toggled {
id: switch.id,
value: true
})
);
}
#[test]
fn keyboard_activation_requires_focus() {
let mut switch = mk_switch();
switch.focused = true;
let event = switch.handle_key(&Key::Named(NamedKey::Enter), &Modifiers::default());
assert_eq!(
event,
Some(SwitchEvent::Toggled {
id: switch.id,
value: true
})
);
}
/// `win_switch_layout` must produce byte-identical layout to the
/// painted result.
#[test]
fn no_paint_layout_matches_paint_layout() {
let switch = mk_switch();
let rect = Rect::new(0.0, 0.0, W as f32, H as f32);
let surface = HeadlessSurface::new(W, H).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let theme = Theme::default();
let painted = paint(&surface, &dwrite, rect, &switch, &theme);
let no_paint = win_switch_layout(rect, &switch);
assert_eq!(painted, no_paint);
}
}