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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! Divider/Separator widget — a horizontal or vertical line separator.
//!
//! The Divider widget draws a thin line (horizontal or vertical) centered within
//! its bounding rectangle. It is used to visually separate groups of content,
//! similar to `<hr>` in HTML or `JSeparator` in Swing.
use crate::core::{Color, Rect};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::widget::capability::coercion::{expect_f32, expect_string};
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::display_widgets::draw_line;
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};
/// Divider/Separator widget for visually separating content sections.
///
/// Supports both horizontal and vertical orientations with configurable
/// thickness and color.
pub struct Divider {
base: BaseWidget,
vertical: bool,
thickness: u32,
color: Color,
/// Whether the colour was chosen by the caller through [`Divider::set_color`].
///
/// The caller's choice must win over the active theme, but the constructor's
/// own neutral grey must not: without this flag a theme switch could never
/// reach a divider that was never explicitly coloured, which is exactly the
/// hardcoded-chrome defect the rendering census reports.
color_is_explicit: bool,
}
impl Divider {
/// Creates a new horizontal Divider with default styling (1px thick, gray).
pub fn new(geometry: Rect) -> Self {
Self {
base: BaseWidget::new(WidgetKind::Divider, geometry, "Divider"),
vertical: false,
thickness: 1,
color: Color::rgba(180, 180, 180, 200),
color_is_explicit: false,
}
}
/// Sets whether the divider is vertical (true) or horizontal (false).
pub fn set_vertical(&mut self, vertical: bool) {
self.vertical = vertical;
self.base.request_redraw();
}
/// Returns whether the divider is oriented vertically.
pub fn is_vertical(&self) -> bool {
self.vertical
}
/// Returns the current line thickness in pixels.
pub fn thickness(&self) -> u32 {
self.thickness
}
/// Sets the line thickness in pixels.
pub fn set_thickness(&mut self, thickness: u32) {
self.thickness = thickness;
self.base.request_redraw();
}
/// Sets the divider line color.
///
/// An explicit colour wins over the active theme, so this overrides the
/// theme-resolved line colour until it is set again.
pub fn set_color(&mut self, color: Color) {
self.color = color;
self.color_is_explicit = true;
self.base.request_redraw();
}
/// The colour the line is drawn in.
///
/// Precedence is the caller's explicit colour, then the theme's resolved
/// border colour for this control, then the constructor's neutral grey. The
/// theme step is what makes a theme switch visible: before it, the line kept
/// its literal grey in both appearances.
fn line_color(&self) -> Color {
// The theme read is a separate manager lock, taken and released inside
// `resolved_theme_style`, so it is not held across the draw or across
// another accessor — the global manager's mutex is not re-entrant.
if self.color_is_explicit {
return self.color;
}
crate::style::resolved_theme_style("divider")
.and_then(|style| style.border_color)
// # Why a divider reads `outline_variant`
//
// A divider is the *weakest* separator a theme has: it structures content and must
// recede behind it. `outline` is the stronger role — the one a focus ring and a control
// border read — and `Colors::outline`'s own documentation separates the two for exactly
// this reason. Before this, a divider fell back to whatever `border_color` the theme
// resolved for it, which on several presets is the *same* colour a focused control
// draws its ring in: the two separated quantities had one value.
.or_else(|| {
crate::style::theme_manager()
.current_theme()
.map(|active| active.colors.outline_variant)
})
.unwrap_or(self.color)
}
}
impl Widget for Divider {
fn base(&self) -> &BaseWidget {
&self.base
}
fn set_state_theme_hook(&mut self) {
crate::style::reapply_active_theme_state(self);
}
fn base_mut(&mut self) -> &mut BaseWidget {
&mut self.base
}
fn size_hint(&self) -> crate::core::Size {
crate::core::Size::new(100, 2)
}
impl_draw_bridge!();
impl_widget_property_hooks!();
}
/// `Divider`'s property contract.
///
/// Read/write semantics are carried over unchanged from the centralised
/// `access_read_other.in.rs` / `access_write_other.in.rs` dispatch, so callers see
/// the same coercions and the same errors as before. `orientation` is published as
/// the `horizontal` / `vertical` token pair and `thickness` as the `Float` the
/// schema declares, even though the widget stores it as a pixel count.
impl WidgetProperties for Divider {
fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
match name {
"orientation" => Ok(CapabilityValue::String(
if self.is_vertical() { "vertical" } else { "horizontal" }.to_string(),
)),
"thickness" => Ok(CapabilityValue::Float(self.thickness() as f64)),
_ => base_property_get(self, name),
}
}
fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
match name {
"orientation" => {
let orientation = expect_string(value)?;
match orientation.as_str() {
"horizontal" => self.set_vertical(false),
"vertical" => self.set_vertical(true),
_ => return Err(CapabilityAccessError::TypeMismatch),
}
Ok(())
}
"thickness" => {
let thickness = expect_f32(value)?;
if !thickness.is_finite() || thickness < 0.0 {
return Err(CapabilityAccessError::TypeMismatch);
}
self.set_thickness(thickness as u32);
Ok(())
}
_ => base_property_set(self, name, value),
}
}
fn property_names(&self) -> &'static [&'static str] {
property_names_of!["orientation", "thickness", BASE_PROPERTY_NAMES]
}
/// Runs one of the commands `divider` publishes.
///
/// `set_orientation` takes an orientation token and `set_thickness` a pixel count,
/// so both assign state through the property path rather than performing an
/// action. They are refused as [`CapabilityAccessError::OutOfRange`] so a caller
/// discovers the names are valid and the invocation needs a payload, not
/// [`CapabilityAccessError::UnknownCommand`].
fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
match name {
"set_orientation" | "set_thickness" => Err(CapabilityAccessError::OutOfRange),
_ => Err(CapabilityAccessError::UnknownCommand),
}
}
}
impl Draw for Divider {
fn draw(&mut self, context: &mut RenderContext) {
let rect = self.geometry();
let color = self.line_color();
draw_line(context, rect, self.vertical, self.thickness, color);
}
}
impl EventHandler for Divider {
fn handle_event(&mut self, event: &Event) {
// Divider is purely decorative; delegate all events to base.
self.base.handle_event(event);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::Point;
#[test]
fn divider_default_is_horizontal() {
let div = Divider::new(Rect::new(0, 0, 100, 10));
assert!(!div.is_vertical());
assert_eq!(div.kind(), WidgetKind::Divider);
}
#[test]
fn divider_set_vertical() {
let mut div = Divider::new(Rect::new(0, 0, 10, 100));
assert!(!div.is_vertical());
div.set_vertical(true);
assert!(div.is_vertical());
div.set_vertical(false);
assert!(!div.is_vertical());
}
#[test]
fn divider_set_thickness() {
let mut div = Divider::new(Rect::new(0, 0, 100, 10));
assert_eq!(div.thickness, 1);
div.set_thickness(3);
// thickness is not publicly readable directly; check via the setter
// We verify internal state by checking the draw path (no crash).
div.set_thickness(5);
}
#[test]
fn divider_set_color() {
let mut div = Divider::new(Rect::new(0, 0, 100, 10));
div.set_color(Color::RED);
// No crash and correct kind preserved.
assert_eq!(div.kind(), WidgetKind::Divider);
}
#[test]
fn divider_svg_output_horizontal() {
let mut div = Divider::new(Rect::new(0, 0, 100, 10));
let svg = crate::widget::svg::render_to_svg(&mut div);
assert!(svg.starts_with("<svg"));
// SVG should contain a line element (draw_line_stroke produces a line).
assert!(svg.contains("line") || svg.contains("stroke"));
}
#[test]
fn divider_svg_output_vertical() {
let mut div = Divider::new(Rect::new(0, 0, 10, 100));
div.set_vertical(true);
let svg = crate::widget::svg::render_to_svg(&mut div);
assert!(svg.starts_with("<svg"));
}
#[test]
fn divider_event_delegation() {
let mut div = Divider::new(Rect::new(0, 0, 100, 10));
// Events should not panic/crash.
div.handle_event(&Event::MousePress { pos: Point::new(10, 5), button: 1 });
div.handle_event(&Event::MouseRelease { pos: Point::new(10, 5), button: 1 });
// State remains unchanged (no interactive behavior).
assert!(!div.is_vertical());
}
#[test]
fn divider_geometry_reflects_kind() {
let div = Divider::new(Rect::new(0, 0, 100, 10));
let geom = div.geometry();
assert_eq!(geom.width, 100);
assert_eq!(geom.height, 10);
}
}