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
use crate::{
app::theme::{ThemeBorderSizes, ThemePalette},
node::{BackgroundColor, border_size_value::BorderSizeValue, color::Color},
};
/// A line drawn around an element, outside its box, like CSS `outline` with
/// `outline-offset`: a focus ring. It takes no room in layout and never moves
/// the element or what is around it; it follows the element's corner radii.
/// It is painted over the element's content and inside the clips the element
/// is under, as its outer shadows are.
#[derive(Clone, Debug, PartialEq)]
pub struct Outline {
pub width: BorderSizeValue,
pub color: BackgroundColor,
/// The gap between the element's box and the line, in logical pixels.
/// Negative values draw the line inside the box.
pub offset: f32,
}
/// An outline with its width and color resolved against the theme: what is
/// painted, and what a transition or an animation of the outline moves.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ResolvedOutline {
pub width: f32,
pub color: Color,
pub offset: f32,
}
impl Outline {
/// A line of `width` (logical pixels or a `BorderSize` role) in `color` (a
/// concrete color or a `PaletteColor`), touching the element's box.
pub fn new(width: impl Into<BorderSizeValue>, color: impl Into<BackgroundColor>) -> Self {
Self {
width: width.into(),
color: color.into(),
offset: 0.0,
}
}
/// No outline. A state style uses it to remove the element's own.
pub fn none() -> Self {
Self::new(0.0, Color::new(0, 0, 0, 0))
}
/// Moves the line `offset` logical pixels away from the element's box.
pub fn offset(mut self, offset: f32) -> Self {
self.offset = offset;
self
}
/// `None` for an outline that paints nothing: no width, or a color the
/// palette does not have.
pub(crate) fn resolve(&self, palette: &ThemePalette, border_sizes: &ThemeBorderSizes) -> Option<ResolvedOutline> {
let width = self.width.resolve(border_sizes);
if width.is_nan() || width <= 0.0 || !self.offset.is_finite() {
return None;
}
Some(ResolvedOutline {
width,
color: self.color.resolve(palette)?,
offset: self.offset,
})
}
}
impl ResolvedOutline {
/// How far past the element's box the line reaches on each side.
pub(crate) fn outset(&self) -> f32 {
self.offset + self.width
}
}