use std::{hash::Hash, time::Duration};
use super::{Response, Sense, Tooltip, Ui, Widget};
use crate::{
context::Paint, AccessOrientation, AccessRole, Border, Color, Easing, Id, ImageSource, Rect,
Shape, TweenOptions, Vec2,
};
struct IconTab<T> {
value: T,
icon: ImageSource,
label: String,
}
pub struct IconTabs<'a, T> {
selected: &'a mut T,
tabs: Vec<IconTab<T>>,
id: Option<Id>,
tab_size: Vec2,
icon_size: f32,
gap: f32,
rounding: f32,
idle: Option<Color>,
active: Option<Color>,
fill: Option<Color>,
border: Option<Color>,
selected_opacity: f32,
hover_opacity: f32,
outline_opacity: f32,
duration: Duration,
}
impl<'a, T: PartialEq + Hash + Clone> IconTabs<'a, T> {
pub fn new<I, L>(selected: &'a mut T, tabs: impl IntoIterator<Item = (T, I, L)>) -> Self
where
I: Into<ImageSource>,
L: Into<String>,
{
Self {
selected,
tabs: tabs
.into_iter()
.map(|(value, icon, label)| IconTab {
value,
icon: icon.into(),
label: label.into(),
})
.collect(),
id: None,
tab_size: Vec2::new(42.0, 44.0),
icon_size: 24.0,
gap: 5.0,
rounding: 6.0,
idle: None,
active: None,
fill: None,
border: None,
selected_opacity: 0.52,
hover_opacity: 0.18,
outline_opacity: 0.48,
duration: Duration::from_millis(160),
}
}
pub fn id_source(mut self, source: impl Hash) -> Self {
self.id = Some(Id::new(source));
self
}
pub fn tab_size(mut self, size: Vec2) -> Self {
self.tab_size = size.max(Vec2::splat(8.0));
self
}
pub fn icon_size(mut self, size: f32) -> Self {
self.icon_size = size.max(1.0);
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = gap.max(0.0);
self
}
pub fn corner_radius(mut self, radius: f32) -> Self {
self.rounding = radius.max(0.0);
self
}
pub fn idle_color(mut self, color: Color) -> Self {
self.idle = Some(color);
self
}
pub fn active_color(mut self, color: Color) -> Self {
self.active = Some(color);
self
}
pub fn fill(mut self, color: Color) -> Self {
self.fill = Some(color);
self
}
pub fn border(mut self, color: Color) -> Self {
self.border = Some(color);
self
}
pub fn opacities(mut self, selected: f32, hover: f32, outline: f32) -> Self {
self.selected_opacity = selected.clamp(0.0, 1.0);
self.hover_opacity = hover.clamp(0.0, 1.0);
self.outline_opacity = outline.clamp(0.0, 1.0);
self
}
pub fn duration(mut self, duration: Duration) -> Self {
self.duration = duration;
self
}
}
impl<T: PartialEq + Hash + Clone> Widget for IconTabs<'_, T> {
fn ui(self, ui: &mut Ui<'_>) -> Response {
let style = ui.style().clone();
let count = self.tabs.len();
let strip = Vec2::new(
self.tab_size.x,
self.tab_size.y * count as f32 + self.gap * count.saturating_sub(1) as f32,
);
let bounds = ui.allocate_space(strip);
let id = ui
.scope
.with(("icon-tabs", self.id.unwrap_or_else(|| Id::new(count))));
let mut whole = ui.response(id, bounds, false);
let idle = self.idle.unwrap_or(style.muted_text);
let active = self.active.unwrap_or(style.accent);
let fill = self.fill.unwrap_or(style.selected_fill);
let outline = self.border.unwrap_or(style.border.color);
let ease = || TweenOptions::new(self.duration).easing(Easing::QuadOut);
let list = ui.a11y_begin(id, AccessRole::TabList, |node| {
node.orientation(AccessOrientation::Vertical);
});
let mut nodes = Vec::new();
let mut picked = None;
for (index, tab) in self.tabs.into_iter().enumerate() {
let key = Id::new(&tab.value);
let rect = Rect::from_min_size(
bounds.min + Vec2::new(0.0, (self.tab_size.y + self.gap) * index as f32),
self.tab_size,
);
let mut response = ui.interact(rect, ("icon-tab", key), Sense::CLICK | Sense::FOCUS);
let selected = *self.selected == tab.value;
if response.clicked() && !selected {
*self.selected = tab.value.clone();
whole.changed = true;
response.changed = true;
picked = Some(index);
}
let selected = *self.selected == tab.value;
if ui.context.a11y_on() {
nodes.push(ui.context.a11y_len());
ui.a11y(response.id, rect, AccessRole::Tab, |node| {
node.label(tab.label.as_str())
.selected(selected)
.position_in_set(index, count)
.clicks(response.id);
});
}
let fill_opacity = if selected {
self.selected_opacity
} else if response.hovered && response.enabled {
self.hover_opacity
} else {
0.0
};
let fill_opacity = ui.transition(("fill", key), fill_opacity, ease()).value;
let outline_opacity = ui
.transition(
("outline", key),
if selected { self.outline_opacity } else { 0.0 },
ease(),
)
.value;
let icon = ui
.transition(("icon", key), if selected { active } else { idle }, ease())
.value;
let icon = if response.enabled {
icon
} else {
style.disabled_text
};
let mut shapes = Vec::new();
if fill_opacity > 0.0 {
shapes.push(Paint::Shape(
Shape::rect(rect, fill.with_opacity(fill_opacity))
.corner_radius(self.rounding)
.into(),
));
}
let ring = if response.focus_visible {
Some(style.focus_border)
} else if outline_opacity > 0.0 {
Some(Border::new(1.0, outline.with_opacity(outline_opacity)))
} else {
None
};
if let Some(border) = ring {
shapes.push(Paint::Shape(
Shape::rect(rect, Color::TRANSPARENT)
.corner_radius(self.rounding)
.border(border)
.into(),
));
}
if !shapes.is_empty() {
ui.context
.paint(response.id.with("surface"), ui.window, ui.clip, shapes);
}
let glyph = Rect::from_min_size(
rect.center() - Vec2::splat(self.icon_size * 0.5),
Vec2::splat(self.icon_size),
);
ui.paint_image_in(response.id.with("icon"), tab.icon, glyph, icon);
Tooltip::new(tab.label).show(ui, response);
}
if let Some(picked) = picked {
for (index, node) in nodes.into_iter().enumerate() {
if let Some(node) = ui.context.a11y_node_mut(node) {
node.selected(index == picked);
}
}
}
ui.a11y_end(list, Some(bounds));
whole
}
}
impl Ui<'_> {
pub fn icon_tabs<T, I, L>(
&mut self,
selected: &mut T,
tabs: impl IntoIterator<Item = (T, I, L)>,
) -> Response
where
T: PartialEq + Hash + Clone,
I: Into<ImageSource>,
L: Into<String>,
{
self.add(IconTabs::new(selected, tabs))
}
}