use crate::{
ButtonContent, Rect, ScrollLayout, Size, TextMeasurer,
linear::{Axis, measure_replacement_content},
place_in_direction,
};
use xrack_runtime::{LayoutDirection, Node, Orientation, group_index};
pub const TAB_LABEL_SIZE: f32 = 14.0;
const TAB_CLOSE_RESERVE: f32 = 28.0;
pub(crate) fn tab_close_rect(header: Rect, direction: LayoutDirection) -> Option<Rect> {
let size = (header.height.min(header.width) - 6.0).min(24.0);
if size < 1.0 {
return None;
}
let rect = Rect::new(
header.x + header.width - size - 3.0,
header.y + (header.height - size) * 0.5,
size,
size,
);
Some(place_in_direction(rect, header, direction))
}
pub fn tab_content_rect(header: Rect, close: Rect) -> Rect {
if close.x + close.width / 2.0 < header.x + header.width / 2.0 {
let start = close.x + close.width + 2.0;
Rect::new(
start,
header.y,
(header.x + header.width - start).max(0.0),
header.height,
)
} else {
Rect::new(
header.x,
header.y,
(close.x - header.x - 2.0).max(0.0),
header.height,
)
}
}
pub(crate) fn tab_bar(
node: &Node,
bounds: Rect,
text: &mut impl TextMeasurer,
direction: LayoutDirection,
requested: Option<f32>,
reveal: bool,
) -> (Vec<Rect>, ScrollLayout) {
let (axis, size_property) = match node.orientation() {
Orientation::Horizontal => (Axis::Horizontal, "width"),
Orientation::Vertical => (Axis::Vertical, "height"),
};
let direction = if matches!(axis, Axis::Horizontal) {
direction
} else {
LayoutDirection::LeftToRight
};
let size = Size::new(bounds.width, bounds.height);
let extent = axis.extent(size);
let cross_extent = axis.other().extent(size);
let spacing = node.length("spacing").unwrap_or(0.0).max(0.0);
let widget = node.kind == "TabWidget";
let mut sizes: Vec<_> = node
.children
.iter()
.map(|tab| {
if !tab.is_visible() {
return None;
}
let explicit = if widget {
(axis == Axis::Vertical).then_some(36.0)
} else {
tab.relative_length(size_property, extent)
};
let close_space =
if axis == Axis::Horizontal && xrack_runtime::tab_is_closable(node, tab) {
TAB_CLOSE_RESERVE
} else {
0.0
};
let size = explicit.unwrap_or_else(|| {
if let Some(content) = tab.control_part("ButtonContent") {
let padding = 2.0 * ButtonContent::new(tab).padding;
let inner_cross = (cross_extent - padding).max(0.0);
let (available, lower_limit) = match axis {
Axis::Horizontal => (Size::new(f32::INFINITY, inner_cross), 48.0),
Axis::Vertical => (Size::new(inner_cross, f32::INFINITY), 36.0),
};
let content = if content.is_visible() {
measure_replacement_content(content, available, text)
} else {
Size::default()
};
return (axis.extent(content) + padding).max(lower_limit) + close_space;
}
match axis {
Axis::Horizontal => {
let label = tab
.text(if widget { "title" } else { "text" })
.unwrap_or("");
let font_size = text
.default_font_size()
.unwrap_or(TAB_LABEL_SIZE)
.clamp(1.0, (cross_extent / 1.4).max(1.0));
(text.measure(label, font_size, f32::INFINITY).width + 24.0).max(48.0)
+ close_space
}
Axis::Vertical => 36.0,
}
});
(size > 0.0).then_some((size, explicit.is_none()))
})
.collect();
let count = sizes.iter().flatten().count();
let gap_count = count.saturating_sub(1);
if widget && axis == Axis::Horizontal && count != 0 {
let equal = ((extent - gap_count as f32 * spacing) / count as f32).max(0.0);
if sizes.iter().flatten().all(|&(minimum, _)| minimum <= equal) {
for (size, flexible) in sizes.iter_mut().flatten() {
(*size, *flexible) = (equal, false);
}
}
}
let minimum =
sizes.iter().flatten().map(|(size, _)| *size).sum::<f32>() + gap_count as f32 * spacing;
let flexible = sizes
.iter()
.flatten()
.filter(|(_, flexible)| *flexible)
.count();
let extra = (extent - minimum).max(0.0) / flexible.max(1) as f32;
let max_offset = (minimum - extent).max(0.0);
let content = axis.rect(bounds, 0.0, 0.0, extent + max_offset, cross_extent);
let mut start = 0.0;
let rects: Vec<_> = sizes
.into_iter()
.map(|size| {
let Some((size, flexible)) = size else {
return Rect::new(bounds.x, bounds.y, 0.0, 0.0);
};
let size = size + if flexible { extra } else { 0.0 };
let rect = axis.rect(bounds, start, 0.0, size, cross_extent);
start += size + spacing;
place_in_direction(rect, content, direction)
})
.collect();
let translate = |mut tab: Rect, offset| {
match axis {
Axis::Horizontal => tab.x -= offset,
Axis::Vertical => tab.y -= offset,
}
tab
};
let start_offset = if direction == LayoutDirection::RightToLeft {
max_offset
} else {
0.0
};
let mut scroll = ScrollLayout::new(requested.unwrap_or(start_offset), max_offset);
if reveal && let Some(tab) = group_index(node).and_then(|index| rects.get(index)) {
let offset = tab_reveal_offset(
bounds,
translate(*tab, scroll.offset),
node.orientation(),
direction,
scroll.offset,
);
scroll = ScrollLayout::new(offset, max_offset);
}
let rects = rects
.into_iter()
.map(|tab| translate(tab, scroll.offset))
.collect();
(rects, scroll)
}
pub(crate) fn tab_reveal_offset(
viewport: Rect,
tab: Rect,
orientation: Orientation,
direction: LayoutDirection,
offset: f32,
) -> f32 {
let (start, extent, available) = match orientation {
Orientation::Horizontal => (tab.x - viewport.x, tab.width, viewport.width),
Orientation::Vertical => (tab.y - viewport.y, tab.height, viewport.height),
};
let leading =
if orientation == Orientation::Horizontal && direction == LayoutDirection::RightToLeft {
(extent - available).max(0.0)
} else {
0.0
};
let start = start + offset + leading;
offset
.max(start + extent.min(available) - available)
.min(start)
}
pub(crate) fn radio_title_height(node: &Node, size: f32) -> f32 {
if node.kind == "RadioGroup" && node.text("title").is_some_and(|title| !title.is_empty()) {
size * 1.4 + 6.0
} else {
0.0
}
}
pub(crate) struct TabGeometry {
pub(crate) bar: Rect,
pub(crate) content: Rect,
}
impl TabGeometry {
pub(crate) fn new(bounds: Rect, orientation: Orientation, direction: LayoutDirection) -> Self {
let (bar, content) = match orientation {
Orientation::Horizontal => {
let height = bounds.height.min(34.0);
(
Rect::new(bounds.x, bounds.y, bounds.width, height),
Rect::new(
bounds.x,
bounds.y + height,
bounds.width,
bounds.height - height,
),
)
}
Orientation::Vertical => {
let width = (bounds.width * 0.4).min(124.0);
(
Rect::new(bounds.x, bounds.y, width, bounds.height),
Rect::new(
bounds.x + width,
bounds.y,
bounds.width - width,
bounds.height,
),
)
}
};
Self {
bar: place_in_direction(bar, bounds, direction),
content: place_in_direction(content, bounds, direction),
}
}
}