use std::sync::{Arc, LazyLock};
use gpui_kit::base::{AutoScroll, TestSupportExt as _};
use gpui_kit::component::{
ActiveTheme as _, Icon, IconName, Size as ComponentSize, tooltip::Tooltip,
};
use gpui_kit::{
AnyElement, AppContext as _, Bounds, ClickEvent, Context, CursorStyle, Div, DragMoveEvent,
ElementId, Empty, EntityId, Font, FontFeatures, Hsla, InteractiveElement as _, IntoElement,
MouseButton, MouseDownEvent, ParentElement as _, Pixels, Point, Render, Role, SharedString,
StatefulInteractiveElement as _, Styled as _, TextRun, Window, accesskit, black, div, point,
prelude::FluentBuilder as _, px, size,
};
use rust_i18n::t;
use super::{
CELL_PADDING, ColumnAlign, ColumnOf, ColumnZone, HeaderLabel, LiveGridDelegate, LiveGridState,
SortOrigin,
a11y::{HEADER_ROW_INDEX, column_index},
columns::{Boundary, LayoutDelta, ZoneColumns, ZoneFrame, ZoneGeometry, column_of},
events::{LayoutOrigin, LayoutPart},
ghost::GhostTextStyle,
menu::GridTarget,
stats::{profile_elapsed, profile_start},
tooltip::CellTooltip,
};
use crate::{
engine::SortDirection,
model::{ColumnIx, GridColumns},
policy::{GridArea, SortCycleContext},
};
pub(crate) const HEADER_SORT_LANE: Pixels = px(28.);
pub(super) const SORT_GAP: Pixels = px(4.);
pub(super) const SORT_ICON: Pixels = px(12.);
pub(super) const HEADER_ICON_GAP: Pixels = px(4.);
const HEADER_ICON_ID: &str = "header-icon";
pub(super) fn header_icon_size(row_height: Pixels) -> Pixels {
if row_height < ComponentSize::Medium.table_row_height() {
px(14.)
} else {
px(16.)
}
}
pub(super) fn header_icon_lane(label: &HeaderLabel, row_height: Pixels) -> Pixels {
match label {
HeaderLabel::Title => px(0.),
HeaderLabel::IconAndTitle(_) => header_icon_size(row_height) + HEADER_ICON_GAP,
HeaderLabel::Icon(_) => header_icon_size(row_height),
}
}
const PRIORITIES: [&str; 9] = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
static TABULAR_FIGURES: LazyLock<FontFeatures> =
LazyLock::new(|| FontFeatures(Arc::new(vec![("tnum".into(), 1)])));
fn sort_tooltip(direction: SortDirection, priority: Option<usize>) -> SharedString {
let text = match (direction, priority.map(|priority| priority + 1)) {
(SortDirection::Ascending, None) => t!("live_grid.sort.ascending"),
(SortDirection::Descending, None) => t!("live_grid.sort.descending"),
(SortDirection::Ascending, Some(priority)) => {
t!("live_grid.sort.ascending_priority", priority = priority)
}
(SortDirection::Descending, Some(priority)) => {
t!("live_grid.sort.descending_priority", priority = priority)
}
};
SharedString::from(text.into_owned())
}
fn sort_direction(direction: SortDirection) -> accesskit::SortDirection {
match direction {
SortDirection::Ascending => accesskit::SortDirection::Ascending,
SortDirection::Descending => accesskit::SortDirection::Descending,
}
}
fn sort_indicator(direction: SortDirection, priority: Option<usize>, color: Hsla) -> AnyElement {
let icon = match direction {
SortDirection::Ascending => IconName::SortAscending,
SortDirection::Descending => IconName::SortDescending,
};
div()
.flex_none()
.flex()
.items_center()
.child(
div()
.id("sort-indicator")
.test_support()
.flex_none()
.tooltip(move |window, cx| {
Tooltip::new(sort_tooltip(direction, priority)).build(window, cx)
})
.child(Icon::new(icon).size_3().text_color(color)),
)
.when_some(priority, |this, priority| {
let text: SharedString = match PRIORITIES.get(priority) {
Some(text) => SharedString::new_static(text),
None => (priority + 1).to_string().into(),
};
this.child(
div()
.id("sort-priority")
.test_support()
.flex_none()
.text_xs()
.font_features(TABULAR_FIGURES.clone())
.child(text),
)
})
.into_any_element()
}
const HANDLE_HALF: Pixels = px(4.);
pub(super) fn header_label_style(window: &Window) -> (Font, Pixels) {
let style = window.text_style();
(style.font(), style.font_size.to_pixels(window.rem_size()))
}
#[derive(Default)]
pub(super) struct HeaderLabels {
font: Option<Font>,
font_size: Pixels,
widths: Vec<Option<(SharedString, Pixels)>>,
}
impl HeaderLabels {
pub(super) fn width(
&mut self,
column: ColumnIx,
title: &SharedString,
font: &Font,
font_size: Pixels,
window: &Window,
) -> Pixels {
if self.font.as_ref() != Some(font) || self.font_size != font_size {
self.widths.clear();
self.font = Some(font.clone());
self.font_size = font_size;
}
if self.widths.len() <= column.get() {
self.widths.resize(column.get() + 1, None);
}
if let Some((cached, width)) = &self.widths[column.get()]
&& cached == title
{
return *width;
}
let width = if title.is_empty() {
px(0.)
} else {
let runs = [TextRun {
len: title.len(),
font: font.clone(),
color: black(),
background_color: None,
underline: None,
strikethrough: None,
}];
window
.text_system()
.layout_line(title, font_size, &runs, None)
.width
};
self.widths[column.get()] = Some((title.clone(), width));
width
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum DragSource {
Column(ColumnIx),
Band { band: &'static str, first: ColumnIx },
}
#[derive(Clone)]
pub(crate) struct DraggedColumns {
grid: EntityId,
source: DragSource,
zone: ColumnZone,
title: SharedString,
width: Pixels,
height: Pixels,
text: GhostTextStyle,
}
impl Render for DraggedColumns {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = cx.theme();
self.text.root().child(
div()
.h(self.height)
.w(self.width)
.min_w(px(100.))
.max_w(px(450.))
.px(CELL_PADDING)
.flex()
.items_center()
.overflow_hidden()
.bg(theme.table_head)
.text_color(theme.table_head_foreground)
.border_1()
.border_color(theme.border)
.shadow_md()
.opacity(0.9)
.child(div().min_w(px(0.)).truncate().child(self.title.clone())),
)
}
}
#[derive(Clone)]
pub(crate) struct ResizingColumn {
grid: EntityId,
column: ColumnIx,
}
impl Render for ResizingColumn {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
Empty
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum DropTarget {
Gap { gap: usize, x: Pixels },
Here,
Outside,
}
pub(super) enum ColumnGesture {
Resize {
column: ColumnIx,
start_x: Pixels,
start_width: Pixels,
resized: bool,
},
Move {
source: DragSource,
zone: ColumnZone,
target: DropTarget,
cursor: Option<CursorStyle>,
},
}
#[derive(Default)]
pub(super) struct HeaderState {
pub(super) gesture: Option<ColumnGesture>,
handle_press: Option<ColumnIx>,
hovered_handle: Option<ColumnIx>,
auto_scroll: AutoScroll,
pub(super) labels: HeaderLabels,
menu_press: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum HeaderHit {
Cell(ColumnIx),
Band { band: &'static str, first: ColumnIx },
ResizeHandle(ColumnIx),
Empty,
}
fn header_target<K, C: GridColumns>(hit: Option<HeaderHit>) -> GridTarget<K, C> {
match hit {
Some(HeaderHit::Cell(ix) | HeaderHit::ResizeHandle(ix)) => GridTarget::Header {
column: column_of(ix),
},
Some(HeaderHit::Band { first, .. }) => GridTarget::Header {
column: column_of(first),
},
Some(HeaderHit::Empty) | None => GridTarget::Empty,
}
}
fn handle_clips(geometry: &ZoneGeometry, viewport: Pixels) -> [(Pixels, Pixels); 3] {
let [left, _, right] = &geometry.zones;
let left_end = if left.width > px(0.) {
left.width + HANDLE_HALF
} else {
px(0.)
};
let right_start = if right.width > px(0.) {
right.x - HANDLE_HALF
} else {
viewport
};
[
(px(0.), left_end),
(left_end, (right_start - left_end).max(px(0.))),
(right_start, (viewport - right_start).max(px(0.))),
]
}
fn handle_edge(frame: &ZoneFrame, columns: &ZoneColumns, i: usize) -> Pixels {
match frame.zone {
ColumnZone::FrozenRight => frame.column_x(columns, i),
_ => frame.column_x(columns, i) + columns.width(i),
}
}
impl<D: LiveGridDelegate> LiveGridState<D> {
pub(super) fn header_height(&self) -> Pixels {
self.row_height * (self.columns.depth() + 1) as f32
}
pub(super) fn body_top(&self) -> Pixels {
self.group_panel_height() + self.header_height()
}
fn header_viewport(&self) -> Pixels {
if self.body_bounds.size.width > px(0.) {
self.body_bounds.size.width
} else {
self.columns.total_width()
}
}
pub(super) fn render_header(
&mut self,
tooltip: Option<(ColumnIx, CellTooltip)>,
ghost_text: &GhostTextStyle,
a11y: bool,
cx: &mut Context<Self>,
) -> Div {
let started = profile_start();
let header = self.build_header(tooltip, ghost_text, a11y, cx);
self.stats.last_header_render = profile_elapsed(started);
header
}
fn build_header(
&self,
tooltip: Option<(ColumnIx, CellTooltip)>,
ghost_text: &GhostTextStyle,
a11y: bool,
cx: &Context<Self>,
) -> Div {
let theme = cx.theme();
let viewport = self.header_viewport();
let geometry = self.columns.geometry(viewport, self.scroll_x);
let row_height = self.row_height;
let height = self.header_height();
let right_zone = geometry.zone(ColumnZone::FrozenRight).width > px(0.);
let (resizing, moving) = match &self.header.gesture {
Some(ColumnGesture::Resize { column, .. }) => (Some(*column), false),
Some(ColumnGesture::Move { .. }) => (None, true),
None => (None, false),
};
let sort = self.view.sort();
let icon_size = header_icon_size(row_height);
let panel = self.group_panel.is_shown();
let grid = cx.entity_id();
let this = cx.weak_entity();
let drag = |source: DragSource, zone: ColumnZone, title: SharedString, width: Pixels| {
(
DraggedColumns {
grid,
source,
zone,
title,
width,
height: row_height,
text: ghost_text.clone(),
},
{
let this = this.clone();
move |drag: &DraggedColumns,
_: Point<Pixels>,
_: &mut Window,
cx: &mut gpui_kit::App| {
let _ = this.update(cx, |state, cx| {
state.begin_column_move(drag.source, drag.zone, cx)
});
cx.new(|_| drag.clone())
}
},
)
};
let cells = ColumnZone::ALL.map(|zone| {
let frame = geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
div()
.absolute()
.top_0()
.left(frame.x)
.w(frame.width)
.h_full()
.overflow_hidden()
.children(frame.columns.clone().map(|i| {
let ix = columns.columns[i];
let column = self.columns.column(ix);
let x = columns.offsets[i] - frame.scroll;
let width = columns.width(i);
let top = row_height * self.columns.bands_of(ix).len() as f32;
let right_line = match zone {
ColumnZone::FrozenLeft => i + 1 < columns.len(),
ColumnZone::Scrolling => !(right_zone && x + width >= frame.width),
ColumnZone::FrozenRight => false,
};
let left_line = zone == ColumnZone::FrozenRight && i > 0;
let end = column.align == ColumnAlign::End;
let tooltip = tooltip
.as_ref()
.filter(|(hovered, _)| *hovered == ix)
.map(|(_, tooltip)| tooltip.clone());
let position = sort.iter().position(|(sorted, _)| sorted.ix() == ix);
let direction = position.map(|position| sort[position].1);
let indicator = position.map(|position| {
let priority = (sort.len() >= 2).then_some(position);
sort_indicator(sort[position].1, priority, theme.table_head_foreground)
});
let icon = column.icon().map(|icon| {
div()
.id(HEADER_ICON_ID)
.test_support()
.flex_none()
.size(icon_size)
.child(
Icon::empty()
.path(icon.clone())
.size(icon_size)
.text_color(theme.table_head_foreground),
)
});
let icon_only = matches!(column.header_label, HeaderLabel::Icon(_));
div()
.id(column.column.id())
.test_support()
.role(Role::ColumnHeader)
.aria_label(column.title.clone())
.when_some(self.columns.view_column_ix(ix), |this, view| {
this.aria_column_index(column_index(view))
})
.when_some(direction.filter(|_| a11y), |this, direction| {
this.a11y_synthetic_children(move |builder| {
builder
.parent_node()
.set_sort_direction(sort_direction(direction))
})
})
.absolute()
.top(top)
.left(x)
.w(width)
.h(height - top)
.px(CELL_PADDING)
.line_height(height - top)
.truncate()
.border_color(theme.table_row_border)
.when(right_line, |this| this.border_r_1())
.when(left_line, |this| this.border_l_1())
.when(end, |this| this.text_right())
.when(column.movable || (panel && column.groupable), |this| {
let (value, constructor) =
drag(DragSource::Column(ix), zone, column.title.clone(), width);
this.on_drag(value, constructor)
})
.when(column.sortable, |this| {
this.on_click(cx.listener(move |state, event: &ClickEvent, _, cx| {
state.on_header_click(ix, event, cx)
}))
})
.on_hover(cx.listener(move |state, hovered: &bool, _, cx| {
state.on_header_cell_hover(ix, *hovered, cx)
}))
.when_some(tooltip, |this, tooltip| {
this.tooltip(move |window, cx| tooltip.clone().into_view(window, cx))
})
.map(|this| match (icon, indicator) {
(None, None) => this.child(column.title.clone()),
(icon, indicator) => {
let title = (!icon_only).then(|| {
div().min_w(px(0.)).truncate().child(column.title.clone())
});
let center = icon_only && column.align == ColumnAlign::Auto;
this.flex()
.items_center()
.gap(SORT_GAP)
.when(end, |this| this.justify_end())
.when(center, |this| this.justify_center())
.map(|this| {
if end {
this.children(indicator).children(icon).children(title)
} else {
this.children(icon).children(title).children(indicator)
}
})
}
})
}))
});
let bands = (self.columns.depth() > 0).then(|| {
ColumnZone::ALL.map(|zone| {
let frame = geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
div()
.absolute()
.top_0()
.left(frame.x)
.w(frame.width)
.h_full()
.overflow_hidden()
.children(
self.columns
.runs()
.iter()
.filter(|run| {
run.zone == zone
&& run.columns.start < frame.columns.end
&& frame.columns.start < run.columns.end
})
.map(|run| {
let first = columns.columns[run.columns.start];
let x = columns.offsets[run.columns.start] - frame.scroll;
let width = columns.offsets[run.columns.end]
- columns.offsets[run.columns.start];
let label_left = x.max(px(0.));
let label_right = (x + width).min(frame.width);
let first_id: SharedString =
<ColumnOf<D> as GridColumns>::all()[first.get()].id().into();
let span = run.columns.len();
div()
.id(ElementId::NamedChild(
Arc::new(ElementId::Name(run.band.into())),
first_id,
))
.test_support()
.role(Role::ColumnHeader)
.aria_label(run.title.clone())
.when_some(self.columns.view_column_ix(first), |this, view| {
this.aria_column_index(column_index(view))
})
.when(a11y, |this| {
this.a11y_synthetic_children(move |builder| {
builder.parent_node().set_column_span(span)
})
})
.absolute()
.top(row_height * run.level as f32)
.left(x)
.w(width)
.h(row_height)
.border_b_1()
.border_r_1()
.border_color(theme.table_row_border)
.when(run.movable, |this| {
let (value, constructor) = drag(
DragSource::Band {
band: run.band,
first,
},
zone,
run.title.clone(),
width,
);
this.on_drag(value, constructor)
})
.child(
div()
.absolute()
.top_0()
.h_full()
.left(label_left - x)
.w((label_right - label_left).max(px(0.)))
.px(CELL_PADDING)
.line_height(row_height)
.text_center()
.truncate()
.child(run.title.clone()),
)
}),
)
})
});
let clips = handle_clips(&geometry, viewport);
let handles = (!moving).then(|| {
ColumnZone::ALL.map(|zone| {
let frame = geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
let (clip_x, clip_width) = clips[zone.slot()];
let lit = resizing.or(self.header.hovered_handle);
let line = frame.columns.clone().find_map(|i| {
let ix = columns.columns[i];
(Some(ix) == lit).then(|| {
let edge = handle_edge(frame, columns, i);
let x = match zone {
ColumnZone::FrozenRight => edge,
_ => edge - px(1.),
};
let top = row_height * self.columns.bands_of(ix).len() as f32;
div()
.absolute()
.left(x - clip_x)
.top(top)
.w(px(1.))
.h(height - top)
.bg(theme.border)
})
});
div()
.absolute()
.top_0()
.left(clip_x)
.w(clip_width)
.h_full()
.overflow_hidden()
.children(line)
.children(frame.columns.clone().filter_map(|i| {
let ix = columns.columns[i];
let column = self.columns.column(ix);
if !column.resizable {
return None;
}
let edge = handle_edge(frame, columns, i);
let top = row_height * self.columns.bands_of(ix).len() as f32;
Some(
div()
.id(column.column.id())
.test_support()
.absolute()
.left(edge - clip_x - HANDLE_HALF)
.top(top)
.w(HANDLE_HALF * 2.)
.h(height - top)
.occlude()
.cursor_col_resize()
.on_hover(cx.listener(move |state, hovered: &bool, _, cx| {
state.on_resize_handle_hover(ix, *hovered, cx)
}))
.on_any_mouse_down(cx.listener(
move |state, event: &MouseDownEvent, window, cx| {
state.on_resize_handle_down(ix, event, window, cx)
},
))
.on_drag(ResizingColumn { grid, column: ix }, |drag, _, _, cx| {
cx.new(|_| drag.clone())
}),
)
}))
})
});
let [left, _, right] = &geometry.zones;
let divider = |x: Pixels| {
div()
.absolute()
.top_0()
.h_full()
.left(x)
.w(px(1.))
.bg(theme.border)
};
div()
.relative()
.flex_none()
.w_full()
.h(height)
.overflow_hidden()
.bg(theme.table_head)
.text_color(theme.table_head_foreground)
.map(|this| {
if self.held.is_some() {
this.border_b_2().border_color(theme.primary)
} else {
this.border_b_1().border_color(theme.border)
}
})
.on_any_mouse_down(cx.listener(Self::on_header_mouse_down))
.child(
div()
.id("live-grid-header")
.test_support()
.role(Role::Row)
.aria_row_index(HEADER_ROW_INDEX)
.absolute()
.top_0()
.left_0()
.size_full()
.children(cells)
.when_some(bands, |this, bands| {
this.child(
div()
.id("live-grid-bands")
.test_support()
.absolute()
.top_0()
.left_0()
.size_full()
.children(bands),
)
}),
)
.when(left.width > px(0.), |this| {
this.child(divider(left.width - px(1.)))
})
.when(right.width > px(0.), |this| this.child(divider(right.x)))
.child(
div()
.id("live-grid-resize")
.test_support()
.absolute()
.top_0()
.left_0()
.size_full()
.when_some(handles, |this, handles| this.children(handles)),
)
.when_some(self.drop_gap_x(cx), |this, x| {
this.child(
div()
.id("live-grid-drop-gap")
.test_support()
.absolute()
.top_0()
.h_full()
.left(x - px(1.))
.w(px(2.))
.bg(theme.drag_border),
)
})
}
pub(super) fn drop_gap_x(&self, cx: &gpui_kit::App) -> Option<Pixels> {
match &self.header.gesture {
Some(ColumnGesture::Move {
target: DropTarget::Gap { x, .. },
..
}) if cx.has_active_drag() => Some(*x),
_ => None,
}
}
pub(crate) fn header_hit(&self, position: Point<Pixels>) -> Option<HeaderHit> {
let height = self.header_height();
let viewport = self.header_viewport();
let origin = point(
self.body_bounds.origin.x,
self.body_bounds.origin.y - height,
);
if !Bounds::new(origin, size(viewport, height)).contains(&position) {
return None;
}
let x = position.x - origin.x;
let y = position.y - origin.y;
let geometry = self.columns.geometry(viewport, self.scroll_x);
let clips = handle_clips(&geometry, viewport);
for zone in [
ColumnZone::FrozenLeft,
ColumnZone::FrozenRight,
ColumnZone::Scrolling,
] {
let (clip_x, clip_width) = clips[zone.slot()];
if x < clip_x || x >= clip_x + clip_width {
continue;
}
let frame = geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
for i in frame.columns.clone() {
let ix = columns.columns[i];
let edge = handle_edge(frame, columns, i);
let top = self.row_height * self.columns.bands_of(ix).len() as f32;
if self.columns.column(ix).resizable
&& x >= edge - HANDLE_HALF
&& x < edge + HANDLE_HALF
&& y >= top
{
return Some(HeaderHit::ResizeHandle(ix));
}
}
}
let Some(zone) = geometry.zone_at(x) else {
return Some(HeaderHit::Empty);
};
let frame = geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
let Some(i) = columns.index_at(x - frame.x + frame.scroll) else {
return Some(HeaderHit::Empty);
};
let ix = columns.columns[i];
let level = (y / self.row_height).floor() as usize;
let Some(&band) = self.columns.bands_of(ix).get(level) else {
return Some(HeaderHit::Cell(ix));
};
let first = self
.columns
.runs()
.iter()
.find(|run| {
run.zone == zone
&& run.level == level
&& run.columns.contains(&i)
&& run.band == band
})
.map_or(ix, |run| columns.columns[run.columns.start]);
Some(HeaderHit::Band { band, first })
}
fn on_header_click(&mut self, ix: ColumnIx, event: &ClickEvent, cx: &mut Context<Self>) {
if self.header.menu_press
|| !event.standard_click()
|| !self.columns.get(ix).is_some_and(|column| column.sortable)
{
return;
}
let current: Vec<(ColumnIx, SortDirection)> = self
.view
.sort()
.iter()
.map(|(column, direction)| (column.ix(), *direction))
.collect();
let context = SortCycleContext::new(¤t, ix, event.modifiers());
let next = self.sort_cycle.next_sort(&context);
let columns = <ColumnOf<D> as GridColumns>::all();
let sort = next
.into_iter()
.filter(|(column, _)| column.get() < columns.len())
.map(|(column, direction)| (column_of::<ColumnOf<D>>(column), direction));
self.apply_sort(sort, SortOrigin::User, cx);
}
fn on_header_mouse_down(
&mut self,
event: &MouseDownEvent,
window: &mut Window,
cx: &mut Context<Self>,
) {
let primary = event.button == MouseButton::Left;
if primary {
self.header.menu_press = self.is_menu_open();
if self.header.menu_press {
return;
}
}
if self.is_context_press(
event.button,
event.modifiers,
event.click_count,
GridArea::Header,
) {
self.header.menu_press = primary;
self.on_header_context_down(event, window, cx);
return;
}
if self.is_menu_open() {
return;
}
let hit = self.header_hit(event.position);
self.begin_click(header_target(hit), event);
if !primary {
return;
}
window.focus(&self.focus_handle, cx);
match hit {
Some(HeaderHit::Cell(ix)) => self.set_current_column(ix),
Some(HeaderHit::Band { first, .. }) => self.set_current_column(first),
_ => {}
}
}
fn on_resize_handle_hover(&mut self, ix: ColumnIx, hovered: bool, cx: &mut Context<Self>) {
let handle = if hovered {
Some(ix)
} else if self.header.hovered_handle == Some(ix) {
None
} else {
return;
};
if self.header.hovered_handle != handle {
self.header.hovered_handle = handle;
cx.notify();
}
}
fn on_header_context_down(
&mut self,
event: &MouseDownEvent,
window: &mut Window,
cx: &mut Context<Self>,
) {
window.focus(&self.focus_handle, cx);
let target = header_target(self.header_hit(event.position));
self.open_menu(target, event.position, window, cx);
}
fn on_resize_handle_down(
&mut self,
ix: ColumnIx,
event: &MouseDownEvent,
window: &mut Window,
cx: &mut Context<Self>,
) {
let primary = event.button == MouseButton::Left;
if primary && self.is_menu_open() {
return;
}
if self.is_context_press(
event.button,
event.modifiers,
event.click_count,
GridArea::Header,
) {
self.on_header_context_down(event, window, cx);
return;
}
if !primary {
return;
}
window.focus(&self.focus_handle, cx);
if event.click_count >= 2 {
let target = self.header.handle_press.take().unwrap_or(ix);
let mut columns = crate::model::ColumnSet::EMPTY;
columns.insert(target);
self.request_autofit(columns, LayoutOrigin::User, cx);
return;
}
self.header.handle_press = Some(ix);
self.begin_resize(ix, event.position.x, cx);
}
fn begin_resize(&mut self, column: ColumnIx, x: Pixels, cx: &mut Context<Self>) {
self.header.gesture = Some(ColumnGesture::Resize {
column,
start_x: x,
start_width: self.columns.width(column),
resized: false,
});
self.column_gesture_started(cx);
cx.notify();
}
pub(super) fn on_resize_drag_move(
&mut self,
event: &DragMoveEvent<ResizingColumn>,
_: &mut Window,
cx: &mut Context<Self>,
) {
let drag = event.drag(cx);
if drag.grid != cx.entity_id() {
return;
}
let column = drag.column;
let x = event.event.position.x;
let Some(ColumnGesture::Resize {
column: resized_column,
start_x,
start_width,
..
}) = self.header.gesture
else {
self.begin_resize(column, x, cx);
return;
};
if resized_column != column {
return;
}
let delta = x - start_x;
let width = match self.columns.zone_of(column) {
ColumnZone::FrozenRight => start_width - delta,
_ => start_width + delta,
};
let changed = self.relayout(|layout, _| {
if layout.set_width(column, width) {
LayoutDelta::new(LayoutPart::Width, [column])
} else {
LayoutDelta::default()
}
});
if changed.is_empty() {
return;
}
self.reveal_right_edge(column);
self.clamp_scroll();
if let Some(ColumnGesture::Resize { resized, .. }) = &mut self.header.gesture {
*resized = true;
}
cx.notify();
}
fn finish_resize(&mut self, cx: &mut Context<Self>) {
let Some(ColumnGesture::Resize {
column,
start_width,
resized,
..
}) = self.header.gesture.take()
else {
return;
};
if resized && self.columns.width(column) != start_width {
self.emit_layout_change(
LayoutDelta::new(LayoutPart::Width, [column]),
LayoutOrigin::User,
cx,
);
}
cx.notify();
}
fn begin_column_move(&mut self, source: DragSource, zone: ColumnZone, cx: &mut Context<Self>) {
self.header.gesture = Some(ColumnGesture::Move {
source,
zone,
target: DropTarget::Here,
cursor: None,
});
self.column_gesture_started(cx);
cx.notify();
}
pub(super) fn column_gesture_started(&mut self, cx: &mut Context<Self>) {
self.cancel_click();
if self.pointer_inside && self.held.is_some() {
self.reconsider_held(cx);
}
}
fn drag_members(&self, source: DragSource, zone: ColumnZone) -> Option<Vec<ColumnIx>> {
match source {
DragSource::Column(ix) => (self.columns.column(ix).movable
&& self.columns.is_visible(ix)
&& self.columns.zone_of(ix) == zone)
.then(|| vec![ix]),
DragSource::Band { band, first } => {
if !self.columns.is_visible(first) || self.columns.zone_of(first) != zone {
return None;
}
let members = self.columns.band_segment(zone, band, first);
(!members.is_empty()).then_some(members)
}
}
}
fn drop_target_at(
&self,
source: DragSource,
zone: ColumnZone,
position: Point<Pixels>,
) -> DropTarget {
let Some(members) = self.drag_members(source, zone) else {
return DropTarget::Outside;
};
let x = position.x - self.body_bounds.origin.x;
let geometry = self
.columns
.geometry(self.body_bounds.size.width, self.scroll_x);
if geometry.zone_at(x) != Some(zone) {
return DropTarget::Outside;
}
let frame = geometry.zone(zone);
let columns = self.columns.zone_columns(zone);
let shift = self.columns.zone_move(zone, members);
let policy = &*self.column_move;
let mut first = frame.columns.start;
for i in frame.columns.clone() {
if x < frame.column_x(columns, i) + columns.width(i) / 2. {
break;
}
first = i + 1;
}
let resolved = match shift.resolve(first, policy) {
Boundary::Refused => (frame.columns.start..=frame.columns.end)
.filter_map(|k| match shift.resolve(k, policy) {
Boundary::Refused => None,
boundary => Some((k, boundary)),
})
.min_by(|(a, _), (b, _)| {
let distance = |k: usize| f32::from((frame.column_x(columns, k) - x).abs());
distance(*a).total_cmp(&distance(*b))
}),
boundary => Some((first, boundary)),
};
match resolved {
Some((k, Boundary::Gap(gap))) => DropTarget::Gap {
gap,
x: frame.column_x(columns, k),
},
Some((_, Boundary::Here)) => DropTarget::Here,
_ => DropTarget::Outside,
}
}
fn refresh_drop_target(
&mut self,
position: Point<Pixels>,
inside: bool,
cx: &mut Context<Self>,
) -> Option<DropTarget> {
let Some(ColumnGesture::Move { source, zone, .. }) = self.header.gesture else {
return None;
};
let target = if inside {
self.drop_target_at(source, zone, position)
} else {
DropTarget::Outside
};
if let Some(ColumnGesture::Move {
target: current, ..
}) = &mut self.header.gesture
&& *current != target
{
*current = target;
cx.notify();
}
Some(target)
}
pub(super) fn on_columns_drag_move(
&mut self,
event: &DragMoveEvent<DraggedColumns>,
window: &mut Window,
cx: &mut Context<Self>,
) {
let drag = event.drag(cx);
if drag.grid != cx.entity_id() {
return;
}
let (source, zone) = (drag.source, drag.zone);
if !matches!(self.header.gesture, Some(ColumnGesture::Move { .. })) {
self.begin_column_move(source, zone, cx);
}
let position = event.event.position;
let inside = event.bounds.contains(&position);
let panel = self.refresh_panel_column_target(source, inside.then_some(position), cx);
let Some(target) = self.refresh_drop_target(position, inside && panel.is_none(), cx) else {
return;
};
let refused = match panel {
Some(panel) => !panel.is_accepted(),
None => target == DropTarget::Outside,
};
let cursor = if refused {
CursorStyle::OperationNotAllowed
} else {
CursorStyle::ClosedHand
};
if let Some(ColumnGesture::Move { cursor: shown, .. }) = &mut self.header.gesture
&& *shown != Some(cursor)
{
*shown = Some(cursor);
cx.set_active_drag_cursor_style(cursor, window);
}
let vertical = position.y >= event.bounds.top() && position.y < event.bounds.bottom();
let delta = (zone == ColumnZone::Scrolling && vertical && panel.is_none())
.then(|| {
let frame = self
.columns
.geometry(self.body_bounds.size.width, self.scroll_x)
.zone(ColumnZone::Scrolling)
.clone();
let extent = Bounds::new(
point(px(0.), self.body_bounds.origin.x + frame.x),
size(px(0.), frame.width),
);
(frame.width > px(0.))
.then(|| AutoScroll::compute_delta(position.x, extent))
.flatten()
})
.flatten();
self.header.auto_scroll.last_drag_position = Some(position);
self.header.auto_scroll.set(delta, cx, |delta, state, cx| {
state.auto_scroll_columns(delta, cx)
});
}
fn auto_scroll_columns(&mut self, delta: Pixels, cx: &mut Context<Self>) {
if !matches!(self.header.gesture, Some(ColumnGesture::Move { .. })) {
self.header.auto_scroll.stop();
return;
}
let before = self.scroll_x;
self.scroll_x = (self.scroll_x + delta).max(px(0.));
self.clamp_scroll();
if self.scroll_x == before {
return;
}
if let Some(position) = self.header.auto_scroll.last_drag_position {
self.refresh_drop_target(position, true, cx);
}
cx.notify();
}
pub(super) fn on_columns_drop(
&mut self,
drag: &DraggedColumns,
window: &mut Window,
cx: &mut Context<Self>,
) {
if drag.grid != cx.entity_id() {
return;
}
let (source, zone) = (drag.source, drag.zone);
let position = window.mouse_position();
if self.is_over_group_panel(position) {
self.end_column_move();
self.drop_column_on_group_panel(source, position, cx);
cx.notify();
return;
}
let target = self.drop_target_at(source, zone, position);
self.end_column_move();
if let DropTarget::Gap { gap, .. } = target
&& let Some(members) = self.drag_members(source, zone)
{
self.change_layout(LayoutOrigin::User, cx, |layout, _| {
if layout.move_members(zone, &members, gap) {
LayoutDelta::new(LayoutPart::Order, members.iter().copied()).revealing()
} else {
LayoutDelta::default()
}
});
}
cx.notify();
}
fn end_column_move(&mut self) {
if matches!(self.header.gesture, Some(ColumnGesture::Move { .. })) {
self.header.gesture = None;
}
self.header.auto_scroll.stop();
self.clear_panel_target();
}
pub(crate) fn on_column_mouse_up(&mut self, cx: &mut Context<Self>) {
self.on_level_drag_mouse_up(cx);
match self.header.gesture {
Some(ColumnGesture::Resize { .. }) => self.finish_resize(cx),
Some(ColumnGesture::Move { .. }) => {
let this = cx.weak_entity();
cx.defer(move |cx| {
let _ = this.update(cx, |state, cx| {
if matches!(state.header.gesture, Some(ColumnGesture::Move { .. })) {
state.end_column_move();
cx.notify();
}
});
});
}
None => {}
}
}
pub(super) fn is_column_gesture_active(&self) -> bool {
self.header.gesture.is_some() || self.is_level_dragged()
}
}
#[cfg(test)]
mod tests {
use super::sort_tooltip;
use crate::engine::SortDirection::{Ascending, Descending};
#[test]
fn test_sort_tooltip() {
assert_eq!(sort_tooltip(Ascending, None), "Sorted ascending");
assert_eq!(sort_tooltip(Descending, None), "Sorted descending");
assert_eq!(
sort_tooltip(Ascending, Some(0)),
"Sorted ascending, priority 1"
);
assert_eq!(
sort_tooltip(Descending, Some(1)),
"Sorted descending, priority 2"
);
}
}