Tabs
A bar of tabs drawn by a style (tab, underline, pill, outline, segmented or your own), with the chosen tab’s content below.
Tabs
TabBar::new("tabs", &mut state.tabs)
.tabs(pages())
.show_content(ui, |ui, selected| {
ui.label(format!("The {} page.", PAGES[selected]));
});
Underline
let _ = TabBar::new("underline", &mut state.underline)
.tabs(pages())
.style(&tab_bar_styles::Underline)
.show(ui);
Pill
let _ = TabBar::new("pill", &mut state.pill)
.tabs(pages())
.style(&tab_bar_styles::Pill)
.show(ui);
Outline
let _ = TabBar::new("outline", &mut state.outline)
.tabs(pages())
.style(&tab_bar_styles::Outline)
.show(ui);
Segmented
let _ = TabBar::new("segmented", &mut state.segmented)
.tabs(["Day", "Week", "Month", "Year"])
.style(&tab_bar_styles::Segmented)
.show(ui);
ui.add_space(8.0);
let ctx = ui.ctx().clone();
let _ = TabBar::new("icons", &mut state.icons)
.tab(Tab::icon_only(super::empty::glyph(&ctx, "inbox"), "Inbox"))
.tab(Tab::icon_only(super::empty::glyph(&ctx, "bell"), "Alerts"))
.tab(Tab::icon_only(super::empty::glyph(&ctx, "star"), "Starred"))
.style(&tab_bar_styles::Segmented)
.show(ui);
Sizes
let sizes = [Size::XSmall, Size::Small, Size::Medium, Size::Large];
for (index, size) in sizes.into_iter().enumerate() {
let _ = TabBar::new(("size", index), &mut state.sizes[index])
.tabs(["Overview", "Activity", "Settings"])
.style(&tab_bar_styles::Segmented)
.size(size)
.show(ui);
ui.add_space(8.0);
}
Counts and icons
let ctx = ui.ctx().clone();
let _ = TabBar::new("counts", &mut state.counts)
.tab(
Tab::new("Inbox")
.icon(super::empty::glyph(&ctx, "inbox"))
.suffix("12"),
)
.tab(
Tab::new("Starred")
.icon(super::empty::glyph(&ctx, "star"))
.suffix("3"),
)
.tab(Tab::new("Archive").icon(super::empty::glyph(&ctx, "folder")))
.style(&tab_bar_styles::Underline)
.show(ui);
Closable tabs
let documents = &mut state.documents;
let bar = TabBar::new("documents", &mut state.document)
.tabs(
documents
.iter()
.map(|name| Tab::new(name.clone()).closable()),
)
.show(ui);
if let Some(closed) = bar.closed {
documents.remove(closed);
if state.document >= documents.len() && state.document > 0 {
state.document -= 1;
}
}
ui.add_space(8.0);
if ui.add(Button::new("New tab").small()).clicked() {
documents.push(format!("untitled-{}.rs", state.next_document));
state.next_document += 1;
state.document = documents.len() - 1;
}
Filling space
let _ = TabBar::new("fill", &mut state.fill)
.tabs(["About", "Profile"])
.style(&tab_bar_styles::Segmented)
.fill()
.show(ui);
A style for a whole area
// Every tab bar inside takes the style and size, unless it
// picks its own.
ui.control_size(Size::Small, |ui| {
ui.tab_bar_style(tab_bar_styles::Pill, |ui| {
let [view, range] = &mut state.area;
let _ = TabBar::new("area-view", view)
.tabs(["List", "Board", "Calendar"])
.show(ui);
ui.add_space(8.0);
let _ = TabBar::new("area-range", range)
.tabs(["Day", "Week", "Month"])
.style(&tab_bar_styles::Segmented)
.show(ui);
});
});
Your own style
let ctx = ui.ctx().clone();
let _ = TabBar::new("own", &mut state.own)
.tab(Tab::new("Tones").suffix("128"))
.tab(Tab::new("Setlists").icon(super::empty::glyph(&ctx, "star")))
.tab(Tab::new("Backups").disabled(true))
.tab("Firmware")
.style(&Lamp)
.show(ui);
/// Muted labels that light up when chosen, with a short lamp under the
/// chosen one that slides from tab to tab.
struct Lamp;
impl TabBarStyle for Lamp {
fn metrics(&self, size: Size) -> TabMetrics {
let inner_height = match size {
Size::XSmall => 24.0,
Size::Small => 32.0,
Size::Medium => 36.0,
Size::Large => 40.0,
};
TabMetrics {
height: inner_height + 8.0,
inner_height,
padding: 12.0,
gap: 2.0,
bar_padding: 0.0,
}
}
fn paint(&self, ui: &egui::Ui, tab: &TabConfiguration<'_>) {
let theme = tokens(ui.ctx());
let s = tab.state;
let color = if !s.enabled {
theme.muted_foreground.gamma_multiply(0.5)
} else if s.selected || s.hovered {
theme.foreground
} else {
theme.muted_foreground
};
tab.paint_content(ui, &theme, color);
tab.paint_focus_ring(&theme, tab.rect, theme.shape.radius);
}
fn slides(&self) -> bool {
true
}
fn paint_indicator(&self, ui: &egui::Ui, bar: &TabBarConfiguration<'_>, rect: egui::Rect) {
let theme = tokens(ui.ctx());
let lamp = egui::Rect::from_center_size(
egui::pos2(rect.center().x, rect.bottom() - 2.0),
egui::vec2(rect.width() * 0.4, 3.0),
);
bar.strip_painter()
.rect_filled(lamp, 1.5, theme.primary.background);
}
}
The story above is gallery/src/stories/tabs.rs, running in your browser. Every snippet on this page is cut from it.