| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "gorm.io/gorm" |
| 7 | ) |
| 8 | |
| 9 | // Column widths and row heights. |
| 10 | // |
| 11 | // Row.Height and Col.Width have been on the models since the first |
| 12 | // schema and nothing has ever written one. The consequence is visible |
| 13 | // on the first screen anybody opens: a column headed "Invoice Quarter" |
| 14 | // shows "Invoice Qua", and no amount of dragging fixes it because the |
| 15 | // drag has nowhere to be saved. A spreadsheet whose columns cannot be |
| 16 | // widened is not one. |
| 17 | // |
| 18 | // Sizes are per TAB and stored against the stable row and column ids, |
| 19 | // like everything else here — so widening a column and then inserting |
| 20 | // one to its left keeps the width on the column somebody widened. |
| 21 | // |
| 22 | // Zero means "no size of its own", not "zero pixels". A tab with no |
| 23 | // sizes at all is the common case and costs nothing: the client leaves |
| 24 | // the library's defaults alone rather than being told the default |
| 25 | // twice. |
| 26 | |
| 27 | // minCellSize and maxCellSize bound what may be stored. |
| 28 | // |
| 29 | // The floor is above zero on purpose: a column dragged to nothing is a |
| 30 | // column nobody can find again to drag back, and the grid offers no |
| 31 | // "unhide" because nothing here hides. The ceiling stops one column |
| 32 | // from being wider than any screen, which has the same effect as |
| 33 | // hiding every column after it. |
| 34 | const ( |
| 35 | minCellSize = 16 |
| 36 | maxCellSize = 2000 |
| 37 | ) |
| 38 | |
| 39 | // Sizes reads a tab's column widths and row heights, keyed by grid |
| 40 | // POSITION rather than stable id. |
| 41 | // |
| 42 | // Position, because the caller is painting a grid and has no use for |
| 43 | // ids — the same reasoning as gridCell's R and C. Only non-zero entries |
| 44 | // are returned, so the map is empty for the ordinary unsized tab. |
| 45 | func (h *Handle) Sizes() (cols map[int]int, rows map[int]int, err error) { |
| 46 | cols, rows = map[int]int{}, map[int]int{} |
| 47 | |
| 48 | var colRows []Col |
| 49 | if err := h.db.Select("position", "width"). |
| 50 | Where("tab_id = ? AND width > 0", h.tabID).Find(&colRows).Error; err != nil { |
| 51 | return nil, nil, fmt.Errorf("store: read column widths: %w", err) |
| 52 | } |
| 53 | for _, c := range colRows { |
| 54 | cols[c.Position] = c.Width |
| 55 | } |
| 56 | |
| 57 | var rowRows []Row |
| 58 | if err := h.db.Select("position", "height"). |
| 59 | Where("tab_id = ? AND height > 0", h.tabID).Find(&rowRows).Error; err != nil { |
| 60 | return nil, nil, fmt.Errorf("store: read row heights: %w", err) |
| 61 | } |
| 62 | for _, r := range rowRows { |
| 63 | rows[r.Position] = r.Height |
| 64 | } |
| 65 | return cols, rows, nil |
| 66 | } |
| 67 | |
| 68 | // SetColWidth stores a column's width in pixels. |
| 69 | // |
| 70 | // The column must already be part of the tab — callers reach it through |
| 71 | // EnsureCol, the same way a write does. Sizing a column that does not |
| 72 | // exist yet is a real thing to want (widen an empty column before |
| 73 | // typing in it), and EnsureCol is what makes the coordinate real; doing |
| 74 | // it here instead would put grid positions back into a method that |
| 75 | // otherwise speaks only in stable ids. |
| 76 | func (h *Handle) SetColWidth(colID uint64, width int) error { |
| 77 | return h.setColWidth(nil, colID, width) |
| 78 | } |
| 79 | |
| 80 | // SetRowHeight is SetColWidth's twin. |
| 81 | func (h *Handle) SetRowHeight(rowID uint64, height int) error { |
| 82 | return h.setRowHeight(nil, rowID, height) |
| 83 | } |
| 84 | |
| 85 | func (h *Handle) setColWidth(g Guard, colID uint64, width int) error { |
| 86 | if err := checkSize(width); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | return h.guardedDB(g, func(db *gorm.DB) error { return h.updateSize(db, &Col{}, "width", colID, width, "column") }) |
| 90 | } |
| 91 | |
| 92 | func (h *Handle) setRowHeight(g Guard, rowID uint64, height int) error { |
| 93 | if err := checkSize(height); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | return h.guardedDB(g, func(db *gorm.DB) error { return h.updateSize(db, &Row{}, "height", rowID, height, "row") }) |
| 97 | } |
| 98 | |
| 99 | func checkSize(v int) error { |
| 100 | if v < minCellSize || v > maxCellSize { |
| 101 | return fmt.Errorf("store: size %d is outside %d..%d", v, minCellSize, maxCellSize) |
| 102 | } |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | // updateSize writes one size, scoped to this tab. |
| 107 | // |
| 108 | // Scoped to the tab as well as the id, not because the id is |
| 109 | // ambiguous — it is a primary key — but because it makes the query |
| 110 | // refuse to write to another tab's row if a caller ever passes an id |
| 111 | // from the wrong handle. The id alone would happily do it. |
| 112 | func (h *Handle) updateSize(db *gorm.DB, model any, column string, id uint64, v int, what string) error { |
| 113 | res := db.Model(model). |
| 114 | Where("id = ? AND tab_id = ?", id, h.tabID). |
| 115 | Update(column, v) |
| 116 | if res.Error != nil { |
| 117 | return fmt.Errorf("store: set %s size: %w", what, res.Error) |
| 118 | } |
| 119 | if res.RowsAffected == 0 { |
| 120 | return fmt.Errorf("store: %s %d is not part of tab %d", what, id, h.tabID) |
| 121 | } |
| 122 | return nil |
| 123 | } |
| 124 | |