88 lines
2.5 KiB
Go
88 lines
2.5 KiB
Go
// Copyright (c) 2026 Storm Dragon
|
|
// Authors: Storm Dragon, Angus Kola
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package app
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"git.stormux.org/storm/cthulhu/internal/config"
|
|
)
|
|
|
|
func (worker *preferencesWorker) applicationSettings(application string) (config.Settings, error) {
|
|
if settings, ok := worker.appSettings[application]; ok {
|
|
return settings, nil
|
|
}
|
|
store, err := config.NewStore(worker.globalSettings.Path)
|
|
if err != nil {
|
|
return config.Settings{}, err
|
|
}
|
|
persistedGlobal, err := store.Load()
|
|
if err != nil {
|
|
return config.Settings{}, err
|
|
}
|
|
settings, mapped, err := store.LoadForApplication(application)
|
|
if err != nil {
|
|
return config.Settings{}, err
|
|
}
|
|
if !mapped {
|
|
return worker.globalSettings, nil
|
|
}
|
|
settings = config.RebaseApplicationSettings(persistedGlobal, worker.globalSettings, settings)
|
|
worker.appSettings[application] = settings
|
|
return settings, nil
|
|
}
|
|
|
|
func (worker *preferencesWorker) switchApplication(ctx context.Context, application string) {
|
|
application = strings.TrimSpace(application)
|
|
if application == "" || application == worker.activeApp {
|
|
return
|
|
}
|
|
settings, err := worker.applicationSettings(application)
|
|
if err == nil {
|
|
err = worker.applyRuntime(ctx, settings)
|
|
}
|
|
if err != nil {
|
|
worker.logger.Error("switch application settings", "application", application, "error", err)
|
|
return
|
|
}
|
|
worker.activeApp = application
|
|
if worker.live != nil {
|
|
worker.live.application.Store(&application)
|
|
worker.live.notify_clipboard()
|
|
}
|
|
}
|
|
|
|
func (worker *preferencesWorker) beginEdit(ctx context.Context, application string) bool {
|
|
// A focus event and its shortcut can arrive on separate goroutines. Use the
|
|
// latest accepted focus before deciding which application to edit.
|
|
select {
|
|
case focused := <-worker.applicationChanges:
|
|
worker.switchApplication(ctx, focused)
|
|
default:
|
|
}
|
|
if application == "\x00" {
|
|
application = worker.activeApp
|
|
if application == "" {
|
|
worker.announceSpeechSetting("No active application for application preferences.")
|
|
return false
|
|
}
|
|
}
|
|
worker.editApp = application
|
|
worker.editingActive = true
|
|
worker.editing = worker.globalSettings
|
|
if application != "" {
|
|
settings, err := worker.applicationSettings(application)
|
|
if err != nil {
|
|
worker.logger.Error("load application settings", "application", application, "error", err)
|
|
worker.announceSpeechSetting("Could not load application preferences.")
|
|
worker.editingActive = false
|
|
return false
|
|
}
|
|
worker.editing = settings
|
|
}
|
|
return true
|
|
}
|