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
//! Direct2D / DirectWrite rasteriser for
//! [`crate::primitives::diff_view::DiffView`] (#737).
//!
//! Painting moved to the shared
//! [`crate::primitives::diff_view::native_surface_paint::paint`] (#866,
//! `PaintSurface` Phase 2d slice 9/9) — see that fn's doc for the two
//! named divergences (row/header text vertical alignment; header-label
//! ellipsize vs. hard-clip) found while unifying
//! `gtk::diff_view::draw_diff_view`, `macos::diff_view::draw_diff_view`
//! and `win::diff_view::draw_diff_view` into one implementation. The
//! deprecated `draw_diff_view` compatibility shim over the shared
//! [`super::surface::D2dSurface`] adapter was removed in issue #1109
//! (zero uses in coord-tui's `main` and vimcode's `develop`).
//!
//! Only compiled on `target_os = "windows"` — see `super::mod`'s
//! `#[cfg(target_os = "windows")] mod diff_view;` and `backend.rs`'s
//! module docs for why the rest of this repo's `--features win` compile
//! gate stays meaningful without a Windows host.
//!
//! `diff_view_layout` is **not** overridden on `WinBackend` — same as
//! every other backend. It ships a trait default (`Backend::diff_view_layout`)
//! that is a pure function of `line_height()` + `DiffView::mode`, which is
//! exactly what every backend's own `draw_diff_view` already resolves to
//! (see `tests/conformance/caps.rs`'s `#506` block comment, revisited for
//! #737 — the new shared [`DiffView::layout`] backs that default's
//! reasoning even more directly now, but the default itself was already
//! the honest answer and stays unoverridden here too).
#[cfg(test)]
use super::text::DWrite;
#[cfg(test)]
use crate::event::Rect;
#[cfg(test)]
use crate::primitives::diff_view::{DiffView, DiffViewLayout};
#[cfg(test)]
use crate::theme::Theme;
#[cfg(test)]
mod tests {
use super::*;
use crate::diff::compute_hunks;
use crate::primitives::diff_view::{DiffMode, DiffRowKind};
use crate::types::{Color, WidgetId};
use crate::win::testing::HeadlessSurface;
const W: f32 = 400.0;
const H: f32 = 200.0;
const LINE_HEIGHT: f32 = 16.0;
fn sample_view(mode: DiffMode) -> DiffView {
let left = "one\ntwo\nthree\n";
let right = "one\nTWO\nthree\n";
DiffView {
id: WidgetId::new("diff"),
left: left.into(),
right: right.into(),
left_label: None,
right_label: None,
hunks: compute_hunks(left, right),
mode,
editability: Default::default(),
scroll_offset: 0,
focused_pane: Default::default(),
has_focus: false,
}
}
fn first_row_of(view: &DiffView, kind: DiffRowKind) -> usize {
view.hunks
.iter()
.flat_map(|h| h.rows.iter())
.position(|r| r.kind == kind)
.expect("fixture should contain the requested row kind")
}
/// Paint `view` via the shared
/// [`crate::primitives::diff_view::native_surface_paint::paint`]
/// through a [`super::super::surface::D2dSurface`] over `surface`'s headless
/// target — the same adapter the now-removed `draw_diff_view` shim
/// used (issue #1109), exercised here directly.
fn paint(
surface: &HeadlessSurface,
dwrite: &DWrite,
rect: Rect,
view: &DiffView,
theme: &Theme,
line_height: f32,
) -> DiffViewLayout {
surface
.paint(|target| {
let mut raw = super::super::surface::D2dSurface {
target,
dwrite: Some(dwrite),
};
crate::primitives::diff_view::native_surface_paint::paint(
view,
&mut raw,
theme,
rect,
line_height,
);
})
.map(|_| view.layout(rect, line_height).as_layout())
.expect("paint diff view")
}
/// C0 smoke: `draw_diff_view` must actually paint text + a
/// scroll-clamp-usable layout rather than panicking or hitting a
/// `todo!()` (#737's acceptance bar — "draw_diff_view survives C0
/// with text_ok on win").
#[test]
fn draw_diff_view_paints_text_and_returns_layout() {
let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let theme = Theme {
background: Color::rgb(255, 255, 255),
..Theme::default()
};
let view = sample_view(DiffMode::SideBySide);
let rect = Rect::new(0.0, 0.0, W, H);
let layout = paint(&surface, &dwrite, rect, &view, &theme, LINE_HEIGHT);
assert!(layout.visible_rows > 0);
assert_eq!(layout.total_rows, view.total_rows());
// "text_ok" — some non-background pixel actually painted inside
// the left pane (proves DrawText ran, not just the background/row
// fills).
let mut painted_any = false;
for x in 0..(W as u32 / 2) {
for y in 0..(LINE_HEIGHT as u32 * 3) {
let px = surface.pixel_at(x, y);
if (px.r, px.g, px.b) != (255, 255, 255) {
painted_any = true;
}
}
}
assert!(painted_any, "expected diff_view to paint visible glyphs");
}
/// The divider colour and position are the same shared geometry every
/// backend paints from — pins the DIP position independently of the
/// text-glyph probe above.
#[test]
fn divider_paints_border_colour() {
let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let theme = Theme::default();
let view = sample_view(DiffMode::SideBySide);
let rect = Rect::new(0.0, 0.0, W, H);
paint(&surface, &dwrite, rect, &view, &theme, LINE_HEIGHT);
let changed = first_row_of(&view, DiffRowKind::Changed);
let geometry = view.layout(rect, LINE_HEIGHT);
let divider_x = geometry.panes.expect("side-by-side has panes").divider_x as u32;
let row_y = (LINE_HEIGHT * (changed as f32 + 0.5)) as u32;
let px = surface.pixel_at(divider_x, row_y);
assert_eq!(
(px.r, px.g, px.b),
(theme.border_fg.r, theme.border_fg.g, theme.border_fg.b),
"the pane divider should be painted in border_fg",
);
}
/// Non-zero-origin regression guard (LESSONS.md — the LOCAL/ABSOLUTE
/// mixup) — mirrors every other `win::` rasteriser's own nonzero-origin
/// test (see `win::pipeline_view::paint_and_click_round_trip_action_button_at_nonzero_origin`).
#[test]
fn paints_at_a_nonzero_origin_only() {
let origin_x = 40.0_f32;
let origin_y = 24.0_f32;
let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let theme = Theme::default();
let view = sample_view(DiffMode::SideBySide);
let rect = Rect::new(origin_x, origin_y, W - origin_x, H - origin_y);
// Pre-fill with a sentinel colour distinct from every theme colour
// this paints with, so "untouched" is unambiguous — mirrors
// `win::pipeline_view`/`win::list`'s own nonzero-origin tests and
// this module's own `zero_size_rect_is_a_no_op`. A raw
// `HeadlessSurface` starts black, which collides with real theme
// colours closely enough to make a false pass possible.
surface
.fill_rect(Rect::new(0.0, 0.0, W, H), Color::rgb(255, 255, 255))
.expect("fill sentinel background");
paint(&surface, &dwrite, rect, &view, &theme, LINE_HEIGHT);
let geometry = view.layout(rect, LINE_HEIGHT);
let panes = geometry.panes.expect("side-by-side has panes");
let div_x = panes.divider_x as u32;
let probe_y = (origin_y + LINE_HEIGHT * 0.5) as u32;
let px = surface.pixel_at(div_x, probe_y);
assert_eq!(
(px.r, px.g, px.b),
(theme.border_fg.r, theme.border_fg.g, theme.border_fg.b),
"divider must follow the requested origin",
);
// Untouched sentinel above and to the left.
let above = surface.pixel_at(div_x, origin_y as u32 - 4);
assert_eq!(
(above.r, above.g, above.b),
(255, 255, 255),
"nothing should paint above the requested origin",
);
let left = surface.pixel_at(4, probe_y);
assert_eq!(
(left.r, left.g, left.b),
(255, 255, 255),
"nothing should paint left of the requested origin",
);
}
/// Unified mode's `total_rows` counts hunk headers too — mirrors the
/// GTK/macOS/TUI twins' regression guard.
#[test]
fn unified_mode_counts_hunk_headers_in_total_rows() {
let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let theme = Theme::default();
let view = sample_view(DiffMode::Unified);
let rect = Rect::new(0.0, 0.0, W, H);
let layout = paint(&surface, &dwrite, rect, &view, &theme, LINE_HEIGHT);
let hunk_count = view.hunks.len();
assert!(hunk_count > 0, "fixture should produce at least one hunk");
assert_eq!(
layout.total_rows,
view.total_rows() + hunk_count,
"unified mode reports content rows plus one @@ header per hunk",
);
}
/// No-paint layout must agree with what `draw_diff_view` painted from
/// — same contract every other `win::` rasteriser's
/// `no_paint_layout_matches_paint_layout` test proves.
#[test]
fn no_paint_layout_matches_paint_layout() {
let view = sample_view(DiffMode::SideBySide);
let rect = Rect::new(0.0, 0.0, W, H);
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
let theme = Theme::default();
let painted = paint(&surface, &dwrite, rect, &view, &theme, LINE_HEIGHT);
let no_paint = view.layout(rect, LINE_HEIGHT).as_layout();
assert_eq!(painted, no_paint);
}
/// Zero-size rect is a no-op — mirrors every other `win::` rasteriser's
/// same guard.
#[test]
fn zero_size_rect_is_a_no_op() {
let surface = HeadlessSurface::new(W as u32, H as u32).expect("create surface");
let (dwrite, _, _) = DWrite::new("Segoe UI", 10.0, None).expect("create DWrite");
let theme = Theme {
background: Color::rgb(255, 255, 255),
..Theme::default()
};
let view = sample_view(DiffMode::SideBySide);
let rect = Rect::new(0.0, 0.0, 0.0, H);
surface
.fill_rect(Rect::new(0.0, 0.0, W, H), Color::rgb(255, 255, 255))
.expect("fill background");
paint(&surface, &dwrite, rect, &view, &theme, LINE_HEIGHT);
let px = surface.pixel_at(1, 1);
assert_eq!(
(px.r, px.g, px.b),
(255, 255, 255),
"a zero-width diff view should paint nothing at all",
);
}
}