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.
15// It is plain data rather than a closure, so creating one never allocates.
16type CommonOption struct {
17 kind commonKind
18 s string
19}
20
21type commonKind uint8
22
23const (
24 optKey commonKind = iota
25 optID
26 optClass
27 optInline
28)
29
30func (o CommonOption) apply(e *element) {
31 switch o.kind {
32 case optKey:
33 e.key, e.hasKey = o.s, true
34 case optID:
35 e.id = o.s
36 case optClass:
37 e.classes = append(e.classes, o.s)
38 case optInline:
39 e.inline, _ = css.ParseInline(o.s)
40 }
41}
42func (o CommonOption) container(e *element) { o.apply(e) }
43func (o CommonOption) button(e *element) { o.apply(e) }
44func (o CommonOption) edit(e *element) { o.apply(e) }
45func (o CommonOption) valueOption(e *element) { o.apply(e) }
46func Key(key string) CommonOption { return CommonOption{optKey, key} }
47func ID(id string) CommonOption { return CommonOption{optID, id} }
48func Class(class string) CommonOption { return CommonOption{optClass, class} }
49func Inline(src string) CommonOption { return CommonOption{optInline, src} }
50func applyContainer(e *element, opts []ContainerOption) {
51 for _, o := range opts {
52 o.container(e)
53 }
54}
55
56// DisabledOption is the concrete type of Disabled. It applies to every
57// interactive control (buttons, value controls and editors) but not to
58// containers or headings. Exported only so the root facade can return it.
59type DisabledOption struct{ disabled bool }
60
61func (o DisabledOption) button(e *element) { e.disabled = o.disabled }
62func (o DisabledOption) edit(e *element) { e.disabled = o.disabled }
63func (o DisabledOption) valueOption(e *element) { e.disabled = o.disabled }
64
65// Disabled blocks focus and interaction on an interactive control.
66func Disabled(v bool) DisabledOption { return DisabledOption{v} }
67
68// HeadingOption accepts common CSS options and Level, but not control options.
69type HeadingOption interface{ heading(*element) }
70
71func (o CommonOption) heading(e *element) { o.apply(e) }
72
73type headingLevel int
74
75func (level headingLevel) heading(e *element) { e.level = int(level) }
76
77// Level sets the accessibility heading level (1 through 6).
78func Level(level int) HeadingOption {
79 if level < 1 || level > 6 {
80 panic("nefergui: heading level must be between 1 and 6")
81 }
82 return headingLevel(level)
83}
84
85type editOnly struct{ placeholder string }
86
87func (o editOnly) edit(e *element) { e.placeholder = o.placeholder }
88func Placeholder(v string) EditOption { return editOnly{v} }
89
90type passwordOption bool
91
92func (o passwordOption) edit(e *element) { e.password = bool(o) }
93func Password(v bool) EditOption { return passwordOption(v) }