#![forbid(unsafe_code)]
use egui::{Color32, CornerRadius, Margin, Painter, Rect, Shape, Stroke, Ui};
use makeover_layout::{Bevel, Depth, Edge, Fill, Tone};
pub mod host;
pub mod piece;
pub mod table;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Palette {
pub page: Color32,
pub raised: Color32,
pub overlay: Color32,
pub well: Color32,
pub sunken: Color32,
pub bevel_light: Color32,
pub bevel_dark: Color32,
pub elevation: Color32,
pub content: Color32,
pub content_secondary: Color32,
pub content_muted: Color32,
pub action: Color32,
pub content_on_action: Color32,
pub danger: Color32,
pub success: Color32,
pub warning: Color32,
pub info: Color32,
pub border: Color32,
pub info_surface: Color32,
pub success_surface: Color32,
pub warning_surface: Color32,
pub danger_surface: Color32,
pub row_hover: Color32,
pub row_rule: Color32,
pub row_selected: Color32,
}
impl Palette {
#[must_use]
pub const fn two_tone(page: Color32, ink: Color32) -> Self {
Self {
page,
raised: page,
overlay: page,
well: page,
sunken: page,
bevel_light: page,
bevel_dark: ink,
elevation: Color32::TRANSPARENT,
content: ink,
content_secondary: ink,
content_muted: ink,
action: ink,
content_on_action: page,
danger: ink,
success: ink,
warning: ink,
info: ink,
border: ink,
info_surface: page,
success_surface: page,
warning_surface: page,
danger_surface: page,
row_hover: page,
row_rule: ink,
row_selected: page,
}
}
#[must_use]
pub const fn page(mut self, colour: Color32) -> Self {
self.page = colour;
self
}
#[must_use]
pub const fn raised(mut self, colour: Color32) -> Self {
self.raised = colour;
self
}
#[must_use]
pub const fn overlay(mut self, colour: Color32) -> Self {
self.overlay = colour;
self
}
#[must_use]
pub const fn well(mut self, colour: Color32) -> Self {
self.well = colour;
self
}
#[must_use]
pub const fn sunken(mut self, colour: Color32) -> Self {
self.sunken = colour;
self
}
#[must_use]
pub const fn bevel_light(mut self, colour: Color32) -> Self {
self.bevel_light = colour;
self
}
#[must_use]
pub const fn bevel_dark(mut self, colour: Color32) -> Self {
self.bevel_dark = colour;
self
}
#[must_use]
pub const fn elevation(mut self, colour: Color32) -> Self {
self.elevation = colour;
self
}
#[must_use]
pub const fn content(mut self, colour: Color32) -> Self {
self.content = colour;
self
}
#[must_use]
pub const fn content_secondary(mut self, colour: Color32) -> Self {
self.content_secondary = colour;
self
}
#[must_use]
pub const fn content_muted(mut self, colour: Color32) -> Self {
self.content_muted = colour;
self
}
#[must_use]
pub const fn action(mut self, colour: Color32) -> Self {
self.action = colour;
self
}
#[must_use]
pub const fn content_on_action(mut self, colour: Color32) -> Self {
self.content_on_action = colour;
self
}
#[must_use]
pub const fn danger(mut self, colour: Color32) -> Self {
self.danger = colour;
self
}
#[must_use]
pub const fn success(mut self, colour: Color32) -> Self {
self.success = colour;
self
}
#[must_use]
pub const fn warning(mut self, colour: Color32) -> Self {
self.warning = colour;
self
}
#[must_use]
pub const fn info(mut self, colour: Color32) -> Self {
self.info = colour;
self
}
#[must_use]
pub const fn border(mut self, colour: Color32) -> Self {
self.border = colour;
self
}
#[must_use]
pub const fn info_surface(mut self, colour: Color32) -> Self {
self.info_surface = colour;
self
}
#[must_use]
pub const fn success_surface(mut self, colour: Color32) -> Self {
self.success_surface = colour;
self
}
#[must_use]
pub const fn warning_surface(mut self, colour: Color32) -> Self {
self.warning_surface = colour;
self
}
#[must_use]
pub const fn danger_surface(mut self, colour: Color32) -> Self {
self.danger_surface = colour;
self
}
#[must_use]
pub const fn row_hover(mut self, colour: Color32) -> Self {
self.row_hover = colour;
self
}
#[must_use]
pub const fn row_rule(mut self, colour: Color32) -> Self {
self.row_rule = colour;
self
}
#[must_use]
pub const fn row_selected(mut self, colour: Color32) -> Self {
self.row_selected = colour;
self
}
#[cfg(feature = "theme")]
pub fn from_theme(tokens: &makeover::SemanticTokens) -> Result<Self, MissingToken> {
use makeover::Token;
let colour = |token: Token| {
tokens
.rgba(token)
.map(|(r, g, b, a)| Color32::from_rgba_unmultiplied(r, g, b, a))
.ok_or(MissingToken(token))
};
Ok(Self {
page: colour(Token::SurfacePage)?,
raised: colour(Token::SurfaceRaised)?,
overlay: colour(Token::SurfaceOverlay)?,
well: colour(Token::SurfaceWell)?,
sunken: colour(Token::SurfaceSunken)?,
bevel_light: colour(Token::BevelLight)?,
bevel_dark: colour(Token::BevelDark)?,
elevation: colour(Token::Elevation)?,
content: colour(Token::Content)?,
content_secondary: colour(Token::ContentSecondary)?,
content_muted: colour(Token::ContentMuted)?,
action: colour(Token::Action)?,
content_on_action: colour(Token::ContentOnAction)?,
danger: colour(Token::Danger)?,
success: colour(Token::Success)?,
warning: colour(Token::Warning)?,
info: colour(Token::Info)?,
border: colour(Token::Border)?,
info_surface: colour(Token::InfoSurface)?,
success_surface: colour(Token::SuccessSurface)?,
warning_surface: colour(Token::WarningSurface)?,
danger_surface: colour(Token::DangerSurface)?,
row_hover: colour(Token::RowHover)?,
row_rule: colour(Token::RowRule)?,
row_selected: colour(Token::RowSelected)?,
})
}
}
#[cfg(feature = "theme")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MissingToken(pub makeover::Token);
#[cfg(feature = "theme")]
impl std::fmt::Display for MissingToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "the theme resolves no `{}`", self.0.as_str())
}
}
#[cfg(feature = "theme")]
impl std::error::Error for MissingToken {}
impl Palette {
#[must_use]
pub const fn fill(&self, fill: Fill) -> Option<Color32> {
match fill {
Fill::Page => Some(self.page),
Fill::Raised => Some(self.raised),
Fill::Overlay => Some(self.overlay),
Fill::Well => Some(self.well),
Fill::Sunken => Some(self.sunken),
_ => None,
}
}
#[must_use]
pub const fn tone(&self, tone: Tone) -> Color32 {
match tone {
Tone::Neutral => self.content,
Tone::Info => self.info,
Tone::Success => self.success,
Tone::Warning => self.warning,
Tone::Danger => self.danger,
_ => self.content,
}
}
#[must_use]
pub const fn tone_surface(&self, tone: Tone) -> Color32 {
match tone {
Tone::Neutral => self.raised,
Tone::Info => self.info_surface,
Tone::Success => self.success_surface,
Tone::Warning => self.warning_surface,
Tone::Danger => self.danger_surface,
_ => self.raised,
}
}
#[must_use]
pub const fn tone_edge(&self, tone: Tone) -> Color32 {
match tone {
Tone::Neutral => self.border,
other => self.tone(other),
}
}
#[must_use]
pub const fn cast(&self) -> egui::Shadow {
egui::Shadow {
offset: [0, 2],
blur: 24,
spread: 0,
color: self.elevation,
}
}
#[must_use]
pub const fn edge(&self, edge: Edge) -> Color32 {
match edge {
Edge::Light => self.bevel_light,
Edge::Dark => self.bevel_dark,
_ => self.border,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FrameStyle {
pub radius: CornerRadius,
pub margin: Margin,
pub stroke: f32,
}
impl Default for FrameStyle {
fn default() -> Self {
Self {
radius: CornerRadius::ZERO,
margin: Margin::ZERO,
stroke: 1.0,
}
}
}
pub fn paint_bevel(painter: &Painter, rect: Rect, bevel: Bevel, palette: &Palette, stroke: f32) {
let (top_left, bottom_right) = bevel.edges();
let r = rect.shrink(stroke / 2.0);
painter.add(Shape::line(
vec![r.left_bottom(), r.left_top(), r.right_top()],
Stroke::new(stroke, palette.edge(top_left)),
));
let shaded = if bevel.draws_bottom() {
vec![r.right_top(), r.right_bottom(), r.left_bottom()]
} else {
vec![r.right_top(), r.right_bottom()]
};
painter.add(Shape::line(
shaded,
Stroke::new(stroke, palette.edge(bottom_right)),
));
}
pub fn frame<R>(
ui: &mut Ui,
depth: Depth,
palette: &Palette,
style: FrameStyle,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> R {
let mut f = egui::Frame::new()
.corner_radius(style.radius)
.inner_margin(style.margin);
if let Some(fill) = depth.fill().and_then(|f| palette.fill(f)) {
f = f.fill(fill);
}
if depth.fill() == Some(Fill::Overlay) {
f = f.shadow(palette.cast());
}
let framed = f.show(ui, add_contents);
if let Some(bevel) = depth.bevel() {
paint_bevel(
ui.painter(),
framed.response.rect,
bevel,
palette,
style.stroke,
);
}
framed.inner
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FieldStyle {
pub frame: FrameStyle,
pub gap: f32,
pub group_gap: f32,
pub required_marker: &'static str,
}
impl Default for FieldStyle {
fn default() -> Self {
Self {
frame: FrameStyle::default(),
gap: 0.0,
group_gap: 0.0,
required_marker: "*",
}
}
}
#[derive(Debug, Default)]
#[non_exhaustive]
pub enum Editing<'a> {
#[default]
Absent,
Text(&'a mut String),
On(&'a mut bool),
Ticked(&'a mut Vec<String>),
Between {
lower: &'a mut String,
upper: &'a mut String,
},
}
#[cfg(test)]
mod tests;