-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
dock_tab.go
270 lines (251 loc) · 7.12 KB
/
dock_tab.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
// 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
import (
"strings"
"github.com/richardwilkes/toolbox/i18n"
"github.com/richardwilkes/unison/enums/align"
"github.com/richardwilkes/unison/enums/paintstyle"
)
// TabCloser defines the methods that must be implemented to cause the tabs to show a close button.
type TabCloser interface {
// MayAttemptClose returns true if a call to AttemptClose() is permitted.
MayAttemptClose() bool
// AttemptClose attempts to close the tab. On success, returns true.
AttemptClose() bool
}
// DefaultDockTabTheme holds the default DockTabTheme values for DockTabs. Modifying this data will not alter existing
// DockTabs, but will alter any DockTabs created in the future.
var DefaultDockTabTheme = DockTabTheme{
BackgroundInk: ThemeAboveSurface,
OnBackgroundInk: ThemeOnAboveSurface,
EdgeInk: ThemeSurfaceEdge,
TabFocusedInk: ThemeFocus,
OnTabFocusedInk: ThemeOnFocus,
TabCurrentInk: ThemeDeepestFocus,
OnTabCurrentInk: ThemeOnDeepestFocus,
TabBorder: NewEmptyBorder(Insets{Top: 2, Left: 4, Bottom: 2, Right: 4}),
Gap: 4,
LabelTheme: defaultDockLabelTheme(),
ButtonTheme: defaultDockButtonTheme(),
}
func defaultDockLabelTheme() LabelTheme {
theme := DefaultLabelTheme
theme.Font = SystemFont
return theme
}
func defaultDockButtonTheme() ButtonTheme {
theme := DefaultButtonTheme
theme.HideBase = true
theme.SelectionInk = ThemeWarning
return theme
}
// DockTabTheme holds theming data for a DockTab.
type DockTabTheme struct {
BackgroundInk Ink
OnBackgroundInk Ink
EdgeInk Ink
TabFocusedInk Ink
OnTabFocusedInk Ink
TabCurrentInk Ink
OnTabCurrentInk Ink
TabBorder Border
LabelTheme LabelTheme
ButtonTheme ButtonTheme
Gap float32
}
type dockTab struct {
title *Label
button *Button
dockable Dockable
Panel
DockTabTheme
pressed bool
}
func newDockTab(dockable Dockable) *dockTab {
t := &dockTab{
DockTabTheme: DefaultDockTabTheme,
dockable: dockable,
title: NewLabel(),
}
t.Self = t
t.DrawCallback = t.draw
t.SetBorder(t.DockTabTheme.TabBorder)
flex := &FlexLayout{
Columns: 1,
HSpacing: t.Gap,
}
t.SetLayout(flex)
t.title.LabelTheme = t.LabelTheme
t.title.SetTitle(t.fullTitle())
t.title.Drawable = t.TitleIcon()
t.title.SetLayoutData(&FlexLayoutData{HGrab: true, VAlign: align.Middle})
t.AddChild(t.title)
if _, ok := t.dockable.(TabCloser); ok {
t.button = NewButton()
t.button.ButtonTheme = t.ButtonTheme
t.button.SetFocusable(false)
fSize := t.LabelTheme.Font.Baseline()
t.button.Drawable = &DrawableSVG{
SVG: CircledXSVG,
Size: Size{Width: fSize, Height: fSize},
}
t.button.SetLayoutData(&FlexLayoutData{HAlign: align.End, VAlign: align.Middle})
t.AddChild(t.button)
t.button.ClickCallback = func() { t.attemptClose() }
flex.Columns++
}
t.MouseDownCallback = t.mouseDown
t.MouseUpCallback = t.mouseUp
t.MouseDragCallback = t.mouseDrag
t.UpdateTooltipCallback = t.updateTooltip
return t
}
func (t *dockTab) TitleIcon() Drawable {
fSize := t.title.Font.Baseline()
return t.dockable.TitleIcon(Size{Width: fSize, Height: fSize})
}
func (t *dockTab) fullTitle() string {
var buffer strings.Builder
if t.dockable.Modified() {
buffer.WriteByte('*')
}
buffer.WriteString(t.dockable.Title())
return buffer.String()
}
func (t *dockTab) updateTitle() {
drawable := t.TitleIcon()
title := t.fullTitle()
if title != t.title.String() || t.title.Drawable != drawable {
t.title.SetTitle(title)
t.title.Drawable = drawable
t.NeedsLayout = true
t.title.NeedsLayout = true
if p := t.Parent(); p != nil {
p.NeedsLayout = true
}
t.MarkForRedraw()
}
}
func (t *dockTab) draw(gc *Canvas, _ Rect) {
var bg, fg Ink
if t.pressed {
bg = t.TabFocusedInk
fg = t.OnTabFocusedInk
} else if dc := Ancestor[*DockContainer](t.dockable); dc != nil && dc.CurrentDockable() == t.dockable {
if dc == Ancestor[*DockContainer](t.Window().Focus()) {
bg = t.TabFocusedInk
fg = t.OnTabFocusedInk
} else {
bg = t.TabCurrentInk
fg = t.OnTabCurrentInk
}
} else {
bg = t.BackgroundInk
fg = t.OnBackgroundInk
}
if t.title.OnBackgroundInk != fg {
t.title.OnBackgroundInk = fg
t.title.SetTitle(t.title.String())
}
if t.button != nil {
t.button.OnBackgroundInk = fg
}
r := t.ContentRect(true)
p := NewPath()
p.MoveTo(0, r.Height)
p.LineTo(0, 6)
p.CubicTo(0, 6, 0, 1, 6, 1)
rightCornerStart := r.Width - 7
p.LineTo(rightCornerStart, 1)
right := r.Width - 1
p.CubicTo(rightCornerStart, 1, right, 1, right, 7)
p.LineTo(right, r.Height)
p.Close()
gc.DrawPath(p, bg.Paint(gc, r, paintstyle.Fill))
gc.DrawPath(p, t.EdgeInk.Paint(gc, r, paintstyle.Stroke))
}
func (t *dockTab) attemptClose() bool {
if dc := Ancestor[*DockContainer](t.dockable); dc != nil {
return dc.AttemptClose(t.dockable)
}
return false
}
func (t *dockTab) updateTooltip(_ Point, suggestedAvoidInRoot Rect) Rect {
if tip := t.dockable.Tooltip(); tip != "" {
t.Tooltip = NewTooltipWithText(t.dockable.Tooltip())
} else {
t.Tooltip = nil
}
return suggestedAvoidInRoot
}
func (t *dockTab) mouseDown(where Point, button, clickCount int, _ Modifiers) bool {
if button == ButtonRight && clickCount == 1 && !t.Window().InDrag() {
if dc := Ancestor[*DockContainer](t.dockable); dc != nil {
if len(dc.Dockables()) > 1 {
f := DefaultMenuFactory()
cm := f.NewMenu(PopupMenuTemporaryBaseID|ContextMenuIDFlag, "", nil)
cm.InsertItem(-1, f.NewItem(-1, i18n.Text("Close Other Tabs"), KeyBinding{}, nil, func(MenuItem) {
dc.AttemptCloseAllExcept(t.dockable)
}))
cm.InsertItem(-1, f.NewItem(-1, i18n.Text("Close All Tabs"), KeyBinding{}, nil, func(MenuItem) {
dc.AttemptCloseAll()
}))
cm.Popup(Rect{
Point: t.PointToRoot(where),
Size: Size{
Width: 1,
Height: 1,
},
}, 0)
cm.Dispose()
return true
}
}
}
t.pressed = true
t.MarkForRedraw()
return true
}
func (t *dockTab) mouseDrag(where Point, _ int, _ Modifiers) bool {
if !t.pressed {
return true
}
if t.IsDragGesture(where) {
if dc := Ancestor[*DockContainer](t.dockable); dc != nil {
icon := t.TitleIcon()
size := icon.LogicalSize()
t.StartDataDrag(&DragData{
Data: map[string]any{dc.Dock.DragKey: t.dockable},
Drawable: icon,
Ink: t.title.OnBackgroundInk,
Offset: Point{X: -size.Width / 2, Y: -size.Height / 2},
})
}
}
return true
}
func (t *dockTab) mouseUp(where Point, _ int, _ Modifiers) bool {
if !t.pressed {
return true
}
if where.In(t.ContentRect(true)) {
if dc := Ancestor[*DockContainer](t.dockable); dc != nil {
switch {
case dc.CurrentDockable() != t.dockable:
dc.SetCurrentDockable(t.dockable)
case dc != Ancestor[*DockContainer](t.Window().Focus()):
dc.AcquireFocus()
}
}
}
t.pressed = false
t.MarkForRedraw()
return true
}