use crate::DefaultTheme;
use crate::Progress;
use crate::SemanticTone;
use crate::composites::forms::set_hover_animation_target;
use crate::draw::{inset_rect, mix_color};
use crate::frame::{
draw_control_shape, draw_focus_ring, physical_pixels, snap_to_pixels, snap_width_to_pixels,
stroke_border,
};
use crate::{
GlowTone, ThemeShadow, paint_theme_glow, paint_theme_inset_shadow, paint_theme_shadow,
};
use sui_core::Color;
use sui_core::Event;
use sui_core::Path;
use sui_core::PointerEventKind;
use sui_core::Rect;
use sui_core::SemanticsNode;
use sui_core::SemanticsRole;
use sui_core::Size;
use sui_core::WidgetId;
use sui_layout::Constraints;
use sui_layout::Padding as Insets;
use sui_runtime::ArrangeCtx;
use sui_runtime::EventCtx;
use sui_runtime::MeasureCtx;
use sui_runtime::PaintCtx;
use sui_runtime::SemanticsCtx;
use sui_runtime::SingleChild;
use sui_runtime::Widget;
use sui_runtime::WidgetPod;
use sui_runtime::WidgetPodMutVisitor;
use sui_runtime::WidgetPodVisitor;
type ShadowToken = Box<dyn Fn(&DefaultTheme) -> ThemeShadow>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SurfaceRole {
Window,
Sidebar,
Panel,
Titlebar,
Field,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SurfaceBorder {
None,
All,
Top,
Right,
Bottom,
Left,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SurfaceElevation {
None,
Small,
Medium,
Large,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum SurfaceAppearance {
#[default]
Standard,
Raised,
Soft,
Filled,
}
pub struct Surface {
pub(super) theme: Box<DefaultTheme>,
pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
pub(super) name: Option<String>,
pub(super) role: SurfaceRole,
pub(super) appearance: SurfaceAppearance,
pub(super) tone: SemanticTone,
pub(super) border: SurfaceBorder,
pub(super) elevation: SurfaceElevation,
pub(super) shadow: Option<ShadowToken>,
pub(super) glow: Option<GlowTone>,
pub(super) radius: f32,
pub(super) padding: Insets,
pub(super) fill_width: bool,
pub(super) fill_height: bool,
pub(super) child: SingleChild,
}
impl Surface {
pub fn new<W>(role: SurfaceRole, child: W) -> Self
where
W: Widget + 'static,
{
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
name: None,
role,
appearance: SurfaceAppearance::Standard,
tone: SemanticTone::Neutral,
border: SurfaceBorder::None,
elevation: SurfaceElevation::None,
shadow: None,
glow: None,
radius: 0.0,
padding: Insets::ZERO,
fill_width: false,
fill_height: false,
child: SingleChild::new(child),
}
}
pub fn window<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(SurfaceRole::Window, child)
}
pub fn sidebar<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(SurfaceRole::Sidebar, child).border(SurfaceBorder::Right)
}
pub fn panel<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(SurfaceRole::Panel, child)
.border(SurfaceBorder::All)
.corner_radius(8.0)
}
pub fn titlebar<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(SurfaceRole::Titlebar, child).border(SurfaceBorder::Bottom)
}
pub fn field<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self::new(SurfaceRole::Field, child)
.border(SurfaceBorder::All)
.corner_radius(6.0)
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn border(mut self, border: SurfaceBorder) -> Self {
self.border = border;
self
}
pub fn appearance(mut self, appearance: SurfaceAppearance) -> Self {
self.appearance = appearance;
self
}
pub fn tone(mut self, tone: SemanticTone) -> Self {
self.tone = tone;
self
}
pub fn elevation(mut self, elevation: SurfaceElevation) -> Self {
self.elevation = elevation;
self
}
pub fn shadow<F>(mut self, shadow: F) -> Self
where
F: Fn(&DefaultTheme) -> ThemeShadow + 'static,
{
self.shadow = Some(Box::new(shadow));
self
}
pub fn glow(mut self, tone: GlowTone) -> Self {
self.glow = Some(tone);
self
}
pub fn corner_radius(mut self, corner_radius: f32) -> Self {
self.radius = corner_radius.max(0.0);
self
}
pub fn padding(mut self, padding: Insets) -> Self {
self.padding = padding;
self
}
pub fn fill(mut self) -> Self {
self.fill_width = true;
self.fill_height = true;
self
}
pub fn fill_width(mut self, fill_width: bool) -> Self {
self.fill_width = fill_width;
self
}
pub fn fill_height(mut self, fill_height: bool) -> Self {
self.fill_height = fill_height;
self
}
pub(super) fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
pub(super) fn background_for_role(theme: &DefaultTheme, role: SurfaceRole) -> Color {
match role {
SurfaceRole::Window => theme.surfaces.window,
SurfaceRole::Sidebar => theme.surfaces.sidebar,
SurfaceRole::Panel => theme.surfaces.panel,
SurfaceRole::Titlebar => theme.surfaces.titlebar,
SurfaceRole::Field => theme.surfaces.field,
}
}
pub(super) fn resolved_colors(&self, theme: &DefaultTheme) -> (Color, Color) {
match self.appearance {
SurfaceAppearance::Standard => (
Self::background_for_role(theme, self.role),
theme.surfaces.border,
),
SurfaceAppearance::Raised => (theme.palette.surface_raised, theme.surfaces.border),
SurfaceAppearance::Soft => {
let (fill, _) = theme.semantic_tone_soft_colors(self.tone);
let border = if self.tone == SemanticTone::Neutral {
theme.surfaces.border
} else {
theme.semantic_tone_color(self.tone).with_alpha(0.36)
};
(fill, border)
}
SurfaceAppearance::Filled => {
let (fill, _) = theme.semantic_tone_colors(self.tone);
(fill, fill)
}
}
}
pub(super) fn content_rect(&self, bounds: Rect) -> Rect {
inset_rect(bounds, self.padding)
}
}
impl Widget for Surface {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let max_child = Size::new(
if constraints.max.width.is_finite() {
(constraints.max.width - self.padding.left - self.padding.right).max(0.0)
} else {
f32::INFINITY
},
if constraints.max.height.is_finite() {
(constraints.max.height - self.padding.top - self.padding.bottom).max(0.0)
} else {
f32::INFINITY
},
);
let min_child = Size::new(
if self.fill_width && max_child.width.is_finite() {
max_child.width
} else {
(constraints.min.width - self.padding.left - self.padding.right)
.max(0.0)
.min(max_child.width)
},
if self.fill_height && max_child.height.is_finite() {
max_child.height
} else {
(constraints.min.height - self.padding.top - self.padding.bottom)
.max(0.0)
.min(max_child.height)
},
);
let child_size = self
.child
.measure(ctx, Constraints::new(min_child, max_child));
let mut size = Size::new(
child_size.width + self.padding.left + self.padding.right,
child_size.height + self.padding.top + self.padding.bottom,
);
if self.fill_width && constraints.max.width.is_finite() {
size.width = constraints.max.width;
}
if self.fill_height && constraints.max.height.is_finite() {
size.height = constraints.max.height;
}
constraints.clamp(size)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, self.content_rect(bounds));
}
fn paint(&self, ctx: &mut PaintCtx) {
let theme = self.resolved_theme();
let bounds = snap_to_pixels(ctx, ctx.bounds());
let radius = self.radius.min(bounds.width().min(bounds.height()) * 0.5);
let shadow = match (&self.shadow, self.elevation) {
(Some(shadow), _) => Some(shadow(&theme)),
(None, SurfaceElevation::None) => None,
(None, SurfaceElevation::Small) => Some(theme.shadows.box_shadow.sm),
(None, SurfaceElevation::Medium) => Some(theme.shadows.box_shadow.md),
(None, SurfaceElevation::Large) => Some(theme.shadows.box_shadow.lg),
};
if let Some(shadow) = &shadow {
paint_theme_shadow(ctx, bounds, [radius; 4], shadow);
}
if let Some(tone) = self.glow {
let glow = theme.glow_for_output(tone, ctx.output_color_range());
paint_theme_glow(ctx, bounds, [radius; 4], &glow);
}
let (background, border) = self.resolved_colors(&theme);
if radius > 0.0 {
ctx.fill(Path::rounded_rect(bounds, radius), background);
} else {
ctx.fill_rect(bounds, background);
}
let stroke_width = snap_width_to_pixels(
ctx,
physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
);
if let Some(shadow) = &shadow {
let (inside, inside_radius) = inside_border(bounds, radius, self.border, stroke_width);
paint_theme_inset_shadow(ctx, inside, [inside_radius; 4], shadow);
}
match self.border {
SurfaceBorder::None => {}
SurfaceBorder::All => stroke_border(ctx, bounds, radius, stroke_width, border),
SurfaceBorder::Top => ctx.fill_rect(
Rect::new(bounds.x(), bounds.y(), bounds.width(), stroke_width),
border,
),
SurfaceBorder::Right => ctx.fill_rect(
Rect::new(
bounds.max_x() - stroke_width,
bounds.y(),
stroke_width,
bounds.height(),
),
border,
),
SurfaceBorder::Bottom => ctx.fill_rect(
Rect::new(
bounds.x(),
bounds.max_y() - stroke_width,
bounds.width(),
stroke_width,
),
border,
),
SurfaceBorder::Left => ctx.fill_rect(
Rect::new(bounds.x(), bounds.y(), stroke_width, bounds.height()),
border,
),
}
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
if let Some(name) = &self.name {
let mut node = SemanticsNode::new(
ctx.widget_id(),
SemanticsRole::GenericContainer,
ctx.bounds(),
);
node.name = Some(name.clone());
ctx.push(node);
}
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
pub struct ShadowBox {
theme: Box<DefaultTheme>,
theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
radius: f32,
shadow: Option<ShadowToken>,
glow: Option<GlowTone>,
child: SingleChild,
}
impl ShadowBox {
pub fn new<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
radius: 0.0,
shadow: None,
glow: None,
child: SingleChild::new(child),
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn corner_radius(mut self, corner_radius: f32) -> Self {
self.radius = corner_radius.max(0.0);
self
}
pub fn shadow<F>(mut self, shadow: F) -> Self
where
F: Fn(&DefaultTheme) -> ThemeShadow + 'static,
{
self.shadow = Some(Box::new(shadow));
self
}
pub fn glow(mut self, tone: GlowTone) -> Self {
self.glow = Some(tone);
self
}
fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
}
fn inside_border(
bounds: Rect,
radius: f32,
border: SurfaceBorder,
stroke_width: f32,
) -> (Rect, f32) {
let (left, top, right, bottom) = match border {
SurfaceBorder::None => (0.0, 0.0, 0.0, 0.0),
SurfaceBorder::All => (stroke_width, stroke_width, stroke_width, stroke_width),
SurfaceBorder::Top => (0.0, stroke_width, 0.0, 0.0),
SurfaceBorder::Right => (0.0, 0.0, stroke_width, 0.0),
SurfaceBorder::Bottom => (0.0, 0.0, 0.0, stroke_width),
SurfaceBorder::Left => (stroke_width, 0.0, 0.0, 0.0),
};
let inside = Rect::new(
bounds.x() + left,
bounds.y() + top,
(bounds.width() - left - right).max(0.0),
(bounds.height() - top - bottom).max(0.0),
);
let widest = left.max(top).max(right).max(bottom);
(inside, (radius - widest).max(0.0))
}
impl Widget for ShadowBox {
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
self.child.measure(ctx, constraints)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, bounds);
}
fn paint(&self, ctx: &mut PaintCtx) {
let theme = self.resolved_theme();
let bounds = ctx.bounds();
let radii = [self.radius.min(bounds.width().min(bounds.height()) * 0.5); 4];
let shadow = self.shadow.as_ref().map(|shadow| shadow(&theme));
if let Some(shadow) = &shadow {
paint_theme_shadow(ctx, bounds, radii, shadow);
}
if let Some(tone) = self.glow {
let glow = theme.glow_for_output(tone, ctx.output_color_range());
paint_theme_glow(ctx, bounds, radii, &glow);
}
self.child.paint(ctx);
if let Some(shadow) = &shadow {
paint_theme_inset_shadow(ctx, bounds, radii, shadow);
}
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}
pub struct FramedField {
pub(super) theme: Box<DefaultTheme>,
pub(super) theme_reader: Option<Box<dyn Fn() -> DefaultTheme>>,
pub(super) name: Option<String>,
pub(super) description: Option<String>,
pub(super) padding: Insets,
pub(super) min_height: Option<f32>,
pub(super) fill_width: bool,
pub(super) focused: Option<bool>,
pub(super) focused_reader: Option<Box<dyn Fn() -> bool>>,
pub(super) invalid: bool,
pub(super) invalid_reader: Option<Box<dyn Fn() -> bool>>,
pub(super) hovered: bool,
pub(super) hover_animation: Progress,
pub(super) child: SingleChild,
}
impl FramedField {
pub fn new<W>(child: W) -> Self
where
W: Widget + 'static,
{
Self {
theme: Box::new(DefaultTheme::default()),
theme_reader: None,
name: None,
description: None,
padding: Insets::ZERO,
min_height: None,
fill_width: false,
focused: None,
focused_reader: None,
invalid: false,
invalid_reader: None,
hovered: false,
hover_animation: Progress::new(0.0),
child: SingleChild::new(child),
}
}
pub fn theme(mut self, theme: DefaultTheme) -> Self {
self.theme = Box::new(theme);
self.theme_reader = None;
self
}
pub fn theme_when<F>(mut self, theme: F) -> Self
where
F: Fn() -> DefaultTheme + 'static,
{
self.theme_reader = Some(Box::new(theme));
self
}
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn padding(mut self, padding: Insets) -> Self {
self.padding = padding;
self
}
pub fn min_height(mut self, min_height: f32) -> Self {
self.min_height = Some(min_height.max(0.0));
self
}
pub fn fill_width(mut self, fill_width: bool) -> Self {
self.fill_width = fill_width;
self
}
pub fn focused(mut self, focused: bool) -> Self {
self.focused = Some(focused);
self.focused_reader = None;
self
}
pub fn focused_when<F>(mut self, focused: F) -> Self
where
F: Fn() -> bool + 'static,
{
self.focused_reader = Some(Box::new(focused));
self
}
pub fn invalid(mut self, invalid: bool) -> Self {
self.invalid = invalid;
self.invalid_reader = None;
self
}
pub fn invalid_when<F>(mut self, invalid: F) -> Self
where
F: Fn() -> bool + 'static,
{
self.invalid_reader = Some(Box::new(invalid));
self
}
pub(super) fn resolved_theme(&self) -> DefaultTheme {
self.theme_reader
.as_ref()
.map(|theme| theme())
.unwrap_or_else(|| *self.theme)
}
pub(super) fn child_contains(&self, target: WidgetId) -> bool {
if self.child.child().id() == target {
return true;
}
struct Finder {
target: WidgetId,
found: bool,
}
impl WidgetPodVisitor for Finder {
fn visit(&mut self, child: &WidgetPod) {
if self.found {
return;
}
if child.id() == self.target {
self.found = true;
} else {
child.visit_children(self);
}
}
}
let mut finder = Finder {
target,
found: false,
};
self.child.child().visit_children(&mut finder);
finder.found
}
pub(super) fn is_focused(&self, focused_widget_id: Option<WidgetId>) -> bool {
if let Some(focused) = &self.focused_reader {
return focused();
}
if let Some(focused) = self.focused {
return focused;
}
focused_widget_id
.map(|focused| self.child_contains(focused))
.unwrap_or(false)
}
pub(super) fn is_invalid(&self) -> bool {
self.invalid_reader
.as_ref()
.map(|invalid| invalid())
.unwrap_or(self.invalid)
}
pub(super) fn content_rect(&self, bounds: Rect) -> Rect {
inset_rect(bounds, self.padding)
}
pub(super) fn set_hovered(&mut self, hovered: bool, ctx: &mut EventCtx) {
if self.hovered == hovered {
return;
}
self.hovered = hovered;
let theme = self.resolved_theme();
set_hover_animation_target(&mut self.hover_animation, hovered as u8 as f32, &theme, ctx);
ctx.request_paint();
}
}
impl Widget for FramedField {
fn event(&mut self, ctx: &mut EventCtx, event: &Event) {
match event {
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Enter => {
self.set_hovered(ctx.bounds().contains(pointer.position), ctx);
}
Event::Pointer(pointer) if pointer.kind == PointerEventKind::Leave => {
self.set_hovered(false, ctx);
}
_ => {}
}
}
fn measure(&mut self, ctx: &mut MeasureCtx, constraints: Constraints) -> Size {
let max_child = Size::new(
if constraints.max.width.is_finite() {
(constraints.max.width - self.padding.left - self.padding.right).max(0.0)
} else {
f32::INFINITY
},
if constraints.max.height.is_finite() {
(constraints.max.height - self.padding.top - self.padding.bottom).max(0.0)
} else {
f32::INFINITY
},
);
let child = self
.child
.measure(ctx, Constraints::new(Size::ZERO, max_child));
let theme = self.resolved_theme();
let mut size = Size::new(
child.width + self.padding.left + self.padding.right,
(child.height + self.padding.top + self.padding.bottom)
.max(self.min_height.unwrap_or(theme.metrics.min_height)),
);
if self.fill_width && constraints.max.width.is_finite() {
size.width = constraints.max.width;
}
constraints.clamp(size)
}
fn arrange(&mut self, ctx: &mut ArrangeCtx, bounds: Rect) {
self.child.arrange(ctx, self.content_rect(bounds));
}
fn paint(&self, ctx: &mut PaintCtx) {
let theme = self.resolved_theme();
let bounds = ctx.bounds();
let radius = theme
.metrics
.corner_radius
.min(bounds.width().min(bounds.height()) * 0.5);
let invalid = self.is_invalid();
let focused = self.is_focused(ctx.focused_widget_id());
let interaction_border = mix_color(
theme.palette.button_border,
theme.palette.border_hover,
self.hover_animation.get(ctx),
);
let border = if invalid {
theme.semantic_tone_color(SemanticTone::Danger)
} else if focused {
theme.palette.border_focus
} else {
interaction_border
};
let background = mix_color(
theme.surfaces.field,
theme.palette.surface_focus,
focused as u8 as f32,
);
draw_control_shape(
ctx,
bounds,
radius,
physical_pixels(ctx, theme.metrics.border_width.max(1.0)),
background,
border,
);
if focused {
draw_focus_ring(
ctx,
bounds,
radius,
theme.metrics,
if invalid {
theme.semantic_tone_color(SemanticTone::Danger)
} else {
theme.palette.focus_ring
},
);
}
self.child.paint(ctx);
}
fn semantics(&self, ctx: &mut SemanticsCtx) {
if self.name.is_some() || self.description.is_some() {
let mut node = SemanticsNode::new(
ctx.widget_id(),
SemanticsRole::GenericContainer,
ctx.bounds(),
);
node.name = self.name.clone();
node.description = self.description.clone();
node.state.focused = self.is_focused(ctx.focused_widget_id());
node.state.hovered = self.hovered;
ctx.push(node);
}
self.child.semantics(ctx);
}
fn visit_children(&self, visitor: &mut dyn WidgetPodVisitor) {
self.child.visit_children(visitor);
}
fn visit_children_mut(&mut self, visitor: &mut dyn WidgetPodMutVisitor) {
self.child.visit_children_mut(visitor);
}
}