settings

package
v0.5.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

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

This section is empty.

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 CategoryChangeFunc added in v0.3.0

type CategoryChangeFunc func(values map[string]any)

CategoryChangeFunc is called after settings in a category are saved. The map contains all current setting values for the category.

type Provider

type Provider interface {
	// ListSettings returns the settings store
	ListSettings() Store

	// 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

	// 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)

	// GetString returns the value of the setting by ID as a string.
	GetString(id string) (string, error)

	// GetStringSlice returns the value of the setting by ID as a string slice.
	GetStringSlice(id string) ([]string, error)

	// GetInt returns the value of the setting by ID as an int.
	GetInt(id string) (int, error)

	// GetIntSlice returns the value of the setting by ID as an int slice.
	GetIntSlice(id string) ([]int, error)

	// GetFloat returns the value of the setting by ID as a float64.
	GetFloat(id string) (float64, error)

	// GetFloatSlice returns the value of the setting by ID as a float64 slice.
	GetFloatSlice(id string) ([]float64, error)

	// GetBool returns the value of the setting by ID as a bool.
	GetBool(id string) (bool, 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

	// RegisterChangeHandler registers a callback that fires after settings in the
	// given category are saved. Only one handler per category.
	RegisterChangeHandler(categoryID string, fn CategoryChangeFunc)
}

Provider manages the settings for the application as a pure in-memory store.

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"`
	// DevOnly indicates the setting should only be visible when dev mode is active.
	DevOnly bool `json:"devOnly"`
}

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