| 1 | use std::collections::HashMap; |
| 2 | use std::ops::Range; |
| 3 | |
| 4 | use gpui_kit::component::input::{ |
| 5 | Copy, Cut, InlineToken, InputEvent, Paste, Textarea, TextareaState, |
| 6 | }; |
| 7 | use gpui_kit::component::native_menu::NativeMenu; |
| 8 | use gpui_kit::component::{Sizable as _, h_flex, v_flex}; |
| 9 | use gpui_kit::*; |
| 10 | use teams_core::{Cell, PICKER_COLUMNS, PICKER_ROWS, TABLE_MARK, Table, TableEdit}; |
| 11 | |
| 12 | use super::{Composer, StepKind}; |
| 13 | use crate::theme::{self, ElevatedSurface as _}; |
| 14 | use crate::views::actions::{ |
| 15 | DeleteTable, DeleteTableColumn, DeleteTableRow, InsertTableColumnLeft, InsertTableColumnRight, |
| 16 | InsertTableRowAbove, InsertTableRowBelow, |
| 17 | }; |
| 18 | |
| 19 | pub(super) const TOKEN_PREFIX: &str = "table-"; |
| 20 | const CELL_MAX_ROWS: usize = 12; |
| 21 | const PICKER_SQUARE: f32 = 16.; |
| 22 | const PICKER_GAP: f32 = 3.; |
| 23 | const TABLE_MARGIN: f32 = 4.; |
| 24 | const CELL_PADDING_X: f32 = 2.; |
| 25 | const CELL_PADDING_Y: f32 = 3.; |
| 26 | |
| 27 | pub(super) struct TableView { |
| 28 | cells: Vec<Vec<Entity<TextareaState>>>, |
| 29 | _subscriptions: Vec<Subscription>, |
| 30 | } |
| 31 | |
| 32 | impl TableView { |
| 33 | fn shape(&self) -> (usize, usize) { |
| 34 | (self.cells.len(), self.cells.first().map_or(0, Vec::len)) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// The rows and columns the pointer covers in the size picker. |
| 39 | pub(super) struct TablePicker { |
| 40 | rows: usize, |
| 41 | columns: usize, |
| 42 | } |
| 43 | |
| 44 | pub(super) fn token(number: usize) -> InlineToken { |
| 45 | InlineToken::new(format!("{TOKEN_PREFIX}{number}"), TABLE_MARK.to_string()).block() |
| 46 | } |
| 47 | |
| 48 | pub(super) fn is_table_token(token: &InlineToken) -> bool { |
| 49 | token.id().starts_with(TOKEN_PREFIX) |
| 50 | } |
| 51 | |
| 52 | fn token_number(id: &str) -> usize { |
| 53 | id.strip_prefix(TOKEN_PREFIX) |
| 54 | .and_then(|number| number.parse().ok()) |
| 55 | .unwrap_or_default() |
| 56 | } |
| 57 | |
| 58 | fn caret_on_first_line(state: &TextareaState) -> bool { |
| 59 | !state.value()[..state.cursor()].contains('\n') |
| 60 | } |
| 61 | |
| 62 | fn caret_on_last_line(state: &TextareaState) -> bool { |
| 63 | !state.value()[state.cursor()..].contains('\n') |
| 64 | } |
| 65 | |
| 66 | impl Composer { |
| 67 | fn table_tokens(&self, cx: &App) -> Vec<(Range<usize>, String)> { |
| 68 | self.input |
| 69 | .read(cx) |
| 70 | .tokens() |
| 71 | .iter() |
| 72 | .filter(|span| is_table_token(span.token())) |
| 73 | .map(|span| (span.range(), span.token().id().to_string())) |
| 74 | .collect() |
| 75 | } |
| 76 | |
| 77 | fn table_line(&self, token_id: &str, cx: &App) -> Option<usize> { |
| 78 | let (range, _) = self |
| 79 | .table_tokens(cx) |
| 80 | .into_iter() |
| 81 | .find(|(_, id)| id == token_id)?; |
| 82 | let index = self.draft.line_at(range.start); |
| 83 | self.draft.table(index).is_some().then_some(index) |
| 84 | } |
| 85 | |
| 86 | /// Every table line gets its token and cell inputs; inputs follow the draft's cells. |
| 87 | pub(super) fn sync_tables(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 88 | if self.input.read(cx).value().as_ref() != self.draft.text() { |
| 89 | return; |
| 90 | } |
| 91 | let ranges = self.draft.line_ranges(); |
| 92 | let tokens = self.table_tokens(cx); |
| 93 | for (index, line) in ranges.iter().enumerate() { |
| 94 | if self.draft.table(index).is_none() || tokens.iter().any(|(range, _)| range == line) { |
| 95 | continue; |
| 96 | } |
| 97 | self.table_counter += 1; |
| 98 | let token = token(self.table_counter); |
| 99 | self.input.update(cx, |state, cx| { |
| 100 | let selection = state.selected_range(); |
| 101 | if state |
| 102 | .replace_range_with_token(line.clone(), token, window, cx) |
| 103 | .is_ok() |
| 104 | { |
| 105 | state.set_selected_range(selection, cx); |
| 106 | } |
| 107 | }); |
| 108 | } |
| 109 | let tokens = self.table_tokens(cx); |
| 110 | self.tables |
| 111 | .retain(|id, _| tokens.iter().any(|(_, token_id)| token_id == id)); |
| 112 | for (range, id) in tokens { |
| 113 | let Some(table) = self.draft.table(self.draft.line_at(range.start)).cloned() else { |
| 114 | continue; |
| 115 | }; |
| 116 | self.sync_table_view(&id, &table, window, cx); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | fn sync_table_view( |
| 121 | &mut self, |
| 122 | token_id: &str, |
| 123 | table: &Table, |
| 124 | window: &mut Window, |
| 125 | cx: &mut Context<Self>, |
| 126 | ) { |
| 127 | let shape = (table.row_count(), table.column_count()); |
| 128 | if self |
| 129 | .tables |
| 130 | .get(token_id) |
| 131 | .is_some_and(|view| view.shape() == shape) |
| 132 | { |
| 133 | let cells = self.tables[token_id].cells.clone(); |
| 134 | for (row, inputs) in cells.iter().enumerate() { |
| 135 | for (column, input) in inputs.iter().enumerate() { |
| 136 | let text = table.cell(Cell::new(row, column)).unwrap_or_default(); |
| 137 | if input.read(cx).value().as_ref() != text { |
| 138 | let text = text.to_owned(); |
| 139 | input.update(cx, |state, cx| state.set_value(text, window, cx)); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | return; |
| 144 | } |
| 145 | let mut subscriptions = Vec::new(); |
| 146 | let cells = table |
| 147 | .rows |
| 148 | .iter() |
| 149 | .enumerate() |
| 150 | .map(|(row, texts)| { |
| 151 | texts |
| 152 | .iter() |
| 153 | .enumerate() |
| 154 | .map(|(column, text)| { |
| 155 | let text = text.clone(); |
| 156 | let input = cx.new(|cx| { |
| 157 | TextareaState::new(window, cx) |
| 158 | .auto_grow(1, CELL_MAX_ROWS) |
| 159 | .default_value(text) |
| 160 | }); |
| 161 | let id = token_id.to_owned(); |
| 162 | let cell = Cell::new(row, column); |
| 163 | subscriptions.push(cx.subscribe_in( |
| 164 | &input, |
| 165 | window, |
| 166 | move |this, _, event: &InputEvent, window, cx| { |
| 167 | this.on_cell_event(&id, cell, event, window, cx) |
| 168 | }, |
| 169 | )); |
| 170 | input |
| 171 | }) |
| 172 | .collect() |
| 173 | }) |
| 174 | .collect(); |
| 175 | self.tables.insert( |
| 176 | token_id.to_owned(), |
| 177 | TableView { |
| 178 | cells, |
| 179 | _subscriptions: subscriptions, |
| 180 | }, |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | fn on_cell_event( |
| 185 | &mut self, |
| 186 | token_id: &str, |
| 187 | cell: Cell, |
| 188 | event: &InputEvent, |
| 189 | _: &mut Window, |
| 190 | cx: &mut Context<Self>, |
| 191 | ) { |
| 192 | match event { |
| 193 | InputEvent::Focus => { |
| 194 | self.active_cell = Some((token_id.to_owned(), cell)); |
| 195 | self.table_picker = None; |
| 196 | } |
| 197 | InputEvent::Change => { |
| 198 | let Some(index) = self.table_line(token_id, cx) else { |
| 199 | return; |
| 200 | }; |
| 201 | let Some(input) = self.cell_input(token_id, cell) else { |
| 202 | return; |
| 203 | }; |
| 204 | let text = input.read(cx).value().to_string(); |
| 205 | let Some(mut table) = self.draft.table(index).cloned() else { |
| 206 | return; |
| 207 | }; |
| 208 | if table.cell(cell) == Some(text.as_str()) { |
| 209 | return; |
| 210 | } |
| 211 | table.set_cell(cell, &text); |
| 212 | self.draft.set_table(index, table); |
| 213 | self.record( |
| 214 | StepKind::CellTyping { |
| 215 | table: token_number(token_id), |
| 216 | row: cell.row, |
| 217 | column: cell.column, |
| 218 | }, |
| 219 | cx, |
| 220 | ); |
| 221 | self.schedule_draft_save(cx); |
| 222 | } |
| 223 | _ => {} |
| 224 | } |
| 225 | cx.notify(); |
| 226 | } |
| 227 | |
| 228 | fn cell_input(&self, token_id: &str, cell: Cell) -> Option<Entity<TextareaState>> { |
| 229 | self.tables |
| 230 | .get(token_id)? |
| 231 | .cells |
| 232 | .get(cell.row)? |
| 233 | .get(cell.column) |
| 234 | .cloned() |
| 235 | } |
| 236 | |
| 237 | pub(super) fn focused_cell_input( |
| 238 | &self, |
| 239 | window: &Window, |
| 240 | cx: &App, |
| 241 | ) -> Option<Entity<TextareaState>> { |
| 242 | let (token_id, cell) = self.focused_cell(window, cx)?; |
| 243 | self.cell_input(&token_id, cell) |
| 244 | } |
| 245 | |
| 246 | /// The cell that has the keyboard, if any. |
| 247 | pub(super) fn focused_cell(&self, window: &Window, cx: &App) -> Option<(String, Cell)> { |
| 248 | self.tables.iter().find_map(|(id, view)| { |
| 249 | view.cells.iter().enumerate().find_map(|(row, inputs)| { |
| 250 | inputs.iter().enumerate().find_map(|(column, input)| { |
| 251 | input |
| 252 | .focus_handle(cx) |
| 253 | .is_focused(window) |
| 254 | .then(|| (id.clone(), Cell::new(row, column))) |
| 255 | }) |
| 256 | }) |
| 257 | }) |
| 258 | } |
| 259 | |
| 260 | pub(super) fn focus_cell( |
| 261 | &mut self, |
| 262 | token_id: &str, |
| 263 | cell: Cell, |
| 264 | window: &mut Window, |
| 265 | cx: &mut Context<Self>, |
| 266 | ) { |
| 267 | let Some(input) = self.cell_input(token_id, cell) else { |
| 268 | return; |
| 269 | }; |
| 270 | input.update(cx, |state, cx| { |
| 271 | let end = state.value().len(); |
| 272 | state.focus(window, cx); |
| 273 | state.set_selected_range(end..end, cx); |
| 274 | }); |
| 275 | self.active_cell = Some((token_id.to_owned(), cell)); |
| 276 | cx.notify(); |
| 277 | } |
| 278 | |
| 279 | fn token_at_line(&self, index: usize, cx: &App) -> Option<String> { |
| 280 | let line = self.draft.line_ranges().get(index)?.clone(); |
| 281 | self.table_tokens(cx) |
| 282 | .into_iter() |
| 283 | .find(|(range, _)| *range == line) |
| 284 | .map(|(_, id)| id) |
| 285 | } |
| 286 | |
| 287 | pub(super) fn focus_table_line( |
| 288 | &mut self, |
| 289 | index: usize, |
| 290 | last: bool, |
| 291 | window: &mut Window, |
| 292 | cx: &mut Context<Self>, |
| 293 | ) { |
| 294 | let Some(token_id) = self.token_at_line(index, cx) else { |
| 295 | return; |
| 296 | }; |
| 297 | let Some(table) = self.draft.table(index) else { |
| 298 | return; |
| 299 | }; |
| 300 | let cell = if last { |
| 301 | table.last_cell() |
| 302 | } else { |
| 303 | Cell::new(0, 0) |
| 304 | }; |
| 305 | self.focus_cell(&token_id, cell, window, cx); |
| 306 | } |
| 307 | |
| 308 | pub(super) fn insert_table( |
| 309 | &mut self, |
| 310 | rows: usize, |
| 311 | columns: usize, |
| 312 | window: &mut Window, |
| 313 | cx: &mut Context<Self>, |
| 314 | ) { |
| 315 | self.table_picker = None; |
| 316 | let cursor = match self.focused_cell(window, cx) { |
| 317 | Some((token_id, _)) => self |
| 318 | .table_line(&token_id, cx) |
| 319 | .map_or(self.selection(cx).end, |index| { |
| 320 | self.draft.line_ranges()[index].end |
| 321 | }), |
| 322 | None => self.selection(cx).end, |
| 323 | }; |
| 324 | let index = self.draft.insert_table(cursor, Table::new(rows, columns)); |
| 325 | let after = self.draft.line_ranges()[index].end; |
| 326 | self.commit(Some(after), window, cx); |
| 327 | self.focus_table_line(index, false, window, cx); |
| 328 | } |
| 329 | |
| 330 | fn leave_table( |
| 331 | &mut self, |
| 332 | token_id: &str, |
| 333 | below: bool, |
| 334 | window: &mut Window, |
| 335 | cx: &mut Context<Self>, |
| 336 | ) { |
| 337 | let Some(index) = self.table_line(token_id, cx) else { |
| 338 | return; |
| 339 | }; |
| 340 | let cursor = self.draft.line_beside_table(index, below); |
| 341 | self.active_cell = None; |
| 342 | self.focus(window, cx); |
| 343 | self.commit(Some(cursor), window, cx); |
| 344 | } |
| 345 | |
| 346 | /// Tab, Shift+Tab, Up and Down while a cell has the keyboard. `false` leaves the key to |
| 347 | /// the cell. |
| 348 | pub(super) fn table_key( |
| 349 | &mut self, |
| 350 | key: TableKey, |
| 351 | window: &mut Window, |
| 352 | cx: &mut Context<Self>, |
| 353 | ) -> bool { |
| 354 | let Some((token_id, cell)) = self.focused_cell(window, cx) else { |
| 355 | return false; |
| 356 | }; |
| 357 | let Some(index) = self.table_line(&token_id, cx) else { |
| 358 | return false; |
| 359 | }; |
| 360 | let Some(mut table) = self.draft.table(index).cloned() else { |
| 361 | return false; |
| 362 | }; |
| 363 | let Some(input) = self.cell_input(&token_id, cell) else { |
| 364 | return false; |
| 365 | }; |
| 366 | let target = match key { |
| 367 | TableKey::Next => { |
| 368 | let (next, added_row) = table.next_cell(cell); |
| 369 | if added_row { |
| 370 | self.draft.set_table(index, table); |
| 371 | self.record(StepKind::Other, cx); |
| 372 | self.sync_tables(window, cx); |
| 373 | self.schedule_draft_save(cx); |
| 374 | } |
| 375 | next |
| 376 | } |
| 377 | TableKey::Previous => table.previous_cell(cell), |
| 378 | TableKey::Down if !caret_on_last_line(input.read(cx)) => return false, |
| 379 | TableKey::Up if !caret_on_first_line(input.read(cx)) => return false, |
| 380 | TableKey::Down if cell.row + 1 == table.row_count() => { |
| 381 | self.leave_table(&token_id, true, window, cx); |
| 382 | return true; |
| 383 | } |
| 384 | TableKey::Up if cell.row == 0 => { |
| 385 | self.leave_table(&token_id, false, window, cx); |
| 386 | return true; |
| 387 | } |
| 388 | TableKey::Down => Cell::new(cell.row + 1, cell.column), |
| 389 | TableKey::Up => Cell::new(cell.row - 1, cell.column), |
| 390 | }; |
| 391 | self.focus_cell(&token_id, target, window, cx); |
| 392 | true |
| 393 | } |
| 394 | |
| 395 | pub(super) fn edit_table( |
| 396 | &mut self, |
| 397 | edit: TableEdit, |
| 398 | window: &mut Window, |
| 399 | cx: &mut Context<Self>, |
| 400 | ) { |
| 401 | let Some((token_id, cell)) = self |
| 402 | .focused_cell(window, cx) |
| 403 | .or_else(|| self.active_cell.clone()) |
| 404 | else { |
| 405 | return; |
| 406 | }; |
| 407 | let Some(index) = self.table_line(&token_id, cx) else { |
| 408 | return; |
| 409 | }; |
| 410 | let Some(mut table) = self.draft.table(index).cloned() else { |
| 411 | return; |
| 412 | }; |
| 413 | match table.apply(edit, cell) { |
| 414 | Some(target) => { |
| 415 | self.draft.set_table(index, table); |
| 416 | self.record(StepKind::Other, cx); |
| 417 | self.sync_tables(window, cx); |
| 418 | self.schedule_draft_save(cx); |
| 419 | self.focus_cell(&token_id, target, window, cx); |
| 420 | } |
| 421 | None => { |
| 422 | let cursor = self.draft.remove_table(index); |
| 423 | self.active_cell = None; |
| 424 | self.focus(window, cx); |
| 425 | self.commit(Some(cursor), window, cx); |
| 426 | } |
| 427 | } |
| 428 | cx.notify(); |
| 429 | } |
| 430 | |
| 431 | /// The caret never rests on a table's line: coming from above it enters the first |
| 432 | /// cell, from below the first cell of the last row. Only a move without an edit |
| 433 | /// counts, so a paste that passes the table on its way does not enter it. |
| 434 | pub(super) fn redirect_caret_from_table( |
| 435 | &mut self, |
| 436 | window: &mut Window, |
| 437 | cx: &mut Context<Self>, |
| 438 | ) { |
| 439 | let state = self.input.read(cx); |
| 440 | let selection = state.selected_range(); |
| 441 | let focused = state.focus_handle(cx).is_focused(window); |
| 442 | let value = state.value(); |
| 443 | if value.as_ref() != self.caret_text { |
| 444 | self.caret_text = value.to_string(); |
| 445 | self.caret_before_table = selection.start; |
| 446 | return; |
| 447 | } |
| 448 | if !focused || !selection.is_empty() || value.as_ref() != self.draft.text() { |
| 449 | return; |
| 450 | } |
| 451 | let Some(index) = self.draft.table_at(selection.start) else { |
| 452 | self.caret_before_table = selection.start; |
| 453 | return; |
| 454 | }; |
| 455 | let line = self.draft.line_ranges()[index].clone(); |
| 456 | let from_below = self.caret_before_table > line.end; |
| 457 | let resting = if from_below { |
| 458 | self.draft |
| 459 | .line_ranges() |
| 460 | .get(index + 1) |
| 461 | .map_or(line.end, |next| next.start) |
| 462 | } else { |
| 463 | line.start.saturating_sub(1) |
| 464 | }; |
| 465 | self.input.update(cx, |state, cx| { |
| 466 | state.set_selected_range(resting..resting, cx) |
| 467 | }); |
| 468 | self.caret_before_table = resting; |
| 469 | let token = self.token_at_line(index, cx); |
| 470 | let Some(token_id) = token else { |
| 471 | return; |
| 472 | }; |
| 473 | let row = if from_below { |
| 474 | self.draft |
| 475 | .table(index) |
| 476 | .map_or(0, |table| table.row_count() - 1) |
| 477 | } else { |
| 478 | 0 |
| 479 | }; |
| 480 | self.focus_cell(&token_id, Cell::new(row, 0), window, cx); |
| 481 | } |
| 482 | |
| 483 | pub(super) fn open_table_picker(&mut self, cx: &mut Context<Self>) { |
| 484 | self.table_picker = Some(TablePicker { |
| 485 | rows: 1, |
| 486 | columns: 1, |
| 487 | }); |
| 488 | cx.notify(); |
| 489 | } |
| 490 | |
| 491 | pub(super) fn render_table_picker(&self, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 492 | let picker = self.table_picker.as_ref()?; |
| 493 | let grid = v_flex() |
| 494 | .gap(px(PICKER_GAP)) |
| 495 | .children((1..=PICKER_ROWS).map(|row| { |
| 496 | h_flex() |
| 497 | .gap(px(PICKER_GAP)) |
| 498 | .children((1..=PICKER_COLUMNS).map(|column| { |
| 499 | let covered = row <= picker.rows && column <= picker.columns; |
| 500 | div() |
| 501 | .id(ElementId::Name(format!("table-size-{row}-{column}").into())) |
| 502 | .size(px(PICKER_SQUARE)) |
| 503 | .rounded(px(2.)) |
| 504 | .bg(if covered { |
| 505 | theme::accent().opacity(0.6) |
| 506 | } else { |
| 507 | theme::border_strong() |
| 508 | }) |
| 509 | .cursor_pointer() |
| 510 | .on_hover(cx.listener(move |this, hovered: &bool, _, cx| { |
| 511 | if *hovered && let Some(picker) = this.table_picker.as_mut() { |
| 512 | picker.rows = row; |
| 513 | picker.columns = column; |
| 514 | cx.notify(); |
| 515 | } |
| 516 | })) |
| 517 | .on_click(cx.listener(move |this, _, window, cx| { |
| 518 | this.insert_table(row, column, window, cx) |
| 519 | })) |
| 520 | })) |
| 521 | })); |
| 522 | Some( |
| 523 | v_flex() |
| 524 | .id("table-picker") |
| 525 | .absolute() |
| 526 | .bottom(relative(1.)) |
| 527 | .left_0() |
| 528 | .mb(px(6.)) |
| 529 | .p(px(10.)) |
| 530 | .gap(px(8.)) |
| 531 | .rounded(px(10.)) |
| 532 | .flyout_surface() |
| 533 | .occlude() |
| 534 | .on_mouse_down_out(cx.listener(|this, _, _, cx| { |
| 535 | this.table_picker = None; |
| 536 | cx.notify(); |
| 537 | })) |
| 538 | .child( |
| 539 | div() |
| 540 | .text_size(px(12.)) |
| 541 | .text_color(theme::text_muted()) |
| 542 | .child(format!("Insert {}x{} table", picker.columns, picker.rows)), |
| 543 | ) |
| 544 | .child(grid) |
| 545 | .into_any_element(), |
| 546 | ) |
| 547 | } |
| 548 | |
| 549 | /// The table token: a grid of cell inputs over the full width, solid lines like Teams. |
| 550 | pub(super) fn render_table_token(&self, token_id: &str, width: Pixels) -> Option<AnyElement> { |
| 551 | let view = self.tables.get(token_id)?; |
| 552 | let rows = view.cells.iter().map(|inputs| { |
| 553 | h_flex() |
| 554 | .w_full() |
| 555 | .items_stretch() |
| 556 | .children(inputs.iter().map(|input| { |
| 557 | div() |
| 558 | .flex_1() |
| 559 | .min_w_0() |
| 560 | .px(px(CELL_PADDING_X)) |
| 561 | .py(px(CELL_PADDING_Y)) |
| 562 | .border_r_1() |
| 563 | .border_b_1() |
| 564 | .border_color(theme::border_strong()) |
| 565 | .child( |
| 566 | Textarea::new(input) |
| 567 | .appearance(false) |
| 568 | .bordered(false) |
| 569 | .small() |
| 570 | .context_menu(|menu, _, _| table_menu(menu)), |
| 571 | ) |
| 572 | })) |
| 573 | }); |
| 574 | Some( |
| 575 | v_flex() |
| 576 | .id(ElementId::Name(format!("composer-{token_id}").into())) |
| 577 | .w(width) |
| 578 | .my(px(TABLE_MARGIN)) |
| 579 | .border_t_1() |
| 580 | .border_l_1() |
| 581 | .border_color(theme::border_strong()) |
| 582 | .on_any_mouse_down(|_, window, cx| { |
| 583 | window.prevent_default(); |
| 584 | cx.stop_propagation(); |
| 585 | }) |
| 586 | .children(rows) |
| 587 | .into_any_element(), |
| 588 | ) |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | fn table_menu(menu: NativeMenu) -> NativeMenu { |
| 593 | let menu = TableEdit::INSERTS |
| 594 | .into_iter() |
| 595 | .fold(menu, |menu, edit| { |
| 596 | menu.menu(edit.label(), edit_action(edit)) |
| 597 | }) |
| 598 | .separator(); |
| 599 | TableEdit::DELETES |
| 600 | .into_iter() |
| 601 | .fold(menu, |menu, edit| { |
| 602 | menu.menu(edit.label(), edit_action(edit)) |
| 603 | }) |
| 604 | .separator() |
| 605 | .menu("Cut", Box::new(Cut)) |
| 606 | .menu("Copy", Box::new(Copy)) |
| 607 | .menu("Paste", Box::new(Paste)) |
| 608 | } |
| 609 | |
| 610 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 611 | pub(super) enum TableKey { |
| 612 | Next, |
| 613 | Previous, |
| 614 | Up, |
| 615 | Down, |
| 616 | } |
| 617 | |
| 618 | fn edit_action(edit: TableEdit) -> Box<dyn Action> { |
| 619 | match edit { |
| 620 | TableEdit::InsertRowAbove => Box::new(InsertTableRowAbove), |
| 621 | TableEdit::InsertRowBelow => Box::new(InsertTableRowBelow), |
| 622 | TableEdit::InsertColumnLeft => Box::new(InsertTableColumnLeft), |
| 623 | TableEdit::InsertColumnRight => Box::new(InsertTableColumnRight), |
| 624 | TableEdit::DeleteRow => Box::new(DeleteTableRow), |
| 625 | TableEdit::DeleteColumn => Box::new(DeleteTableColumn), |
| 626 | TableEdit::DeleteTable => Box::new(DeleteTable), |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | pub(super) type TableViews = HashMap<String, TableView>; |