feat(gui): the tab bar spans the window above the sidebar; tabs carry a badge and a name

A tab is the top level (D-78): the sidebar, with its badge, pages and
status, is the company in the tab, so the bar now runs across the whole
window above it. The bar's height (tabs::BAR_H) is taken from the window:
the four sidenav tests that size a window for the sidebar add it, through
that constant, and the smallest window is 400 points plus the bar. The
sidebar below the bar is pixel for pixel the old 1440x920 golden image;
only its shapes hash moved, with the shapes lower by the bar.

Tabs are as wide as badge plus name between a least and a greatest width,
the name elided with the full name in the hover; the one in front is the
lit surface with the accent bar, open at its foot. Room is left before the
close control for a later state mark. The + follows the last tab (at the
right edge, whole, when the tabs scroll), says 'Open a company' on hover,
and lists companies by remembered name with the id beside it; a name is
remembered when a company's books load (company.<id>.name) and titles a
tab before its books load.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 11:41:10 -07:00
1 parent d7358b605b
commit 80a24a6262
17 files changed
+531 -140

No files matched your search

+67 -20
View File
@@ -22,7 +22,8 @@
//! 4. What is kept for next time: the open companies (`open_companies`,
//! the ids of their books, comma separated) and the one in front
//! (`active_company`); for each company its page, its report's
//! period, columns and comparison (`company.<id>.…`); and, as
//! period, columns and comparison and its name as last known
//! (`company.<id>.…`); and, as
//! before and under the same keys, the scheme and its filter
//! (`scheme`, `scheme_filter`, `dark`) and the dock (`dock_open`,
//! `dock_agent`, the front tab's).
@@ -35,7 +36,7 @@ use crate::company::CompanyView;
use crate::data::Source;
use crate::nav::Route;
use crate::scheme_menu::SchemeMenu;
use crate::tabs::{Tab, TabAction, TabBar};
use crate::tabs::{Listed, Tab, TabAction, TabBar};
use crate::theme::{self, pal};
use egui::{Frame, RichText, Ui};
use std::collections::HashMap;
@@ -73,6 +74,10 @@ pub struct App {
/// What was kept for companies not open now (and for those that are,
/// as they were last closed), by key.
kept: HashMap<String, String>,
/// Each company's name as last known, by the id of its books: kept for
/// the tab's title before the books load and for the "+" list, which
/// doesn't read every company's journal for it.
names: HashMap<String, String>,
/// The dock's preferences as they were kept, for a dock made while no
/// tab has one.
dock_prefs: (Option<String>, Option<String>),
@@ -223,6 +228,7 @@ impl App {
root: None,
today: None,
kept: HashMap::new(),
names: HashMap::new(),
dock_prefs: (None, None),
title: TITLE.to_string(),
welcome: None,
@@ -269,7 +275,7 @@ impl App {
/// The window's title now.
pub fn window_title(&self) -> String {
match self.active_view() {
Some(v) => format!("{} · {TITLE}", v.title()),
Some(v) => format!("{} · {TITLE}", self.title_of(v)),
None => TITLE.to_string(),
}
}
@@ -356,6 +362,9 @@ impl App {
return;
};
for id in list_books(&root) {
if let Some(name) = get(&format!("company.{id}.name")) {
self.names.insert(id.clone(), name);
}
for k in crate::company::KEPT_KEYS {
let key = format!("company.{id}.{k}");
if let Some(v) = get(&key) {
@@ -383,20 +392,55 @@ impl App {
}
}
/// The ids of the data root's companies that aren't open.
fn not_open(&self) -> impl Fn() -> Vec<String> + use<> {
/// The data root's companies that aren't open, with the names last
/// known (reading the ids is all it does).
fn not_open(&self) -> impl Fn() -> Vec<Listed> + use<> {
let root = self.root.clone();
let open: Vec<String> = self.views.iter().filter_map(CompanyView::book_id).collect();
let names = self.names.clone();
move || {
root.as_deref()
.map(list_books)
.unwrap_or_default()
.into_iter()
.filter(|id| !open.contains(id))
.map(|id| Listed {
name: names.get(&id).cloned(),
id,
})
.collect()
}
}
/// Note the name of every company whose books are loaded.
fn learn_names(&mut self) {
for v in &self.views {
if let (Some(id), Some(name)) = (v.book_id(), v.name())
&& self.names.get(&id) != Some(&name)
{
self.names.insert(id, name);
}
}
}
/// The name last known for the company with books `id` (tests).
pub fn remembered_name(&self, id: &str) -> Option<&str> {
self.names.get(id).map(String::as_str)
}
/// A view's tab title: the name its books carry, else the name last
/// known, else the id.
fn title_of(&self, v: &CompanyView) -> String {
v.name()
.or_else(|| self.names.get(&v.book_id()?).cloned())
.unwrap_or_else(|| v.title())
}
/// Tab `i`'s title as the bar shows it (tests).
pub fn tab_title(&self, i: usize) -> Option<String> {
self.views.get(i).map(|v| self.title_of(v))
}
/// Do what the tab bar was asked.
fn act(&mut self, ctx: &egui::Context, action: TabAction) {
match action {
@@ -561,30 +605,30 @@ impl eframe::App for App {
for (i, v) in self.views.iter_mut().enumerate() {
v.pump(&ctx, i == self.active);
}
self.learn_names();
let tabs: Vec<Tab> = self
.views
.iter()
.map(|v| Tab {
title: v.title(),
detail: v.book_id().unwrap_or_else(|| v.title()),
.map(|v| {
let title = self.title_of(v);
Tab {
detail: match v.book_id() {
Some(id) if id != title => format!("{title}\n{id}"),
_ => title.clone(),
},
title,
}
})
.collect();
let shown = self.active();
let others = self.not_open();
let mut action = None;
// The bar goes where the front view says (beside its sidebar, which
// keeps the window's whole height), or across the window when
// there is no view.
let mut bar = |ui: &mut Ui| {
action = self.tabs.show(ui, &tabs, shown, &others);
};
// The bar goes across the whole window, above everything of the
// company in front: a tab is the top level (D-78).
let action = self.tabs.show(ui, &tabs, shown, &others);
let active = self.active;
match self.views.get_mut(active) {
Some(v) => v.ui(ui, &mut self.schemes, &mut bar),
None => {
bar(ui);
self.empty(ui);
}
Some(v) => v.ui(ui, &mut self.schemes),
None => self.empty(ui),
}
if let Some(action) = action {
self.act(&ctx, action);
@@ -634,6 +678,9 @@ impl eframe::App for App {
for (k, v) in &self.kept {
storage.set_string(k, v.clone());
}
for (id, name) in &self.names {
storage.set_string(&format!("company.{id}.name"), name.clone());
}
for v in &self.views {
if let Some(id) = v.book_id() {
v.keep(&mut |k, val| storage.set_string(&format!("company.{id}.{k}"), val));
+18 -25
View File
@@ -34,9 +34,7 @@ use crate::receipts::Receipts;
use crate::scheme_menu::SchemeMenu;
use crate::schemes;
use crate::theme::{self, pal};
use egui::{
Align, CornerRadius, Frame, Key, Layout, Margin, RichText, Sense, Stroke, Ui, UiBuilder, vec2,
};
use egui::{Align, CornerRadius, Frame, Key, Layout, Margin, RichText, Sense, Stroke, Ui, vec2};
use egui_phosphor::regular as icon;
use std::sync::Arc;
@@ -191,15 +189,19 @@ impl CompanyView {
.flatten()
}
/// The name the books carry, once they are loaded (and it isn't
/// blank).
pub fn name(&self) -> Option<String> {
self.books
.as_ref()
.map(|b| b.company_name().trim().to_string())
.filter(|n| !n.is_empty())
}
/// The tab's title: the name the books carry, else the id of the
/// books, else the folder's name.
pub fn title(&self) -> String {
let named = self
.books
.as_ref()
.map(|b| b.company_name().trim().to_string())
.filter(|n| !n.is_empty());
named
self.name()
.or_else(|| self.book_id())
.or_else(|| {
let dir = self.data_dir.as_deref()?;
@@ -350,28 +352,19 @@ impl CompanyView {
/// The window: the sidebar, the top bar, the dock and the page, with
/// every id in them scoped to this view (INVARIANT 2).
///
/// `bar` draws the app's tab bar. It goes where the sidebar has already
/// taken its column and nothing else has been laid out: along the top
/// of the rest of the window, so the sidebar keeps all the height the
/// owner's window gives it (`tests/sidenav.rs`), and in a scope of its
/// own, so the bar's ids are the app's and not this view's.
pub fn ui(&mut self, ui: &mut Ui, schemes: &mut SchemeMenu, bar: &mut dyn FnMut(&mut Ui)) {
/// The app's tab bar is already across the top of `ui` (`tabs.rs`):
/// this window is what is below it, and the sidebar's height is the
/// window's less the bar's (`tests/sidenav.rs` adds the bar's height
/// to the windows it sizes).
pub fn ui(&mut self, ui: &mut Ui, schemes: &mut SchemeMenu) {
let id = self.id;
ui.push_id(id, |ui| self.window(ui, schemes, bar));
ui.push_id(id, |ui| self.window(ui, schemes));
}
fn window(&mut self, ui: &mut Ui, schemes: &mut SchemeMenu, bar: &mut dyn FnMut(&mut Ui)) {
fn window(&mut self, ui: &mut Ui, schemes: &mut SchemeMenu) {
self.handle_keys(ui);
let compact = ui.available_width() < COMPACT_BELOW;
self.sidebar(ui, schemes, compact);
// No space between the bar and what follows it (the scope's
// advance would add the item spacing: a strip of bare window).
let spacing = std::mem::take(&mut ui.spacing_mut().item_spacing);
ui.scope_builder(
UiBuilder::new().id(egui::Id::new("company_tab_bar")),
|ui| bar(ui),
);
ui.spacing_mut().item_spacing = spacing;
self.topbar(ui);
self.draw_dock(ui);
let p = pal(ui.ctx());
+4 -3
View File
@@ -22,7 +22,7 @@ mod prefs;
mod receipts;
mod scheme_menu;
mod schemes;
mod tabs;
pub mod tabs;
#[cfg(test)]
mod test_support;
mod theme;
@@ -140,8 +140,9 @@ pub fn run(
.with_app_id("wanderbooks")
.with_inner_size([1440.0, 920.0])
// Small on purpose: tiling window managers hand out small slots
// regardless, and every screen lays itself out to fit.
.with_min_inner_size([360.0, 400.0]),
// regardless, and every screen lays itself out to fit. The
// height is the window's below the tab bar (tabs::BAR_H).
.with_min_inner_size([360.0, 400.0 + tabs::BAR_H]),
// The platform's place for app settings, never the books' folder
// (prefs.rs).
persistence_path: prefs::prepare(),
+207 -80
View File
@@ -7,6 +7,12 @@
//! points); with no company open there is no sidebar and it runs the whole
//! width.
//!
//! The bar spans the whole window, above the sidebar: a tab is the top
//! level, and the sidebar (its company badge, its pages, its status) is
//! the company in the tab's, which the layout shows. With no company open
//! it is the window's top all the same. The "+" follows the last tab, and
//! stays at the right edge, whole, while the tabs scroll.
//!
//! ```text
//! GOAL: a person has several companies open at once, each in its own
//! tab, and moves between them, opens and closes them, without
@@ -16,8 +22,9 @@
//! company is reachable exactly while it has a tab).
//! INVARIANTS:
//! 1. One tab per open company, in the order opened, showing the
//! company's name (the id of its books until the name is read). The
//! bar is there with one company open, and with none.
//! company's badge (as the sidebar's header draws it, small) and
//! name (the name last known, else the id of its books, until it is
//! read). The bar is there with one company open, and with none.
//! 2. The tab shown is marked by more than colour: a heavier name, a
//! bar in the accent colour along its top edge, and "selected" to
//! assistive technology.
@@ -25,12 +32,15 @@
//! technology with the company's name; a middle click on a tab
//! closes it as well.
//! 4. "+" lists only companies of the data root that are not open
//! (a company can't be opened twice), and says so when there are
//! none.
//! 5. Tabs never overlap or leave the bar: they shrink to a least
//! width, then the strip scrolls, with a step control at each end
//! that is clipped; the tab shown is scrolled into view when it
//! changes.
//! (a company can't be opened twice), by name with the id of the
//! books beside it (the id alone for a company never opened), and
//! says so when there are none.
//! 5. Tabs never overlap or leave the bar: each is as wide as its
//! content between a least and a greatest width (a longer name is
//! cut, whole in its hover text), then the strip scrolls, with a
//! step control at each end; the tab shown is scrolled into view
//! when it changes. Each tab leaves room left of its close control
//! for a state mark (drawn by a later unit, nothing now).
//! 6. This only says what was asked (show, close, open); the app does
//! it.
//! ```
@@ -42,13 +52,30 @@ use egui::{
use egui_phosphor::regular as icon;
use std::time::{Duration, Instant};
/// The bar's height.
/// The bar's height: what the bar takes from the window, which the tests
/// of the sidebar's fit add to their window heights (D-78).
pub const BAR_H: f32 = 36.0;
/// A tab's widest and least widths (a name is cut with an ellipsis to fit).
const TAB_MAX: f32 = 220.0;
const TAB_MIN: f32 = 120.0;
const TAB_MAX: f32 = 240.0;
const TAB_MIN: f32 = 110.0;
/// The close control's size.
const CLOSE: f32 = 20.0;
/// The badge's size, and its gap to the name.
const BADGE: f32 = 18.0;
const GAP: f32 = 8.0;
/// Room left of the close control for the connection state, which a later
/// unit draws there.
const STATE_W: f32 = 14.0;
/// The room round a tab's content: left and right.
const PAD_L: f32 = 10.0;
const PAD_R: f32 = 6.0;
/// The "+"'s width, and the gap before it.
const PLUS_W: f32 = 30.0;
const PLUS_GAP: f32 = 6.0;
/// The "+" list's width.
const LIST_W: f32 = 260.0;
/// The gap between tabs.
const TAB_GAP: f32 = 2.0;
/// How long a reading of the data root's companies is kept while "+" is
/// open.
const LIST_KEPT: Duration = Duration::from_millis(750);
@@ -61,6 +88,15 @@ pub struct Tab {
pub detail: String,
}
/// A company of the data root that is not open, as "+" lists it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Listed {
/// The id of its books.
pub id: String,
/// Its name as last known, if it was ever opened here.
pub name: Option<String>,
}
/// What a person asked of the bar.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TabAction {
@@ -77,7 +113,7 @@ pub enum TabAction {
#[derive(Default)]
pub struct TabBar {
reveal: bool,
listed: Option<(Instant, Vec<String>)>,
listed: Option<(Instant, Vec<Listed>)>,
}
impl TabBar {
@@ -86,15 +122,16 @@ impl TabBar {
self.reveal = true;
}
/// The bar. `shown` is the tab in front, if there is one; `others`
/// reads the ids of the root's companies that are not open (called
/// now and then while "+" is open, never otherwise).
/// The bar, across the top of the window. `shown` is the tab in
/// front, if there is one; `others` reads the companies of the root
/// that are not open (called now and then while "+" is open, never
/// otherwise).
pub fn show(
&mut self,
ui: &mut Ui,
tabs: &[Tab],
shown: Option<usize>,
others: &dyn Fn() -> Vec<String>,
others: &dyn Fn() -> Vec<Listed>,
) -> Option<TabAction> {
let p = pal(ui.ctx());
let mut action = None;
@@ -102,19 +139,27 @@ impl TabBar {
egui::Panel::top("company_tabs")
.exact_size(BAR_H)
.resizable(false)
.frame(
Frame::new()
.fill(p.sidebar)
.inner_margin(Margin::symmetric(8, 0))
.stroke(Stroke::new(1.0, p.border)),
)
.frame(Frame::new().inner_margin(Margin::symmetric(8, 0)))
.show(ui, |ui| {
ui.spacing_mut().item_spacing = vec2(2.0, 0.0);
ui.spacing_mut().item_spacing = vec2(TAB_GAP, 0.0);
let bar = ui.max_rect();
// The "+" at the right end, always whole; the tabs get the
// rest.
let plus_w = 30.0;
let strip_w = (ui.available_width() - plus_w - 6.0).max(0.0);
// The line along the bar's foot; the tab in front covers
// it, and so is one with what is below.
let outer = ui.clip_rect();
ui.painter().rect_filled(outer, 0.0, p.sidebar);
ui.painter().hline(
outer.x_range(),
outer.bottom() - 0.5,
Stroke::new(1.0, p.border),
);
// The tabs as wide as they are, the "+" right after them;
// when they don't fit, the "+" stays at the right edge
// and the tabs scroll.
let widths: Vec<f32> = tabs.iter().map(|t| tab_width(ui, t)).collect();
let total: f32 =
widths.iter().sum::<f32>() + widths.len().saturating_sub(1) as f32 * TAB_GAP;
let room = (ui.available_width() - PLUS_W - PLUS_GAP).max(0.0);
let strip_w = total.min(room);
let strip = egui::Rect::from_min_size(bar.min, vec2(strip_w, bar.height()));
let mut strip_ui = ui.new_child(
UiBuilder::new()
@@ -122,10 +167,18 @@ impl TabBar {
.layout(Layout::left_to_right(Align::Max)),
);
strip_ui.set_clip_rect(strip);
self.strip(&mut strip_ui, tabs, shown, &mut reveal, &mut action);
self.strip(
&mut strip_ui,
tabs,
&widths,
total > room + 0.5,
shown,
&mut reveal,
&mut action,
);
let plus = egui::Rect::from_min_size(
egui::pos2(strip.right() + 6.0, bar.top()),
vec2(plus_w, bar.height()),
egui::pos2(strip.right() + PLUS_GAP, bar.top()),
vec2(PLUS_W, bar.height()),
);
let mut plus_ui = ui.new_child(
UiBuilder::new()
@@ -143,28 +196,29 @@ impl TabBar {
}
/// The tabs, scrolling sideways when they don't fit.
#[allow(clippy::too_many_arguments)]
fn strip(
&mut self,
ui: &mut Ui,
tabs: &[Tab],
widths: &[f32],
overflows: bool,
shown: Option<usize>,
reveal: &mut bool,
action: &mut Option<TabAction>,
) {
let p = pal(ui.ctx());
let room = ui.available_width();
let n = tabs.len().max(1) as f32;
let each = ((room - (n - 1.0) * 2.0) / n).clamp(TAB_MIN, TAB_MAX);
let overflows = each * n + (n - 1.0) * 2.0 > room + 0.5;
// A step control at each end while the tabs overflow.
let step_w = if overflows { 22.0 } else { 0.0 };
let step_by = widths.first().copied().unwrap_or(TAB_MIN) + TAB_GAP;
let area_id = ui.id().with("tab_scroll");
let mut step = 0.0_f32;
if overflows {
let (r, resp) = ui.allocate_exact_size(vec2(step_w, 26.0), Sense::click());
step_button(ui, r, &resp, icon::CARET_LEFT, "Earlier tabs");
if resp.clicked() {
step = -each;
step = -step_by;
}
}
let view_w = room - 2.0 * step_w;
@@ -178,11 +232,11 @@ impl TabBar {
.max_width(view_w.max(0.0))
.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden)
.show(ui, |ui| {
ui.spacing_mut().item_spacing.x = 2.0;
ui.spacing_mut().item_spacing.x = TAB_GAP;
ui.horizontal_top(|ui| {
for (i, tab) in tabs.iter().enumerate() {
let here = shown == Some(i);
let r = one_tab(ui, i, tab, here, each, &p, action);
let r = one_tab(ui, i, tab, here, widths[i], &p, action);
if here && *reveal {
r.scroll_to_me(Some(Align::Center));
*reveal = false;
@@ -197,7 +251,7 @@ impl TabBar {
step_button(ui, r, &resp, icon::CARET_RIGHT, "Later tabs");
if resp.clicked() {
let mut s = out.state;
s.offset.x = (offset + each).min(max);
s.offset.x = (offset + step_by).min(max);
s.store(ui.ctx(), out.id);
}
}
@@ -210,16 +264,16 @@ impl TabBar {
fn plus(
&mut self,
ui: &mut Ui,
others: &dyn Fn() -> Vec<String>,
others: &dyn Fn() -> Vec<Listed>,
action: &mut Option<TabAction>,
) {
let p = pal(ui.ctx());
let button = egui::Button::new(RichText::new(icon::PLUS).size(16.0).color(p.muted))
let button = egui::Button::new(RichText::new(icon::PLUS).size(16.0).color(p.text))
.frame(false)
.min_size(vec2(28.0, 28.0));
let (response, open) =
egui::containers::menu::MenuButton::from_button(button).ui(ui, |ui| {
ui.set_min_width(200.0);
ui.set_min_width(LIST_W);
let stale = self
.listed
.as_ref()
@@ -231,11 +285,11 @@ impl TabBar {
let list = self
.listed
.as_ref()
.map(|(_, l)| l.as_slice())
.unwrap_or(&[]);
.map(|(_, l)| l.clone())
.unwrap_or_default();
if list.is_empty() {
ui.label(
RichText::new("Every company here is open.")
RichText::new("Every company is already open.")
.font(theme::regular(13.0))
.color(p.muted),
);
@@ -245,9 +299,9 @@ impl TabBar {
.font(theme::regular(12.0))
.color(p.muted),
);
for id in list {
if ui.button(id).clicked() {
*action = Some(TabAction::Open(id.clone()));
for c in &list {
if listed_row(ui, c, &p).clicked() {
*action = Some(TabAction::Open(c.id.clone()));
ui.close();
}
}
@@ -257,13 +311,54 @@ impl TabBar {
if open.is_none() {
self.listed = None;
}
// Named as it always was for assistive technology; the hover says
// what it does.
response.widget_info(|| {
egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Open another company")
});
response.on_hover_text("Open another company");
response.on_hover_text("Open a company");
}
}
/// One company in the "+" list: its name, the id of its books beside it
/// in small text (the id alone when the name was never known).
fn listed_row(ui: &mut Ui, c: &Listed, p: &theme::Palette) -> egui::Response {
let (rect, resp) = ui.allocate_exact_size(vec2(LIST_W, 28.0), Sense::click());
let primary = c.name.as_deref().unwrap_or(&c.id);
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, primary));
if resp.hovered() {
ui.painter()
.rect_filled(rect, CornerRadius::same(6), p.hover);
}
let mut right = rect.right() - 8.0;
if c.name.is_some() {
let g = ui
.painter()
.layout_no_wrap(c.id.clone(), theme::regular(11.0), p.muted);
right -= g.size().x;
ui.painter().galley(
egui::pos2(right, rect.center().y - g.size().y / 2.0),
g,
p.muted,
);
}
let mut job = egui::text::LayoutJob::single_section(
primary.to_string(),
egui::TextFormat::simple(theme::regular(13.0), p.text),
);
job.wrap.max_width = (right - 8.0 - (rect.left() + 8.0)).max(10.0);
job.wrap.max_rows = 1;
job.wrap.break_anywhere = true;
job.wrap.overflow_character = Some('…');
let g = ui.painter().layout_job(job);
ui.painter().galley(
egui::pos2(rect.left() + 8.0, rect.center().y - g.size().y / 2.0),
g,
p.text,
);
resp
}
/// A step control at an end of the strip.
fn step_button(ui: &Ui, r: egui::Rect, resp: &egui::Response, glyph: &str, label: &'static str) {
let p = pal(ui.ctx());
@@ -280,6 +375,22 @@ fn step_button(ui: &Ui, r: egui::Rect, resp: &egui::Response, glyph: &str, label
);
}
/// What a tab holds besides its name: the badge, the room for the state
/// mark, the close control and the room round them.
const TAB_FIXED: f32 = PAD_L + BADGE + GAP + GAP + STATE_W + CLOSE + PAD_R;
/// A tab's width: its content's, between the least and the greatest. The
/// name is measured in the heavier face for every tab, so the tab in front
/// doesn't move the others.
fn tab_width(ui: &Ui, tab: &Tab) -> f32 {
let g = ui.painter().layout_no_wrap(
tab.title.clone(),
theme::semibold(13.0),
egui::Color32::PLACEHOLDER,
);
(TAB_FIXED + g.size().x.ceil()).clamp(TAB_MIN, TAB_MAX)
}
/// One tab, `width` wide.
fn one_tab(
ui: &mut Ui,
@@ -290,12 +401,12 @@ fn one_tab(
p: &theme::Palette,
action: &mut Option<TabAction>,
) -> egui::Response {
let height = BAR_H - 6.0;
let height = BAR_H - 4.0;
let (rect, resp) = ui.allocate_exact_size(vec2(width, height), Sense::click());
// The tab's own id for its close control: tabs are told apart by
// their place, and two tabs never share one.
let close_rect = egui::Rect::from_center_size(
egui::pos2(rect.right() - 6.0 - CLOSE / 2.0, rect.center().y),
egui::pos2(rect.right() - PAD_R - CLOSE / 2.0, rect.center().y),
vec2(CLOSE, CLOSE),
);
let close = ui.interact(close_rect, resp.id.with("close"), Sense::click());
@@ -317,47 +428,62 @@ fn one_tab(
sw: 0,
se: 0,
};
let fill = if here {
Some(p.bg)
} else if resp.hovered() || close.hovered() {
Some(p.hover)
} else {
None
};
if let Some(fill) = fill {
painter.rect_filled(rect, top, fill);
}
if here {
// A bar along the top edge and the page's own fill: the tab
// in front is part of the page below it.
painter.rect_filled(
egui::Rect::from_min_size(rect.min, vec2(rect.width(), 3.0)),
CornerRadius {
nw: 8,
ne: 8,
sw: 0,
se: 0,
},
p.accent,
// The lit tab: the page's surface, outlined on its top and
// sides, open at the foot where it meets what is below, with a
// bar in the accent colour along its top edge.
painter.rect_filled(rect, top, p.surface);
painter.rect_stroke(
rect,
top,
Stroke::new(1.0, p.border),
egui::StrokeKind::Inside,
);
painter.rect_filled(
egui::Rect::from_min_max(
egui::pos2(rect.left(), rect.bottom() - 1.0),
egui::pos2(rect.right(), rect.bottom() + 1.0),
egui::pos2(rect.left() + 1.0, rect.bottom() - 1.0),
egui::pos2(rect.right() - 1.0, rect.bottom() + 1.0),
),
0.0,
p.bg,
p.surface,
);
painter.rect_filled(
egui::Rect::from_min_size(rect.min, vec2(rect.width(), 3.0)),
top,
p.accent,
);
} else if resp.hovered() || close.hovered() {
painter.rect_filled(rect.shrink2(vec2(0.0, 2.0)), CornerRadius::same(8), p.hover);
}
// The badge, as the sidebar's header draws it.
let badge = egui::Rect::from_center_size(
egui::pos2(rect.left() + PAD_L + BADGE / 2.0, rect.center().y + 1.0),
vec2(BADGE, BADGE),
);
painter.rect_filled(badge, CornerRadius::same(5), p.accent);
let initial = tab
.title
.chars()
.next()
.map(|c| c.to_uppercase().next().unwrap_or(c))
.unwrap_or('W');
painter.text(
badge.center(),
egui::Align2::CENTER_CENTER,
initial,
theme::bold(11.0),
crate::schemes::on(p.accent),
);
let font = if here {
theme::semibold(13.0)
} else {
theme::regular(13.0)
};
let text_w = rect.width() - 14.0 - CLOSE - 10.0;
let text_x = badge.right() + GAP;
let text_w = close_rect.left() - STATE_W - GAP - text_x;
let mut job = egui::text::LayoutJob::single_section(
tab.title.clone(),
egui::TextFormat::simple(font, if here { p.text } else { p.muted }),
egui::TextFormat::simple(font, p.text),
);
job.wrap.max_width = text_w.max(10.0);
job.wrap.max_rows = 1;
@@ -365,10 +491,7 @@ fn one_tab(
job.wrap.overflow_character = Some('…');
let galley = painter.layout_job(job);
painter.galley(
egui::pos2(
rect.left() + 14.0,
rect.center().y - galley.size().y / 2.0 + 1.0,
),
egui::pos2(text_x, rect.center().y - galley.size().y / 2.0 + 1.0),
galley,
p.text,
);
@@ -380,7 +503,11 @@ fn one_tab(
egui::Align2::CENTER_CENTER,
icon::X,
egui::FontId::proportional(12.0),
if close.hovered() { p.text } else { p.muted },
if close.hovered() || here {
p.text
} else {
p.muted
},
);
}
let resp = resp.on_hover_text(&tab.detail);