tiny-rdm/backend/services/preferences_service.go

83 lines
1.7 KiB
Go

package services
import (
"github.com/adrg/sysfont"
"sort"
"strings"
"sync"
storage2 "tinyrdm/backend/storage"
"tinyrdm/backend/types"
"tinyrdm/backend/utils/coll"
)
type preferencesService struct {
pref *storage2.PreferencesStorage
}
var preferences *preferencesService
var oncePreferences sync.Once
func Preferences() *preferencesService {
if preferences == nil {
oncePreferences.Do(func() {
preferences = &preferencesService{
pref: storage2.NewPreferences(),
}
})
}
return preferences
}
func (p *preferencesService) GetPreferences() (resp types.JSResp) {
resp.Data = p.pref.GetPreferences()
resp.Success = true
return
}
func (p *preferencesService) SetPreferences(values map[string]any) (resp types.JSResp) {
err := p.pref.SetPreferencesN(values)
if err != nil {
resp.Msg = err.Error()
return
}
resp.Success = true
return
}
func (p *preferencesService) RestorePreferences() (resp types.JSResp) {
defaultPref := p.pref.RestoreDefault()
resp.Data = map[string]any{
"pref": defaultPref,
}
resp.Success = true
return
}
type FontItem struct {
Name string `json:"name"`
Path string `json:"path"`
}
func (p *preferencesService) GetFontList() (resp types.JSResp) {
finder := sysfont.NewFinder(nil)
fontSet := coll.NewSet[string]()
var fontList []FontItem
for _, font := range finder.List() {
if len(font.Family) > 0 && !strings.HasPrefix(font.Family, ".") && fontSet.Add(font.Family) {
fontList = append(fontList, FontItem{
Name: font.Family,
Path: font.Filename,
})
}
}
sort.Slice(fontList, func(i, j int) bool {
return fontList[i].Name < fontList[j].Name
})
resp.Data = map[string]any{
"fonts": fontList,
}
resp.Success = true
return
}