forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappStore.ts
More file actions
315 lines (267 loc) · 9.74 KB
/
Copy pathappStore.ts
File metadata and controls
315 lines (267 loc) · 9.74 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
export type UiScale = 'small' | 'medium' | 'large' | 'very-large'
export type BackendStatus = 'not_started' | 'starting' | 'ready' | 'error'
export type SetupStatus = 'idle' | 'checking' | 'needed' | 'installing' | 'done' | 'error'
export interface SetupProgress { step: string; percent: number; currentPackage?: string }
export type GenerationStatus =
| 'idle'
| 'uploading'
| 'generating'
| 'done'
| 'error'
export interface GenerationJob {
id: string
imageFile: string
status: GenerationStatus
progress: number
step?: string
outputUrl?: string
originalOutputUrl?: string // mesh URL before any optimization
thumbnailUrl?: string
modelId?: string // model used for this generation
originalTriangles?: number // polygon count of the original mesh
generationOptions?: GenerationOptions
error?: string
createdAt: number
}
export interface GenerationOptions {
modelId: string
remesh: 'quad' | 'triangle' | 'none'
enableTexture: boolean
textureResolution: number
modelParams: Record<string, any>
}
export interface LightSettings {
mainIntensity: number
mainColor: string
fillIntensity: number
fillColor: string
ambientIntensity: number
envIntensity: number
}
export interface AppToast {
id: number
message: string
durationMs?: number
}
const DEFAULT_OPTIONS: GenerationOptions = {
modelId: '',
remesh: 'quad',
enableTexture: false,
textureResolution: 512,
modelParams: {},
}
export const DEFAULT_LIGHT_SETTINGS: LightSettings = {
// Matches the offline debug renderer's flat studio rig: two soft directional
// lights (key ~0.8 / fill ~0.35) + high ambient (0.45) that lifts dark albedo
// (black cat) out of "void" shadows, NO IBL (envIntensity 0).
// All live-adjustable from the Lighting popover (Reset returns here).
mainIntensity: 0.8,
mainColor: '#ffffff',
fillIntensity: 0.35,
fillColor: '#ffffff',
ambientIntensity: 0.45,
envIntensity: 0.0,
}
interface AppState {
// Backend
backendStatus: BackendStatus
apiUrl: string
backendError: string | null
// Current generation
currentJob: GenerationJob | null
// Selected image (shared between ImageUpload and the Generate button)
selectedImagePath: string | null
setSelectedImagePath: (path: string | null) => void
selectedImagePreviewUrl: string | null
setSelectedImagePreviewUrl: (url: string | null) => void
selectedImageData: string | null // base64 content for drag & drop (when path is unavailable)
setSelectedImageData: (data: string | null) => void
// Generation options
generationOptions: GenerationOptions
// Mesh stats (set by Viewer3D, read by GenerationHUD)
meshStats: { vertices: number; triangles: number } | null
setMeshStats: (stats: { vertices: number; triangles: number } | null) => void
// Mesh selection (set by Viewer3D click, read by the Generate tools bar)
meshSelected: boolean
setMeshSelected: (selected: boolean) => void
// Setup
setupStatus: SetupStatus
setupProgress: SetupProgress | null
setupError: string | null
defaultDataDir: string
platform: string
arch: string
checkSetup: () => Promise<void>
runSetup: () => Promise<void>
saveDataDir: (baseDir: string) => Promise<void>
// Patch auto-update
patchUpdateReady: boolean
setPatchUpdateReady: (ready: boolean) => void
// Error modal
errorModal: string | null
showError: (message: string) => void
hideError: () => void
// Toast
toast: AppToast | null
showToast: (message: string, durationMs?: number) => void
hideToast: () => void
// Mesh URL history (undo/redo)
meshHistory: string[]
historyIndex: number
pushMeshUrl: (url: string) => void
undoMesh: () => void
redoMesh: () => void
clearMeshHistory: () => void
// UI preferences
showRamIndicator: boolean
setShowRamIndicator: (v: boolean) => void
showVramIndicator: boolean
setShowVramIndicator: (v: boolean) => void
// Accessibility
useAtkinsonFont: boolean
setUseAtkinsonFont: (v: boolean) => void
uiScale: UiScale
setUiScale: (v: UiScale) => void
// 3D viewer lighting
lightSettings: LightSettings
setLightSettings: (settings: LightSettings) => void
// Actions
initApp: () => Promise<void>
setCurrentJob: (job: GenerationJob | null) => void
updateCurrentJob: (patch: Partial<GenerationJob>) => void
setGenerationOptions: (patch: Partial<GenerationOptions>) => void
}
export const useAppStore = create<AppState>()(
persist(
(set, get) => ({
backendStatus: 'not_started',
apiUrl: '',
backendError: null,
setupStatus: 'idle',
setupProgress: null,
setupError: null,
defaultDataDir: '',
platform: '',
arch: '',
checkSetup: async () => {
set({ setupStatus: 'checking' })
const { needed, defaultDataDir, platform, arch } = await window.electron.setup.check()
set({ setupStatus: needed ? 'needed' : 'done', defaultDataDir, platform, arch })
},
saveDataDir: async (baseDir: string) => {
await window.electron.setup.saveDataDir(baseDir)
get().runSetup()
},
runSetup: async () => {
set({ setupStatus: 'installing', setupProgress: null, setupError: null })
window.electron.setup.offProgress()
window.electron.setup.offComplete()
window.electron.setup.offError()
window.electron.setup.onProgress((data) => {
set({ setupProgress: data })
})
window.electron.setup.onComplete(() => {
set({ setupStatus: 'done', setupProgress: null })
})
window.electron.setup.onError((data) => {
set({ setupStatus: 'error', setupError: data.message })
})
// Fire and forget — progress comes via IPC events
window.electron.setup.run()
},
patchUpdateReady: false,
setPatchUpdateReady: (ready) => set({ patchUpdateReady: ready }),
errorModal: null,
showError: (message) => set({ errorModal: message }),
hideError: () => set({ errorModal: null }),
toast: null,
showToast: (message, durationMs) => set({ toast: { id: Date.now(), message, durationMs } }),
hideToast: () => set({ toast: null }),
meshHistory: [],
historyIndex: -1,
pushMeshUrl: (url) => {
const { meshHistory, historyIndex } = get()
const next = [...meshHistory.slice(0, historyIndex + 1), url]
set({ meshHistory: next, historyIndex: next.length - 1 })
},
undoMesh: () => {
const { meshHistory, historyIndex } = get()
if (historyIndex <= 0) return
const newIndex = historyIndex - 1
set({ historyIndex: newIndex })
get().updateCurrentJob({ outputUrl: meshHistory[newIndex] })
},
redoMesh: () => {
const { meshHistory, historyIndex } = get()
if (historyIndex >= meshHistory.length - 1) return
const newIndex = historyIndex + 1
set({ historyIndex: newIndex })
get().updateCurrentJob({ outputUrl: meshHistory[newIndex] })
},
clearMeshHistory: () => set({ meshHistory: [], historyIndex: -1 }),
showRamIndicator: true,
setShowRamIndicator: (v) => set({ showRamIndicator: v }),
showVramIndicator: true,
setShowVramIndicator: (v) => set({ showVramIndicator: v }),
useAtkinsonFont: false,
setUseAtkinsonFont: (v) => set({ useAtkinsonFont: v }),
uiScale: 'medium',
setUiScale: (v) => set({ uiScale: v }),
lightSettings: DEFAULT_LIGHT_SETTINGS,
setLightSettings: (settings) => set({ lightSettings: settings }),
currentJob: null,
selectedImagePath: null,
setSelectedImagePath: (path) => set({ selectedImagePath: path }),
selectedImagePreviewUrl: null,
setSelectedImagePreviewUrl: (url) => set({ selectedImagePreviewUrl: url }),
selectedImageData: null,
setSelectedImageData: (data) => set({ selectedImageData: data }),
generationOptions: DEFAULT_OPTIONS,
meshStats: null,
setMeshStats: (stats) => set({ meshStats: stats }),
meshSelected: false,
setMeshSelected: (selected) => set({ meshSelected: selected }),
initApp: async () => {
set({ backendStatus: 'starting', backendError: null })
window.electron.python.offCrashed()
window.electron.python.onCrashed(({ code }) => {
const msg = `FastAPI process crashed unexpectedly (exit code: ${code ?? 'unknown'})`
set({ backendStatus: 'error', apiUrl: '', backendError: msg })
get().showError(msg)
})
try {
const result = await window.electron.python.start()
if (!result.success) throw new Error(result.error ?? 'Failed to start backend')
const { apiUrl } = await window.electron.app.info()
set({ backendStatus: 'ready', apiUrl })
} catch (err) {
const msg = err instanceof Error ? err.message : String(err)
set({ backendStatus: 'error', backendError: msg })
get().showError(msg)
}
},
setCurrentJob: (job) => set({ currentJob: job, meshStats: job === null ? null : get().meshStats }),
updateCurrentJob: (patch) => {
const current = get().currentJob
if (!current) return
set({ currentJob: { ...current, ...patch } })
},
setGenerationOptions: (patch) => {
set((state) => ({ generationOptions: { ...state.generationOptions, ...patch } }))
},
}),
{
name: 'modly-store',
partialize: (state) => ({
generationOptions: state.generationOptions,
showRamIndicator: state.showRamIndicator,
showVramIndicator: state.showVramIndicator,
useAtkinsonFont: state.useAtkinsonFont,
uiScale: state.uiScale,
lightSettings: state.lightSettings,
}),
}
)
)