mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 15:44:29 +00:00
CellGrid: filter buttons sit in the table's header cells at their own size, as a spreadsheet's filter arrows do
This commit is contained in:
@@ -72,8 +72,8 @@ pub struct GridOutput {
|
||||
pub draft: Option<String>,
|
||||
/// Whether Cmd/Ctrl was held at a fill commit this pass (copy and series swap).
|
||||
pub fill_swapped: bool,
|
||||
/// The filter button pressed this pass: its column and event.
|
||||
pub filter: Option<(u32, FilterButtonEvent)>,
|
||||
/// The filter button pressed this pass: its header cell and event.
|
||||
pub filter: Option<(CellAt<u32>, FilterButtonEvent)>,
|
||||
/// The fix chosen from the error tag's menu, by index.
|
||||
pub fix: Option<usize>,
|
||||
/// The outline buttons pressed this pass.
|
||||
@@ -100,7 +100,7 @@ pub(crate) struct PassOutput {
|
||||
pub(crate) committed: Option<CellEdit>,
|
||||
pub(crate) draft: Option<String>,
|
||||
pub(crate) fill_swapped: bool,
|
||||
pub(crate) filter: Option<(u32, FilterButtonEvent)>,
|
||||
pub(crate) filter: Option<(CellAt<u32>, FilterButtonEvent)>,
|
||||
pub(crate) fix: Option<usize>,
|
||||
pub(crate) outline: Vec<OutlineAction>,
|
||||
}
|
||||
@@ -134,7 +134,8 @@ pub struct CellGridStyle {
|
||||
pub(crate) mark: f32,
|
||||
pub(crate) pad: f32,
|
||||
pub(crate) border_reach: f32,
|
||||
pub(crate) side: f32,
|
||||
/// The room between a filter button and its header cell's end edge.
|
||||
pub(crate) inset: f32,
|
||||
pub(crate) gap: f32,
|
||||
}
|
||||
|
||||
@@ -186,7 +187,7 @@ pub struct CellGrid<'a> {
|
||||
/// The ranges the copy marks surround.
|
||||
#[prop]
|
||||
copy_mark: &'a [CellRange<u32>],
|
||||
/// The columns that show a filter button, with its state.
|
||||
/// The header cells that show a filter button, with its state.
|
||||
#[prop]
|
||||
filters: &'a [ColumnFilter],
|
||||
/// The cell whose problem shows an error tag, with its fixes.
|
||||
|
||||
@@ -232,12 +232,13 @@ pub enum GridMenuTarget {
|
||||
RowHeader(u32),
|
||||
}
|
||||
|
||||
/// A column that shows a filter button at the end corner of its header cell.
|
||||
/// A filtered column's drop-down button, at the end of the column's header cell: the cell of the table's header row
|
||||
/// in that column, as a spreadsheet's filter arrows sit on the table, not on the column letters.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
#[non_exhaustive]
|
||||
pub struct ColumnFilter {
|
||||
/// The column.
|
||||
pub column: u32,
|
||||
/// The header cell; the filter applies to its column.
|
||||
pub cell: CellAt<u32>,
|
||||
/// Whether the column is filtered.
|
||||
pub filtered: bool,
|
||||
/// The column's sort, shown beside the funnel or chevron.
|
||||
@@ -245,9 +246,9 @@ pub struct ColumnFilter {
|
||||
}
|
||||
|
||||
impl ColumnFilter {
|
||||
/// A filter button on `column`, unfiltered and unsorted.
|
||||
pub const fn new(column: u32) -> Self {
|
||||
ColumnFilter { column, filtered: false, sort: None }
|
||||
/// A filter button in the header cell `cell`, unfiltered and unsorted.
|
||||
pub const fn new(cell: CellAt<u32>) -> Self {
|
||||
ColumnFilter { cell, filtered: false, sort: None }
|
||||
}
|
||||
|
||||
/// Whether the column is filtered.
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! The grid's style, its own parts, and the paint over the rows: headers, the range outline, the fill handle, the copy
|
||||
//! marks, the in-cell editor and the chrome that sits on the sheet.
|
||||
|
||||
use craft_behavior::{CursorIcon, HitLevel, HitPriority, Metrics, PartKey, Positions, RowAt, ScrollPos, SelectionModel as _, Sense, TextPos};
|
||||
use craft_behavior::{
|
||||
CursorIcon, HitLevel, HitPriority, Metrics, PartKey, Positions, Proposal, RowAt, ScrollPos, SelectionModel as _, Sense, TextPos,
|
||||
};
|
||||
use craft_theme::ControlSize;
|
||||
use craft_ui::contract::{Access, Cx, Focus as _, Name, PaintUi, PartsCx, Remember as _, Role, Styled as _};
|
||||
use craft_ui::primitives::{
|
||||
@@ -14,7 +16,9 @@ use egui::{CornerRadius, Pos2, Rect, Vec2};
|
||||
|
||||
use super::editor::{EditState, Mode};
|
||||
use super::row::CellPaint;
|
||||
use super::{CellAt, CellGrid, CellGridStyle, CellGridStyleKey, CellGridWords, CellRange, CellSelection, GridLayout, SheetGeometry, SheetMetrics};
|
||||
use super::{
|
||||
CellAt, CellGrid, CellGridStyle, CellGridStyleKey, CellGridWords, CellRange, CellSelection, ColumnFilter, GridLayout, SheetGeometry, SheetMetrics,
|
||||
};
|
||||
use super::{edges, header_insets};
|
||||
use crate::ui::error_tag::ErrorTag;
|
||||
use crate::ui::filter::FilterButton;
|
||||
@@ -39,6 +43,15 @@ pub(super) struct Frame {
|
||||
pub(super) geometry: SheetGeometry,
|
||||
pub(super) columns: Vec<u32>,
|
||||
pub(super) rows: Vec<u32>,
|
||||
chrome: Chrome,
|
||||
}
|
||||
|
||||
/// Where the chrome over the cells shows this pass: each filter button in view (its index in the grid's filters, its
|
||||
/// rect and the pane its header cell is in) and the error tag.
|
||||
#[derive(Clone, Default)]
|
||||
struct Chrome {
|
||||
filters: Vec<(usize, Rect, Rect)>,
|
||||
tag: Option<Rect>,
|
||||
}
|
||||
|
||||
pub(super) fn style(cx: &StyleCx<'_>, key: CellGridStyleKey) -> CellGridStyle {
|
||||
@@ -69,7 +82,7 @@ pub(super) fn style(cx: &StyleCx<'_>, key: CellGridStyleKey) -> CellGridStyle {
|
||||
mark: control.gap,
|
||||
pad: control.pad_x,
|
||||
border_reach: cx.tokens.size.hit.slop,
|
||||
side: control.icon,
|
||||
inset: cx.tokens.space.gap.tight,
|
||||
gap: control.gap,
|
||||
}
|
||||
}
|
||||
@@ -242,7 +255,45 @@ impl CellGrid<'_> {
|
||||
at = Rect::from_x_y_ranges(at.x_range(), row.y_range());
|
||||
}
|
||||
cx.editor(EDITOR, at, EditorPurpose::Text);
|
||||
self.frame = Some(Frame { geometry, columns, rows });
|
||||
let chrome = self.place_chrome(&geometry, &columns, &rows, cx);
|
||||
self.frame = Some(Frame { geometry, columns, rows, chrome });
|
||||
}
|
||||
|
||||
/// The filter button of `filter`, named by its column's header text.
|
||||
fn filter_button(&self, filter: &ColumnFilter) -> FilterButton {
|
||||
let button = FilterButton::new(self.model.cell_text(filter.cell)).filtered(filter.filtered);
|
||||
match filter.sort.clone() {
|
||||
Some(sort) => button.sort(sort),
|
||||
None => button,
|
||||
}
|
||||
}
|
||||
|
||||
/// Places the chrome at its own sizes: each filter button in view at the end of its header cell, `style.inset` in
|
||||
/// and centred on the cell's height (no taller than the cell), and the error tag `style.inset` before its cell, or
|
||||
/// after it where the tag would leave the cells.
|
||||
fn place_chrome(&self, geometry: &SheetGeometry, columns: &[u32], rows: &[u32], cx: &PartsCx<'_, Self>) -> Chrome {
|
||||
let (rtl, inset) = (geometry.right_to_left(), cx.style.inset);
|
||||
let mut chrome = Chrome::default();
|
||||
for (at, filter) in self.filters.iter().enumerate() {
|
||||
let cell = filter.cell;
|
||||
if !rows.contains(&cell.row) || !columns.contains(&cell.column) {
|
||||
continue;
|
||||
}
|
||||
let Some(pane) = geometry.range_panes(&CellRange::cell(cell)).first().map(|pane| geometry.mirror_rect(*pane)) else { continue };
|
||||
let rect = geometry.mirror_rect(geometry.cell_rect(cell));
|
||||
let size = cx.child(filter_key(cell), &mut self.filter_button(filter), Proposal::Ideal);
|
||||
let size = Vec2::new(size.x, size.y.min(rect.height()));
|
||||
let x = if rtl { rect.left() + inset } else { rect.right() - inset - size.x };
|
||||
chrome.filters.push((at, Rect::from_min_size(Pos2::new(x, rect.center().y - size.y / 2.0), size), pane));
|
||||
}
|
||||
if let Some(problem) = &self.error_tag {
|
||||
let size = cx.child(TAG_KEY, &mut ErrorTag::new(problem.problem.clone(), problem.fixes), Proposal::Ideal);
|
||||
let cell = geometry.cell_rect(problem.cell);
|
||||
let before = cell.left() - size.x - inset;
|
||||
let left = if before >= geometry.cells().left() { before } else { cell.right() + inset };
|
||||
chrome.tag = Some(geometry.mirror_rect(Rect::from_min_size(Pos2::new(left, cell.top()), size)));
|
||||
}
|
||||
chrome
|
||||
}
|
||||
|
||||
/// The facts AccessKit gets: the sheet's full size and the headers as sub-nodes.
|
||||
@@ -323,7 +374,7 @@ impl CellGrid<'_> {
|
||||
}
|
||||
}
|
||||
}
|
||||
self.place_chrome(ui, &frame, style, cx.env.words::<CellGridWords>().outline.clone());
|
||||
self.show_chrome(ui, &frame, cx.env.words::<CellGridWords>().outline.clone());
|
||||
}
|
||||
|
||||
/// Paints the merged cells in view, each whole over the rows it spans and cut to each pane it shows in, as a row
|
||||
@@ -456,36 +507,21 @@ impl CellGrid<'_> {
|
||||
}
|
||||
|
||||
/// Shows the filter buttons, the error tag and the outline band over the sheet.
|
||||
fn place_chrome(&mut self, ui: &mut PaintUi<'_>, frame: &Frame, style: &CellGridStyle, outline: Label) {
|
||||
let geometry = &frame.geometry;
|
||||
let (side, gap) = (style.side, style.gap);
|
||||
let rtl = geometry.right_to_left();
|
||||
for filter in self.filters {
|
||||
if !frame.columns.contains(&filter.column) {
|
||||
continue;
|
||||
}
|
||||
let header = Self::column_header(geometry, filter.column);
|
||||
let origin = if rtl { header.left_bottom() - Vec2::new(0.0, side) } else { header.right_bottom() - Vec2::new(side, side) };
|
||||
let name = self.model.column_label(filter.column);
|
||||
let button = FilterButton::new(name).filtered(filter.filtered);
|
||||
let button = match filter.sort.clone() {
|
||||
Some(sort) => button.sort(sort),
|
||||
None => button,
|
||||
};
|
||||
let out = ui.child(("filter", filter.column), Rect::from_min_size(origin, Vec2::splat(side)), button);
|
||||
fn show_chrome(&mut self, ui: &mut PaintUi<'_>, frame: &Frame, outline: Label) {
|
||||
let (geometry, filters) = (&frame.geometry, self.filters);
|
||||
for &(at, rect, pane) in &frame.chrome.filters {
|
||||
let filter = &filters[at];
|
||||
let button = self.filter_button(filter);
|
||||
let out = ui.clipped(pane, |ui| ui.child(filter_key(filter.cell), rect, button));
|
||||
if let Some(event) = out.event {
|
||||
self.pass.filter = Some((filter.column, event));
|
||||
self.pass.filter = Some((filter.cell, event));
|
||||
}
|
||||
}
|
||||
if let Some(problem) = &self.error_tag {
|
||||
let cell = geometry.cell_rect(problem.cell);
|
||||
let before = cell.left() - side - gap;
|
||||
let left = if before >= geometry.cells().left() { before } else { cell.right() + gap };
|
||||
let tag = geometry.mirror_rect(Rect::from_min_size(Pos2::new(left, cell.top()), Vec2::splat(side)));
|
||||
if geometry.mirror_rect(geometry.cells()).intersects(tag) {
|
||||
let out = ui.child("error_tag", tag, ErrorTag::new(problem.problem.clone(), problem.fixes));
|
||||
self.pass.fix = out.fix.or(self.pass.fix);
|
||||
}
|
||||
if let (Some(problem), Some(tag)) = (&self.error_tag, frame.chrome.tag)
|
||||
&& geometry.mirror_rect(geometry.cells()).intersects(tag)
|
||||
{
|
||||
let out = ui.child(TAG_KEY, tag, ErrorTag::new(problem.problem.clone(), problem.fixes));
|
||||
self.pass.fix = out.fix.or(self.pass.fix);
|
||||
}
|
||||
if let Some((rows, columns)) = self.outline
|
||||
&& self.levels != (0, 0)
|
||||
@@ -496,6 +532,14 @@ impl CellGrid<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The child key of the filter button in the header cell `cell`.
|
||||
fn filter_key(cell: CellAt<u32>) -> (&'static str, u32, u32) {
|
||||
("filter", cell.row, cell.column)
|
||||
}
|
||||
|
||||
/// The child key of the error tag.
|
||||
const TAG_KEY: &str = "error_tag";
|
||||
|
||||
/// The closed outline of `rect`, as a path that starts and ends at its top-left corner.
|
||||
fn corners(rect: Rect) -> [Pos2; 5] {
|
||||
[rect.left_top(), rect.right_top(), rect.right_bottom(), rect.left_bottom(), rect.left_top()]
|
||||
|
||||
@@ -191,7 +191,7 @@ fn last_event(data: &mut StoryData, out: &GridOutput) -> String {
|
||||
last.as_ref().map_or_else(|| "No event yet".to_owned(), |event| format!("Last event: {event}"))
|
||||
}
|
||||
|
||||
/// The sales sheet with a filter button on every heading (Region sorted, Channel filtered), the error tag on a
|
||||
/// The sales sheet with a filter button in every heading cell (Region sorted, Channel filtered), the error tag on a
|
||||
/// number stored as text and two selected ranges.
|
||||
#[story(CellGrid<'static>, covers = [Default], drive = Drive::Never, title = "Sheet")]
|
||||
fn cell_grid(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
@@ -201,7 +201,9 @@ fn cell_grid(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let selection = CellSelection::from_ranges(ranges, CellAt::new(2, 1)).unwrap_or_default();
|
||||
let mut state = data.value("state", GridState { selection, ..GridState::default() }).clone();
|
||||
let filters: Vec<ColumnFilter> = (0..SALES_COLUMNS)
|
||||
.map(|column| ColumnFilter::new(column).filtered(column == 2).sort((column == 0).then(|| ColumnSort::ascending(ColumnKey::index(0)))))
|
||||
.map(|column| {
|
||||
ColumnFilter::new(CellAt::new(0, column)).filtered(column == 2).sort((column == 0).then(|| ColumnSort::ascending(ColumnKey::index(0))))
|
||||
})
|
||||
.collect();
|
||||
let fixes = [Label::from("Convert to Number"), Label::from("Ignore Error")];
|
||||
let out = CellGrid::new("Sheet", &model, &mut state)
|
||||
@@ -263,7 +265,7 @@ fn cell_grid_point_mode(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
fn cell_grid_chrome(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let model = StorySheet::numbered(ui, SHEET_ROWS, false);
|
||||
let mut state = data.value("state", GridState::default()).clone();
|
||||
let filters = [ColumnFilter::new(0).filtered(true), ColumnFilter::new(1)];
|
||||
let filters = [ColumnFilter::new(CellAt::new(0, 0)).filtered(true), ColumnFilter::new(CellAt::new(0, 1))];
|
||||
let fixes = [Label::from("Convert to number"), Label::from("Ignore")];
|
||||
let rows = OutlineAxis { levels: 1, groups: vec![OutlineGroup { start: 4, end: 6, level: 1, collapsed: false, summary_before: false }] };
|
||||
let columns = OutlineAxis::default();
|
||||
|
||||
Reference in New Issue
Block a user