options.go RawHistoryFolder
1package ui
2
3import "github.com/bnema/nefergui/internal/css"
4
5// ContainerOption, ButtonOption and EditOption are separate option families.
6type ContainerOption interface{ container(*element) }
7type ButtonOption interface{ button(*element) }
8type EditOption interface{ edit(*element) }
9
10// ValueOption accepts CSS options and Disabled for pointer-backed controls.
11type ValueOption interface{ valueOption(*element) }
12
13// CommonOption is the concrete type of Key, ID, Class and Inline; it belongs
14// to every option family. It is exported only so the root facade can return it.
15type CommonOption struct{ apply func(*element) }
16
17func (o CommonOption) container(e *element) { o.apply(e) }
18func (o CommonOption) button(e *element) { o.apply(e) }
19func (o CommonOption) edit(e *element) { o.apply(e) }
20func (o CommonOption) valueOption(e *element) { o.apply(e) }
21func Key(key string) CommonOption {
22 return CommonOption{func(e *element) { e.identity = &identity{key: key} }}
23}
24func ID(id string) CommonOption { return CommonOption{func(e *element) { e.id = id }} }
25func Class(class string) CommonOption {
26 return CommonOption{func(e *element) { e.classes = append(e.classes, class) }}
27}
28func Inline(src string) CommonOption {
29 return CommonOption{func(e *element) { e.inline, _ = css.ParseInline(src) }}
30}
31func applyContainer(e *element, opts []ContainerOption) {
32 for _, o := range opts {
33 o.container(e)
34 }
35}
36
37// DisabledOption is the concrete type of Disabled. It applies to every
38// interactive control (buttons, value controls and editors) but not to
39// containers or headings. Exported only so the root facade can return it.
40type DisabledOption struct{ disabled bool }
41
42func (o DisabledOption) button(e *element) { e.disabled = o.disabled }
43func (o DisabledOption) edit(e *element) { e.disabled = o.disabled }
44func (o DisabledOption) valueOption(e *element) { e.disabled = o.disabled }
45
46// Disabled blocks focus and interaction on an interactive control.
47func Disabled(v bool) DisabledOption { return DisabledOption{v} }
48
49// HeadingOption accepts common CSS options and Level, but not control options.
50type HeadingOption interface{ heading(*element) }
51
52func (o CommonOption) heading(e *element) { o.apply(e) }
53
54type headingLevel int
55
56func (level headingLevel) heading(e *element) { e.level = int(level) }
57
58// Level sets the accessibility heading level (1 through 6).
59func Level(level int) HeadingOption {
60 if level < 1 || level > 6 {
61 panic("nefergui: heading level must be between 1 and 6")
62 }
63 return headingLevel(level)
64}
65
66type editOnly struct{ placeholder string }
67
68func (o editOnly) edit(e *element) { e.placeholder = o.placeholder }
69func Placeholder(v string) EditOption { return editOnly{v} }
70
71type passwordOption bool
72
73func (o passwordOption) edit(e *element) { e.password = bool(o) }
74func Password(v bool) EditOption { return passwordOption(v) }