use std::{
ops::Range,
rc::Rc,
sync::{Arc, LazyLock},
time::Duration,
};
use gpui_kit::component::{ActiveTheme as _, IconName, IconNamed as _};
use gpui_kit::{
App, Bounds, Context, FontFeatures, FontWeight, Hsla, Pixels, RenderImage, SharedString,
WeakEntity, Window, point, px, size,
};
use rust_i18n::t;
use super::{
CELL_PADDING, CellChange, CellStyle, CellStyleContext, ColumnAlign, ColumnOf, ColumnZone,
KeyOf, LiveGridDelegate, LiveGridState, ViewColumnIx,
a11y::{
A11yInput, A11yPlan, PaintedColumn, PaintedRow, RowContent, RowOutline, data_row_id_name,
plan_a11y,
},
change::{ChangePaint, ChangePainter as _, ChangePalette, RowChanges},
columns::{CellContent, ColumnLayout, ZoneGeometry},
disclosure::{Chevrons, INDENT, chevron_size},
edit::{EditSlot, EditState},
fade::FadingLine,
footer_hash,
groups::{CaptionPalette, CaptionTexts, PlannedCaption},
images::{CellImage, CellImages, ImageFit},
key_hash,
pending::PendingEdits,
pinned::{BodyLine, EDGES, band_slot, pinned_key_hash},
selection::ShownRows,
stats::{profile_elapsed, profile_start},
style::{AggregateLine, Palette},
summary::{PlannedSummary, TOTALS_WEIGHT},
tooltip::{CellTooltip, CellTooltipContext, HoveredCell},
totals_hash,
widgets::WidgetSlot,
};
#[cfg(any(test, feature = "test-support"))]
use crate::element::{CellHost, HostLayer};
use crate::{
element::{FramePlan, HostPlan, IconItem, ImageItem, Region},
engine::{Aggregate, DisplayRow, Expansion, GroupPath, Projection},
highlight::{ChangeDirection, HighlightPolicy},
model::{
CellValue, ColumnId, ColumnIx, ColumnSet, GridColumns, GridRow, NumberFormat,
display_text_in,
},
policy::SelectableLine,
store::{PinnedEdge, RowId, StoreSnapshot},
text::{GlyphArena, TextCaches, TextItem},
};
pub(super) const LEAD_ICON_LANE: Pixels = px(20.);
const TREND_LANE: Pixels = px(14.);
const TREND_ICON: Pixels = px(12.);
static TABULAR_FIGURES: LazyLock<FontFeatures> =
LazyLock::new(|| FontFeatures(Arc::new(vec![("tnum".into(), 1)])));
fn with_tabular_figures(inherited: FontFeatures) -> FontFeatures {
let features = inherited.tag_value_list();
if features.is_empty() {
return TABULAR_FIGURES.clone();
}
if features.iter().any(|(tag, _)| tag == "tnum") {
return inherited;
}
let mut merged = Vec::with_capacity(features.len() + 1);
merged.extend_from_slice(features);
merged.push(("tnum".into(), 1));
FontFeatures(Arc::new(merged))
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum PaintedLine {
Row(RowId),
Caption(GroupPath),
Footer(GroupPath),
Ghost(RowId),
}
impl PaintedLine {
pub(super) fn row(&self) -> Option<RowId> {
match self {
Self::Row(id) => Some(*id),
Self::Caption(_) | Self::Footer(_) | Self::Ghost(_) => None,
}
}
pub(super) fn group(&self) -> Option<&GroupPath> {
match self {
Self::Caption(path) => Some(path),
Self::Row(_) | Self::Footer(_) | Self::Ghost(_) => None,
}
}
pub(super) fn footer(&self) -> Option<&GroupPath> {
match self {
Self::Footer(path) => Some(path),
Self::Row(_) | Self::Caption(_) | Self::Ghost(_) => None,
}
}
}
pub(super) struct CellLead {
pub(super) indent: Pixels,
pub(super) icon: Option<LeadIcon>,
}
pub(super) struct LeadIcon {
pub(super) path: SharedString,
pub(super) color: Hsla,
pub(super) size: Pixels,
}
pub(super) type LeadProvider<'a> = &'a dyn Fn(&DisplayRow, ColumnIx) -> Option<CellLead>;
impl<D: LiveGridDelegate> LiveGridState<D> {
fn schedule_fade(&mut self, deadline_ms: u32, now_ms: u32, cx: &mut Context<Self>) {
if let Some((existing, _)) = &self.fade
&& *existing <= deadline_ms
{
return;
}
let wait = Duration::from_millis(u64::from(deadline_ms.saturating_sub(now_ms)));
let task = cx.spawn(async move |this, cx| {
cx.background_executor().timer(wait).await;
let _ = this.update(cx, |state, cx| {
state.fade = None;
state.stats.fade_wakeups += 1;
cx.notify();
});
});
self.fade = Some((deadline_ms, task));
}
pub(crate) fn plan_frame(
&mut self,
bounds: Bounds<Pixels>,
window: &mut Window,
cx: &mut Context<Self>,
) -> FramePlan {
let started = profile_start();
let previous_size = self.body_bounds.size;
let previous_scroll_x = self.scroll_x;
self.body_bounds = bounds;
self.clamp_scroll();
let theme = cx.theme();
let palette = Palette {
foreground: theme.foreground,
row_border: theme.table_row_border,
stripe: theme.table_even,
selected: theme.table_active,
ring: theme.ring,
divider: theme.border,
pinned: theme.table_foot,
pinned_foreground: theme.table_foot_foreground,
muted: theme.muted_foreground,
danger: theme.danger,
link: theme.link,
};
let drop_gap_color = theme.drag_border;
let (drop_fill, drop_border) = (theme.drop_target, theme.drag_border);
let change_palette = ChangePalette::new(theme);
let lead_color = theme.muted_foreground;
let caption_palette = CaptionPalette {
background: theme.table_head,
foreground: theme.table_head_foreground,
chevron: lead_color,
};
self.caption_texts.refresh();
self.summary_texts.refresh();
let text_style = window.text_style();
let mut font = text_style.font();
font.features = with_tabular_figures(font.features);
let font_size = text_style.font_size.to_pixels(window.rem_size());
self.text_cache.begin_frame(&font, font_size);
let ellipsis = self.text_cache.regular().ellipsis(window);
let policy = self.store.options().highlight();
let now_ms = self
.store
.now()
.saturating_duration_since(self.snapshot.epoch())
.as_millis() as u32;
let tick = policy.tick(now_ms);
let row_height = self.row_height;
let bands = self.body_bands();
let rows_area = bands.rows();
let needed_lines =
((rows_area.size.height + self.scroll_offset) / row_height).ceil() as usize;
if let Some((delta, origin)) = self.apply_pending_autofit(needed_lines, window, cx) {
let this = cx.weak_entity();
cx.defer(move |cx| {
let _ = this.update(cx, |state, cx| {
state.emit_layout_change(delta, origin, cx);
cx.notify();
});
});
}
if previous_size != bounds.size || previous_scroll_x != self.scroll_x {
cx.on_next_frame(window, |_, _, cx| cx.notify());
}
let geometry = self.columns.geometry(bounds.size.width, self.scroll_x);
let has_right_zone = geometry.zone(ColumnZone::FrozenRight).width > px(0.);
let zone_regions = |area: Bounds<Pixels>| {
ColumnZone::ALL.map(|zone| {
let frame = geometry.zone(zone);
Region::new(Bounds::new(
point(bounds.origin.x + frame.x, area.origin.y),
size(frame.width, area.size.height),
))
})
};
let mut regions = zone_regions(rows_area);
let mut band_regions = EDGES.map(|edge| zone_regions(bands.band(edge)));
let row_extents = ColumnZone::ALL.map(|zone| {
let frame = geometry.zone(zone);
if zone == ColumnZone::Scrolling && !has_right_zone {
(self.columns.zone_columns(zone).content_width() - frame.scroll)
.max(px(0.))
.min(frame.width)
} else {
frame.width
}
});
let mut planned = Vec::with_capacity(64);
let mut zone_ranges = [0..0, 0..0, 0..0];
for zone in ColumnZone::ALL {
let frame = geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
let start = planned.len();
for i in frame.columns.clone() {
let ix = columns.columns[i];
let column = self.columns.column(ix);
planned.push(PlannedColumn {
column: column.column,
view: self
.columns
.view_column_ix(ix)
.expect("a painted column is visible"),
align: column.align,
content: column.content,
title: column.title.clone(),
image_fit: column.image_fit,
image_radius: column.image_corner_radius,
number_format: column.number_format,
x: bounds.origin.x + frame.column_x(columns, i),
width: columns.width(i),
});
}
zone_ranges[zone.slot()] = start..planned.len();
}
let drop_gap_x = self.drop_gap_x(cx);
let hover = self.hover_target();
let snapshot = self.snapshot.clone();
let projection = self.projection.clone();
let visible_columns = self.columns.visible_set();
let painter = self
.selection
.painter(ShownRows::new(&projection, &snapshot), &self.columns);
let dragging = self.selection.is_dragging();
let selection_policy = &*self.selection_policy;
let focus = self
.selection
.focus()
.and_then(|cell| Some((snapshot.row_id(cell.row()?)?, cell.column().ix())));
let focus_group = self
.selection
.focus()
.and_then(|cell| cell.group())
.cloned();
let tree_column = projection.is_tree().then(|| self.tree_column()).flatten();
let structured = projection.is_grouped() || projection.is_tree();
let grid_focused = self.focus_handle.is_focused(window);
let focus_visible = self.is_body_focused(window, cx) && window.last_input_was_keyboard();
let a11y = self.is_a11y_planned(window).then(|| {
let mut input = A11yInput::new(self.a11y_numbering(), window.scale_factor());
input.reserve(self.stats.a11y_rows, self.stats.a11y_cells + 1);
input.set_selection_unit(Some(self.selection.mode().selection_unit()));
input.set_texts(self.caption_texts.a11y());
if let Some(column) = tree_column {
input.set_tree_column(column);
}
input.set_band_clip(None, rows_area);
for edge in EDGES {
input.set_band_clip(Some(edge), bands.band(edge));
}
for zone in ColumnZone::ALL {
let frame = geometry.zone(zone);
input.set_zone(zone, bounds.origin.x + frame.x, frame.width);
for planned in &planned[zone_ranges[zone.slot()].clone()] {
let ix = planned.column.ix();
input.push_column(PaintedColumn {
column: ix,
view: planned.view,
id: planned.column.id(),
title: &self.columns.column(ix).title,
zone,
x: planned.x,
width: planned.width,
});
}
}
input
});
let a11y_focus = a11y.is_some() && grid_focused;
let expansion = self.view.expansion().clone();
let chevron_icon = chevron_size(row_height);
let tree_leads = tree_column.map(|column| TreeLeads {
column,
projection: &projection,
snapshot: &snapshot,
expansion: &expansion,
chevrons: Chevrons::new(),
color: lead_color,
size: chevron_icon,
});
let tree_lead = tree_leads
.as_ref()
.map(|leads| move |line: &DisplayRow, column: ColumnIx| leads.lead(line, column));
#[cfg(any(test, feature = "test-support"))]
let test_lead = self
.test_cell_lead
.clone()
.map(|(lead_column, indent, icon)| {
move |_: &DisplayRow, column: ColumnIx| {
(column == lead_column).then(|| CellLead {
indent,
icon: icon.clone().map(|path| LeadIcon {
path,
color: lead_color,
size: px(12.),
}),
})
}
});
#[cfg(any(test, feature = "test-support"))]
let test_lead = test_lead.as_ref().map(|lead| lead as LeadProvider<'_>);
#[cfg(not(any(test, feature = "test-support")))]
let test_lead: Option<LeadProvider<'_>> = None;
let lead = test_lead.or(tree_lead.as_ref().map(|lead| lead as LeadProvider<'_>));
let titles: Vec<(ColumnId, &str)> = self
.view
.group_by()
.iter()
.map(|level| {
let title = match (level.as_column(), level.as_key()) {
(Some(column), _) => self.columns.column(column.ix()).title.as_ref(),
(None, Some(key)) => key.title(),
(None, None) => "",
};
(level.id(), title)
})
.collect();
let levels = self.view.group_by().len();
let row_right = bounds.origin.x
+ ColumnZone::ALL
.iter()
.map(|zone| {
let frame = geometry.zone(*zone);
frame.x + row_extents[zone.slot()]
})
.fold(px(0.), Pixels::max);
let hover_point = self.hover_point();
let mut fills: [FillRuns; 3] = Default::default();
let mut lane = Region::new(rows_area);
let mut caption_lines: Vec<usize> = Vec::new();
let painted = &mut self.painted;
painted.clear();
let band_clips = [
bands.band(PinnedEdge::Top),
rows_area,
bands.band(PinnedEdge::Bottom),
];
let mut hosts = HostPlan::new(ColumnZone::ALL.map(|zone| {
let frame = geometry.zone(zone);
(bounds.origin.x + frame.x, frame.width)
}));
hosts.reserve(self.stats.cell_hosts);
self.widgets.begin_frame();
self.images.begin_frame();
let grid = cx.weak_entity();
let mut link_rects = std::mem::take(&mut self.links.rects);
link_rects.clear();
let mut planner = RowPlanner {
delegate: &self.delegate,
app: cx,
text_cache: &mut self.text_cache,
planned: &planned,
zone_ranges: &zone_ranges,
row_extents,
palette,
change_paint: &self.change_paint,
change_palette,
lead,
arrows: Arrows::new(),
policy,
tick,
row_height,
focus_visible,
buffer: String::new(),
glyphs: Vec::with_capacity(4_096),
any_hot: false,
cells: 0,
icons: 0,
hovered: None,
a11y,
a11y_focus,
band_clips,
hosts,
edit: &self.edit,
edited_row_painted: false,
pending: &self.pending_edits,
#[cfg(any(test, feature = "test-support"))]
test_host: self.test_cell_host.clone(),
grid,
widgets: &self.widgets,
images: &self.images,
images_painted: 0,
host_text: String::new(),
links: link_rects,
captions: CaptionPlanner {
palette: caption_palette,
texts: &self.caption_texts,
aggregates: self.view.aggregates(),
chevrons: Chevrons::new(),
chevron_size: chevron_icon,
body_x: bounds.origin.x,
row_right,
visible_columns: visible_columns.len(),
caption: String::new(),
tag: String::new(),
value: String::new(),
painted: 0,
},
};
let mut tombstones = 0;
let mut line = 0;
let mut next_display_ix = self.scroll_row;
while line < needed_lines && next_display_ix < projection.len() {
let display_ix = next_display_ix;
next_display_ix += 1;
let y = rows_area.origin.y + row_height * line as f32 - self.scroll_offset;
let (id, display_row) = match projection.line(display_ix) {
Some(display_row @ DisplayRow::Data { row, .. }) => (row, display_row),
Some(DisplayRow::GroupCaption { group, depth }) => {
let Some(group) = projection.groups().get(group) else {
continue;
};
for (runs, region) in fills.iter_mut().zip(&mut regions) {
runs.end_row(y, &mut region.fills, palette.selected);
}
let path = group.path();
let title = titles
.iter()
.find(|(id, _)| Some(id) == path.level_id(depth))
.map_or_else(
|| path.level_id(depth).map_or("", ColumnId::as_str),
|(_, title)| title,
);
planner.plan_caption(
&PlannedCaption {
group,
depth,
y,
display_ix,
title,
expanded: expansion
.is_group_expanded(path, levels.max(usize::from(depth) + 1)),
focused: focus_group.as_ref() == Some(path),
hover_x: hover_point.and_then(|(at, position)| {
(at == BodyLine::Data(line)).then_some(position.x)
}),
stamps: projection.group_stamps(group.id()),
stamp: projection.caption_stamp(group.id()),
},
&mut regions,
&mut lane,
window,
);
painted.push(PaintedLine::Caption(path.clone()));
caption_lines.push(line);
line += 1;
continue;
}
Some(DisplayRow::GroupFooter { group, depth }) => {
let Some(group) = projection.groups().get(group) else {
continue;
};
for (runs, region) in fills.iter_mut().zip(&mut regions) {
runs.end_row(y, &mut region.fills, palette.selected);
}
let path = group.path();
planner.plan_summary(
&PlannedSummary {
line: AggregateLine::Footer(group),
aggregates: group.aggregates(),
stamps: projection.group_stamps(group.id()),
y,
background: Some(palette.pinned),
weight: None,
label: None,
hovered_column: hover.and_then(|(at, column)| {
(at == BodyLine::Data(line)).then_some(column)
}),
row: PaintedRow {
pinned: None,
position: display_ix,
key_hash: footer_hash(path),
y,
height: row_height,
selected: false,
},
outline: RowOutline {
content: RowContent::Footer,
level: Some(depth.saturating_add(1)),
expanded: None,
},
},
&mut regions,
window,
);
painted.push(PaintedLine::Footer(path.clone()));
line += 1;
continue;
}
None => break,
};
let Some(row) = snapshot.row(id) else {
let fading = FadingLine {
id,
y,
display_ix,
line: display_row,
background: ((self.scroll_row + line) % 2 == 1).then_some(palette.stripe),
};
if planner.plan_fading_line(&fading, &snapshot, &mut regions, &mut fills, window) {
painted.push(PaintedLine::Ghost(id));
line += 1;
} else {
tombstones += 1;
}
continue;
};
let mut selected = painter.columns(row.key(), display_ix);
if dragging
&& !selected.is_empty()
&& !selection_policy.is_selectable(&SelectableLine::Row {
row: &**row,
branch: projection.branch(id),
})
{
selected = ColumnSet::EMPTY;
}
let whole_row = !selected.is_empty() && {
let mut with_visible = selected;
with_visible.union_with(visible_columns);
with_visible == selected
};
let outline = if structured {
let depth = display_row.depth();
RowOutline {
content: RowContent::Data,
level: Some(depth.saturating_add(1)),
expanded: projection
.branch(id)
.map(|_| expansion.is_row_expanded(row.key(), depth)),
}
} else {
RowOutline::default()
};
planner.plan_row(
&PlannedRow {
row,
y,
changes: RowChanges::Stamps(snapshot.stamps(id)),
display_ix,
line: Some(display_row),
outline,
pinned: None,
background: ((self.scroll_row + line) % 2 == 1).then_some(palette.stripe),
foreground: palette.foreground,
selected,
whole_row,
focused_column: focus
.and_then(|(focused, column)| (focused == id).then_some(column)),
hovered_column: hover
.and_then(|(at, column)| (at == BodyLine::Data(line)).then_some(column)),
},
&mut regions,
Some(&mut fills),
window,
);
painted.push(PaintedLine::Row(id));
line += 1;
}
let rows_end = rows_area.origin.y + row_height * line as f32 - self.scroll_offset;
for (runs, region) in fills.iter_mut().zip(&mut regions) {
runs.finish(rows_end, &mut region.fills, palette.selected);
}
let total_label = self.summary_texts.total();
for edge in EDGES {
let area = bands.band(edge);
self.painted_pinned.begin(edge);
if area.size.height <= px(0.) {
continue;
}
let band = &mut band_regions[band_slot(edge)];
for region in band.iter_mut() {
region.quads.push((region.mask(), palette.pinned));
}
for (band_line, y) in bands.visible_lines(edge) {
let hovered_column =
hover.and_then(|(at, column)| (at == band_line).then_some(column));
let ix = match band_line {
BodyLine::Pinned(_, ix) => ix,
BodyLine::Totals(_) => {
planner.plan_summary(
&PlannedSummary {
line: AggregateLine::Totals(edge),
aggregates: projection.totals(),
stamps: projection.totals_stamps(),
y,
background: None,
weight: Some(TOTALS_WEIGHT),
label: Some(total_label),
hovered_column,
row: PaintedRow {
pinned: Some(edge),
position: 0,
key_hash: totals_hash(edge),
y,
height: row_height,
selected: false,
},
outline: RowOutline {
content: RowContent::Totals,
..RowOutline::default()
},
},
band,
window,
);
continue;
}
BodyLine::Data(_) => continue,
};
let Some(row) = snapshot.pinned_rows(edge).get(ix) else {
continue;
};
planner.plan_row(
&PlannedRow {
row,
y,
changes: RowChanges::Stamps(snapshot.pinned_stamps(edge, ix)),
display_ix: ix,
line: None,
outline: RowOutline::default(),
pinned: Some(edge),
background: None,
foreground: palette.pinned_foreground,
selected: ColumnSet::EMPTY,
whole_row: false,
focused_column: None,
hovered_column,
},
band,
None,
window,
);
self.painted_pinned.push(edge, ix, row.clone());
}
}
let edited_row_painted = planner.edited_row_painted;
let RowPlanner {
glyphs,
any_hot,
cells,
icons,
images_painted,
hovered,
a11y,
links,
mut hosts,
captions: CaptionPlanner {
painted: captions, ..
},
..
} = planner;
self.links.rects = links;
self.links.hovered = self
.tooltip
.position
.is_some_and(|position| self.links.is_over(position));
if self.is_editing() {
self.plan_hidden_editor_host(&mut hosts, edited_row_painted, window, cx);
}
let line_top = |line: usize| {
(rows_area.origin.y + row_height * line as f32 - self.scroll_offset)
.max(rows_area.origin.y)
.min(rows_area.bottom())
};
let column_lines = ColumnLines {
columns: &self.columns,
geometry: &geometry,
body_x: bounds.origin.x,
has_right_zone,
color: palette.row_border,
};
let caption_spans: Vec<(Pixels, Pixels)> = caption_lines
.iter()
.map(|&caption| (line_top(caption), line_top(caption + 1)))
.collect();
let mut run_start = 0;
for end in caption_lines.iter().copied().chain([line]) {
let (top, bottom) = (line_top(run_start), line_top(end));
if bottom > top {
column_lines.push(&mut regions, top, bottom - top);
}
run_start = end + 1;
}
for edge in EDGES {
let area = bands.band(edge);
if area.size.height > px(0.) {
column_lines.push(
&mut band_regions[band_slot(edge)],
area.origin.y,
area.size.height,
);
}
}
let mut overlay = Vec::new();
let [left, _, right] = &geometry.zones;
if left.width > px(0.) {
push_divider(
&mut overlay,
bounds.origin.x + left.width - px(1.),
bounds,
&caption_spans,
palette.divider,
);
}
if right.width > px(0.) {
push_divider(
&mut overlay,
bounds.origin.x + right.x,
bounds,
&caption_spans,
palette.divider,
);
}
let top = bands.band(PinnedEdge::Top);
if top.size.height > px(0.) {
overlay.push((
Bounds::new(
point(bounds.origin.x, top.bottom() - px(1.)),
size(bounds.size.width, px(1.)),
),
palette.divider,
));
}
let bottom = bands.band(PinnedEdge::Bottom);
if bottom.size.height > px(0.) {
overlay.push((
Bounds::new(
point(bounds.origin.x, bottom.origin.y),
size(bounds.size.width, px(1.)),
),
palette.divider,
));
}
if let Some(x) = drop_gap_x {
overlay.push((
Bounds::new(
point(bounds.origin.x + x - px(1.), bounds.origin.y),
size(px(2.), bounds.size.height),
),
drop_gap_color,
));
}
self.retarget_drop();
let drop_indicator = self.drop_indicator(rows_area, drop_fill, drop_border);
if any_hot {
self.schedule_fade(policy.next_tick(now_ms), now_ms, cx);
}
self.stats.frames += 1;
self.stats.visible_cells = cells;
self.stats.icons = icons;
self.stats.images = images_painted;
self.stats.tombstones = tombstones;
self.stats.fading_lines = self
.painted
.iter()
.filter(|line| matches!(line, PaintedLine::Ghost(_)))
.count();
self.stats.painted_captions = captions;
self.stats.cell_hosts = hosts.len();
if hosts.is_empty() {
self.stats.last_hosts_prepaint = Duration::ZERO;
}
self.stats.last_prepaint = profile_elapsed(started);
#[cfg(any(test, feature = "test-support"))]
{
self.focus_visible = focus_visible;
}
self.publish_scroll();
let hover = self.finish_hover(hovered);
let a11y = a11y.map(|input| self.finish_a11y(input, window));
self.stats.a11y_rows = a11y.as_ref().map_or(0, |plan| plan.rows().len());
self.stats.a11y_cells = a11y.as_ref().map_or(0, |plan| plan.cell_count());
if a11y.is_none() {
self.stats.last_a11y_plan = Duration::ZERO;
#[cfg(any(test, feature = "test-support"))]
{
self.last_a11y = None;
}
}
let mut planned_regions = Vec::with_capacity(10);
for (band, [left, scrolling, right]) in
[regions].into_iter().chain(band_regions).enumerate()
{
planned_regions.extend([scrolling, left, right]);
if band == 0 {
planned_regions.push(std::mem::replace(&mut lane, Region::new(Bounds::default())));
}
}
#[cfg(any(test, feature = "test-support"))]
{
self.painted_icons.clear();
self.painted_icons
.extend(planned_regions.iter().flat_map(|region| {
region
.icons
.iter()
.map(|icon| (icon.bounds, icon.path.clone(), icon.color))
}));
self.painted_pills.clear();
self.painted_pills
.extend(planned_regions.iter().flat_map(|region| {
region
.pills
.iter()
.map(|(bounds, color, _)| (*bounds, *color))
}));
self.painted_images.clear();
self.painted_images
.extend(planned_regions.iter().flat_map(|region| {
region.images.iter().map(|image| {
(
image.bounds,
image.image_bounds,
image.corner_radius,
image.data.clone(),
)
})
}));
}
FramePlan {
regions: planned_regions,
overlay,
ellipsis,
line_height: row_height,
glyphs,
hover,
a11y,
hosts,
drop_indicator,
}
}
#[cfg(any(test, feature = "test-support"))]
pub fn painted_icons(&self) -> &[(Bounds<Pixels>, SharedString, Hsla)] {
&self.painted_icons
}
#[cfg(any(test, feature = "test-support"))]
pub fn painted_caption_tags(&self) -> &[(Bounds<Pixels>, Hsla)] {
&self.painted_pills
}
#[cfg(any(test, feature = "test-support"))]
pub fn painted_images(&self) -> &[PaintedImage] {
&self.painted_images
}
fn finish_a11y(&mut self, input: A11yInput, window: &Window) -> Rc<A11yPlan> {
let started = profile_start();
let plan = plan_a11y(input);
#[cfg(any(test, feature = "test-support"))]
if !window.is_a11y_active() {
plan.build_nodes();
}
#[cfg(not(any(test, feature = "test-support")))]
let _ = window;
self.stats.last_a11y_plan = profile_elapsed(started);
let plan = Rc::new(plan);
#[cfg(any(test, feature = "test-support"))]
{
self.last_a11y = Some(plan.clone());
}
plan
}
#[cfg(any(test, feature = "test-support"))]
fn last_a11y_row(
&self,
id: &gpui_kit::ElementId,
) -> Option<(&A11yPlan, &super::a11y::A11yRow)> {
let plan = self.last_a11y.as_deref()?;
let row = plan.rows().iter().find(|row| {
*id == gpui_kit::ElementId::NamedInteger(
SharedString::new_static(row.id_name()),
row.key_hash(),
)
})?;
Some((plan, row))
}
#[cfg(any(test, feature = "test-support"))]
pub fn a11y_row_label(&self, id: &gpui_kit::ElementId) -> Option<String> {
let (plan, row) = self.last_a11y_row(id)?;
let mut node = gpui_kit::accesskit::Node::new(gpui_kit::accesskit::Role::Row);
plan.write_row(row, &mut node);
node.label().map(str::to_owned)
}
#[cfg(any(test, feature = "test-support"))]
pub fn a11y_row_cells(&self, id: &gpui_kit::ElementId) -> Vec<(String, Option<String>)> {
let Some((plan, row)) = self.last_a11y_row(id) else {
return Vec::new();
};
plan.cells(row)
.iter()
.map(|cell| {
let node = plan.cell_node(row, cell);
(
node.label().unwrap_or_default().to_owned(),
node.description().map(str::to_owned),
)
})
.collect()
}
}
pub(super) struct PlannedRow<'r, R: GridRow> {
pub(super) row: &'r Arc<R>,
pub(super) y: Pixels,
pub(super) changes: RowChanges<'r>,
pub(super) display_ix: usize,
pub(super) line: Option<DisplayRow>,
pub(super) outline: RowOutline,
pub(super) pinned: Option<PinnedEdge>,
pub(super) background: Option<Hsla>,
pub(super) foreground: Hsla,
pub(super) selected: ColumnSet,
pub(super) whole_row: bool,
pub(super) focused_column: Option<ColumnIx>,
pub(super) hovered_column: Option<ColumnIx>,
}
impl<R: GridRow> PlannedRow<'_, R> {
pub(super) fn is_live(&self) -> bool {
matches!(self.changes, RowChanges::Stamps(_))
}
pub(super) fn key_hash(&self) -> u64 {
match self.pinned {
None => key_hash(self.row.key()),
Some(edge) => pinned_key_hash(edge, self.row.key()),
}
}
}
pub(super) struct Arrows {
up: SharedString,
down: SharedString,
error: SharedString,
}
pub(super) struct CellText<'t> {
pub(super) text: &'t str,
pub(super) x: Pixels,
pub(super) width: Pixels,
pub(super) y: Pixels,
pub(super) right: bool,
pub(super) lead: Option<CellLead>,
pub(super) trend: Option<ChangeDirection>,
pub(super) weight: Option<FontWeight>,
pub(super) color: Hsla,
}
pub(super) struct CellTextContext<'c> {
pub(super) text_cache: &'c mut TextCaches,
pub(super) glyphs: &'c mut GlyphArena,
pub(super) arrows: &'c Arrows,
pub(super) row_height: Pixels,
}
#[inline]
pub(super) fn place_cell_text(
cell: CellText<'_>,
frame: &mut CellTextContext<'_>,
region: &mut Region,
window: &mut Window,
) -> (bool, usize) {
place_marked_cell_text(cell, None, frame, region, window)
}
#[inline]
pub(super) fn place_marked_cell_text(
cell: CellText<'_>,
error: Option<Hsla>,
frame: &mut CellTextContext<'_>,
region: &mut Region,
window: &mut Window,
) -> (bool, usize) {
let CellText {
text,
x,
width,
y,
right,
lead,
trend,
weight,
color,
} = cell;
let row_height = frame.row_height;
let indent = lead.as_ref().map_or(px(0.), |lead| lead.indent);
let trend_lane = if trend.is_some() || error.is_some() {
TREND_LANE
} else {
px(0.)
};
let mut truncated = false;
let mut icons = 0;
if !text.is_empty() {
let cache = frame.text_cache.for_weight(weight);
let layout = cache.layout(text, window, frame.glyphs);
let ellipsis = weight.map(|_| cache.ellipsis(window));
let available = (width - CELL_PADDING * 2. - indent - trend_lane).max(px(0.));
truncated = layout.width() > available;
let text_x = if right {
x + width - CELL_PADDING - layout.width().min(available)
} else {
x + CELL_PADDING + indent
};
region.texts.push(TextItem {
origin: point(text_x, y),
layout,
max_width: available,
color,
ellipsis,
});
}
if let Some(icon) = lead.and_then(|lead| lead.icon) {
let lane_x = x + CELL_PADDING + indent - LEAD_ICON_LANE;
region.icons.push(IconItem {
bounds: Bounds::new(
point(
lane_x + (LEAD_ICON_LANE - icon.size) / 2.,
y + (row_height - icon.size) / 2.,
),
size(icon.size, icon.size),
),
path: icon.path,
color: icon.color,
});
icons += 1;
}
let lane_icon = match (error, trend) {
(Some(error), _) => Some((frame.arrows.error_path(), error)),
(None, Some(direction)) => Some((frame.arrows.path(direction), color)),
(None, None) => None,
};
if let Some((path, color)) = lane_icon {
let lane_x = if right {
x + CELL_PADDING + indent
} else {
x + width - CELL_PADDING - TREND_LANE
};
region.icons.push(IconItem {
bounds: Bounds::new(
point(
lane_x + (TREND_LANE - TREND_ICON) / 2.,
y + (row_height - TREND_ICON) / 2.,
),
size(TREND_ICON, TREND_ICON),
),
path,
color,
});
icons += 1;
}
(truncated, icons)
}
pub(super) fn cell_icon_size(row_height: Pixels) -> Pixels {
if row_height <= px(30.) {
px(14.)
} else {
px(16.)
}
}
pub(super) struct CellIcon {
pub(super) path: SharedString,
pub(super) x: Pixels,
pub(super) width: Pixels,
pub(super) y: Pixels,
pub(super) align: ColumnAlign,
pub(super) color: Hsla,
}
pub(super) fn place_cell_icon(icon: CellIcon, row_height: Pixels, region: &mut Region) -> usize {
let side = cell_icon_size(row_height);
let (start, end) = (icon.x + CELL_PADDING, icon.x + icon.width - CELL_PADDING);
let x = if end - start >= side {
match icon.align {
ColumnAlign::Start => start,
ColumnAlign::End => end - side,
ColumnAlign::Auto => start + (end - start - side) / 2.,
}
} else if icon.width >= side {
icon.x + (icon.width - side) / 2.
} else {
return 0;
};
region.icons.push(IconItem {
bounds: Bounds::new(
point(x, icon.y + (row_height - side) / 2.),
size(side, side),
),
path: icon.path,
color: icon.color,
});
1
}
pub(super) fn cell_image_size(row_height: Pixels) -> Pixels {
(row_height - CELL_PADDING * 2.).max(px(0.))
}
pub(super) struct CellPicture {
pub(super) data: Arc<RenderImage>,
pub(super) x: Pixels,
pub(super) width: Pixels,
pub(super) y: Pixels,
pub(super) align: ColumnAlign,
pub(super) fit: ImageFit,
pub(super) corner_radius: Pixels,
}
pub(super) fn place_cell_image(
image: CellPicture,
row_height: Pixels,
region: &mut Region,
) -> usize {
let room = Bounds::new(
point(image.x + CELL_PADDING, image.y + CELL_PADDING),
size(
image.width - CELL_PADDING * 2.,
row_height - CELL_PADDING * 2.,
),
);
let image_size = image.data.size(0);
if room.size.width <= px(0.)
|| room.size.height <= px(0.)
|| image_size.width.0 <= 0
|| image_size.height.0 <= 0
{
return 0;
}
let mut fitted = image.fit.object_fit().get_bounds(room, image_size);
if fitted.size.width < room.size.width {
fitted.origin.x = match image.align {
ColumnAlign::Start => room.origin.x,
ColumnAlign::End => room.right() - fitted.size.width,
ColumnAlign::Auto => fitted.origin.x,
};
}
region.images.push(ImageItem {
bounds: room,
image_bounds: fitted,
corner_radius: image.corner_radius,
data: image.data,
});
1
}
#[cfg(any(test, feature = "test-support"))]
pub(super) type PaintedImage = (Bounds<Pixels>, Bounds<Pixels>, Pixels, Arc<RenderImage>);
impl Arrows {
pub(super) fn new() -> Self {
Self {
up: IconName::ArrowUp.path(),
down: IconName::ArrowDown.path(),
error: IconName::CircleAlert.path(),
}
}
pub(super) fn error_path(&self) -> SharedString {
self.error.clone()
}
pub(super) fn path(&self, direction: ChangeDirection) -> SharedString {
match direction {
ChangeDirection::Up => self.up.clone(),
ChangeDirection::Down => self.down.clone(),
}
}
}
pub(super) struct RowPlanner<'a, D: LiveGridDelegate> {
pub(super) delegate: &'a D,
pub(super) app: &'a mut App,
pub(super) text_cache: &'a mut TextCaches,
pub(super) planned: &'a [PlannedColumn<ColumnOf<D>>],
pub(super) zone_ranges: &'a [Range<usize>; 3],
pub(super) row_extents: [Pixels; 3],
pub(super) palette: Palette,
pub(super) change_paint: &'a ChangePaint,
pub(super) change_palette: ChangePalette,
pub(super) lead: Option<LeadProvider<'a>>,
pub(super) arrows: Arrows,
pub(super) policy: HighlightPolicy,
pub(super) tick: u32,
pub(super) row_height: Pixels,
pub(super) focus_visible: bool,
pub(super) buffer: String,
pub(super) glyphs: GlyphArena,
pub(super) any_hot: bool,
pub(super) cells: usize,
pub(super) icons: usize,
pub(super) hovered: Option<HoveredCell>,
pub(super) a11y: Option<A11yInput>,
pub(super) a11y_focus: bool,
pub(super) band_clips: [Bounds<Pixels>; 3],
pub(super) hosts: HostPlan,
pub(super) links: Vec<Bounds<Pixels>>,
pub(super) edit: &'a EditSlot<KeyOf<D>, ColumnOf<D>>,
pub(super) edited_row_painted: bool,
pub(super) pending: &'a PendingEdits<KeyOf<D>, ColumnOf<D>>,
#[cfg(any(test, feature = "test-support"))]
pub(super) test_host: Option<TestCellHost>,
pub(super) grid: WeakEntity<LiveGridState<D>>,
pub(super) widgets: &'a WidgetSlot<KeyOf<D>, ColumnOf<D>>,
pub(super) host_text: String,
pub(super) images: &'a CellImages,
pub(super) images_painted: usize,
pub(super) captions: CaptionPlanner<'a, D::Row>,
}
const FADING_ROW_ID_NAME: &str = "live-grid-fading-row";
pub(super) struct HostCell<'c, R: GridRow> {
pub(super) row: &'c PlannedRow<'c, R>,
pub(super) column: R::Column,
pub(super) view: ViewColumnIx,
pub(super) zone: ColumnZone,
pub(super) align: ColumnAlign,
pub(super) content: CellContent,
pub(super) title: &'c SharedString,
pub(super) number_format: NumberFormat,
pub(super) bounds: Bounds<Pixels>,
pub(super) indent: Pixels,
pub(super) value: CellValue<'c>,
pub(super) change: Option<CellChange>,
pub(super) selected: bool,
pub(super) focused: bool,
pub(super) edit_state: Option<&'c EditState>,
}
impl<R: GridRow> HostCell<'_, R> {
pub(super) fn host_bounds(&self) -> Bounds<Pixels> {
Bounds::new(
point(self.bounds.origin.x + self.indent, self.bounds.origin.y),
size(
(self.bounds.size.width - self.indent).max(px(0.)),
self.bounds.size.height,
),
)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum CellHosting {
Body,
Real,
Test,
}
#[cfg(any(test, feature = "test-support"))]
pub(super) type TestCellHost = (
ColumnIx,
Rc<dyn Fn(gpui_kit::Stateful<gpui_kit::Div>) -> gpui_kit::Stateful<gpui_kit::Div>>,
);
pub(super) struct CaptionPlanner<'a, R: GridRow> {
pub(super) palette: CaptionPalette,
pub(super) texts: &'a CaptionTexts,
pub(super) aggregates: &'a [(R::Column, Aggregate<R>)],
pub(super) chevrons: Chevrons,
pub(super) chevron_size: Pixels,
pub(super) body_x: Pixels,
pub(super) row_right: Pixels,
pub(super) visible_columns: usize,
pub(super) caption: String,
pub(super) tag: String,
pub(super) value: String,
pub(super) painted: usize,
}
struct TreeLeads<'a, R: GridRow> {
column: ColumnIx,
projection: &'a Projection,
snapshot: &'a StoreSnapshot<R>,
expansion: &'a Expansion<R::Key>,
chevrons: Chevrons,
color: Hsla,
size: Pixels,
}
impl<R: GridRow> TreeLeads<'_, R> {
fn lead(&self, line: &DisplayRow, column: ColumnIx) -> Option<CellLead> {
if column != self.column {
return None;
}
let DisplayRow::Data { row, depth } = *line else {
return None;
};
let icon = self.projection.branch(row).and_then(|_| {
let key = self.snapshot.row(row)?.key();
Some(LeadIcon {
path: self
.chevrons
.path(self.expansion.is_row_expanded(key, depth)),
color: self.color,
size: self.size,
})
});
Some(CellLead {
indent: INDENT * f32::from(depth) + LEAD_ICON_LANE,
icon,
})
}
}
fn push_divider(
overlay: &mut Vec<(Bounds<Pixels>, Hsla)>,
x: Pixels,
body: Bounds<Pixels>,
gaps: &[(Pixels, Pixels)],
color: Hsla,
) {
let mut top = body.origin.y;
for &(gap_top, gap_bottom) in gaps.iter().chain([&(body.bottom(), body.bottom())]) {
if gap_top > top {
overlay.push((
Bounds::new(point(x, top), size(px(1.), gap_top - top)),
color,
));
}
top = top.max(gap_bottom);
}
}
impl<D: LiveGridDelegate> RowPlanner<'_, D> {
pub(super) fn plan_row(
&mut self,
row: &PlannedRow<'_, D::Row>,
regions: &mut [Region; 3],
mut fills: Option<&mut [FillRuns; 3]>,
window: &mut Window,
) {
let (y, row_height, palette) = (row.y, self.row_height, self.palette);
let a11y_key = self.a11y.as_mut().map(|a11y| {
let key_hash = row.key_hash();
a11y.push_outlined_row(
PaintedRow {
pinned: row.pinned,
position: row.display_ix,
key_hash,
y,
height: row_height,
selected: !row.selected.is_empty(),
},
row.outline,
);
key_hash
});
let live = row.is_live();
let first_host = self.hosts.len();
let edit_column = if live && row.pinned.is_none() {
self.edit.column_in(row.row.key()).map(GridColumns::ix)
} else {
None
};
self.edited_row_painted |= edit_column.is_some();
let pending = self.pending;
let overlaid = live && row.pinned.is_none() && !pending.is_empty();
let (planned_columns, zone_ranges) = (self.planned, self.zone_ranges);
for (slot, region) in regions.iter_mut().enumerate() {
let row_x = region.mask().origin.x;
let row_width = self.row_extents[slot];
if let Some(background) = row.background {
region.quads.push((
Bounds::new(point(row_x, y), size(row_width, row_height)),
background,
));
}
if row.whole_row
&& let Some(fills) = fills.as_deref_mut()
{
fills[slot].push(row_x, row_width);
}
for column_slot in zone_ranges[slot].clone() {
let planned = &planned_columns[column_slot];
let column = planned.column;
let (x, width) = (planned.x, planned.width);
let marks = if overlaid {
pending.marks(row.row.key(), column)
} else {
None
};
let value = match marks.and_then(|marks| marks.value) {
Some(value) => value.as_cell(),
None => row.row.value(column),
};
let change = row.changes.cell(column.ix(), &self.policy, self.tick);
self.any_hot |= change.is_some();
let selected = row.selected.contains(column.ix());
let focused = row.focused_column == Some(column.ix());
let mut style = CellStyle::default();
if let Some(change) = &change {
self.change_paint
.paint(change, &self.change_palette, &mut style);
}
if let Some(marks) = marks {
if marks.pending {
style.set_foreground(palette.muted);
}
style.set_error_mark(marks.failed);
}
let edit_state = marks.and_then(|marks| marks.state);
self.delegate.cell_style(
&CellStyleContext {
row: &**row.row,
column,
value,
change,
display_ix: row.display_ix,
pinned: row.pinned,
selected,
focused,
edit_state,
},
&mut style,
&*self.app,
);
let cell_bounds = Bounds::new(point(x, y), size(width, row_height));
if let Some(background) = style.background {
region.quads.push((cell_bounds, background));
}
if selected
&& !row.whole_row
&& let Some(fills) = fills.as_deref_mut()
{
fills[slot].push(x, width);
}
if focused && self.focus_visible {
region
.outline
.extend(focus_outline(cell_bounds).map(|bounds| (bounds, palette.ring)));
}
let lead = match (self.lead, &row.line) {
(Some(lead), Some(line)) => lead(line, column.ix()),
_ => None,
};
let indent = lead.as_ref().map_or(px(0.), |lead| lead.indent);
let edited = edit_column == Some(column.ix());
let hosting = if planned.content != CellContent::Text
|| (live && (edited || self.has_test_host(column.ix())))
{
let cell = HostCell {
row,
column,
view: planned.view,
zone: ColumnZone::ALL[slot],
align: planned.align,
content: planned.content,
title: &planned.title,
number_format: planned.number_format,
bounds: cell_bounds,
indent,
value,
change,
selected,
focused,
edit_state,
};
if (edited && self.plan_editor_host(&cell, window))
|| (planned.content != CellContent::Text
&& self.plan_cell_host(&cell, window))
{
CellHosting::Real
} else if live && self.plan_test_host(&cell) {
CellHosting::Test
} else {
CellHosting::Body
}
} else {
CellHosting::Body
};
let hosted = hosting != CellHosting::Body;
let right = match planned.align {
ColumnAlign::End => true,
ColumnAlign::Start => false,
ColumnAlign::Auto => value.is_number(),
};
let text = display_text_in(value, planned.number_format, &mut self.buffer);
let icon = match value {
CellValue::Icon(icon) if !hosted => Some(icon),
_ => None,
};
let image = match value {
CellValue::Image(image) if !hosted => Some(image),
_ => None,
};
let pictured = icon.is_some() || image.is_some();
let link = matches!(value, CellValue::Link(_)) && !hosted;
let color =
style
.foreground
.unwrap_or(if link { palette.link } else { row.foreground });
let error = (style.error_mark && !hosted).then_some(palette.danger);
let (truncated, icons) = place_marked_cell_text(
CellText {
text: if hosted || pictured { "" } else { text },
x,
width,
y,
right,
lead,
trend: style.trend.filter(|_| !hosted && !pictured),
weight: style.font_weight,
color,
},
error,
&mut CellTextContext {
text_cache: &mut *self.text_cache,
glyphs: &mut self.glyphs,
arrows: &self.arrows,
row_height,
},
region,
window,
);
let truncated = truncated || pictured;
self.icons += icons;
if link && !text.is_empty() {
let layout = self.text_cache.for_weight(style.font_weight).layout(
text,
window,
&mut self.glyphs,
);
let (ascent, descent) = layout.ascent_descent();
let trend_lane = if style.trend.is_some() || error.is_some() {
TREND_LANE
} else {
px(0.)
};
let available = (width - CELL_PADDING * 2. - indent - trend_lane).max(px(0.));
let shown = layout.width().min(available);
let text_x = if right {
x + width - CELL_PADDING - shown
} else {
x + CELL_PADDING + indent
};
let baseline = y + (row_height - ascent - descent) / 2. + ascent;
let hovered = row.hovered_column == Some(column.ix());
let line = if hovered {
color
} else {
color.opacity(color.a * 0.5)
};
region.lines.push((
Bounds::new(
point(text_x, (baseline + descent * 0.5).round()),
size(shown, px(1.)),
),
line,
));
self.links
.push(Bounds::new(point(text_x, y), size(shown, row_height)));
}
if let Some(icon) = icon {
self.icons += place_cell_icon(
CellIcon {
path: self.text_cache.icon_path(icon.path()),
x: x + indent,
width: width - indent,
y,
align: planned.align,
color,
},
row_height,
region,
);
}
if let Some(image) = image
&& let CellImage::Ready(data) = self.images.image(image.id(), window, self.app)
{
self.images_painted += place_cell_image(
CellPicture {
data,
x: x + indent,
width: width - indent,
y,
align: planned.align,
fit: planned.image_fit,
corner_radius: planned.image_radius,
},
row_height,
region,
);
}
if let Some(a11y) = &mut self.a11y {
let label = if hosting == CellHosting::Real
&& !edited
&& planned.content == CellContent::Control
{
""
} else {
text
};
a11y.push_cell(column_slot, label, selected);
if hosting == CellHosting::Real {
a11y.set_real_cell();
}
if let Some(trend) = style.trend {
a11y.set_trend(trend);
}
if link {
a11y.set_edit_note(&self.captions.texts.a11y().link);
}
match edit_state {
Some(EditState::Pending) => {
a11y.set_edit_note(&t!("live_grid.a11y.saving"))
}
Some(EditState::Failed(error)) => a11y.set_edit_note(&format!(
"{}. {}",
t!("live_grid.a11y.not_saved"),
error.message()
)),
_ => {}
}
}
self.cells += 1;
let hovered = row.hovered_column == Some(column.ix());
let a11y_focused = focused && self.a11y_focus;
if !(hovered || a11y_focused) {
continue;
}
let tooltip = self.delegate.tooltip(
&CellTooltipContext {
row: &**row.row,
column,
value,
text,
truncated,
edit_state,
},
&*self.app,
);
if a11y_focused && let (Some(a11y), Some(key_hash)) = (&mut self.a11y, a11y_key) {
let description = tooltip
.as_ref()
.and_then(CellTooltip::as_text)
.map(|tooltip| tooltip.as_ref())
.filter(|tooltip| *tooltip != text);
a11y.set_focus(key_hash, column.ix(), description);
}
if hovered {
self.hovered = Some(HoveredCell {
key_hash: a11y_key.unwrap_or_else(|| row.key_hash()),
column: column.ix(),
id: column.id(),
bounds: cell_bounds.intersect(®ion.mask()),
tooltip,
});
}
}
if let Some(fills) = fills.as_deref_mut() {
fills[slot].end_row(y, &mut region.fills, palette.selected);
}
region.lines.push((
Bounds::new(
point(row_x, y + row_height - px(1.)),
size(row_width, px(1.)),
),
palette.row_border,
));
}
if self.hosts.len() > first_host {
let clip = self.band_clips[match row.pinned {
Some(PinnedEdge::Top) => 0,
None => 1,
Some(PinnedEdge::Bottom) => 2,
}];
self.hosts.end_row(
first_host,
if live {
data_row_id_name(row.pinned)
} else {
FADING_ROW_ID_NAME
},
a11y_key.unwrap_or_else(|| row.key_hash()),
Bounds::new(point(clip.origin.x, y), size(clip.size.width, row_height)),
clip,
);
}
}
#[inline]
fn has_test_host(&self, column: ColumnIx) -> bool {
#[cfg(any(test, feature = "test-support"))]
{
self.test_host
.as_ref()
.is_some_and(|(hosted, _)| *hosted == column)
}
#[cfg(not(any(test, feature = "test-support")))]
{
let _ = column;
false
}
}
fn plan_test_host(&mut self, cell: &HostCell<'_, D::Row>) -> bool {
#[cfg(any(test, feature = "test-support"))]
if let Some((column, build)) = &self.test_host
&& *column == cell.column.ix()
{
let bounds = cell.host_bounds();
let element = build(CellHost::host_div(HostLayer::Widget, cell.column.id()));
self.hosts.push(CellHost {
layer: HostLayer::Widget,
column: *column,
zone: cell.zone,
bounds,
element,
});
return true;
}
#[cfg(not(any(test, feature = "test-support")))]
let _ = cell;
false
}
}
struct ColumnLines<'a, C> {
columns: &'a ColumnLayout<C>,
geometry: &'a ZoneGeometry,
body_x: Pixels,
has_right_zone: bool,
color: Hsla,
}
impl<C: GridColumns> ColumnLines<'_, C> {
fn push(&self, regions: &mut [Region; 3], top: Pixels, height: Pixels) {
for zone in ColumnZone::ALL {
let frame = self.geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
let region = &mut regions[zone.slot()];
for i in frame.columns.clone() {
let left = frame.column_x(columns, i);
let right = left + columns.width(i);
let x = match zone {
ColumnZone::FrozenLeft if i + 1 == columns.len() => continue,
ColumnZone::Scrolling if self.has_right_zone && right >= frame.right() => {
continue;
}
ColumnZone::FrozenRight if i == 0 => continue,
ColumnZone::FrozenRight => left,
_ => right - px(1.),
};
region.lines.push((
Bounds::new(point(self.body_x + x, top), size(px(1.), height)),
self.color,
));
}
}
}
}
pub(super) fn focus_outline(cell: Bounds<Pixels>) -> [Bounds<Pixels>; 4] {
const WIDTH: Pixels = px(2.);
let (origin, extent) = (cell.origin, cell.size);
let side = (extent.height - WIDTH * 2.).max(px(0.));
[
Bounds::new(origin, size(extent.width, WIDTH)),
Bounds::new(
point(origin.x, origin.y + extent.height - WIDTH),
size(extent.width, WIDTH),
),
Bounds::new(point(origin.x, origin.y + WIDTH), size(WIDTH, side)),
Bounds::new(
point(origin.x + extent.width - WIDTH, origin.y + WIDTH),
size(WIDTH, side),
),
]
}
#[derive(Default)]
pub(super) struct FillRuns {
open: Vec<(Pixels, Pixels, Pixels)>,
row: Vec<(Pixels, Pixels)>,
}
impl FillRuns {
fn push(&mut self, x: Pixels, width: Pixels) {
match self.row.last_mut() {
Some((left, run)) if (*left + *run - x).abs() < px(0.5) => *run = x + width - *left,
_ => self.row.push((x, width)),
}
}
fn end_row(&mut self, y: Pixels, fills: &mut Vec<(Bounds<Pixels>, Hsla)>, color: Hsla) {
let row = &mut self.row;
self.open.retain(|&(x, width, top)| {
if let Some(position) = row.iter().position(|run| *run == (x, width)) {
row.swap_remove(position);
true
} else {
fills.push((Bounds::new(point(x, top), size(width, y - top)), color));
false
}
});
self.open
.extend(row.drain(..).map(|(x, width)| (x, width, y)));
}
fn finish(&mut self, y: Pixels, fills: &mut Vec<(Bounds<Pixels>, Hsla)>, color: Hsla) {
for (x, width, top) in self.open.drain(..) {
fills.push((Bounds::new(point(x, top), size(width, y - top)), color));
}
}
}
pub(super) struct PlannedColumn<C> {
pub(super) column: C,
pub(super) view: ViewColumnIx,
pub(super) align: ColumnAlign,
pub(super) content: CellContent,
pub(super) title: SharedString,
pub(super) image_fit: ImageFit,
pub(super) image_radius: Pixels,
pub(super) number_format: NumberFormat,
pub(super) x: Pixels,
pub(super) width: Pixels,
}
#[cfg(test)]
mod tests {
use super::*;
fn features(list: &[(&str, u32)]) -> FontFeatures {
FontFeatures(Arc::new(
list.iter()
.map(|(tag, value)| ((*tag).to_owned(), *value))
.collect(),
))
}
#[test]
fn test_place_cell_icon() {
let icon = |x: f32, width: f32, align: ColumnAlign| CellIcon {
path: SharedString::new_static("icons/check.svg"),
x: px(x),
width: px(width),
y: px(100.),
align,
color: Hsla::default(),
};
let place = |icon: CellIcon, row_height: f32| {
let mut region = Region::new(Bounds::default());
let placed = place_cell_icon(icon, px(row_height), &mut region);
(placed, region.icons.first().map(|icon| icon.bounds))
};
let bounds = |x: f32, y: f32, side: f32| {
Some(Bounds::new(point(px(x), px(y)), size(px(side), px(side))))
};
assert_eq!(
place(icon(0., 80., ColumnAlign::Auto), 26.),
(1, bounds(33., 106., 14.))
);
assert_eq!(
place(icon(0., 80., ColumnAlign::Auto), 32.),
(1, bounds(32., 108., 16.))
);
assert_eq!(
place(icon(0., 80., ColumnAlign::Start), 40.),
(1, bounds(6., 112., 16.))
);
assert_eq!(
place(icon(0., 80., ColumnAlign::End), 30.),
(1, bounds(60., 108., 14.))
);
assert_eq!(
place(icon(10., 20., ColumnAlign::Start), 26.),
(1, bounds(13., 106., 14.))
);
assert_eq!(place(icon(10., 12., ColumnAlign::Auto), 26.), (0, None));
}
#[test]
fn test_tabular_figures_keep_inherited_features() {
assert_eq!(
with_tabular_figures(features(&[("zero", 1), ("ss01", 1)])),
features(&[("zero", 1), ("ss01", 1), ("tnum", 1)]),
);
assert_eq!(
with_tabular_figures(FontFeatures::default()),
features(&[("tnum", 1)])
);
assert_eq!(
with_tabular_figures(features(&[("tnum", 0), ("zero", 1)])),
features(&[("tnum", 0), ("zero", 1)]),
);
}
}