refactor: organize host modules and document architecture with UML
Verify Tessera GUI / verify (push) Failing after 2m2s
Verify Tessera GUI / verify (push) Failing after 2m2s
This commit is contained in:
@@ -0,0 +1,290 @@
|
||||
use super::{
|
||||
Axis, Interaction, Role, Target, VisualState, WidgetId,
|
||||
theme::{self, Theme},
|
||||
};
|
||||
use crate::{
|
||||
display_list::{Color, DisplayList, DrawCommand},
|
||||
geometry::{Point, Rect},
|
||||
text::{self, TextStyle},
|
||||
};
|
||||
use std::hash::Hash;
|
||||
|
||||
pub struct Button<'a> {
|
||||
pub label: &'a str,
|
||||
pub selected: bool,
|
||||
pub enabled: bool,
|
||||
pub role: Role,
|
||||
}
|
||||
impl<'a> Button<'a> {
|
||||
pub fn new(label: &'a str) -> Self {
|
||||
Self {
|
||||
label,
|
||||
selected: false,
|
||||
enabled: true,
|
||||
role: Role::Button,
|
||||
}
|
||||
}
|
||||
pub fn selected(mut self, value: bool) -> Self {
|
||||
self.selected = value;
|
||||
self
|
||||
}
|
||||
pub fn enabled(mut self, value: bool) -> Self {
|
||||
self.enabled = value;
|
||||
self
|
||||
}
|
||||
pub fn role(mut self, value: Role) -> Self {
|
||||
self.role = value;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Ui<A> {
|
||||
pub list: DisplayList,
|
||||
pub targets: Vec<Target<A>>,
|
||||
pub theme: Theme,
|
||||
interaction: Interaction,
|
||||
}
|
||||
impl<A: Hash> Ui<A> {
|
||||
pub fn new(theme: Theme, interaction: Interaction) -> Self {
|
||||
Self {
|
||||
list: DisplayList::default(),
|
||||
targets: Vec::new(),
|
||||
theme,
|
||||
interaction,
|
||||
}
|
||||
}
|
||||
pub fn fill(&mut self, r: Rect, color: Color) {
|
||||
self.list.push(DrawCommand::FillRoundedRect {
|
||||
rect: r,
|
||||
radius: 0.0,
|
||||
color,
|
||||
});
|
||||
}
|
||||
fn outline(&mut self, r: Rect, color: Color, width: f32) {
|
||||
self.list.push(DrawCommand::StrokeRoundedRect {
|
||||
rect: Rect::new(
|
||||
r.x + width / 2.0,
|
||||
r.y + width / 2.0,
|
||||
(r.width - width).max(0.0),
|
||||
(r.height - width).max(0.0),
|
||||
),
|
||||
radius: 3.0,
|
||||
width,
|
||||
color,
|
||||
});
|
||||
}
|
||||
pub fn label(&mut self, x: f32, y: f32, value: &str, color: Color, code: bool) {
|
||||
self.list.push(DrawCommand::Text {
|
||||
origin: Point::new(x, y),
|
||||
value: value.into(),
|
||||
style: if code {
|
||||
TextStyle::code()
|
||||
} else {
|
||||
TextStyle {
|
||||
font_size: 12.0,
|
||||
line_height: 18.0,
|
||||
..TextStyle::ui(1)
|
||||
}
|
||||
},
|
||||
color,
|
||||
});
|
||||
}
|
||||
fn register(
|
||||
&mut self,
|
||||
id: WidgetId,
|
||||
rect: Rect,
|
||||
role: Role,
|
||||
action: Option<A>,
|
||||
selected: bool,
|
||||
enabled: bool,
|
||||
) -> VisualState {
|
||||
let response = self.interaction.visual(id, selected, enabled);
|
||||
self.targets.push(Target {
|
||||
id,
|
||||
rect,
|
||||
role,
|
||||
enabled,
|
||||
action,
|
||||
});
|
||||
response
|
||||
}
|
||||
fn surface(&mut self, rect: Rect, s: VisualState, base: Option<Color>) {
|
||||
let color = if !s.enabled {
|
||||
base
|
||||
} else if s.pressed {
|
||||
Some(self.theme.pressed())
|
||||
} else if s.hovered {
|
||||
Some(self.theme.hover())
|
||||
} else if s.selected {
|
||||
Some(self.theme.soft())
|
||||
} else {
|
||||
base
|
||||
};
|
||||
if let Some(color) = color {
|
||||
self.fill(rect, color);
|
||||
}
|
||||
if s.focused {
|
||||
self.outline(rect, self.theme.accent, 1.5);
|
||||
}
|
||||
}
|
||||
pub fn button(&mut self, rect: Rect, action: A, button: Button<'_>) -> VisualState {
|
||||
let id = WidgetId::new(&action);
|
||||
let s = self.register(
|
||||
id,
|
||||
rect,
|
||||
button.role,
|
||||
Some(action),
|
||||
button.selected,
|
||||
button.enabled,
|
||||
);
|
||||
self.surface(rect, s, None);
|
||||
self.list.push(DrawCommand::PushClip(rect));
|
||||
let label = text::fit(
|
||||
button.label,
|
||||
(rect.width - 16.0).max(0.0),
|
||||
TextStyle {
|
||||
font_size: 12.0,
|
||||
line_height: 18.0,
|
||||
..TextStyle::ui(1)
|
||||
},
|
||||
);
|
||||
self.label(
|
||||
rect.x + 8.0,
|
||||
rect.y + (rect.height - 18.0) / 2.0,
|
||||
&label,
|
||||
if s.enabled {
|
||||
theme::TEXT
|
||||
} else {
|
||||
Color::rgb(151, 160, 172)
|
||||
},
|
||||
false,
|
||||
);
|
||||
if s.selected && button.role == Role::Tab {
|
||||
self.fill(
|
||||
Rect::new(rect.x, rect.y + rect.height - 2.0, rect.width, 2.0),
|
||||
self.theme.accent,
|
||||
);
|
||||
}
|
||||
self.list.push(DrawCommand::PopClip);
|
||||
s
|
||||
}
|
||||
pub fn text_field(&mut self, rect: Rect, action: A, focused: bool) {
|
||||
let s = self.register(
|
||||
WidgetId::new(&action),
|
||||
rect,
|
||||
Role::TextField,
|
||||
Some(action),
|
||||
false,
|
||||
true,
|
||||
);
|
||||
self.surface(rect, s, Some(theme::BG));
|
||||
self.outline(
|
||||
rect,
|
||||
if focused || s.focused {
|
||||
self.theme.accent
|
||||
} else if s.hovered {
|
||||
theme::MUTED
|
||||
} else {
|
||||
theme::BORDER
|
||||
},
|
||||
if focused || s.focused { 1.5 } else { 1.0 },
|
||||
);
|
||||
}
|
||||
/// A terminal prompt shares its surface with output; its caret is the focus cue.
|
||||
pub fn inline_input(&mut self, rect: Rect, action: A) {
|
||||
self.register(
|
||||
WidgetId::new(&action),
|
||||
rect,
|
||||
Role::TextField,
|
||||
Some(action),
|
||||
false,
|
||||
true,
|
||||
);
|
||||
}
|
||||
pub fn row(&mut self, paint: Rect, hit: Rect, action: A, selected: bool) {
|
||||
let s = self.register(
|
||||
WidgetId::new(&action),
|
||||
hit,
|
||||
Role::Row,
|
||||
Some(action),
|
||||
selected,
|
||||
true,
|
||||
);
|
||||
self.surface(paint, s, None);
|
||||
}
|
||||
pub fn swatch(&mut self, rect: Rect, action: A, color: Color, selected: bool) {
|
||||
let s = self.register(
|
||||
WidgetId::new(&action),
|
||||
rect,
|
||||
Role::Swatch,
|
||||
Some(action),
|
||||
selected,
|
||||
true,
|
||||
);
|
||||
self.fill(
|
||||
rect,
|
||||
if s.pressed {
|
||||
theme::mix(color, theme::TEXT, 0.2)
|
||||
} else {
|
||||
color
|
||||
},
|
||||
);
|
||||
if s.hovered || s.focused || selected {
|
||||
self.outline(
|
||||
Rect::new(
|
||||
rect.x - 3.0,
|
||||
rect.y - 3.0,
|
||||
rect.width + 6.0,
|
||||
rect.height + 6.0,
|
||||
),
|
||||
if s.focused {
|
||||
self.theme.accent
|
||||
} else {
|
||||
theme::TEXT
|
||||
},
|
||||
if s.pressed { 3.0 } else { 1.5 },
|
||||
);
|
||||
}
|
||||
if selected {
|
||||
self.label(rect.x + 10.0, rect.y + 6.0, "✓", theme::BG, false);
|
||||
}
|
||||
}
|
||||
pub fn splitter(&mut self, rect: Rect, id: WidgetId, axis: Axis) {
|
||||
let hit = Rect::new(
|
||||
rect.x - 3.0,
|
||||
rect.y - 3.0,
|
||||
rect.width + 6.0,
|
||||
rect.height + 6.0,
|
||||
);
|
||||
let mut s = self.register(id, hit, Role::Splitter(axis), None, false, true);
|
||||
s.pressed |= self.interaction.captured(id);
|
||||
let color = if s.pressed || s.hovered {
|
||||
self.theme.accent
|
||||
} else {
|
||||
theme::BORDER
|
||||
};
|
||||
let line = if s.hovered || s.pressed {
|
||||
match axis {
|
||||
Axis::Horizontal => Rect::new(rect.x - 1.0, rect.y, 3.0, rect.height),
|
||||
Axis::Vertical => Rect::new(rect.x, rect.y - 1.0, rect.width, 3.0),
|
||||
}
|
||||
} else {
|
||||
rect
|
||||
};
|
||||
self.fill(line, color);
|
||||
}
|
||||
pub fn scrollbar(&mut self, rect: Rect, id: WidgetId, axis: Axis) {
|
||||
let mut s = self.register(id, rect, Role::Scrollbar(axis), None, false, true);
|
||||
s.pressed |= self.interaction.captured(id);
|
||||
self.fill(
|
||||
rect,
|
||||
if s.pressed {
|
||||
self.theme.accent
|
||||
} else if s.hovered {
|
||||
theme::mix(theme::MUTED, self.theme.accent, 0.55)
|
||||
} else {
|
||||
Color::rgb(161, 173, 190)
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user