Documentation ¶
Overview ¶
Package prefs provides a publish-subscription API for global settinsg management. It exists as a schema-less version of the GSettings API.
Index ¶
- Variables
- func AsyncLoadSaved(ctx context.Context, done func(error))
- func Hide(props ...Prop)
- func LoadData(data []byte) error
- func Order(props ...Prop)
- func OrderBefore(prop, isBefore Prop)
- func ReadSavedData(ctx context.Context) ([]byte, error)
- func RegisterProp(p Prop)
- type Bool
- func (b *Bool) AnyPublish(v interface{}) error
- func (b *Bool) AnyValue() interface{}
- func (b *Bool) CreateWidget(ctx context.Context, save func()) gtk.Widgetter
- func (b *Bool) MarshalJSON() ([]byte, error)
- func (b *Bool) Publish(v bool)
- func (b *Bool) UnmarshalJSON(blob []byte) error
- func (b *Bool) Value() bool
- func (b *Bool) WidgetIsLarge() bool
- type EnumList
- func (l *EnumList[T]) CreateWidget(ctx context.Context, save func()) gtk.Widgetter
- func (l *EnumList[T]) IsValid(str T) bool
- func (l *EnumList[T]) MarshalJSON() ([]byte, error)
- func (l *EnumList[T]) Publish(v T)
- func (l *EnumList[T]) UnmarshalJSON(blob []byte) error
- func (l *EnumList[T]) Value() T
- func (l *EnumList[T]) WidgetIsLarge() bool
- type EnumListMeta
- type ID
- type Int
- type IntMeta
- type ListedSection
- type LocalizedProp
- type Prop
- type PropMeta
- type Pubsub
- type Slug
- type Snapshot
- type String
- type StringMeta
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidAnyType = errors.New("incorrect value type")
ErrInvalidAnyType is returned by a preference property if it has the wrong type.
Functions ¶
func AsyncLoadSaved ¶
AsyncLoadSaved asynchronously loads the saved preferences.
func Hide ¶
func Hide(props ...Prop)
Hide hides the given props from showing up in methods that snapshot properties. This is useful for the main application to hide the settings of certain libraries.
Props that are hidden cannot be unhidden, so only call this method during initialization on a constant list of properties.
func LoadData ¶
LoadData loads the given JSON data (usually returned from ReadSavedData) directly into the global preference values.
func Order ¶
func Order(props ...Prop)
Order registers the given names for ordering properties. It is only valid within the same sections.
func OrderBefore ¶
func OrderBefore(prop, isBefore Prop)
func ReadSavedData ¶
ReadSavedData reads the saved preferences from a predetermined location. Users should give the returned byte slice to LoadData. A nil byte slice is a valid value.
func RegisterProp ¶
func RegisterProp(p Prop)
RegisterProp registers a property globally. This function should ideally be called only during init.
Types ¶
type Bool ¶
Bool is a preference property of type boolean.
func (*Bool) AnyPublish ¶
AnyPublish implements Prop.
func (*Bool) CreateWidget ¶
CreateWidget creates a *gtk.Switch.
func (*Bool) MarshalJSON ¶
func (*Bool) UnmarshalJSON ¶
type EnumList ¶
type EnumList[T comparable] struct { Pubsub EnumListMeta[T] // contains filtered or unexported fields }
EnumList is a preference property of type stringer.
func NewEnumList ¶
func NewEnumList[T comparable](def T, prop EnumListMeta[T]) *EnumList[T]
NewEnumList creates a new EnumList instance.
func (*EnumList[T]) CreateWidget ¶
CreateWidget creates either a *gtk.Entry or a *gtk.TextView.
func (*EnumList[T]) MarshalJSON ¶
func (*EnumList[T]) Publish ¶
func (l *EnumList[T]) Publish(v T)
Publish publishes the new value. If the value isn't within Options, then the method will panic.
func (*EnumList[T]) UnmarshalJSON ¶
func (*EnumList[T]) WidgetIsLarge ¶
WidgetIsLarge returns false.
type EnumListMeta ¶
type EnumListMeta[T comparable] struct { PropMeta Validate func(T) error Options []T }
EnumListMeta is the metadata of an EnumList.
type Int ¶
Int is a preference property of type int.
func (*Int) CreateWidget ¶
CreateWidget creates either a *gtk.Scale or a *gtk.SpinButton.
func (*Int) MarshalJSON ¶
func (*Int) UnmarshalJSON ¶
func (*Int) WidgetIsLarge ¶
WidgetIsLarge is true if Slider is true.
type IntMeta ¶
type IntMeta struct { Name locale.Localized Section locale.Localized Description locale.Localized Min int Max int Slider bool }
IntMeta wraps PropMeta for Int.
type ListedSection ¶
type ListedSection struct { Name string // localized Props []LocalizedProp // contains filtered or unexported fields }
ListedSection holds a list of properties returned from ListProperties.
func ListProperties ¶
func ListProperties(ctx context.Context) []ListedSection
ListProperties enumerates all known global properties into a map of
type LocalizedProp ¶
LocalizedProp wraps Prop and localizes its name and description.
type Prop ¶
type Prop interface { json.Marshaler json.Unmarshaler // Meta returns the property's meta. Meta() PropMeta // Pubsubber returns the internal publisher/subscriber instance. Pubsubber() *Pubsub // CreateWidget creates the widget containing the value of this property. CreateWidget(ctx context.Context, save func()) gtk.Widgetter // WidgetIsLarge returns true if the widget created by CreateWidget should // take up space and shouldn't be inlined. WidgetIsLarge() bool }
Prop describes a property type.
type PropMeta ¶
type PropMeta struct { Name locale.Localized Section locale.Localized Description locale.Localized }
PropMeta describes the metadata of a preference value.
func (PropMeta) EnglishName ¶
EnglishName returns the unlocalized name.
func (PropMeta) EnglishSectionName ¶
EnglishSectionName returns the unlocalized section name.
type Pubsub ¶
type Pubsub struct {
// contains filtered or unexported fields
}
Pubsub provides a simple publish-subscribe API. This instance is safe to use concurrently.
func (*Pubsub) Publish ¶
func (p *Pubsub) Publish()
Publish publishes changes to all subscribe routines.
func (*Pubsub) Subscribe ¶
func (p *Pubsub) Subscribe(f func()) (rm func())
Subscribe adds f into the pubsub's subscription queue. f will always be invoked in the main thread.
func (*Pubsub) SubscribeInit ¶
func (p *Pubsub) SubscribeInit(f func())
SubscribeInit is like Subscribe, except you can't unsubscribe, the callback is not called, and the method is not thread-safe. It is only meant to be called in init() functions.
func (*Pubsub) SubscribeWidget ¶
func (p *Pubsub) SubscribeWidget(widget gtk.Widgetter, f func())
SubscribeWidget subscribes the given widget and callback to changes. If rm is called, then the subscription is removed. The given callback will be called once in the receiving goroutine to signal a change. It is guaranteed for the callback to only be consistently called on that goroutine.
type Snapshot ¶
type Snapshot map[string]json.RawMessage
Snapshot describes a snapshot of the preferences state.
func TakeSnapshot ¶
func TakeSnapshot() Snapshot
TakeSnapshot takes a snapshot of the global preferences into a flat map. This function should only be called on the main thread, but the returned snapshot can be used anywhere.
type String ¶
type String struct { Pubsub StringMeta // contains filtered or unexported fields }
String is a preference property of type string.
func NewString ¶
func NewString(def string, prop StringMeta) *String
NewString creates a new String instance.
func (*String) CreateWidget ¶
CreateWidget creates either a *gtk.Entry or a *gtk.TextView.
func (*String) MarshalJSON ¶
func (*String) Publish ¶
Publish publishes the new string value. An error is returned and nothing is published if the string fails the verifier.
func (*String) UnmarshalJSON ¶
type StringMeta ¶
type StringMeta struct { Name locale.Localized Section locale.Localized Description locale.Localized Placeholder locale.Localized Validate func(string) error Multiline bool }
StringMeta is the metadata of a string.
func (StringMeta) Meta ¶
func (m StringMeta) Meta() PropMeta
Meta returns the PropMeta for StringMeta. It implements Prop.