mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-24 19:41:51 +01:00
Measure document tab controls on every layout sweep
This commit is contained in:
2 files changed
+62
-35
No files matched your search
@@ -188,7 +188,7 @@ pub struct DocumentTabsOutput<D> {
|
||||
pub hovered: Option<D>,
|
||||
}
|
||||
|
||||
type Area<'a> = Box<dyn FnOnce(&mut Ui) + 'a>;
|
||||
type Area<'a> = Box<dyn FnMut(&mut Ui) + 'a>;
|
||||
|
||||
/// The tab strip of one document group. Give the strips of all groups the same `accepts` rule (the default takes
|
||||
/// every group) and a tab dropped from one reaches the other as `DroppedInto`.
|
||||
@@ -254,13 +254,13 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen
|
||||
}
|
||||
|
||||
/// Host content before the tabs, at the reading start, as wide as it measures.
|
||||
pub fn leading(mut self, area: impl FnOnce(&mut Ui) + 'a) -> Self {
|
||||
pub fn leading(mut self, area: impl FnMut(&mut Ui) + 'a) -> Self {
|
||||
self.leading = Some(Box::new(area));
|
||||
self
|
||||
}
|
||||
|
||||
/// Host content after the tabs, at the reading end, as wide as it measures.
|
||||
pub fn trailing(mut self, area: impl FnOnce(&mut Ui) + 'a) -> Self {
|
||||
pub fn trailing(mut self, area: impl FnMut(&mut Ui) + 'a) -> Self {
|
||||
self.trailing = Some(Box::new(area));
|
||||
self
|
||||
}
|
||||
@@ -269,31 +269,34 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen
|
||||
pub fn show(self, ui: &mut Ui) -> DocumentTabsOutput<D> {
|
||||
let DocumentTabs { label, tabs, selected, reorderable, tear_off, leading, trailing, large, takes_files } = self;
|
||||
let source = keyed(tabs, |tab: &DocumentTab<D>| tab.id.clone()).with_text(|tab| Cow::Borrowed(tab.label.as_str()));
|
||||
let mut strip = TabStrip::new(label)
|
||||
.takes_drop(DropKind::new(DropPlace::Between, |_: &ListDrag<D>, _| true))
|
||||
.can_close(move |key: &D| tabs.iter().any(|tab| &tab.id == key && tab.closable));
|
||||
if takes_files {
|
||||
strip = strip.takes_drop(DropKind::new(DropPlace::Between, |_: &OsFiles, _| true));
|
||||
}
|
||||
if tear_off {
|
||||
strip = strip.tear_off();
|
||||
}
|
||||
if large {
|
||||
strip = strip.large();
|
||||
}
|
||||
if let Some(document) = selected {
|
||||
strip = strip.shown(document);
|
||||
}
|
||||
if reorderable {
|
||||
strip = strip
|
||||
.reorder(ReorderRule::new())
|
||||
.can_drag(move |key: &D| tabs.iter().find(|tab| &tab.id == key).is_none_or(|tab| tab.movable))
|
||||
.accepts(move |drag: &ListDrag<D>, _, insert| {
|
||||
// `insert` counts every tab; `keeps_fixed` takes the place among the tabs left without the dragged ones.
|
||||
let passed = tabs.iter().take(insert).filter(|tab| drag.keys.contains(&tab.id)).count();
|
||||
keeps_fixed(tabs, &drag.keys, insert - passed)
|
||||
});
|
||||
}
|
||||
let make_strip = || {
|
||||
let mut strip = TabStrip::new(label.clone())
|
||||
.takes_drop(DropKind::new(DropPlace::Between, |_: &ListDrag<D>, _| true))
|
||||
.can_close(move |key: &D| tabs.iter().any(|tab| &tab.id == key && tab.closable));
|
||||
if takes_files {
|
||||
strip = strip.takes_drop(DropKind::new(DropPlace::Between, |_: &OsFiles, _| true));
|
||||
}
|
||||
if tear_off {
|
||||
strip = strip.tear_off();
|
||||
}
|
||||
if large {
|
||||
strip = strip.large();
|
||||
}
|
||||
if let Some(document) = selected.clone() {
|
||||
strip = strip.shown(document);
|
||||
}
|
||||
if reorderable {
|
||||
strip = strip
|
||||
.reorder(ReorderRule::new())
|
||||
.can_drag(move |key: &D| tabs.iter().find(|tab| &tab.id == key).is_none_or(|tab| tab.movable))
|
||||
.accepts(move |drag: &ListDrag<D>, _, insert| {
|
||||
// `insert` counts every tab; `keeps_fixed` takes the place among the tabs left without the dragged ones.
|
||||
let passed = tabs.iter().take(insert).filter(|tab| drag.keys.contains(&tab.id)).count();
|
||||
keeps_fixed(tabs, &drag.keys, insert - passed)
|
||||
});
|
||||
}
|
||||
strip
|
||||
};
|
||||
let hovered = std::cell::RefCell::new(None);
|
||||
let show_strip = |ui: &mut Ui, strip: TabStrip<'_, D>| {
|
||||
strip.show_items(ui, &source, &mut |key, cx| {
|
||||
@@ -323,18 +326,18 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen
|
||||
})
|
||||
};
|
||||
let shown = if leading.is_none() && trailing.is_none() {
|
||||
Ok(show_strip(ui, strip))
|
||||
Ok(show_strip(ui, make_strip()))
|
||||
} else {
|
||||
let (mut strip, mut leading, mut trailing) = (Some(strip), leading, trailing);
|
||||
let (mut leading, mut trailing) = (leading, trailing);
|
||||
let mut shown = None;
|
||||
// The row measures more than once to reserve non-filling children before sharing the
|
||||
// remaining width. Recreate the strip and show both areas on every sweep.
|
||||
let row = Row::new().show(ui, |ui, _| {
|
||||
if let Some(area) = leading.take() {
|
||||
if let Some(area) = leading.as_mut() {
|
||||
area(ui);
|
||||
}
|
||||
if let Some(strip) = strip.take() {
|
||||
shown = Some(show_strip(ui, strip));
|
||||
}
|
||||
if let Some(area) = trailing.take() {
|
||||
shown = Some(show_strip(ui, make_strip()));
|
||||
if let Some(area) = trailing.as_mut() {
|
||||
area(ui);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -21,6 +21,30 @@ fn document_tabs_shrink_before_overflow() {
|
||||
assert!(fit.shown.iter().all(|(_, width)| *width < 100.0));
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn document_tabs_reserve_space_for_the_trailing_control() {
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::Show as _;
|
||||
use craft_ui::tabs::{DocumentTab, DocumentTabs};
|
||||
let trailing_rect = Rc::new(Cell::new(Rect::NOTHING));
|
||||
let seen = trailing_rect.clone();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let tabs: Vec<_> = (0..5).map(|i| DocumentTab::new(i, format!("Long document number {i}, scanned.pdf")).closable(true)).collect();
|
||||
DocumentTabs::new("Documents", &tabs)
|
||||
.selected(4)
|
||||
.trailing(|ui| {
|
||||
seen.set(Button::new("Open").show(ui).rect);
|
||||
})
|
||||
.show(ui);
|
||||
});
|
||||
fixture.room(egui::vec2(1000.0, 100.0)).settle();
|
||||
let open = fixture.by_label("Open").unwrap_or_else(|| panic!("trailing button: {:?}", trailing_rect.get())).rect;
|
||||
for i in 0..5 {
|
||||
let tab = fixture.by_label(&format!("Long document number {i}, scanned.pdf")).expect("each tab is visible").rect;
|
||||
assert!(tab.right() <= open.left() + 0.5, "tab {i} overlaps the trailing button: {tab:?}, {open:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An arrow moves the focus to the next tab without showing it; Enter shows it.
|
||||
#[trial]
|
||||
fn an_arrow_moves_the_focus_and_enter_shows_the_tab() {
|
||||
|
||||
Reference in new issue
Block a user