forked from bjarneo/aether
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
954 lines (828 loc) · 30.8 KB
/
app.go
File metadata and controls
954 lines (828 loc) · 30.8 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"aether/internal/batch"
"aether/internal/blueprint"
"aether/internal/color"
"aether/internal/extraction"
"aether/internal/favorites"
"aether/internal/omarchy"
"aether/internal/platform"
"aether/internal/template"
"aether/internal/theme"
"aether/internal/wallhaven"
"aether/internal/wallpaper"
wailsrt "github.com/wailsapp/wails/v2/pkg/runtime"
)
// App is the main Wails application struct.
// All exported methods are automatically bound as frontend API.
type App struct {
ctx context.Context
state *theme.ThemeState
history *theme.HistoryManager
writer *theme.Writer
blueprints *blueprint.Service
favorites *favorites.Service
wallhaven *wallhaven.Client
batch *batch.Processor
themeWatcher *theme.ThemeWatcher
media *MediaServer
widgetMode bool
focusTab string
}
// IsWidgetMode returns true when running in --widget-blueprint mode.
func (a *App) IsWidgetMode() bool { return a.widgetMode }
// IsMacOS returns true when running on macOS.
func (a *App) IsMacOS() bool { return runtime.GOOS == "darwin" }
// GetFocusTab returns the tab to focus on startup (empty = default editor).
func (a *App) GetFocusTab() string { return a.focusTab }
// NewApp creates a new App instance.
func NewApp() *App {
return &App{
state: theme.NewThemeState(),
history: theme.NewHistoryManager(),
writer: theme.NewWriter(EmbeddedTemplates, "templates"),
blueprints: blueprint.NewService(),
favorites: favorites.NewService(),
wallhaven: wallhaven.NewClient(),
batch: batch.NewProcessor(),
themeWatcher: theme.NewThemeWatcher(),
}
}
// startup is called by Wails when the application starts.
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
_ = platform.EnsureAllDirs()
a.themeWatcher.Start(ctx)
a.media = &MediaServer{}
if err := a.media.Start(); err != nil {
log.Printf("media server: %v", err)
}
}
// GetMediaURL returns an http://localhost URL for streaming a local media file.
// Used by the frontend for <video> elements since webkit2gtk's GStreamer backend
// cannot fetch from the custom wails:// scheme.
func (a *App) GetMediaURL(path string) string {
return a.media.URL(path)
}
// ---------------------------------------------------------------------------
// Color Extraction
// ---------------------------------------------------------------------------
// ExtractColors extracts a 16-color ANSI palette from an image or video.
// For video files, a frame is extracted first via ffmpeg.
func (a *App) ExtractColors(path string, lightMode bool, mode string) ([16]string, error) {
if theme.IsVideoFile(path) {
framePath, err := wallpaper.ExtractVideoFrame(path)
if err != nil {
return [16]string{}, fmt.Errorf("video frame extraction failed: %w", err)
}
path = framePath
}
return extraction.ExtractColors(path, lightMode, mode)
}
// AdjustPaletteColors applies the adjustment pipeline to all palette colors.
// Uses []string (slice) instead of [16]string for Wails JSON compatibility.
func (a *App) AdjustPaletteColors(palette []string, adj color.Adjustments) []string {
result := make([]string, len(palette))
for i, hex := range palette {
if hex != "" {
result[i] = color.AdjustColor(hex, adj)
}
}
return result
}
// ---------------------------------------------------------------------------
// Theme State
// ---------------------------------------------------------------------------
// SetExtractionMode sets the palette extraction mode.
func (a *App) SetExtractionMode(mode string) {
a.state.ExtractionMode = mode
}
// ComputeVariables builds the full template variable map from the given
// palette and extended colors. Returns all base + derived color values.
func (a *App) ComputeVariables(paletteSlice []string, extendedColors map[string]string, lightMode bool) map[string]string {
var palette [16]string
for i := 0; i < 16 && i < len(paletteSlice); i++ {
palette[i] = paletteSlice[i]
}
accent, cursor, selFg, selBg := palette[4], palette[7], palette[0], palette[7]
if v := extendedColors["accent"]; v != "" {
accent = v
}
if v := extendedColors["cursor"]; v != "" {
cursor = v
}
if v := extendedColors["selection_foreground"]; v != "" {
selFg = v
}
if v := extendedColors["selection_background"]; v != "" {
selBg = v
}
roles := template.ColorRoles{
Background: palette[0], Foreground: palette[7],
Black: palette[0], Red: palette[1], Green: palette[2], Yellow: palette[3],
Blue: palette[4], Magenta: palette[5], Cyan: palette[6], White: palette[7],
BrightBlack: palette[8], BrightRed: palette[9], BrightGreen: palette[10],
BrightYellow: palette[11], BrightBlue: palette[12], BrightMagenta: palette[13],
BrightCyan: palette[14], BrightWhite: palette[15],
Accent: accent, Cursor: cursor,
SelectionForeground: selFg, SelectionBackground: selBg,
}
return template.BuildVariables(roles, lightMode)
}
// ---------------------------------------------------------------------------
// Theme Application
// ---------------------------------------------------------------------------
// ApplyThemeRequest is the payload from the frontend containing all current state.
type ApplyThemeRequest struct {
Palette []string `json:"palette"`
WallpaperPath string `json:"wallpaperPath"`
LightMode bool `json:"lightMode"`
AdditionalImages []string `json:"additionalImages"`
ExtendedColors map[string]string `json:"extendedColors"`
Settings theme.Settings `json:"settings"`
AppOverrides map[string]map[string]string `json:"appOverrides"`
}
// ApplyTheme processes all templates and applies the theme to the system.
func (a *App) ApplyTheme(req ApplyThemeRequest) (*theme.ApplyResult, error) {
var palette [16]string
for i := 0; i < 16 && i < len(req.Palette); i++ {
palette[i] = req.Palette[i]
}
// Use extended colors if provided, otherwise derive from palette
accent := palette[4]
cursor := palette[7]
selFg := palette[0]
selBg := palette[7]
if v, ok := req.ExtendedColors["accent"]; ok && v != "" {
accent = v
}
if v, ok := req.ExtendedColors["cursor"]; ok && v != "" {
cursor = v
}
if v, ok := req.ExtendedColors["selection_foreground"]; ok && v != "" {
selFg = v
}
if v, ok := req.ExtendedColors["selection_background"]; ok && v != "" {
selBg = v
}
roles := template.ColorRoles{
Background: palette[0], Foreground: palette[7],
Black: palette[0], Red: palette[1], Green: palette[2], Yellow: palette[3],
Blue: palette[4], Magenta: palette[5], Cyan: palette[6], White: palette[7],
BrightBlack: palette[8], BrightRed: palette[9], BrightGreen: palette[10],
BrightYellow: palette[11], BrightBlue: palette[12], BrightMagenta: palette[13],
BrightCyan: palette[14], BrightWhite: palette[15],
Accent: accent, Cursor: cursor,
SelectionForeground: selFg, SelectionBackground: selBg,
}
appOverrides := req.AppOverrides
if appOverrides == nil {
appOverrides = make(map[string]map[string]string)
}
state := &theme.ThemeState{
Palette: palette,
WallpaperPath: req.WallpaperPath,
LightMode: req.LightMode,
ColorRoles: roles,
AdditionalImages: req.AdditionalImages,
AppOverrides: appOverrides,
}
return a.writer.ApplyTheme(state, req.Settings)
}
// ClearTheme removes the Aether theme and reverts to the default.
func (a *App) ClearTheme() error {
return theme.ClearTheme()
}
// ---------------------------------------------------------------------------
// Blueprints
// ---------------------------------------------------------------------------
// ListBlueprints returns all saved blueprints.
func (a *App) ListBlueprints() ([]map[string]interface{}, error) {
bps, err := a.blueprints.LoadAll()
if err != nil {
return nil, err
}
// Convert to raw maps to avoid Wails model conversion issues
result := make([]map[string]interface{}, len(bps))
for i, bp := range bps {
result[i] = map[string]interface{}{
"name": bp.Name,
"timestamp": bp.Timestamp,
"palette": map[string]interface{}{
"colors": bp.Palette.Colors,
"wallpaper": bp.Palette.Wallpaper,
"lightMode": bp.Palette.LightMode,
},
"adjustments": bp.Adjustments,
"appOverrides": bp.AppOverrides,
}
}
return result, nil
}
// SaveBlueprint saves the current state as a named blueprint.
// SaveBlueprintRequest contains all state needed to save a blueprint.
type SaveBlueprintRequest struct {
Name string `json:"name"`
Palette []string `json:"palette"`
WallpaperPath string `json:"wallpaperPath"`
LightMode bool `json:"lightMode"`
AdditionalImages []string `json:"additionalImages"`
LockedColors []int `json:"lockedColors"`
ExtendedColors map[string]string `json:"extendedColors"`
AppOverrides map[string]map[string]string `json:"appOverrides"`
Adjustments map[string]float64 `json:"adjustments"`
}
func (a *App) SaveBlueprint(req SaveBlueprintRequest) error {
bp := blueprint.Blueprint{
Palette: blueprint.PaletteData{
Colors: req.Palette,
Wallpaper: req.WallpaperPath,
LightMode: req.LightMode,
AdditionalImages: req.AdditionalImages,
LockedColors: req.LockedColors,
ExtendedColors: req.ExtendedColors,
},
Adjustments: req.Adjustments,
AppOverrides: req.AppOverrides,
}
return a.blueprints.Save(req.Name, bp)
}
// resolveWallpaper returns a local wallpaper path from a blueprint. If the local
// path exists it is returned directly. Otherwise, if a URL is available, the
// wallpaper is downloaded first.
func (a *App) resolveWallpaper(palette blueprint.PaletteData) string {
// Use local path if it exists.
if palette.Wallpaper != "" && platform.FileExists(palette.Wallpaper) {
return palette.Wallpaper
}
// Try downloading from URL.
if palette.WallpaperURL != "" {
localPath, err := a.wallhaven.DownloadFromURL(palette.WallpaperURL)
if err != nil {
log.Printf("Warning: could not download wallpaper from %s: %v", palette.WallpaperURL, err)
return ""
}
log.Printf("Downloaded wallpaper: %s", localPath)
return localPath
}
return ""
}
// LoadBlueprint loads a blueprint by name into the current state.
func (a *App) LoadBlueprint(name string) error {
bp, err := a.blueprints.FindByName(name)
if err != nil {
return err
}
if bp == nil {
return fmt.Errorf("blueprint %q not found", name)
}
a.history.Push(*a.state)
var palette [16]string
for i := 0; i < 16 && i < len(bp.Palette.Colors); i++ {
palette[i] = bp.Palette.Colors[i]
}
a.state.SetPalette(palette)
a.state.WallpaperPath = a.resolveWallpaper(bp.Palette)
a.state.LightMode = bp.Palette.LightMode
a.state.Adjustments = a.adjustmentsFromBlueprint(bp)
a.state.AppOverrides = a.appOverridesFromBlueprint(bp)
return nil
}
// ApplyBlueprint loads a blueprint by name and applies it as the active theme.
func (a *App) ApplyBlueprint(name string) (*theme.ApplyResult, error) {
bp, err := a.blueprints.FindByName(name)
if err != nil {
return nil, err
}
if bp == nil {
return nil, fmt.Errorf("blueprint %q not found", name)
}
var palette [16]string
for i := 0; i < 16 && i < len(bp.Palette.Colors); i++ {
palette[i] = bp.Palette.Colors[i]
}
a.state.SetPalette(palette)
a.state.WallpaperPath = a.resolveWallpaper(bp.Palette)
a.state.LightMode = bp.Palette.LightMode
a.state.Adjustments = a.adjustmentsFromBlueprint(bp)
a.state.AppOverrides = a.appOverridesFromBlueprint(bp)
return a.writer.ApplyTheme(a.state, theme.Settings{})
}
// BlueprintExists checks whether a blueprint with the given name already exists.
func (a *App) BlueprintExists(name string) bool {
bp, _ := a.blueprints.FindByName(name)
return bp != nil
}
// DeleteBlueprint removes a blueprint by name.
func (a *App) DeleteBlueprint(name string) error {
return a.blueprints.Delete(name)
}
// adjustmentsFromBlueprint converts a blueprint's stored adjustments map to a
// typed Adjustments struct, falling back to defaults when the blueprint has none.
func (a *App) adjustmentsFromBlueprint(bp *blueprint.Blueprint) color.Adjustments {
adj := color.DefaultAdjustments()
if len(bp.Adjustments) == 0 {
return adj
}
data, err := json.Marshal(bp.Adjustments)
if err != nil {
return adj
}
_ = json.Unmarshal(data, &adj)
return adj
}
// appOverridesFromBlueprint returns a deep copy of the blueprint's app overrides
// or an empty map. A copy is needed to avoid aliasing the blueprint's data.
func (a *App) appOverridesFromBlueprint(bp *blueprint.Blueprint) map[string]map[string]string {
result := make(map[string]map[string]string, len(bp.AppOverrides))
for app, roles := range bp.AppOverrides {
m := make(map[string]string, len(roles))
for k, v := range roles {
m[k] = v
}
result[app] = m
}
return result
}
// ---------------------------------------------------------------------------
// Favorites
// ---------------------------------------------------------------------------
// GetFavorites returns all favorited wallpapers.
func (a *App) GetFavorites() []favorites.Favorite {
return a.favorites.GetAll()
}
// ToggleFavorite adds or removes a favorite. Returns true if now favorited.
func (a *App) ToggleFavorite(path, favType string, data map[string]interface{}) bool {
return a.favorites.Toggle(path, favType, data)
}
// IsFavorite checks if a path is favorited.
func (a *App) IsFavorite(path string) bool {
return a.favorites.IsFavorite(path)
}
// ---------------------------------------------------------------------------
// App Settings (template toggles, neovim config)
// ---------------------------------------------------------------------------
// GetSettings reads saved app settings from ~/.config/aether/settings.json.
func (a *App) GetSettings() map[string]interface{} {
return readConfigJSON("settings.json")
}
// SaveSettings writes app settings to ~/.config/aether/settings.json.
func (a *App) SaveSettings(config map[string]interface{}) error {
return writeConfigJSON("settings.json", config)
}
// ---------------------------------------------------------------------------
// Wallpaper Tags
// ---------------------------------------------------------------------------
// GetWallpaperTags reads wallpaper tags from ~/.config/aether/tags.json.
func (a *App) GetWallpaperTags() map[string]interface{} {
return readConfigJSON("tags.json")
}
// SaveWallpaperTags writes wallpaper tags to ~/.config/aether/tags.json.
func (a *App) SaveWallpaperTags(tags map[string]interface{}) error {
return writeConfigJSON("tags.json", tags)
}
// ---------------------------------------------------------------------------
// Color Utilities
// ---------------------------------------------------------------------------
// GenerateGradient generates a 16-step gradient between two colors.
func (a *App) GenerateGradient(start, end string) [16]string {
return color.GenerateGradient(start, end)
}
// GeneratePaletteFromColor generates a full palette from a single color.
func (a *App) GeneratePaletteFromColor(baseColor string) [16]string {
return color.GeneratePaletteFromColor(baseColor)
}
// ContrastRatio calculates the WCAG contrast ratio between two colors.
func (a *App) ContrastRatio(hex1, hex2 string) float64 {
return color.ContrastRatio(hex1, hex2)
}
// ---------------------------------------------------------------------------
// Wallhaven
// ---------------------------------------------------------------------------
// SetWallhavenAPIKey sets the wallhaven API key for authenticated requests.
func (a *App) SetWallhavenAPIKey(key string) {
a.wallhaven.SetAPIKey(key)
}
// GetWallhavenConfig reads the saved wallhaven settings from ~/.config/aether/wallhaven.json.
func (a *App) GetWallhavenConfig() map[string]interface{} {
return readConfigJSON("wallhaven.json")
}
// SaveWallhavenConfig writes wallhaven settings to ~/.config/aether/wallhaven.json.
func (a *App) SaveWallhavenConfig(config map[string]interface{}) error {
return writeConfigJSON("wallhaven.json", config)
}
// SearchWallhaven searches wallhaven.cc for wallpapers.
func (a *App) SearchWallhaven(params wallhaven.SearchParams) (*wallhaven.SearchResult, error) {
return a.wallhaven.Search(params)
}
// DownloadWallpaper downloads a wallpaper from a URL. Returns local path.
func (a *App) DownloadWallpaper(imageURL string) (string, error) {
return a.wallhaven.Download(imageURL)
}
// ---------------------------------------------------------------------------
// Local Wallpapers
// ---------------------------------------------------------------------------
// ScanLocalWallpapers scans default directories for local wallpaper images.
func (a *App) ScanLocalWallpapers() ([]wallpaper.WallpaperInfo, error) {
return wallpaper.ScanDefaultDirs()
}
// GetThumbnail returns a thumbnail path for an image.
func (a *App) GetThumbnail(path string) (string, error) {
return wallpaper.GetThumbnail(path)
}
// ---------------------------------------------------------------------------
// Omarchy Themes
// ---------------------------------------------------------------------------
// LoadOmarchyThemes discovers all installed Omarchy themes.
func (a *App) LoadOmarchyThemes() ([]omarchy.Theme, error) {
return omarchy.LoadAllThemes()
}
// IsOmarchyInstalled returns true if the current system has Omarchy.
func (a *App) IsOmarchyInstalled() bool {
return theme.IsOmarchyInstalled()
}
// IsAetherWpAvailable returns true if the aether-wp binary is available for animated wallpapers.
func (a *App) IsAetherWpAvailable() bool {
return theme.IsAetherWpAvailable()
}
// ---------------------------------------------------------------------------
// Batch Processing
// ---------------------------------------------------------------------------
// StartBatchProcessing begins batch color extraction.
func (a *App) StartBatchProcessing(paths []string, lightMode bool) {
a.batch.Start(a.ctx, paths, lightMode)
}
// CancelBatchProcessing stops the current batch.
func (a *App) CancelBatchProcessing() {
a.batch.Cancel()
}
// ---------------------------------------------------------------------------
// File / Image Utilities
// ---------------------------------------------------------------------------
// ReadImageAsDataURL reads a local image or video file and returns it as a base64 data URL.
// This is needed because webkit2gtk cannot load file:// paths directly.
func (a *App) ReadImageAsDataURL(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("read file: %w", err)
}
// Detect MIME type from extension
ext := strings.ToLower(filepath.Ext(path))
mime := "image/jpeg"
switch ext {
case ".png":
mime = "image/png"
case ".gif":
mime = "image/gif"
case ".webp":
mime = "image/webp"
case ".bmp":
mime = "image/bmp"
case ".svg":
mime = "image/svg+xml"
case ".mp4":
mime = "video/mp4"
case ".webm":
mime = "video/webm"
}
encoded := base64.StdEncoding.EncodeToString(data)
return fmt.Sprintf("data:%s;base64,%s", mime, encoded), nil
}
// SaveDataURLToFile saves a base64 data URL as a file.
// Returns the saved file path.
func (a *App) SaveDataURLToFile(dataURL string, originalPath string) (string, error) {
// Parse data URL: "data:image/jpeg;base64,/9j/4AAQ..."
parts := strings.SplitN(dataURL, ",", 2)
if len(parts) != 2 {
return "", fmt.Errorf("invalid data URL")
}
decoded, err := base64.StdEncoding.DecodeString(parts[1])
if err != nil {
return "", fmt.Errorf("base64 decode failed: %w", err)
}
// Determine extension from the data URL mime type
ext := ".jpg"
if strings.Contains(parts[0], "image/png") {
ext = ".png"
}
// Save to cache dir with a unique name based on original
cacheDir := filepath.Join(platform.CacheDir(), "filtered")
_ = platform.EnsureDir(cacheDir)
baseName := strings.TrimSuffix(filepath.Base(originalPath), filepath.Ext(originalPath))
outPath := filepath.Join(cacheDir, baseName+"_filtered"+ext)
if err := os.WriteFile(outPath, decoded, 0644); err != nil {
return "", fmt.Errorf("write file failed: %w", err)
}
log.Printf("[canvas-export] Saved filtered image: %s (%d bytes)", outPath, len(decoded))
return outPath, nil
}
// OpenFileDialog opens a native file dialog for selecting an image.
// Returns the selected file path or empty string if cancelled.
func (a *App) OpenFileDialog() (string, error) {
path, err := wailsrt.OpenFileDialog(a.ctx, wailsrt.OpenDialogOptions{
Title: "Select Wallpaper",
Filters: []wailsrt.FileFilter{
{
DisplayName: "Images & Videos",
Pattern: "*.jpg;*.jpeg;*.png;*.gif;*.webp;*.bmp;*.mp4;*.webm",
},
},
})
if err != nil {
return "", err
}
return path, nil
}
// HandleDroppedFiles processes file paths from drag-and-drop.
// Takes the first image file from the dropped list.
// Returns the file path if valid, error otherwise.
func (a *App) HandleDroppedFiles(paths []string) (string, error) {
validExts := map[string]bool{
".jpg": true, ".jpeg": true, ".png": true,
".gif": true, ".webp": true, ".bmp": true,
".mp4": true, ".webm": true,
}
for _, path := range paths {
ext := strings.ToLower(filepath.Ext(path))
if validExts[ext] {
return path, nil
}
}
return "", fmt.Errorf("no valid image files in drop")
}
// ---------------------------------------------------------------------------
// Export / Import via GUI dialogs
// ---------------------------------------------------------------------------
// ExportThemeRequest is the payload from the frontend for exporting a theme.
type ExportThemeRequest struct {
Name string `json:"name"`
IncludedApps []string `json:"includedApps"`
Palette []string `json:"palette"`
WallpaperPath string `json:"wallpaperPath"`
LightMode bool `json:"lightMode"`
AdditionalImages []string `json:"additionalImages"`
ExtendedColors map[string]string `json:"extendedColors"`
InstallToOmarchy bool `json:"installToOmarchy"`
AppOverrides map[string]map[string]string `json:"appOverrides"`
}
// allExportableApps is the full set of app names that can be exported.
var allExportableApps = map[string]bool{
"alacritty": true, "btop": true, "chromium": true, "colors": true,
"ghostty": true, "gtk": true, "hyprland": true, "hyprlock": true,
"icons": true, "kitty": true, "mako": true, "neovim": true,
"swayosd": true, "vencord": true, "vscode": true, "walker": true,
"warp": true, "waybar": true, "wofi": true, "zed": true,
}
// ExportTheme exports the current theme to a user-chosen directory.
func (a *App) ExportTheme(req ExportThemeRequest) (string, error) {
dir, err := wailsrt.OpenDirectoryDialog(a.ctx, wailsrt.OpenDialogOptions{
Title: "Choose Export Directory",
})
if err != nil || dir == "" {
return "", fmt.Errorf("export cancelled")
}
// Build theme state from the frontend's current palette
var palette [16]string
for i := 0; i < 16 && i < len(req.Palette); i++ {
palette[i] = req.Palette[i]
}
accent := palette[4]
cursor := palette[7]
selFg := palette[0]
selBg := palette[7]
if v, ok := req.ExtendedColors["accent"]; ok && v != "" {
accent = v
}
if v, ok := req.ExtendedColors["cursor"]; ok && v != "" {
cursor = v
}
if v, ok := req.ExtendedColors["selection_foreground"]; ok && v != "" {
selFg = v
}
if v, ok := req.ExtendedColors["selection_background"]; ok && v != "" {
selBg = v
}
roles := template.ColorRoles{
Background: palette[0], Foreground: palette[7],
Black: palette[0], Red: palette[1], Green: palette[2], Yellow: palette[3],
Blue: palette[4], Magenta: palette[5], Cyan: palette[6], White: palette[7],
BrightBlack: palette[8], BrightRed: palette[9], BrightGreen: palette[10],
BrightYellow: palette[11], BrightBlue: palette[12], BrightMagenta: palette[13],
BrightCyan: palette[14], BrightWhite: palette[15],
Accent: accent, Cursor: cursor,
SelectionForeground: selFg, SelectionBackground: selBg,
}
exportOverrides := req.AppOverrides
if exportOverrides == nil {
exportOverrides = make(map[string]map[string]string)
}
state := &theme.ThemeState{
Palette: palette,
WallpaperPath: req.WallpaperPath,
LightMode: req.LightMode,
ColorRoles: roles,
AdditionalImages: req.AdditionalImages,
AppOverrides: exportOverrides,
}
// Build included set from the request
included := make(map[string]bool, len(req.IncludedApps))
for _, app := range req.IncludedApps {
included[app] = true
}
// Derive excluded apps: everything in allExportableApps that is NOT included
excluded := make(map[string]bool)
for app := range allExportableApps {
if !included[app] {
excluded[app] = true
}
}
settings := theme.Settings{
IncludeGtk: included["gtk"],
IncludeZed: included["zed"],
IncludeVscode: included["vscode"],
IncludeNeovim: included["neovim"],
ExcludedApps: excluded,
}
exportDir := filepath.Join(dir, "omarchy-"+req.Name+"-theme")
if err := a.writer.GenerateOnly(state, settings, exportDir); err != nil {
return "", fmt.Errorf("export failed: %w", err)
}
if req.InstallToOmarchy && theme.IsOmarchyInstalled() {
linkPath := filepath.Join(platform.OmarchyThemesDir(), req.Name)
if err := platform.CreateSymlink(exportDir, linkPath); err != nil {
log.Printf("Warning: could not symlink to omarchy themes: %v", err)
} else {
log.Printf("Installed as omarchy theme: %s -> %s", linkPath, exportDir)
}
}
return exportDir, nil
}
// ImportResult is returned by ImportFileDialog with the imported colors.
type ImportResult struct {
Colors []string `json:"colors"`
Name string `json:"name"`
Path string `json:"path"`
WallpaperPath string `json:"wallpaperPath"`
LightMode bool `json:"lightMode"`
}
// ImportFileDialog opens a file dialog for importing a theme file.
// fileType: "base16", "toml", or "blueprint"
// Returns the imported colors so the frontend can update its store.
func (a *App) ImportFileDialog(fileType string) (*ImportResult, error) {
var filters []wailsrt.FileFilter
var title string
switch fileType {
case "base16":
title = "Import Base16 Scheme"
filters = []wailsrt.FileFilter{
{DisplayName: "YAML Files", Pattern: "*.yaml"},
{DisplayName: "YML Files", Pattern: "*.yml"},
{DisplayName: "All Files", Pattern: "*"},
}
case "toml":
title = "Import Colors TOML"
filters = []wailsrt.FileFilter{
{DisplayName: "TOML Files", Pattern: "*.toml"},
{DisplayName: "All Files", Pattern: "*"},
}
case "blueprint":
title = "Import Blueprint"
filters = []wailsrt.FileFilter{
{DisplayName: "JSON Files", Pattern: "*.json"},
{DisplayName: "All Files", Pattern: "*"},
}
default:
return nil, fmt.Errorf("unknown file type: %s", fileType)
}
path, err := wailsrt.OpenFileDialog(a.ctx, wailsrt.OpenDialogOptions{
Title: title,
Filters: filters,
})
if err != nil {
log.Printf("[import] dialog error: %v", err)
return nil, fmt.Errorf("dialog error: %w", err)
}
if path == "" {
return nil, fmt.Errorf("cancelled")
}
log.Printf("[import] selected file: %s (type=%s)", path, fileType)
return a.importFile(path, fileType)
}
func (a *App) importFile(path, fileType string) (*ImportResult, error) {
var bp *blueprint.Blueprint
var err error
switch fileType {
case "base16":
bp, err = blueprint.ImportBase16(path)
case "toml":
bp, err = blueprint.ImportColorsToml(path)
case "blueprint":
bp, err = blueprint.ImportJSON(path)
default:
return nil, fmt.Errorf("unknown type: %s", fileType)
}
if err != nil {
log.Printf("[import] parse error: %v", err)
return nil, fmt.Errorf("parse error: %w", err)
}
savedPath, err := blueprint.SaveImported(bp)
if err != nil {
log.Printf("[import] save error: %v", err)
return nil, fmt.Errorf("save error: %w", err)
}
var palette [16]string
for i := 0; i < 16 && i < len(bp.Palette.Colors); i++ {
palette[i] = bp.Palette.Colors[i]
}
a.history.Push(*a.state)
a.state.SetPalette(palette)
a.state.WallpaperPath = a.resolveWallpaper(bp.Palette)
a.state.LightMode = bp.Palette.LightMode
log.Printf("[import] success: %s (%d colors)", bp.Name, len(bp.Palette.Colors))
return &ImportResult{
Colors: palette[:],
Name: bp.Name,
Path: savedPath,
WallpaperPath: a.state.WallpaperPath,
LightMode: a.state.LightMode,
}, nil
}
// GetTemplateColors returns the color variable names used in each app template.
// The result maps app name -> list of color variable names (only overridable color roles).
func (a *App) GetTemplateColors() map[string][]string {
// The set of color variable names that are overridable.
// Includes base roles, aliases (bg/fg), and derived shades.
colorVars := map[string]bool{
"background": true, "foreground": true,
"bg": true, "fg": true,
"black": true, "red": true, "green": true, "yellow": true,
"blue": true, "magenta": true, "cyan": true, "white": true,
"bright_black": true, "bright_red": true, "bright_green": true, "bright_yellow": true,
"bright_blue": true, "bright_magenta": true, "bright_cyan": true, "bright_white": true,
"accent": true, "cursor": true, "selection_foreground": true, "selection_background": true,
"dark_bg": true, "darker_bg": true, "lighter_bg": true,
"dark_fg": true, "light_fg": true, "bright_fg": true,
"muted": true, "orange": true, "brown": true, "purple": true, "bright_purple": true,
"selection": true,
}
// Collect unique color vars per app (multiple files may map to the same app)
appColorSets := make(map[string]map[string]bool)
files, err := template.ListTemplates(EmbeddedTemplates, "templates")
if err != nil {
return make(map[string][]string)
}
for _, f := range files {
content, err := template.ReadTemplate(EmbeddedTemplates, "templates", f)
if err != nil {
continue
}
appName := theme.GetAppNameFromFileName(f)
vars := template.ExtractVariableNames(content)
if appColorSets[appName] == nil {
appColorSets[appName] = make(map[string]bool)
}
for _, v := range vars {
if colorVars[v] {
appColorSets[appName][v] = true
}
}
}
// Aether's own templates are internal and should not be user-overridable
delete(appColorSets, "aether")
result := make(map[string][]string, len(appColorSets))
for app, colorSet := range appColorSets {
colors := make([]string, 0, len(colorSet))
for v := range colorSet {
colors = append(colors, v)
}
if len(colors) > 0 {
result[app] = colors
}
}
return result
}
// ResetState resets the theme state to defaults.
func (a *App) ResetState() {
a.history.Push(*a.state)
*a.state = *theme.NewThemeState()
}
// --- Config JSON helpers ---
func readConfigJSON(filename string) map[string]interface{} {
v, err := platform.ReadJSON[map[string]interface{}](filepath.Join(platform.ConfigDir(), filename))
if err != nil {
return nil
}
return v
}
func writeConfigJSON(filename string, v interface{}) error {
return platform.WriteJSON(filepath.Join(platform.ConfigDir(), filename), v)
}