use std::sync::Arc;
use egui::{
Color32, CornerRadius, FontData, FontFamily, FontId, RichText, Sense, Stroke, StrokeKind,
Theme, Vec2, Visuals,
};
use crate::icons;
pub const ROW_HEIGHT: f32 = 26.0;
const RADIUS: u8 = 7;
struct Palette {
panel: Color32,
card: Color32,
extreme: Color32,
inactive: Color32,
hovered: Color32,
active: Color32,
border: Color32,
text: Color32,
weak: Color32,
accent: Color32,
error: Color32,
}
const DARK: Palette = Palette {
panel: Color32::from_rgb(12, 17, 24),
card: Color32::from_rgb(20, 28, 39),
extreme: Color32::from_rgb(8, 12, 18),
inactive: Color32::from_rgb(29, 40, 54),
hovered: Color32::from_rgb(39, 53, 71),
active: Color32::from_rgb(49, 66, 87),
border: Color32::from_rgb(33, 45, 60),
text: Color32::from_rgb(234, 239, 245),
weak: Color32::from_rgb(140, 154, 171),
accent: Color32::from_rgb(36, 136, 194),
error: Color32::from_rgb(242, 104, 96),
};
const LIGHT: Palette = Palette {
panel: Color32::from_rgb(243, 246, 250),
card: Color32::from_rgb(255, 255, 255),
extreme: Color32::from_rgb(255, 255, 255),
inactive: Color32::from_rgb(229, 236, 243),
hovered: Color32::from_rgb(216, 227, 238),
active: Color32::from_rgb(201, 215, 230),
border: Color32::from_rgb(213, 222, 232),
text: Color32::from_rgb(17, 29, 44),
weak: Color32::from_rgb(84, 99, 118),
accent: Color32::from_rgb(22, 114, 168),
error: Color32::from_rgb(196, 43, 28),
};
fn visuals(base: Visuals, p: &Palette) -> Visuals {
let mut v = base;
let radius = CornerRadius::same(RADIUS);
v.panel_fill = p.panel;
v.window_fill = p.card;
v.faint_bg_color = p.card;
v.extreme_bg_color = p.extreme;
v.code_bg_color = p.inactive;
v.window_stroke = Stroke::new(1.0, p.border);
v.window_corner_radius = CornerRadius::same(12);
v.menu_corner_radius = CornerRadius::same(10);
v.weak_text_color = Some(p.weak);
v.error_fg_color = p.error;
v.hyperlink_color = p.accent;
v.selection.bg_fill = p.accent;
v.selection.stroke = Stroke::new(1.0, Color32::WHITE);
v.slider_trailing_fill = true;
v.collapsing_header_frame = false;
v.indent_has_left_vline = false;
let w = &mut v.widgets;
w.noninteractive.bg_fill = p.card;
w.noninteractive.weak_bg_fill = p.card;
w.noninteractive.bg_stroke = Stroke::new(1.0, p.border);
w.noninteractive.fg_stroke = Stroke::new(1.0, p.text);
for (state, fill) in [
(&mut w.inactive, p.inactive),
(&mut w.hovered, p.hovered),
(&mut w.active, p.active),
(&mut w.open, p.hovered),
] {
state.bg_fill = fill;
state.weak_bg_fill = fill;
state.bg_stroke = Stroke::NONE;
state.fg_stroke.color = p.text;
state.expansion = 0.0;
}
w.hovered.bg_stroke = Stroke::new(1.0, p.border);
w.active.bg_stroke = Stroke::new(1.5, p.accent);
for state in [
&mut w.noninteractive,
&mut w.inactive,
&mut w.hovered,
&mut w.active,
&mut w.open,
] {
state.corner_radius = radius;
}
v
}
const SEMIBOLD: &str = "semibold";
fn fonts() -> egui::FontDefinitions {
let mut fonts = egui::FontDefinitions::default();
for (name, data) in [
(
"Inter",
&include_bytes!("../assets/fonts/Inter-Regular.ttf")[..],
),
(
"Inter SemiBold",
&include_bytes!("../assets/fonts/Inter-SemiBold.ttf")[..],
),
] {
fonts
.font_data
.insert(name.to_owned(), Arc::new(FontData::from_static(data)));
}
let proportional = fonts.families.entry(FontFamily::Proportional).or_default();
let fallbacks = proportional.clone();
proportional.insert(0, "Inter".to_owned());
let mut semibold = vec!["Inter SemiBold".to_owned()];
semibold.extend(fallbacks);
fonts
.families
.insert(FontFamily::Name(SEMIBOLD.into()), semibold);
fonts
}
pub fn semibold(size: f32) -> FontId {
FontId::new(size, FontFamily::Name(SEMIBOLD.into()))
}
pub fn apply(ctx: &egui::Context) {
ctx.set_fonts(fonts());
ctx.set_visuals_of(Theme::Dark, visuals(Visuals::dark(), &DARK));
ctx.set_visuals_of(Theme::Light, visuals(Visuals::light(), &LIGHT));
ctx.all_styles_mut(|style| {
style.spacing.item_spacing = Vec2::new(8.0, 8.0);
style.spacing.button_padding = Vec2::new(12.0, 4.0);
style.spacing.interact_size.y = ROW_HEIGHT - 4.0;
style.spacing.menu_margin = egui::Margin::same(8);
style.interaction.selectable_labels = false;
});
}
pub fn accent(ui: &egui::Ui) -> Color32 {
ui.visuals().selection.bg_fill
}
pub fn card<R>(ui: &mut egui::Ui, add_contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
let visuals = ui.visuals();
egui::Frame::new()
.fill(visuals.faint_bg_color)
.stroke(visuals.widgets.noninteractive.bg_stroke)
.corner_radius(12.0)
.inner_margin(egui::Margin::symmetric(14, 12))
.show(ui, |ui| {
ui.set_width(ui.available_width());
add_contents(ui)
})
.inner
}
pub fn row_label(ui: &mut egui::Ui, text: &str) {
ui.allocate_ui_with_layout(
Vec2::new(0.0, ROW_HEIGHT),
egui::Layout::left_to_right(egui::Align::Center),
|ui| ui.label(RichText::new(text).color(ui.visuals().weak_text_color())),
);
}
pub fn info_icon(ui: &mut egui::Ui, text: &str) -> egui::Response {
let size = ui.spacing().interact_size.y.min(16.0);
let (rect, response) = ui.allocate_exact_size(Vec2::splat(size), egui::Sense::click());
let popup_id = response.id.with("info");
let mut open = ui.data(|d| d.get_temp::<bool>(popup_id).unwrap_or(false));
if response.clicked() {
open = !open;
}
let shown = egui::Popup::from_response(&response)
.id(popup_id)
.open_bool(&mut open)
.close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
.width(320.0)
.show(|ui| ui.label(text))
.is_some();
if shown {
egui::containers::menu::MenuState::from_id(ui.ctx(), ui.layer_id().id, |menu| {
menu.open_item = Some(popup_id);
});
}
ui.data_mut(|d| d.insert_temp(popup_id, open));
focus_ring(ui, &response, size / 2.0);
let color = if response.hovered() || open {
ui.visuals().strong_text_color()
} else {
ui.visuals().weak_text_color()
};
let painter = ui.painter();
painter.circle_stroke(
rect.center(),
size / 2.0 - 1.0,
egui::Stroke::new(1.2, color),
);
let x = rect.center().x;
painter.circle_filled(
egui::pos2(x, rect.center().y - size * 0.2),
size * 0.07,
color,
);
painter.line_segment(
[
egui::pos2(x, rect.center().y - size * 0.05),
egui::pos2(x, rect.center().y + size * 0.25),
],
egui::Stroke::new(1.5, color),
);
let response = response.on_hover_cursor(egui::CursorIcon::PointingHand);
if open {
response
} else {
response.on_hover_text(text)
}
}
pub fn primary_button(text: &str) -> egui::Button<'static> {
egui::Button::new(
RichText::new(text.to_owned())
.font(semibold(14.5))
.color(Color32::WHITE),
)
.min_size(Vec2::new(150.0, BIG_HEIGHT))
.corner_radius(9.0)
}
pub fn secondary_button(text: &str) -> egui::Button<'static> {
egui::Button::new(RichText::new(text.to_owned()).font(semibold(14.5)))
.min_size(Vec2::new(90.0, BIG_HEIGHT))
.corner_radius(9.0)
}
pub const BIG_HEIGHT: f32 = 34.0;
pub fn progress_bar(ui: &mut egui::Ui, width: f32, fraction: f32, text: &str) -> egui::Response {
let (rect, response) = ui.allocate_exact_size(Vec2::new(width, BIG_HEIGHT), Sense::hover());
let accent = accent(ui);
let text_color = ui.visuals().strong_text_color();
let painter = ui.painter();
painter.rect_filled(rect, 8.0, accent.gamma_multiply(0.22));
let split = rect.left() + rect.width() * fraction.clamp(0.0, 1.0);
let filled = rect.with_max_x(split);
if filled.width() > 0.5 {
painter
.with_clip_rect(filled)
.rect_filled(rect, 8.0, accent);
}
let galley = painter.layout_no_wrap(text.to_owned(), semibold(14.0), Color32::PLACEHOLDER);
let pos = rect.center() - galley.size() / 2.0;
painter
.with_clip_rect(filled)
.galley_with_override_text_color(pos, galley.clone(), Color32::WHITE);
painter
.with_clip_rect(rect.with_min_x(split))
.galley_with_override_text_color(pos, galley, text_color);
response
}
pub fn focus_ring(ui: &egui::Ui, response: &egui::Response, radius: f32) {
if response.has_focus() {
ui.painter().rect_stroke(
response.rect.expand(2.0),
radius + 2.0,
Stroke::new(1.5, accent(ui)),
StrokeKind::Outside,
);
}
}
pub fn primary(ui: &egui::Ui, button: egui::Button<'static>) -> egui::Button<'static> {
button.fill(accent(ui))
}
const SEGMENT_PAD: f32 = 12.0;
fn segment_galleys<'a>(
ui: &egui::Ui,
labels: impl Iterator<Item = &'a str>,
) -> Vec<std::sync::Arc<egui::Galley>> {
let font = egui::FontId::proportional(13.5);
labels
.map(|label| {
ui.painter()
.layout_no_wrap(label.to_owned(), font.clone(), Color32::PLACEHOLDER)
})
.collect()
}
fn segments_width(galleys: &[std::sync::Arc<egui::Galley>]) -> f32 {
galleys
.iter()
.map(|g| g.size().x + 2.0 * SEGMENT_PAD)
.sum::<f32>()
+ 4.0
}
pub fn segmented_width<'a>(ui: &egui::Ui, labels: impl IntoIterator<Item = &'a str>) -> f32 {
segments_width(&segment_galleys(ui, labels.into_iter()))
}
pub fn segmented<T: PartialEq + Copy>(
ui: &mut egui::Ui,
value: &mut T,
options: &[(T, &str, &str)],
) -> bool {
let galleys = segment_galleys(ui, options.iter().map(|(_, label, _)| *label));
let width = segments_width(&galleys);
let pad = SEGMENT_PAD;
let base_id = ui.next_auto_id();
let (rect, _) = ui.allocate_exact_size(Vec2::new(width, ROW_HEIGHT), Sense::hover());
let enabled = ui.is_enabled();
let visuals = ui.visuals().clone();
ui.painter().rect(
rect,
RADIUS,
visuals.widgets.inactive.bg_fill,
Stroke::NONE,
StrokeKind::Inside,
);
let mut changed = false;
let mut x = rect.left() + 2.0;
for (index, ((option, _, hint), galley)) in options.iter().zip(galleys).enumerate() {
let segment = egui::Rect::from_min_size(
egui::pos2(x, rect.top() + 2.0),
Vec2::new(galley.size().x + 2.0 * pad, ROW_HEIGHT - 4.0),
);
x = segment.right();
let id = base_id.with(index);
let response = ui
.interact(segment, id, Sense::click())
.on_hover_text(*hint);
focus_ring(ui, &response, f32::from(RADIUS - 2));
let selected = *value == *option;
if response.clicked() && !selected {
*value = *option;
changed = true;
}
let t = fade(ui, id, response.hovered() && enabled);
let fill = if selected {
visuals.selection.bg_fill
} else {
visuals.widgets.hovered.bg_fill.gamma_multiply(t)
};
let mut text = if selected {
Color32::WHITE
} else {
visuals.text_color()
};
if !enabled {
text = text.gamma_multiply(0.45);
}
let painter = ui.painter();
painter.rect_filled(segment, RADIUS - 2, fill);
painter.galley_with_override_text_color(
segment.center() - galley.size() / 2.0,
galley,
text,
);
}
changed
}
pub fn icon_button(ui: &mut egui::Ui, paint: icons::Icon) -> egui::Response {
let size = Vec2::splat(ROW_HEIGHT + 4.0);
let (rect, response) = ui.allocate_exact_size(size, Sense::click());
let t = fade(ui, response.id, response.hovered());
let visuals = ui.visuals();
let fill = mix(
visuals.widgets.inactive.bg_fill,
visuals.widgets.hovered.bg_fill,
t,
);
let color = mix(visuals.text_color(), visuals.strong_text_color(), t);
ui.painter().rect_filled(rect, RADIUS, fill);
paint(ui.painter(), rect.shrink(8.0), color);
focus_ring(ui, &response, f32::from(RADIUS));
response.on_hover_cursor(egui::CursorIcon::PointingHand)
}
pub fn capture_button(ui: &mut egui::Ui, paint: icons::Icon) -> egui::Response {
let size = Vec2::splat(ROW_HEIGHT + 4.0);
let (rect, response) = ui.allocate_exact_size(size, Sense::click());
let t = fade(ui, response.id, response.hovered());
let (fill, hover) = if ui.visuals().dark_mode {
(
Color32::from_rgb(242, 204, 104),
Color32::from_rgb(250, 220, 140),
)
} else {
(
Color32::from_rgb(240, 196, 76),
Color32::from_rgb(228, 180, 52),
)
};
let ink = Color32::from_rgb(46, 36, 10);
ui.painter().rect_filled(rect, RADIUS, mix(fill, hover, t));
paint(ui.painter(), rect.shrink(8.0), ink);
focus_ring(ui, &response, f32::from(RADIUS));
response.on_hover_cursor(egui::CursorIcon::PointingHand)
}
pub fn small_icon_button(ui: &mut egui::Ui, paint: icons::Icon) -> egui::Response {
let (rect, response) = ui.allocate_exact_size(Vec2::splat(24.0), Sense::click());
let t = fade(ui, response.id, response.hovered());
let visuals = ui.visuals();
if t > 0.0 {
ui.painter()
.rect_filled(rect, 6.0, visuals.widgets.hovered.bg_fill.gamma_multiply(t));
}
let color = mix(visuals.text_color(), visuals.strong_text_color(), t);
paint(ui.painter(), rect.shrink(6.0), color);
focus_ring(ui, &response, 6.0);
response.on_hover_cursor(egui::CursorIcon::PointingHand)
}
pub fn icon_text_button(ui: &mut egui::Ui, paint: icons::Icon, text: &str) -> egui::Response {
let font = FontId::proportional(13.5);
let galley = ui
.painter()
.layout_no_wrap(text.to_owned(), font, Color32::PLACEHOLDER);
let icon = 12.0;
let size = Vec2::new(galley.size().x + icon + 6.0 + 20.0, ROW_HEIGHT);
let (rect, response) = ui.allocate_exact_size(size, Sense::click());
let enabled = ui.is_enabled();
let t = fade(ui, response.id, response.hovered() && enabled);
let visuals = ui.visuals();
let fill = mix(
visuals.widgets.inactive.bg_fill,
visuals.widgets.hovered.bg_fill,
t,
);
let mut color = visuals.text_color();
if !enabled {
color = color.gamma_multiply(0.45);
}
let painter = ui.painter();
painter.rect_filled(rect, RADIUS, fill);
let icon_rect = egui::Rect::from_center_size(
egui::pos2(rect.left() + 10.0 + icon / 2.0, rect.center().y),
Vec2::splat(icon),
);
paint(painter, icon_rect, color);
let text_pos = egui::pos2(
icon_rect.right() + 6.0,
rect.center().y - galley.size().y / 2.0,
);
painter.galley_with_override_text_color(text_pos, galley, color);
focus_ring(ui, &response, f32::from(RADIUS));
response
}
pub fn section(ui: &mut egui::Ui, text: &str) {
ui.add_space(6.0);
ui.label(
RichText::new(text)
.font(semibold(12.5))
.color(ui.visuals().weak_text_color()),
);
}
const FADE: f32 = 0.12;
pub fn fade(ui: &egui::Ui, id: egui::Id, hovered: bool) -> f32 {
ui.ctx()
.animate_bool_with_time(id.with("hover fade"), hovered, FADE)
}
pub fn mix(a: Color32, b: Color32, t: f32) -> Color32 {
a.lerp_to_gamma(b, t.clamp(0.0, 1.0))
}
pub fn surface(
painter: &egui::Painter,
rect: egui::Rect,
radius: f32,
fill: Color32,
edge: Color32,
dark: bool,
) {
if !dark {
let shadow = egui::Shadow {
offset: [0, 1],
blur: 3,
spread: 0,
color: Color32::from_black_alpha(14),
};
painter.add(shadow.as_shape(rect, radius));
}
painter.rect(
rect,
radius,
fill,
Stroke::new(1.0, edge),
StrokeKind::Inside,
);
if dark {
let y = rect.top() + 1.5;
painter.line_segment(
[
egui::pos2(rect.left() + radius, y),
egui::pos2(rect.right() - radius, y),
],
Stroke::new(1.0, Color32::from_white_alpha(7)),
);
}
}
pub struct StripItem {
pub label: &'static str,
pub value: String,
pub accented: bool,
pub open: bool,
}
pub const STRIP_HEIGHT: f32 = 42.0;
pub fn strip(ui: &mut egui::Ui, items: &[StripItem]) -> Vec<egui::Response> {
let pad = 10.0;
let label_font = FontId::proportional(10.5);
let value_font = FontId::proportional(12.5);
let enabled = ui.is_enabled();
let width = ui.available_width();
let (rect, _) = ui.allocate_exact_size(Vec2::new(width, STRIP_HEIGHT), Sense::hover());
let base_id = ui.scope_id().with("strip");
let natural: Vec<f32> = items
.iter()
.map(|item| {
let measure = |text: &str, font: &FontId| {
ui.painter()
.layout_no_wrap(text.to_owned(), font.clone(), Color32::PLACEHOLDER)
.size()
.x
};
measure(&item.value, &value_font).max(measure(item.label, &label_font)) + 2.0 * pad
})
.collect();
let total: f32 = natural.iter().sum();
let widths: Vec<f32> = if total <= width {
let extra = (width - total) / items.len().max(1) as f32;
natural.iter().map(|w| w + extra).collect()
} else {
let mut cap = natural.iter().cloned().fold(0.0_f32, f32::max);
let fits = |cap: f32| natural.iter().map(|w| w.min(cap)).sum::<f32>() <= width;
while cap > 40.0 && !fits(cap) {
cap -= 1.0;
}
natural.iter().map(|w| w.min(cap)).collect()
};
let visuals = ui.visuals().clone();
let edge = visuals.widgets.noninteractive.bg_stroke.color;
let radius = 10.0;
surface(
ui.painter(),
rect,
radius,
visuals.faint_bg_color,
edge,
visuals.dark_mode,
);
let mut responses = Vec::new();
let mut x = rect.left();
let count = items.len();
for (index, (item, w)) in items.iter().zip(widths).enumerate() {
let part = egui::Rect::from_min_size(egui::pos2(x, rect.top()), Vec2::new(w, STRIP_HEIGHT));
x += w;
let response = ui.interact(part, base_id.with(index), Sense::click());
let t = fade(
ui,
response.id,
(response.hovered() && enabled) || item.open,
);
let corner = |first: bool, last: bool| egui::CornerRadius {
nw: if first { radius as u8 } else { 0 },
sw: if first { radius as u8 } else { 0 },
ne: if last { radius as u8 } else { 0 },
se: if last { radius as u8 } else { 0 },
};
let painter = ui.painter();
if t > 0.0 {
painter.rect_filled(
part.shrink(1.0),
corner(index == 0, index + 1 == count),
visuals.widgets.hovered.bg_fill.gamma_multiply(t),
);
}
if index > 0 {
painter.line_segment(
[
egui::pos2(part.left(), part.top() + 9.0),
egui::pos2(part.left(), part.bottom() - 9.0),
],
Stroke::new(1.0, edge),
);
}
let mut weak = visuals.weak_text_color();
let mut value = if item.accented {
visuals.selection.bg_fill
} else {
visuals.text_color()
};
if !enabled {
weak = weak.gamma_multiply(0.45);
value = value.gamma_multiply(0.45);
}
let max = (w - 2.0 * pad).max(10.0);
let elided = |text: &str, font: &FontId, color: Color32| {
let mut job =
egui::text::LayoutJob::simple_singleline(text.to_owned(), font.clone(), color);
job.wrap = egui::text::TextWrapping::truncate_at_width(max);
painter.layout_job(job)
};
let label = elided(item.label, &label_font, weak);
let shown = elided(&item.value, &value_font, value);
let top = part.center().y - (label.size().y + 1.0 + shown.size().y) / 2.0;
painter.galley(egui::pos2(part.left() + pad, top), label.clone(), weak);
painter.galley(
egui::pos2(part.left() + pad, top + label.size().y + 1.0),
shown,
value,
);
focus_ring(ui, &response, 6.0);
responses.push(response.on_hover_cursor(egui::CursorIcon::PointingHand));
}
responses
}