1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package tui
type Modal struct {
open *State[bool]
backdrop string
closeOnEscape bool
closeOnBackdrop bool
trapFocus bool
elementOpts []Option
customKeyMap KeyMap
app *App
element *Element
previousFocusIdx int
wasOpen bool
}
var (
_ Component = (*Modal)(nil)
_ KeyListener = (*Modal)(nil)
_ MouseListener = (*Modal)(nil)
_ AppBinder = (*Modal)(nil)
)
func NewModal(opts ...ModalOption) *Modal {
m := &Modal{
backdrop: "dim",
closeOnEscape: true,
closeOnBackdrop: true,
trapFocus: true,
previousFocusIdx: -1,
}
for _, opt := range opts {
opt(m)
}
return m
}
func (m *Modal) BindApp(app *App) {
m.app = app
if m.open != nil {
m.open.BindApp(app)
}
}
func (m *Modal) Render(app *App) *Element {
isOpen := m.open != nil && m.open.Get()
if !isOpen {
if m.wasOpen {
if m.trapFocus && m.app != nil {
m.app.focus.ClearScope()
}
if m.trapFocus && m.previousFocusIdx >= 0 && m.app != nil {
m.app.focus.setFocusIndex(m.previousFocusIdx)
m.previousFocusIdx = -1
}
m.wasOpen = false
}
m.element = New(WithOverlay(true), WithHidden(true))
return m.element
}
m.element = New(WithOverlay(true))
for _, opt := range m.elementOpts {
opt(m.element)
}
needsFocusInit := false
if !m.wasOpen {
if m.trapFocus && m.app != nil {
m.previousFocusIdx = m.app.focus.focusedIndex()
}
m.wasOpen = true
needsFocusInit = true
}
app.registerOverlay(m.element, m.backdrop, m.trapFocus, needsFocusInit)
return m.element
}
func (m *Modal) KeyMap() KeyMap {
if m.open == nil || !m.open.Get() {
return nil
}
if m.app != nil && !m.app.inAlternateScreen && m.app.inlineHeight > 0 {
return nil
}
var km KeyMap
if m.closeOnEscape {
km = append(km, OnPreemptStop(KeyEscape, func(ke KeyEvent) {
m.open.Set(false)
}))
}
if m.trapFocus && m.app != nil {
km = append(
km,
OnPreemptStop(KeyTab, func(ke KeyEvent) {
m.app.FocusNext()
}),
OnPreemptStop(KeyTab.Shift(), func(ke KeyEvent) {
m.app.FocusPrev()
}),
)
}
km = append(km, OnPreemptStop(KeyEnter, func(ke KeyEvent) {
if m.app == nil {
return
}
if focused, ok := m.app.Focused().(*Element); ok && focused != nil {
focused.Activate()
}
}))
km = append(km, m.customKeyMap...)
if m.trapFocus {
km = append(km, OnPreemptStop(AnyKey, func(ke KeyEvent) {}))
}
return km
}
func (m *Modal) HandleMouse(me MouseEvent) bool {
if m.open == nil || !m.open.Get() {
return false
}
if me.Action != MousePress || me.Button != MouseLeft {
return false
}
if m.element == nil {
return false
}
hit := m.element.ElementAt(me.X, me.Y)
if hit == nil {
return false
}
if hit == m.element {
if m.closeOnBackdrop {
m.open.Set(false)
}
return true
}
for el := hit; el != nil && el != m.element; el = el.parent {
if el.onActivate != nil {
el.Activate()
return true
}
}
return true
}
|