mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-24 11:31:28 +01:00
Space document tabs while preserving gap drops
This commit is contained in:
5 files changed
+59
-8
No files matched your search
@@ -30,6 +30,12 @@ pub trait ItemLayout {
|
||||
/// The item under `point`.
|
||||
fn hit(&self, point: Pos2, offset: ScrollPos) -> Option<usize>;
|
||||
|
||||
/// The item whose drop area contains `point`. Layouts with visible gaps can include those in a drop area while
|
||||
/// leaving them out of ordinary pointer hits.
|
||||
fn drop_hit(&self, point: Pos2, offset: ScrollPos) -> Option<usize> {
|
||||
self.hit(point, offset)
|
||||
}
|
||||
|
||||
/// How arrow keys move through the items.
|
||||
fn nav(&self) -> NavShape;
|
||||
|
||||
|
||||
@@ -481,7 +481,7 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
return None;
|
||||
}
|
||||
let nav = layout.nav();
|
||||
let hit = layout.hit(Self::logical(layout, rect, rtl, at), offset);
|
||||
let hit = layout.drop_hit(Self::logical(layout, rect, rtl, at), offset);
|
||||
let across =
|
||||
!matches!(nav, NavShape::Flat | NavShape::Tree | NavShape::WrappedGrid { columns: 1, .. } | NavShape::BoundedGrid { columns: 1, .. });
|
||||
// With no item under the pointer, a point after the last item in reading order is over the last item. Across a
|
||||
|
||||
@@ -7,7 +7,7 @@ use craft_host::target::Os;
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum TabKind {
|
||||
/// Document tabs: shrinking titles, a close button on every closable tab, every document in the overflow menu.
|
||||
/// Document tabs: spaced apart, shrinking titles, a close button on every closable tab, every document in the overflow menu.
|
||||
#[default]
|
||||
Document,
|
||||
/// Panel tabs: tabs shrink before they overflow, no close button, only the hidden tabs in the overflow menu.
|
||||
|
||||
@@ -116,6 +116,8 @@ pub struct StripLayout {
|
||||
min: Vec<f32>,
|
||||
width: f32,
|
||||
height: f32,
|
||||
/// Space between document tabs; included in the fit budget but not in their hit rectangles.
|
||||
gap: f32,
|
||||
measured_height: bool,
|
||||
selected: usize,
|
||||
mode: FitMode,
|
||||
@@ -128,13 +130,15 @@ pub struct StripLayout {
|
||||
}
|
||||
|
||||
impl StripLayout {
|
||||
fn new(count: usize, width: f32, height: f32, selected: usize, mode: FitMode, report: Arc<Mutex<Report>>, ctx: egui::Context) -> Self {
|
||||
fn new(count: usize, size: Vec2, gap: f32, selected: usize, mode: FitMode, report: Arc<Mutex<Report>>, ctx: egui::Context) -> Self {
|
||||
let (width, height) = (size.x, size.y);
|
||||
let each = if count == 0 { 0.0 } else { width / count as f32 };
|
||||
let mut layout = StripLayout {
|
||||
ideal: vec![each; count],
|
||||
min: vec![0.0; count],
|
||||
width,
|
||||
height,
|
||||
gap,
|
||||
measured_height: false,
|
||||
selected,
|
||||
mode,
|
||||
@@ -153,18 +157,25 @@ impl StripLayout {
|
||||
fn refit(&mut self) {
|
||||
let count = self.ideal.len();
|
||||
let minimum = self.min.iter().copied().fold(0.0, f32::max);
|
||||
let fit = self.mode.fit(&self.ideal, self.selected, self.width, minimum, self.height);
|
||||
// Give every fitted tab a trailing gap, then return the last gap to the available width.
|
||||
// This keeps the spaces in the shrink and overflow budget without adding one after the last tab.
|
||||
let padded: Vec<f32> = self.ideal.iter().map(|width| width + self.gap).collect();
|
||||
let fit = self.mode.fit(&padded, self.selected, self.width + self.gap, minimum + self.gap, self.height);
|
||||
self.shown = vec![false; count];
|
||||
self.left = vec![self.width; count];
|
||||
self.wide = vec![0.0; count];
|
||||
let mut at = 0.0;
|
||||
let mut placed = Vec::with_capacity(fit.shown.len());
|
||||
for &(index, width) in &fit.shown {
|
||||
for &(index, padded_width) in &fit.shown {
|
||||
let width = (padded_width - self.gap).max(0.0);
|
||||
self.shown[index] = true;
|
||||
self.left[index] = at;
|
||||
self.wide[index] = width;
|
||||
placed.push((index, at, width));
|
||||
at += width;
|
||||
at += width + self.gap;
|
||||
}
|
||||
if !placed.is_empty() {
|
||||
at -= self.gap;
|
||||
}
|
||||
if !crate::runtime::measure::active(&self.ctx) {
|
||||
*self.report.lock().unwrap_or_else(PoisonError::into_inner) =
|
||||
@@ -190,6 +201,18 @@ impl ItemLayout for StripLayout {
|
||||
(0..self.ideal.len()).find(|&at| self.shown[at] && self.rect(at, offset).contains(point))
|
||||
}
|
||||
|
||||
fn drop_hit(&self, point: Pos2, offset: ScrollPos) -> Option<usize> {
|
||||
self.hit(point, offset).or_else(|| {
|
||||
(0..self.ideal.len()).find(|&at| {
|
||||
if !self.shown[at] || self.left[at] <= 0.0 {
|
||||
return false;
|
||||
}
|
||||
let rect = self.rect(at, offset);
|
||||
point.y >= rect.top() && point.y <= rect.bottom() && point.x >= rect.left() - self.gap && point.x < rect.left()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn nav(&self) -> NavShape {
|
||||
NavShape::RaggedGrid
|
||||
}
|
||||
@@ -572,7 +595,16 @@ impl<K: Clone + Eq + Hash + Send + Sync + std::fmt::Debug + 'static> Collection
|
||||
let count = source.len();
|
||||
*self.texts.borrow_mut() = (0..count).map(|at| (source.key(at), source.text(at).map(|text| text.into_owned()).unwrap_or_default())).collect();
|
||||
let selected = self.shown.as_ref().and_then(|key| source.index_of(key)).unwrap_or(usize::MAX);
|
||||
StripLayout::new(count, width, cx.tokens.size.bar.tab, selected, self.kind.fit_mode(), self.report.clone(), cx.frame_ctx().clone())
|
||||
let gap = if self.kind == TabKind::Document { cx.tokens.space.gap.inline } else { 0.0 };
|
||||
StripLayout::new(
|
||||
count,
|
||||
Vec2::new(width, cx.tokens.size.bar.tab),
|
||||
gap,
|
||||
selected,
|
||||
self.kind.fit_mode(),
|
||||
self.report.clone(),
|
||||
cx.frame_ctx().clone(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Every tab takes the strip's kind and size.
|
||||
|
||||
@@ -14,6 +14,19 @@ use std::rc::Rc;
|
||||
|
||||
const TITLES: [&str; 3] = ["One", "Two", "Three"];
|
||||
|
||||
#[trial]
|
||||
fn document_tabs_leave_space_between_neighbours() {
|
||||
use craft_ui::tabs::{DocumentTab, DocumentTabs};
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let tabs = [DocumentTab::new(1, "One"), DocumentTab::new(2, "Two")];
|
||||
DocumentTabs::new("Documents", &tabs).selected(1).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let first = fixture.by_label("One").expect("first tab").rect;
|
||||
let second = fixture.by_label("Two").expect("second tab").rect;
|
||||
assert!(first.right() < second.left(), "neighbouring document tabs have a visible gap: {first:?}, {second:?}");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn document_tabs_shrink_before_overflow() {
|
||||
let fit = TabKind::Document.fit_mode().fit(&[100.0, 100.0, 100.0], 0, 240.0, 50.0, 20.0);
|
||||
@@ -296,7 +309,7 @@ fn files_dropped_between_tabs_land_before_the_tab_after_the_gap() {
|
||||
let tree = frames.tree();
|
||||
let (one, two, three) = (tab_rect(&tree, "One"), tab_rect(&tree, "Two"), tab_rect(&tree, "Three"));
|
||||
|
||||
drop_files_at(&mut frames, Pos2::new(two.left() + 1.0, two.center().y), &mut show);
|
||||
drop_files_at(&mut frames, Pos2::new((one.right() + two.left()) / 2.0, two.center().y), &mut show);
|
||||
assert_eq!(files_before(&seen.borrow()), Some(Some(1)), "files in the gap go before the tab after it: {:?}", seen.borrow());
|
||||
|
||||
seen.borrow_mut().clear();
|
||||
|
||||
Reference in new issue
Block a user