-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
dock_container.go
277 lines (256 loc) · 7.89 KB
/
dock_container.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (c) 2021-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
var (
_ Layout = &DockContainer{}
_ DockLayoutNode = &DockContainer{}
)
// Dockable represents a dockable Panel.
type Dockable interface {
Paneler
// TitleIcon returns an Drawable representing this Dockable.
TitleIcon(suggestedSize Size) Drawable
// Title returns the title of this Dockable.
Title() string
// Tooltip returns the tooltip of this Dockable.
Tooltip() string
// Modified returns true if the dockable has been modified.
Modified() bool
}
// DockContainer holds one or more Dockable panels.
type DockContainer struct {
Dock *Dock
header *dockHeader
content *dockContainerContent
Panel
}
// NewDockContainer creates a new DockContainer.
func NewDockContainer(dock *Dock, dockable Dockable) *DockContainer {
d := &DockContainer{
Dock: dock,
content: newDockContainerContent(),
}
d.Self = d
d.SetLayout(d)
d.content.AddChild(resolveDockable(dockable))
d.content.SetCurrentIndex(0)
d.header = newDockHeader(d)
d.AddChild(d.header)
d.AddChild(d.content)
return d
}
// Dockables returns the list of Dockables within this DockContainer, in tab order.
func (d *DockContainer) Dockables() []Dockable {
children := d.content.Children()
dockables := make([]Dockable, 0, len(children))
for _, c := range children {
if dockable, ok := c.Self.(Dockable); ok {
dockables = append(dockables, dockable)
}
}
return dockables
}
// CurrentDockableIndex returns the index of the frontmost Dockable within this DockContainer, or -1 if there are no
// Dockables.
func (d *DockContainer) CurrentDockableIndex() int {
return d.content.CurrentIndex()
}
// CurrentDockable returns the frontmost Dockable within this DockContainer. May return nil.
func (d *DockContainer) CurrentDockable() Dockable {
return resolveDockable(d.content.Current())
}
// SetCurrentDockable makes the provided dockable the current one.
func (d *DockContainer) SetCurrentDockable(dockable Dockable) {
dockable = resolveDockable(dockable)
if d.CurrentDockable() != dockable {
for i, c := range d.content.Children() {
if c.Self == dockable {
d.content.SetCurrentIndex(i)
break
}
}
}
d.AcquireFocus()
}
// resolveDockable makes sure we're pointing to the Self version of the Dockable and not some intermediate layer.
func resolveDockable(dockable Dockable) Dockable {
if dockable == nil {
return nil
}
if resolved, ok := dockable.AsPanel().Self.(Dockable); ok {
return resolved
}
return nil
}
// AcquireFocus will set the focus within the current Dockable of this DockContainer. If the focus is already within it,
// nothing is changed.
func (d *DockContainer) AcquireFocus() {
if wnd := d.Window(); wnd != nil {
current := d.CurrentDockable()
focus := wnd.Focus()
for focus != nil && focus.Self != current {
focus = focus.Parent()
}
if focus == nil {
wnd.SetFocus(current)
}
}
}
// UpdateTitle will cause the dock tab for the given Dockable to update itself.
func (d *DockContainer) UpdateTitle(dockable Dockable) {
dockable = resolveDockable(dockable)
for i, c := range d.content.Children() {
if c.Self == dockable {
d.header.updateTitle(i)
break
}
}
}
// DockableHasFocus returns true if the given Dockable has the current focus inside it.
func DockableHasFocus(dockable Dockable) bool {
if wnd := dockable.AsPanel().Window(); wnd != nil {
dockable = resolveDockable(dockable)
focus := wnd.Focus()
for focus != nil {
if d, ok := focus.Self.(Dockable); ok && d == dockable {
return true
}
focus = focus.Parent()
}
}
return false
}
// Stack adds the Dockable to this DockContainer at the specified index. An out-of-bounds index will cause the Dockable
// to be added at the end.
func (d *DockContainer) Stack(dockable Dockable, index int) {
dockable = resolveDockable(dockable)
if dc := Ancestor[*DockContainer](dockable); dc != nil {
if dc == d && len(d.content.Children()) == 1 {
d.AcquireFocus()
return
}
dc.Close(dockable)
}
d.content.AddChildAtIndex(dockable, index)
d.header.addTab(dockable, index)
d.SetCurrentDockable(dockable)
d.AcquireFocus()
}
// AttemptCloseAll attempts to close all Dockables within this DockContainer. Returns true if all Dockables are closed.
func (d *DockContainer) AttemptCloseAll() bool {
return d.AttemptCloseAllExcept(nil)
}
// AttemptCloseAllExcept attempts to close all Dockables within this DockContainer except for the specified Dockable.
// Returns true if all Dockables except for the specified Dockable are closed.
func (d *DockContainer) AttemptCloseAllExcept(dockable Dockable) bool {
for _, one := range d.Dockables() {
if one != dockable && !d.AttemptClose(one) {
return false
}
}
return true
}
// AttemptClose attempts to close a Dockable within this DockContainer. This only has an affect if the Dockable is
// contained by this DockContainer and implements the TabCloser interface. Note that the TabCloser must call this
// DockContainer's Close(Dockable) method to actually close the tab. Returns true if dockable is closed.
func (d *DockContainer) AttemptClose(dockable Dockable) bool {
if closer, ok := dockable.(TabCloser); ok {
dockable = resolveDockable(dockable)
for _, c := range d.content.Children() {
if c.Self == dockable {
if closer.MayAttemptClose() {
return closer.AttemptClose()
}
break
}
}
}
return false
}
// Close the specified Dockable. If the last Dockable within this DockContainer is closed, then this DockContainer is
// also removed from the Dock.
func (d *DockContainer) Close(dockable Dockable) {
dockable = resolveDockable(dockable)
children := d.Dockables()
for i, c := range children {
if c != dockable {
continue
}
var next Dockable
if DockableHasFocus(dockable) {
switch {
case i+1 < len(children):
next = children[i+1]
d.content.SetCurrentIndex(i + 1)
case i > 0:
next = children[i-1]
d.content.SetCurrentIndex(i - 1)
default:
next = d.Dock.NextDockableFor(dockable)
}
} else {
next = d.CurrentDockable()
}
d.content.RemoveChild(dockable)
d.header.close(dockable)
d.MarkForRedraw()
if len(children) == 1 {
d.Dock.Restore()
if dl := d.Dock.layout.findLayout(d); dl != nil {
dl.Remove(d)
}
d.Dock.RemoveChild(d)
d.Dock.MarkForLayoutAndRedraw()
d.Dock = nil
}
if next != nil {
if dc := Ancestor[*DockContainer](next); dc != nil {
if dc == d {
dc.SetCurrentDockable(next)
}
dc.AcquireFocus()
}
}
return
}
}
// PreferredSize implements DockLayoutNode.
func (d *DockContainer) PreferredSize() Size {
_, pref, _ := d.LayoutSizes(d.AsPanel(), Size{})
return pref
}
// LayoutSizes implements Layout.
func (d *DockContainer) LayoutSizes(target *Panel, hint Size) (minSize, prefSize, maxSize Size) {
minSize, prefSize, maxSize = d.header.Sizes(Size{Width: hint.Width})
minSize.Height = prefSize.Height
maxSize.Height = prefSize.Height
min2, pref2, max2 := d.content.Sizes(Size{Width: hint.Width, Height: max(hint.Height-prefSize.Height, 0)})
minSize.Width = min2.Width
prefSize.Width = pref2.Width
maxSize.Width = max2.Width
minSize.Height += min2.Height
prefSize.Height += pref2.Height
maxSize.Height += max2.Height
if b := target.Border(); b != nil {
prefSize = prefSize.Add(b.Insets().Size())
}
return minSize, prefSize, maxSize
}
// PerformLayout implements Layout.
func (d *DockContainer) PerformLayout(_ *Panel) {
r := d.ContentRect(false)
_, pref, _ := d.header.Sizes(Size{Width: r.Width})
hr := r
hr.Height = pref.Height
d.header.SetFrameRect(hr)
fr := r
fr.Y += pref.Height
fr.Height = max(r.Height-pref.Height, 0)
d.content.SetFrameRect(fr)
}