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
//! Direct2D rasteriser for [`crate::primitives::split_tree::SplitTree`]
//!
//! This module carries [`win_split_tree_layout`]; painting is handled by the shared
//! [`crate::primitives::split_tree::native_surface_paint::paint`]. Geometry is not
//! re-derived: [`win_split_tree_layout`] delegates to
//! [`crate::primitives::layout_metrics::pixel_split_tree_layout`], which uses the
//! same divider thickness as `super::split`, so a `SplitTree` and a plain `Split`
//! line up across backends.
//!
//! Only compiled on `target_os = "windows"` — see `super::mod`'s
//! `#[cfg(target_os = "windows")] mod split_tree;` and `backend.rs`'s
//! module docs for why the rest of this repo's `--features win` compile
//! gate stays meaningful without a Windows host.
//!
//! # Theme
//!
//! `WinBackend::draw_split_tree` passes `&self.current_theme` to the
//! shared [`crate::primitives::split_tree::native_surface_paint::paint`]
//! — the same live theme `Backend::set_theme` writes, the same shape
//! `win::status_bar`'s module doc documents.
#[cfg(test)]
use windows::Win32::Graphics::Direct2D::ID2D1RenderTarget;
use crate::event::Rect;
use crate::primitives::layout_metrics::pixel_split_tree_layout;
use crate::primitives::split_tree::{SplitTree, SplitTreeLayout};
#[cfg(test)]
use crate::theme::Theme;
/// Compute a [`SplitTree`]'s layout without painting — the twin of
/// `crate::Backend::draw_split_tree` (the removed `draw_split_tree`
/// free function shim this backed — see module doc). Both call
/// [`SplitTree::layout`] with the identical divider thickness, so a
/// no-paint hit-test call always agrees with what the last paint drew.
pub fn win_split_tree_layout(rect: Rect, tree: &SplitTree) -> SplitTreeLayout {
pixel_split_tree_layout(tree, rect)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::primitives::split_tree::SplitDirection;
use crate::types::WidgetId;
use crate::win::testing::HeadlessSurface;
const W: u32 = 200;
const H: u32 = 100;
fn wid(s: &str) -> WidgetId {
WidgetId::new(s)
}
/// Paint `tree` via the shared
/// [`crate::primitives::split_tree::native_surface_paint::paint`]
/// through a [`super::super::surface::D2dSurface`] over `target`.
fn paint(target: &ID2D1RenderTarget, layout: &SplitTreeLayout) {
let mut raw = super::super::surface::D2dSurface {
target,
dwrite: None,
};
crate::primitives::split_tree::native_surface_paint::paint(
layout,
&mut raw,
&Theme::default(),
);
}
fn two_pane() -> SplitTree {
SplitTree::split(
SplitDirection::Horizontal,
0.5,
SplitTree::leaf(wid("a")),
SplitTree::leaf(wid("b")),
)
}
fn nested() -> SplitTree {
SplitTree::split(
SplitDirection::Horizontal,
0.5,
SplitTree::split(
SplitDirection::Vertical,
0.5,
SplitTree::leaf(wid("a")),
SplitTree::leaf(wid("c")),
),
SplitTree::leaf(wid("b")),
)
}
/// Paint↔click round trip: the divider's painted bg and the
/// layout's own `hit_test_divider`/`hit_test_leaf` over that same
/// bounds must agree.
#[test]
fn paint_and_hit_test_round_trip() {
let surface = HeadlessSurface::new(W, H).expect("create surface");
let tree = two_pane();
let rect = Rect::new(0.0, 0.0, W as f32, H as f32);
let layout = win_split_tree_layout(rect, &tree);
surface
.paint(|target| {
paint(target, &layout);
})
.expect("paint split tree");
let theme = Theme::default();
let d = &layout.dividers[0];
let cx = (d.position + d.thickness / 2.0) as u32;
let cy = (d.cross_start + d.cross_size / 2.0) as u32;
let div_px = surface.pixel_at(cx, cy);
assert_eq!(
(div_px.r, div_px.g, div_px.b),
(theme.separator.r, theme.separator.g, theme.separator.b)
);
use crate::event::Point;
assert_eq!(
layout.hit_test_divider(
Point {
x: d.position + 1.0,
y: cy as f32
},
1.0
),
Some(d.split_index)
);
assert_eq!(
layout.hit_test_leaf(Point {
x: 1.0,
y: H as f32 / 2.0
}),
Some(&wid("a"))
);
assert_eq!(
layout.hit_test_leaf(Point {
x: W as f32 - 1.0,
y: H as f32 / 2.0
}),
Some(&wid("b"))
);
}
/// `win_split_tree_layout` must produce byte-identical layout to what the
/// paint function would produce.
#[test]
fn no_paint_layout_matches_paint_layout() {
let tree = two_pane();
let rect = Rect::new(0.0, 0.0, W as f32, H as f32);
let painted = win_split_tree_layout(rect, &tree);
let surface = HeadlessSurface::new(W, H).expect("create surface");
surface
.paint(|target| {
paint(target, &painted);
})
.expect("paint");
let no_paint = win_split_tree_layout(rect, &tree);
assert_eq!(painted, no_paint);
}
#[test]
fn nested_tree_produces_all_dividers() {
let tree = nested();
let rect = Rect::new(0.0, 0.0, 400.0, 300.0);
let layout = win_split_tree_layout(rect, &tree);
assert_eq!(layout.leaves.len(), 3);
assert_eq!(layout.dividers.len(), 2);
assert_eq!(layout.dividers[0].split_index, 0);
assert_eq!(layout.dividers[1].split_index, 1);
}
#[test]
fn divider_thickness_matches_the_plain_split_rasteriser() {
let layout = win_split_tree_layout(Rect::new(0.0, 0.0, 100.0, 50.0), &two_pane());
assert_eq!(
layout.dividers[0].thickness,
crate::primitives::layout_metrics::pixel::DIVIDER
);
// available = 96, 0.5 * 96 = 48 — identical to the GTK/macOS twins.
assert!((layout.dividers[0].position - 48.0).abs() < 0.001);
}
/// `split_tree_layout` is documented **ABSOLUTE**: leaf
/// bounds are shifted by the origin.
#[test]
fn non_zero_origin_shifts_leaves() {
let tree = two_pane();
let layout = win_split_tree_layout(Rect::new(7.0, 13.0, 100.0, 40.0), &tree);
assert_eq!(layout.leaves[0].1.x, 7.0);
assert_eq!(layout.leaves[0].1.y, 13.0);
}
}