settings

package
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: AGPL-3.0 Imports: 9 Imported by: 9

Documentation

Index

Constants

View Source
const (
	BaseDir   = ".omniview"
	StoreFile = "settings"
)

TODO - move this to a globally usable package.

Variables

View Source
var (
	ErrSettingNotFound         = errors.New("setting not found")
	ErrSettingTypeMismatch     = errors.New("setting type mismatch")
	ErrSettingCategoryNotFound = errors.New("setting category not found")
	ErrInvalidSettingID        = errors.New("invalid setting ID")
)
View Source
var AllSettingTypes = []struct {
	Value  SettingType
	TSName string
}{
	{Text, "TEXT"},
	{Select, "SELECT"},
	{MultiSelect, "MULTISELECT"},
	{Integer, "INTEGER"},
	{Float, "FLOAT"},
	{Toggle, "TOGGLE"},
	{Color, "COLOR"},
	{DateTime, "DATETIME"},
	{Password, "PASSWORD"},
}

AllSettingTypes is a list of all setting types. Necessary for Wails to bind the enums.

Functions

func GetStore

func GetStore(pluginID string) (*os.File, error)

GetStore returns the settings store, initializing it if necessary.

func GetStorePath

func GetStorePath(pluginID string) (string, error)

GetStorePath returns the path to the settings store.

func RemoveStore

func RemoveStore(pluginID string) error

RemoveStore removes the settings store from the system. This should only ever be used when a user decides to reset to factory settings.

Types

type Category

type Category struct {
	Settings    map[string]Setting `json:"settings"`
	ID          string             `json:"id"`
	Label       string             `json:"label"`
	Description string             `json:"description"`
	Icon        string             `json:"icon"`
}

Category is a group of settings. This is used to group settings together in the UI.

type Provider

type Provider interface {
	// Initialize initializes the settings provider with a set of base settings. Add the wails
	// context so we can eventually dispatch events when settings change.
	Initialize(ctx context.Context, categories ...Category) error

	// LoadSettings loads the settings from local storage
	LoadSettings() error

	// SaveSettings saves the settings to local storage
	SaveSettings() error

	// ListSettings returns the settings store
	ListSettings() Store

	// Values returns all of the values in the store as a map
	Values() map[string]any

	// GetSetting returns the setting by ID. This ID should be in the form of a dot separated string
	// that represents the path to the setting. For example, "appearance.theme"
	GetSetting(id string) (Setting, error)

	// GetSettingValue returns the value of the setting by ID
	GetSettingValue(id string) (any, error)

	// SetSetting sets the value of the setting by ID
	SetSetting(id string, value any) error

	// SetSettings sets multiple settings at once
	SetSettings(settings map[string]any) error

	// ResetSetting resets the value of the setting by ID to the default value
	ResetSetting(id string) error

	// RegisterSetting registers a setting with the provider
	RegisterSetting(categoryID string, setting Setting) error

	// RegisterSettings registers a list of settings with the provider to a category
	RegisterSettings(categoryID string, settings ...Setting) error

	// GetCategories returns a list of all categories, with the settings removed. This
	// is intended for use in the UI to display the categories menu.
	GetCategories() []Category

	// GetCategorySettings returns the settings by category
	GetCategory(id string) (Category, error)

	// GetCategoryValues returns a map of the values of the settings by category
	GetCategoryValues(id string) (map[string]interface{}, error)

	// GetString returns the value of the setting by ID as a string.
	// This is a convenience method for getting a string setting.
	GetString(id string) (string, error)

	// GetStringSlice returns the value of the setting by ID as a string slice.
	// This is a convenience method for getting a string slice setting.
	GetStringSlice(id string) ([]string, error)

	// GetInt returns the value of the setting by ID as an int.
	// This is a convenience method for getting an int setting.
	GetInt(id string) (int, error)

	// GetIntSlice returns the value of the setting by ID as an int slice.
	// This is a convenience method for getting an int slice setting.
	GetIntSlice(id string) ([]int, error)

	// GetFloat returns the value of the setting by ID as a float64.
	// This is a convenience method for getting a float setting.
	GetFloat(id string) (float64, error)

	// GetFloatSlice returns the value of the setting by ID as a float64 slice.
	// This is a convenience method for getting a float slice setting.
	GetFloatSlice(id string) ([]float64, error)

	// GetBool returns the value of the setting by ID as a bool.
	// This is a convenience method for getting a bool setting.
	GetBool(id string) (bool, error)
}

Provider manages the settings for the application. This is used to load and save settings to and from local storage.

func NewProvider

func NewProvider(opts ProviderOpts) Provider

type ProviderOpts

type ProviderOpts struct {
	// Logger is the logger for the provider
	Logger *zap.SugaredLogger
	// PluginID is the ID of the plugin
	PluginID string
	// PluginSettings
	PluginSettings []Category
}

ProviderOpts are the options for creating a new settings provider.

type Setting

type Setting struct {
	// ID is the unique identifier of the setting
	ID string `json:"id"`
	// Label is the human readable label of the setting
	Label string `json:"label"`
	// Description is the human readable description of the setting
	Description string `json:"description"`
	// Type is the type of the setting
	Type SettingType `json:"type"`
	// Value is the value of the setting
	Value interface{} `json:"value"`
	// Default is the default value of the setting
	Default interface{} `json:"default"`
	// Validator is an optional function to validate the setting, which should return an error
	// if the value is invalid
	Validator func(interface{}) error `json:"-"`
	// Options is an optional list of options for a select setting
	Options []SettingOption `json:"options"`
	// FileSelection is an optional setting for file selection
	FileSelection *SettingFileSelection `json:"fileSelection"`
	// Sensitive is a flag to indicate if the setting is sensitive and should not be
	// shown in the UI, nor allowed to be used by any other plugin.
	Sensitive bool `json:"sensitive"`
}

func (*Setting) GetValue

func (s *Setting) GetValue() interface{}

GetValue returns the value of the setting.

func (*Setting) ResetValue

func (s *Setting) ResetValue()

ResetValue resets the value of the setting to its default value.

func (*Setting) SetValue

func (s *Setting) SetValue(value interface{}) error

SetValue sets the value of the setting and validates it using the validator function defined.

func (*Setting) Validate

func (s *Setting) Validate(value interface{}) error

Validate checks if the value of the setting is valid using an optional validation function. If no validation function is provided, it returns nil.

type SettingFileSelection

type SettingFileSelection struct {
	// Whether file selection should be allowed.
	Enabled bool `json:"enabled"`
	// Allow the selection of folders.
	AllowFolders bool `json:"allowFolders"`
	// The allowed extensions that should be selectable.
	Extensions []string `json:"extensions"`
	// Multiple files can be selected.
	Multiple bool `json:"multiple"`
	// Whether the file selection should be saved as a relative path.
	Relative bool `json:"relative"`
	// DefaultPath is the default path for the file selection.
	DefaultPath string `json:"defaultPath"`
}

type SettingOption

type SettingOption struct {
	// Label is the human readable label of the option
	Label string `json:"label"`
	// Description is an optional human readable description of the option
	Description string `json:"description"`
	// Value is the value of the option
	Value interface{} `json:"value"`
}

type SettingType

type SettingType string
const (
	// Text is the type for a text field entry.
	Text SettingType = "text"
	// Select is the type for a single-select field entry.
	Select SettingType = "select"
	// MultiSelect is the type for a multi-select field entry.
	MultiSelect SettingType = "multiselect"
	// Integer is the type for an integer field entry.
	Integer SettingType = "integer"
	// Float is the type for a float field entry.
	Float SettingType = "float"
	// Toggle is the type for a toggle field entry.
	Toggle SettingType = "toggle"
	// Color is the type for a color field entry.
	Color SettingType = "color"
	// DateTime is the type for a date time field entry.
	DateTime SettingType = "datetime"
	// SettingTypePassword is the type for a password field entry.
	Password SettingType = "password"
)

type Store

type Store map[string]Category

The settings store is a map of maps. The first map is the category of the settings, and the second map is the settings themselves. The key of the first map is the category name, and the key of the second map is the setting ID.

Directories

Path Synopsis
Package settingstest provides test doubles for the settings.Provider interface.
Package settingstest provides test doubles for the settings.Provider interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL