Repository navigation
Expand file tree
/
Copy pathstate.go
More file actions
204 lines (179 loc) · 6.36 KB
/
Copy pathstate.go
File metadata and controls
204 lines (179 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import "slices"
// AppStatusKind describes the kind of user-facing backend status.
type AppStatusKind string
const (
// AppStatusKindReady indicates the app is idle and usable.
AppStatusKindReady AppStatusKind = "ready"
// AppStatusKindLoading indicates the app is doing busy workbook work.
AppStatusKindLoading AppStatusKind = "loading"
// AppStatusKindError indicates the latest backend action failed.
AppStatusKindError AppStatusKind = "error"
)
// AllAppStatusKinds lists AppStatusKind values for Wails enum binding.
var AllAppStatusKinds = []struct {
Value AppStatusKind
TSName string
}{
{AppStatusKindReady, "Ready"},
{AppStatusKindLoading, "Loading"},
{AppStatusKindError, "Error"},
}
const (
defaultStatusMessage = "Ready"
unsavedChangesStatusMessage = "Unsaved changes"
savingWorkbookMessage = "Saving workbook..."
savedStatusMessage = "Saved"
defaultWorkbookTitle = "Untitled"
defaultSheetName = "Sheet 1"
defaultSheetIndex = 0
minExcelRow = 1
maxExcelRow = 1048576
minExcelColumn = 1
maxExcelColumn = 16384
minZoomPercent = 25
maxZoomPercent = 400
defaultZoomPercent = 100
defaultColumnWidth = 8.43
defaultRowHeight = 15.0
)
// AppState is the complete Go-owned state snapshot rendered by the frontend.
type AppState struct {
Workbook WorkbookState `json:"workbook"`
View WorkbookViewState `json:"view"`
Status AppStatus `json:"status"`
Appearance AppearanceState `json:"appearance"`
}
// AppStatus describes current user-facing backend status.
type AppStatus struct {
Kind AppStatusKind `json:"kind"`
Message string `json:"message"`
Busy bool `json:"busy"`
}
// WorkbookState describes the loaded workbook and its sheets.
type WorkbookState struct {
Title string `json:"title"`
FilePath string `json:"filePath"`
FileName string `json:"fileName"`
Dirty bool `json:"dirty"`
Sheets []WorkbookSheet `json:"sheets"`
Styles []CellStyle `json:"styles"`
}
// WorkbookViewState describes the current workbook viewport and selection state.
type WorkbookViewState struct {
ActiveSheetName string `json:"activeSheetName"`
ActiveCell CellAddress `json:"activeCell"`
Selection CellRange `json:"selection"`
ZoomPercent int `json:"zoomPercent"`
Scroll ScrollPosition `json:"scroll"`
}
// ScrollPosition describes the top-left visible cell using 1-based coordinates.
type ScrollPosition struct {
TopRow int `json:"topRow"`
LeftColumn int `json:"leftColumn"`
}
// CellData describes one loaded worksheet cell.
type CellData struct {
Ref string `json:"ref"`
Row int `json:"row"`
Column int `json:"column"`
Value string `json:"value"`
RawValue string `json:"rawValue"`
Formula string `json:"formula"`
HasFormula bool `json:"hasFormula"`
Kind CellKind `json:"kind"`
StyleID int `json:"styleId"`
}
// CellAddress describes one cell using an Excel reference and coordinates.
type CellAddress struct {
Ref string `json:"ref"`
Row int `json:"row"`
Column int `json:"column"`
}
// CellRange describes a rectangular cell range.
type CellRange struct {
Ref string `json:"ref"`
Start CellAddress `json:"start"`
End CellAddress `json:"end"`
}
// MergedCellRange describes one merged cell range and displayed value.
type MergedCellRange struct {
Range CellRange `json:"range"`
Value string `json:"value"`
}
// ColumnLayout describes worksheet column display metadata.
type ColumnLayout struct {
Index int `json:"index"`
Name string `json:"name"`
Width float64 `json:"width"`
Hidden bool `json:"hidden"`
OutlineLevel int `json:"outlineLevel"`
StyleID int `json:"styleId"`
}
// RowLayout describes worksheet row display metadata.
type RowLayout struct {
Index int `json:"index"`
Height float64 `json:"height"`
Hidden bool `json:"hidden"`
OutlineLevel int `json:"outlineLevel"`
}
func initialAppState() AppState {
a1 := CellAddress{Ref: "A1", Row: minExcelRow, Column: minExcelColumn}
// Reuse the exact same single-cell range for sheet bounds and initial selection.
a1Range := CellRange{Ref: "A1", Start: a1, End: a1}
return AppState{
Workbook: WorkbookState{
Title: defaultWorkbookTitle,
FilePath: "",
FileName: "",
Dirty: false,
Sheets: []WorkbookSheet{
{
Index: defaultSheetIndex,
Name: defaultSheetName,
State: SheetStateVisible,
Visible: true,
Bounds: a1Range,
DefaultColumnWidth: defaultColumnWidth,
DefaultRowHeight: defaultRowHeight,
Cells: []CellData{},
MergedCells: []MergedCellRange{},
Columns: []ColumnLayout{},
Rows: []RowLayout{},
},
},
Styles: []CellStyle{},
},
View: WorkbookViewState{
ActiveSheetName: defaultSheetName,
ActiveCell: a1,
Selection: a1Range,
ZoomPercent: defaultZoomPercent,
Scroll: ScrollPosition{
TopRow: minExcelRow,
LeftColumn: minExcelColumn,
},
},
Status: AppStatus{Kind: AppStatusKindReady, Message: defaultStatusMessage, Busy: false},
Appearance: defaultAppearanceState(),
}
}
func cloneAppState(state AppState) AppState {
// Keep zero-value App structs from leaking empty appearance fields.
state.Appearance = normalizeAppearanceState(state.Appearance)
// Returning AppState by value still shares slice backing arrays unless we copy them.
state.Workbook.Sheets = slices.Clone(state.Workbook.Sheets)
for i := range state.Workbook.Sheets {
state.Workbook.Sheets[i].Cells = slices.Clone(state.Workbook.Sheets[i].Cells)
state.Workbook.Sheets[i].MergedCells = slices.Clone(state.Workbook.Sheets[i].MergedCells)
state.Workbook.Sheets[i].Columns = slices.Clone(state.Workbook.Sheets[i].Columns)
state.Workbook.Sheets[i].Rows = slices.Clone(state.Workbook.Sheets[i].Rows)
}
// Style payloads have their own nested slices.
state.Workbook.Styles = slices.Clone(state.Workbook.Styles)
for i := range state.Workbook.Styles {
state.Workbook.Styles[i].Fill.Colors = slices.Clone(state.Workbook.Styles[i].Fill.Colors)
state.Workbook.Styles[i].Borders = slices.Clone(state.Workbook.Styles[i].Borders)
}
return state
}