//! The spreadsheet cell grid: addresses in A1 notation, the sheet's geometry and the grid component. //! //! The grid is a collection on `SheetLayout`: one item per visible row ([`GridRow`]), cells as parts of the row, the //! cell selection and the cell rules of `craft_behavior`, and the in-cell editor on the editor contract. The column //! header strip, the row headers, the corner, the header edges, the fill handle and the range border are the grid's own //! parts and paint. pub use craft_behavior::{CellAdvance, CellAt, CellDirection, CellRange, CellSelection, CellSpace, LineIndex, Positions, SHEET_ZOOM}; use craft_behavior::{DisplayOrder, ItemSource, Metrics, SheetLayout}; use craft_ui::contract::{ Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, ContainerProps, ContextMenuRequest, Cx, Outcome, PaintUi, Props as _, Remember as _, Role, SelectMode, Sizing, }; use craft_ui::runtime::env::Label; use craft_ui::runtime::focus::{EndKey, KeepKeys, KeyUse}; use craft_ui::style::{ComponentStyle, Stateful, StyleColor, StyleCx, SurfaceLook}; use egui::{Rect, Response, Vec2}; use self::geometry::ROW_HEADER_LOOKAHEAD; use super::filter::FilterButtonEvent; use super::outline::{OutlineAction, OutlineAxis}; mod editor; mod geometry; mod input; mod model; mod paint; mod reference; mod row; #[cfg(feature = "screen-testing")] mod stories; pub use geometry::{GridLayout, HEADER_MIN_ZOOM, SheetGeometry, SheetMetrics}; pub use model::{CellEdit, CellProblem, ColumnFilter, GridEdit, GridMenuTarget, GridModel, GridState}; pub use reference::{SHEET_COLUMNS, SHEET_ROWS, absolute_range_reference, absolute_reference, column_letters}; pub use row::GridRow; pub(crate) use geometry::{digits_of, header_insets}; pub(crate) use reference::edges; /// The rows of a sheet as the collection's source: one key per row, equal to its position. struct SheetRows(Positions); impl DisplayOrder for SheetRows { fn len(&self) -> usize { self.0.len() } fn key(&self, at: usize) -> u32 { self.0.key(at) } fn index_of(&self, key: &u32) -> Option { self.0.index_of(key) } } impl ItemSource for SheetRows {} /// What the grid returns. #[non_exhaustive] pub struct GridOutput { /// The collection's response and events. pub collection: CollectionResponse, /// This pass's geometry, for the app's objects and overlays. pub geometry: SheetGeometry, /// What the grid asks of the app this pass, in order. pub edits: Vec, /// The draft committed this pass, for the app to write into the cell. pub committed: Option, /// The draft of the open cell, for the app's formula bar; `None` while no cell is open. pub draft: Option, /// 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 fix chosen from the error tag's menu, by index. pub fix: Option, /// The outline buttons pressed this pass. pub outline: Vec, } impl Outcome for GridOutput { type Value = craft_ui::contract::NoValue; fn response(&self) -> &Response { &self.collection.response } #[cfg(feature = "screen-testing")] fn story_facts(&self) -> craft_ui::testing::StoryOutput { self.collection.story_facts() } } /// What one pass of the grid collected for its output. #[derive(Default)] pub(crate) struct PassOutput { pub(crate) edits: Vec, pub(crate) committed: Option, pub(crate) draft: Option, pub(crate) fill_swapped: bool, pub(crate) filter: Option<(u32, FilterButtonEvent)>, pub(crate) fix: Option, pub(crate) outline: Vec, } /// The style of the grid: the sheet look, the header and selection colours and the sizes the geometry reads. #[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct CellGridStyle { #[style(surface)] surface: SurfaceLook, #[style(metrics)] metrics: Metrics, pub(crate) sheet: SheetMetrics, pub(crate) bands: Vec2, pub(crate) background: StyleColor, pub(crate) header: Stateful, pub(crate) header_text: Stateful, /// The line along the frozen panes' edge. pub(crate) freeze_line: StyleColor, pub(crate) accent: StyleColor, pub(crate) preview: StyleColor, pub(crate) selection: StyleColor, pub(crate) handle_fill: StyleColor, pub(crate) handle_ring: StyleColor, pub(crate) edit_fill: StyleColor, pub(crate) edit_edge: StyleColor, pub(crate) handle: f32, pub(crate) mark: f32, pub(crate) pad: f32, pub(crate) border_reach: f32, pub(crate) side: f32, pub(crate) gap: f32, } /// The cell grid: a sheet of cells with headers, frozen panes, merged cells, a selection of ranges, an in-cell editor, /// a fill handle and copy marks. /// /// - Pointer: click selects, drag extends with auto-scroll, Shift+click extends from the anchor, Cmd/Ctrl+click adds a /// range on an unselected cell and removes a selected one; a header click selects the line, a drag over headers /// several; the corner selects all. A header edge drags a resize, a double-click autofits (or unhides a hidden /// line); the fill handle on the current range's corner drags a [`GridEdit::Fill`], the range's border a /// [`GridEdit::MoveCells`] (Esc cancels either). /// - Keys: arrows move (Shift extends; Cmd/Ctrl jumps to [`GridModel::data_edge`]), Page Up and Down a screen, Home the /// row start, Cmd/Ctrl+Home the first cell after the frozen panes, Cmd/Ctrl+End [`GridModel::last_used`], Enter and /// Tab advance inside a selection (Shift back), Cmd/Ctrl+A selects all, Cmd/Ctrl+Space the column, Shift+Space the /// row, F2 edits, Delete clears, Backspace clears and edits, typed text starts an edit with it. Esc and Enter with a /// copy mark report [`GridEdit::ClearCopyMark`] and [`GridEdit::PasteAtActive`]. /// - The editor: typing opens the active cell in Enter mode (the arrows move to another cell and commit), F2 in Edit /// mode (the arrows move the caret), `=` and an operator in point mode (the arrows move a reference cursor). /// /// The scroll lives in the collection. #[derive(craft_ui::contract::Component, craft_ui::contract::Props)] #[component(collection)] pub struct CellGrid<'a> { #[common] common: Common, #[container] props: ContainerProps, items: CollectionProps, /// Whether the row and column headings show. #[style_key] #[prop] headings: bool, #[style_key] digits: u8, #[style_key] percent: u16, #[style_key] levels: (u8, u8), model: &'a dyn GridModel, selection: Bind<'a, CellSelection>, /// The selection as the app held it when the pass began, for the parts that declare before the item step runs. snapshot: CellSelection, zoom: f32, /// The ranges the copy marks surround. #[prop] copy_mark: &'a [CellRange], /// The columns that show a filter button, with its state. #[prop] filters: &'a [ColumnFilter], /// The cell whose problem shows an error tag, with its fixes. #[prop] error_tag: Option>, outline: Option<(&'a OutlineAxis, &'a OutlineAxis)>, selectable: Box) -> bool + 'a>, frame: Option, /// The row the keys of this pass moved the active cell to. reveal: Option, pass: PassOutput, } impl<'a> CellGrid<'a> { /// A grid named `label` over `model` that keeps its selection and zoom in `state`. pub fn new(label: impl Into