mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Size document tabs to available title bar room
This commit is contained in:
@@ -227,7 +227,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
let mut layout = comp.item_layout_from(previous, &*self.source, width, item, scx);
|
||||
// An auto-width layout sets its items side by side: where its width comes from its rows, it takes their run at
|
||||
// their ideal widths, not the widest one (a tab strip in a bar's unbounded row).
|
||||
let run = (layout.auto_widths() && (asked.is_some() || !bounded) && !self.source.is_empty())
|
||||
let run = (layout.auto_widths() && (asked.is_some() || !bounded || self.content_sized(comp)) && !self.source.is_empty())
|
||||
.then(|| self.run_in(ui, id, room).min(room.x))
|
||||
.filter(|run| *run > width);
|
||||
let (width, asked) = match run {
|
||||
|
||||
@@ -8,7 +8,7 @@ mod stories;
|
||||
use super::{Tab, TabStrip};
|
||||
use crate::contract::{
|
||||
CollectionEvent, ContextMenuRequest, DropAt, DropKind, DropPlace, ListDrag, OsFiles, Props as _, ReorderRule, ShowCollection as _,
|
||||
ShowContainer as _,
|
||||
ShowContainer as _, Sizing,
|
||||
};
|
||||
use crate::layout_components::Row;
|
||||
use crate::runtime::drag::OsFile;
|
||||
@@ -269,10 +269,14 @@ 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 has_trailing = trailing.is_some();
|
||||
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 has_trailing {
|
||||
strip = strip.sizing(Sizing::Content);
|
||||
}
|
||||
if takes_files {
|
||||
strip = strip.takes_drop(DropKind::new(DropPlace::Between, |_: &OsFiles, _| true));
|
||||
}
|
||||
@@ -332,7 +336,7 @@ impl<'a, D: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Documen
|
||||
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, _| {
|
||||
let row = Row::new().sizing(Sizing::FillWidth).show(ui, |ui, _| {
|
||||
if let Some(area) = leading.as_mut() {
|
||||
area(ui);
|
||||
}
|
||||
|
||||
@@ -68,6 +68,48 @@ fn document_tabs_shrink_before_overflow() {
|
||||
assert!(fit.shown.iter().all(|(_, width)| *width < 100.0));
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn document_tabs_with_trailing_control_fill_their_parent_row() {
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::tabs::{DocumentTab, DocumentTabs};
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let tabs: Vec<_> = (0..5).map(|i| DocumentTab::new(i, format!("Document number {i}, scanned.pdf"))).collect();
|
||||
Row::new().sizing(Sizing::FillWidth).show(ui, |ui, _| {
|
||||
Button::new("Home").show(ui);
|
||||
let _ = DocumentTabs::new("Documents", &tabs)
|
||||
.trailing(|ui| {
|
||||
Button::new("Open").show(ui);
|
||||
})
|
||||
.show(ui);
|
||||
Button::new("Settings").show(ui);
|
||||
});
|
||||
});
|
||||
fixture.room(egui::vec2(1000.0, 100.0)).settle();
|
||||
let visible = (0..5).filter(|i| fixture.by_label(&format!("Document number {i}, scanned.pdf")).is_some()).count();
|
||||
assert!(visible > 1, "the strip uses the available width: {visible} tab visible");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn trailing_control_follows_short_document_tabs() {
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::Show as _;
|
||||
use craft_ui::tabs::{DocumentTab, DocumentTabs};
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let tabs = [DocumentTab::new(1, "One document")];
|
||||
let _ = DocumentTabs::new("Documents", &tabs)
|
||||
.trailing(|ui| {
|
||||
Button::new("Open").show(ui);
|
||||
})
|
||||
.show(ui);
|
||||
});
|
||||
fixture.room(egui::vec2(1000.0, 100.0)).settle();
|
||||
let tab = fixture.by_label("One document").expect("document tab").rect;
|
||||
let open = fixture.by_label("Open").expect("trailing control").rect;
|
||||
assert!(open.left() - tab.right() < tab.width(), "trailing control follows the tab: {tab:?}, {open:?}");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn document_tabs_reserve_space_for_the_trailing_control() {
|
||||
use craft_ui::buttons::Button;
|
||||
|
||||
Reference in New Issue
Block a user