diff --git a/crates/ui-widgets/src/tabs/tab.rs b/crates/ui-widgets/src/tabs/tab.rs index 959b8565d..69da97979 100644 --- a/crates/ui-widgets/src/tabs/tab.rs +++ b/crates/ui-widgets/src/tabs/tab.rs @@ -59,6 +59,9 @@ const TEXT: PartKey = PartKey::new("text"); const CLOSE: PartKey = PartKey::new("close"); const PROGRESS: PartKey = PartKey::new("progress"); +/// The modified mark. +const MARK: &str = "*"; + const CLOSE_REASON: &str = "The Close command closes the focused tab"; /// The sizes a tab takes from its kind and the tokens. @@ -67,7 +70,6 @@ struct Sizes { gap: f32, icon: f32, close_box: f32, - dot: f32, /// Room after a text tab's contents. tail: f32, /// The narrowest content width: what a shrinking tab keeps. @@ -111,6 +113,8 @@ struct Placed { icon: Option, label: Rect, detail: Option, + /// The modified mark, right after the last text. + mark: Option, progress: Option, } @@ -133,7 +137,7 @@ pub struct Tab<'a> { /// An icon before the label. #[prop(into)] icon: Option>, - /// Unsaved changes: a dot where the close button is, until the pointer is over the tab. + /// Unsaved changes: a `*` after the detail, or after the label without one. #[prop(flag)] modified: bool, /// Whether the tab can be closed; by default whether the kind shows a close button. @@ -182,11 +186,6 @@ impl<'a> Tab<'a> { self.can_close() && self.kind.shows_close() } - /// Whether the end slot (the close button's place) is used: by the button or by the modified dot. - fn has_slot(&self) -> bool { - self.shows_close() || self.modified - } - fn detail_text(&self) -> Option<&str> { self.detail.as_ref().map(Label::as_str).filter(|text| !text.is_empty()) } @@ -195,6 +194,11 @@ impl<'a> Tab<'a> { TextRun::new(text).role(style.label.role) } + /// The modified mark, in the label's type. + fn mark_run(&self, style: &TabStyle) -> TextRun<'static> { + TextRun::new(MARK).role(style.label.role) + } + fn name(&self, label: &str, words: &TabWords) -> String { let name = match self.detail_text() { Some(detail) => format!("{label}, {detail}"), @@ -247,7 +251,6 @@ impl Component for Tab<'_> { gap: space.gap.inline, icon: size.icon.sm, close_box: size.icon.md, - dot: size.handle.sm, tail: if text_only { space.gap.section } else { 0.0 }, min_content: size.icon.md, label_line: line(role), @@ -281,7 +284,10 @@ impl Component for Tab<'_> { if let Some(detail) = self.detail_text() { natural += sizes.gap + width(&TextRun::new(detail).role(cx.style.detail.role)); } - if self.has_slot() { + if self.modified { + natural += width(&self.mark_run(cx.style)); + } + if self.shows_close() { natural += sizes.gap + sizes.close_box; } Vec2::new(natural, sizes.label_line) @@ -301,7 +307,7 @@ impl Component for Tab<'_> { let content = cx.content_rect; let mut flow = Flow::new(content, rtl); let mut close = None; - if self.has_slot() { + if self.shows_close() { let at_start = cx.style.close_side == if rtl { CloseSide::Right } else { CloseSide::Left }; let (slot, rest) = if at_start { flow.take_start(sizes.close_box) } else { flow.take_end(sizes.close_box) }; close = Some(slot); @@ -318,15 +324,22 @@ impl Component for Tab<'_> { let natural = |run: &TextRun<'_>| cx.layout_at(run, f32::INFINITY).size().x; let label_natural = natural(&self.label_run(cx.style, cx.resolved.label)); let detail_natural = self.detail_text().map_or(0.0, |text| natural(&TextRun::new(text).role(cx.style.detail.role))); + let mark_width = if self.modified { natural(&self.mark_run(cx.style)) } else { 0.0 }; let detail_gap = if detail_natural > 0.0 { sizes.gap } else { 0.0 }; - let label_width = label_natural.min((band.width() - detail_gap - detail_natural).max(0.0)); - let detail_width = detail_natural.min((band.width() - label_width - detail_gap).max(0.0)); + let label_width = label_natural.min((band.width() - mark_width - detail_gap - detail_natural).max(0.0)); + let detail_width = detail_natural.min((band.width() - mark_width - label_width - detail_gap).max(0.0)); let line = |rect: Rect, height: f32| { let top = if sizes.text_only { content.top() } else { content.center().y - height / 2.0 }; Rect::from_x_y_ranges(rect.x_range(), top..=top + height) }; let (label, rest) = flow.take_start(label_width); - let detail = (detail_width > 0.0).then(|| line(rest.take_start(detail_gap).1.take_start(detail_width).0, sizes.detail_line)); + let (detail, after) = if detail_width > 0.0 { + let (detail, after) = rest.take_start(detail_gap).1.take_start(detail_width); + (Some(line(detail, sizes.detail_line)), after) + } else { + (None, rest) + }; + let mark = self.modified.then(|| line(after.take_start(mark_width).0, sizes.label_line)); let label = line(label, sizes.label_line); let cut = label_width < label_natural || detail_width < detail_natural; if cut { @@ -336,7 +349,7 @@ impl Component for Tab<'_> { }; let _ = cx.part(TEXT, band).tooltip(full); } - if let Some(slot) = close.filter(|_| self.shows_close()) { + if let Some(slot) = close { let name = self.close_label.clone(); let part = cx.part(CLOSE, slot).sense(Sense::CLICK).pointer_only(CLOSE_REASON); if let Some(name) = name { @@ -353,7 +366,7 @@ impl Component for Tab<'_> { if let Some(bar) = progress { let _ = cx.part(PROGRESS, bar); } - self.placed = Some(Placed { close, icon, label, detail, progress }); + self.placed = Some(Placed { close, icon, label, detail, mark, progress }); } fn update(&mut self, cx: &mut Cx<'_, Self>) { @@ -402,25 +415,22 @@ impl Component for Tab<'_> { if let (Some(at), Some(detail)) = (placed.detail, self.detail_text()) { draw.text(at, &TextRun::new(detail).role(style.detail.role).color(cx.resolve(&style.detail.color)).elide()); } + if let Some(at) = placed.mark { + draw.text(at, &self.mark_run(style).color(cx.resolve(&style.label.color))); + } if let (Some(at), Some(icon)) = (placed.icon, &mut self.icon) { if icon.color.is_none() { icon.color = Some(cx.resolve(&style.icon.color)); } ui.draw().icon(at, icon); } - if let Some(slot) = placed.close { - let over = cx.response.hovered(); - if self.shows_close() && (over || !self.modified) { - let words = cx.words::(); - let name = match &self.close_label { - Some(name) => name.as_str().to_owned(), - None => words.close.fill(&[("tab", cx.resolved.label)]), - }; - ui.face(CLOSE, IconButton::new(IconId::new("close")).label(name)); - } else { - let dot = PaintShape::Circle(slot.center(), sizes.dot / 2.0); - ui.draw().fill(&dot, cx.resolve(&style.label.color)); - } + if placed.close.is_some() { + let words = cx.words::(); + let name = match &self.close_label { + Some(name) => name.as_str().to_owned(), + None => words.close.fill(&[("tab", cx.resolved.label)]), + }; + ui.face(CLOSE, IconButton::new(IconId::new("close")).label(name)); } if let (Some(_), Some(fraction)) = (placed.progress, self.progress) { ui.face(PROGRESS, Progress::new(fraction).label(cx.resolved.label.to_owned()));