Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
tidedeck/workspace_persistence.go at main · allisonhere/tidedeck · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
allisonhere
tidedeck
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
tidedeck
/
workspace_persistence.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
282 lines (265 loc) · 7.32 KB
main
Breadcrumbs
tidedeck
/
workspace_persistence.go
Copy path
Top
File metadata and controls
Code
Blame
282 lines (265 loc) · 7.32 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package
tideui
import
(
"encoding/json"
"fmt"
"sort"
"sync"
)
// LayoutFormatVersion is the persisted layout schema version. It is bumped when
// the serialized shape changes so older or newer files can be migrated or
// rejected cleanly.
const
LayoutFormatVersion
=
1
// LayoutStore is the pluggable backing store for workspace persistence. The
// library never touches the filesystem on its own; applications supply a store
// when they want durable layouts.
type
LayoutStore
interface
{
Load
(
key
string
) ([]
byte
,
error
)
Save
(
key
string
,
data
[]
byte
)
error
}
// MemoryStore is an in-memory LayoutStore, suitable for tests and ephemeral
// sessions.
type
MemoryStore
struct
{
mu
sync.
Mutex
data
map
[
string
][]
byte
}
// NewMemoryStore creates an empty in-memory store.
func
NewMemoryStore
()
*
MemoryStore
{
return
&
MemoryStore
{
data
:
map
[
string
][]
byte
{}}
}
// Load returns the stored bytes for a key.
func
(
m
*
MemoryStore
)
Load
(
key
string
) ([]
byte
,
error
) {
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
data
,
ok
:=
m
.
data
[
key
]
if
!
ok
{
return
nil
,
fmt
.
Errorf
(
"tideui: no layout stored for %q"
,
key
)
}
return
append
([]
byte
(
nil
),
data
...
),
nil
}
// Save stores bytes for a key.
func
(
m
*
MemoryStore
)
Save
(
key
string
,
data
[]
byte
)
error
{
m
.
mu
.
Lock
()
defer
m
.
mu
.
Unlock
()
m
.
data
[
key
]
=
append
([]
byte
(
nil
),
data
...
)
return
nil
}
type
persistedLayout
struct
{
Version
int
`json:"version"`
Preset
string
`json:"preset,omitempty"`
Root
*
persistedNode
`json:"root,omitempty"`
Hidden
[]
string
`json:"hidden,omitempty"`
}
type
persistedNode
struct
{
Type
string
`json:"type"`
Orientation
string
`json:"orientation,omitempty"`
ID
string
`json:"id,omitempty"`
Panels
[]
string
`json:"panels,omitempty"`
Active
int
`json:"active,omitempty"`
Weight
float64
`json:"weight,omitempty"`
Children
[]
persistedNode
`json:"children,omitempty"`
}
// Persist writes the current layout to the configured store. Zoom, peek, and
// other transient modes are deliberately omitted.
func
(
ws
*
Workspace
)
Persist
()
error
{
if
ws
.
store
==
nil
||
ws
.
persistenceID
==
""
{
return
ErrNoStore
}
payload
:=
ws
.
persistedState
()
data
,
err
:=
json
.
Marshal
(
payload
)
if
err
!=
nil
{
return
err
}
return
ws
.
store
.
Save
(
ws
.
persistenceID
,
data
)
}
// Restore loads a persisted layout. Invalid or empty layouts leave the current
// state untouched and return an error. Unknown panels (for example after an app
// upgrade) are dropped, and the remaining layout is used when it is still
// valid.
func
(
ws
*
Workspace
)
Restore
()
error
{
if
ws
.
store
==
nil
||
ws
.
persistenceID
==
""
{
return
ErrNoStore
}
ws
.
restoreTried
=
true
data
,
err
:=
ws
.
store
.
Load
(
ws
.
persistenceID
)
if
err
!=
nil
{
return
err
}
var
payload
persistedLayout
if
err
:=
json
.
Unmarshal
(
data
,
&
payload
);
err
!=
nil
{
return
fmt
.
Errorf
(
"tideui: corrupt layout: %w"
,
err
)
}
if
payload
.
Version
<=
0
||
payload
.
Version
>
LayoutFormatVersion
{
return
fmt
.
Errorf
(
"tideui: unsupported layout version %d"
,
payload
.
Version
)
}
root
:=
ws
.
decodeNode
(
payload
.
Root
)
if
root
==
nil
{
return
fmt
.
Errorf
(
"tideui: layout has no usable panels"
)
}
ws
.
root
=
SanitizeLayout
(
root
)
ws
.
explicitRoot
=
true
ws
.
hidden
=
map
[
string
]
bool
{}
for
_
,
id
:=
range
payload
.
Hidden
{
if
_
,
ok
:=
ws
.
panels
[
id
];
!
ok
{
continue
}
if
panel
:=
ws
.
panels
[
id
];
panel
!=
nil
&&
!
panel
.
CanHide
() {
continue
}
ws
.
hidden
[
id
]
=
true
}
for
_
,
panel
:=
range
ws
.
panels
{
panel
.
hidden
=
ws
.
hidden
[
panel
.
id
]
}
if
payload
.
Preset
!=
""
{
if
_
,
ok
:=
ws
.
presets
[
payload
.
Preset
];
ok
{
ws
.
activePreset
=
payload
.
Preset
}
}
ws
.
ensureFocus
()
ws
.
history
.
Reset
(
ws
.
snapshot
())
return
nil
}
// PersistedJSON returns the serialized layout without writing it, for callers
// that manage their own storage.
func
(
ws
*
Workspace
)
PersistedJSON
() ([]
byte
,
error
) {
return
json
.
Marshal
(
ws
.
persistedState
())
}
// RestoreJSON replaces the layout from serialized bytes.
func
(
ws
*
Workspace
)
RestoreJSON
(
data
[]
byte
)
error
{
var
payload
persistedLayout
if
err
:=
json
.
Unmarshal
(
data
,
&
payload
);
err
!=
nil
{
return
err
}
if
payload
.
Version
<=
0
||
payload
.
Version
>
LayoutFormatVersion
{
return
fmt
.
Errorf
(
"tideui: unsupported layout version %d"
,
payload
.
Version
)
}
root
:=
ws
.
decodeNode
(
payload
.
Root
)
if
root
==
nil
{
return
fmt
.
Errorf
(
"tideui: layout has no usable panels"
)
}
ws
.
root
=
SanitizeLayout
(
root
)
ws
.
explicitRoot
=
true
for
_
,
panel
:=
range
ws
.
panels
{
panel
.
hidden
=
false
}
ws
.
hidden
=
map
[
string
]
bool
{}
for
_
,
id
:=
range
payload
.
Hidden
{
panel
,
ok
:=
ws
.
panels
[
id
]
if
!
ok
||
!
panel
.
CanHide
() {
continue
}
ws
.
hidden
[
id
]
=
true
panel
.
hidden
=
true
}
ws
.
ensureFocus
()
ws
.
commit
()
return
nil
}
func
(
ws
*
Workspace
)
persistedState
()
persistedLayout
{
var
hidden
[]
string
for
_
,
id
:=
range
ws
.
order
{
if
ws
.
isHidden
(
id
) {
hidden
=
append
(
hidden
,
id
)
}
}
sort
.
Strings
(
hidden
)
return
persistedLayout
{
Version
:
LayoutFormatVersion
,
Preset
:
ws
.
activePreset
,
Root
:
encodeNode
(
ws
.
ensureRoot
()),
Hidden
:
hidden
,
}
}
func
encodeNode
(
node
LayoutNode
)
*
persistedNode
{
switch
n
:=
node
.(
type
) {
case
*
LeafNode
:
return
&
persistedNode
{
Type
:
"leaf"
,
ID
:
n
.
ID
,
Weight
:
n
.
Weight
}
case
*
TabStackNode
:
return
&
persistedNode
{
Type
:
"tabs"
,
Panels
:
append
([]
string
(
nil
),
n
.
Panels
...
),
Active
:
n
.
Active
,
Weight
:
n
.
Weight
}
case
*
SplitNode
:
out
:=
&
persistedNode
{
Type
:
"split"
,
Weight
:
n
.
Weight
}
if
n
.
Orientation
==
SplitHorizontal
{
out
.
Orientation
=
"horizontal"
}
else
{
out
.
Orientation
=
"vertical"
}
for
_
,
child
:=
range
n
.
Children
{
if
encoded
:=
encodeNode
(
child
);
encoded
!=
nil
{
out
.
Children
=
append
(
out
.
Children
,
*
encoded
)
}
}
return
out
}
return
nil
}
// decodeNode rebuilds a tree, dropping unknown panels, duplicates, and empty
// nodes. It returns nil when nothing usable remains.
func
(
ws
*
Workspace
)
decodeNode
(
node
*
persistedNode
)
LayoutNode
{
if
node
==
nil
{
return
nil
}
switch
node
.
Type
{
case
"leaf"
:
if
!
ws
.
knownPanel
(
node
.
ID
) {
return
nil
}
return
Weighted
(
Leaf
(
node
.
ID
),
node
.
Weight
)
case
"tabs"
:
var
panels
[]
string
seen
:=
map
[
string
]
bool
{}
for
_
,
id
:=
range
node
.
Panels
{
if
!
ws
.
knownPanel
(
id
)
||
seen
[
id
] {
continue
}
seen
[
id
]
=
true
panels
=
append
(
panels
,
id
)
}
if
len
(
panels
)
==
0
{
return
nil
}
if
len
(
panels
)
==
1
{
return
Leaf
(
panels
[
0
])
}
return
Weighted
(
&
TabStackNode
{
Panels
:
panels
,
Active
:
node
.
Active
},
node
.
Weight
)
case
"split"
:
var
children
[]
LayoutNode
seen
:=
map
[
string
]
bool
{}
for
i
:=
range
node
.
Children
{
child
:=
ws
.
decodeNode
(
&
node
.
Children
[
i
])
if
child
==
nil
{
continue
}
// Drop duplicate panel leaves that a malformed file might contain.
duplicate
:=
false
for
_
,
id
:=
range
LayoutPanelIDs
(
child
) {
if
seen
[
id
] {
duplicate
=
true
break
}
}
if
duplicate
{
continue
}
for
_
,
id
:=
range
LayoutPanelIDs
(
child
) {
seen
[
id
]
=
true
}
children
=
append
(
children
,
child
)
}
if
len
(
children
)
==
0
{
return
nil
}
orientation
:=
SplitHorizontal
if
node
.
Orientation
==
"vertical"
{
orientation
=
SplitVertical
}
return
Weighted
(
&
SplitNode
{
Orientation
:
orientation
,
Children
:
children
},
node
.
Weight
)
}
return
nil
}
func
(
ws
*
Workspace
)
knownPanel
(
id
string
)
bool
{
if
id
==
""
{
return
false
}
_
,
ok
:=
ws
.
panels
[
id
]
return
ok
}
You can’t perform that action at this time.