forked from FyshOS/fynedesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathborder.go
233 lines (197 loc) · 5.2 KB
/
border.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
package wm
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"fyne.io/fynedesk"
wmTheme "fyne.io/fynedesk/theme"
)
func makeFiller(width float32) fyne.CanvasObject {
filler := canvas.NewRectangle(color.Transparent) // make a border on the X axis only
filler.SetMinSize(fyne.NewSize(width, 2)) // width forced
return filler
}
// NewBorder creates a new window border for the given window details
func NewBorder(win fynedesk.Window, icon fyne.Resource, canMaximize bool) *Border {
desk := fynedesk.Instance()
if icon == nil {
iconTheme := desk.Settings().IconTheme()
app := desk.IconProvider().FindAppFromWinInfo(win)
if app != nil {
icon = app.Icon(iconTheme, int(wmTheme.TitleHeight*2))
}
}
max := &widget.Button{Icon: wmTheme.MaximizeIcon, Importance: widget.LowImportance, OnTapped: func() {
if win.Maximized() {
win.Unmaximize()
} else {
win.Maximize()
}
}}
if win.Maximized() {
max.Icon = theme.ViewRestoreIcon()
}
if !canMaximize {
max.Disable()
}
min := &widget.Button{Icon: wmTheme.IconifyIcon, Importance: widget.LowImportance, OnTapped: func() {
win.Iconify()
}}
title := canvas.NewText(win.Properties().Title(), theme.ForegroundColor())
buttonPos := fynedesk.Instance().Settings().BorderButtonPosition()
var titleBar *Border
if buttonPos == "Right" {
titleBar = newColoredHBox(win.Focused(), win, makeFiller(1),
title,
layout.NewSpacer(),
min,
max,
newCloseButton(win),
makeFiller(0),
)
} else {
titleBar = newColoredHBox(win.Focused(), win, makeFiller(0),
newCloseButton(win),
max,
min,
title,
layout.NewSpacer(),
)
}
titleBar.title = title
titleBar.max = max
if icon != nil {
appIcon := &widget.Button{Icon: icon, Importance: widget.LowImportance}
appIcon.OnTapped = func() {
titleBar.showMenu(appIcon)
}
if buttonPos == "Right" {
titleBar.prepend(appIcon)
titleBar.prepend(makeFiller(1))
} else {
titleBar.append(appIcon)
titleBar.append(makeFiller(1))
}
titleBar.appIcon = appIcon
}
return titleBar
}
// Border represents a window border. It draws the title bar and provides functions to manipulate it.
type Border struct {
widget.BaseWidget
content *fyne.Container
appIcon *widget.Button
title *canvas.Text
max *widget.Button
win fynedesk.Window
}
// DoubleTapped is called when the user double taps a frame, it toggles the maximised state.
func (c *Border) DoubleTapped(*fyne.PointEvent) {
if c.win.Maximized() {
c.win.Unmaximize()
return
}
c.win.Maximize()
}
func (c *Border) prepend(obj fyne.CanvasObject) {
c.content.Objects = append([]fyne.CanvasObject{obj}, c.content.Objects...)
c.Refresh()
}
func (c *Border) append(obj fyne.CanvasObject) {
c.content.Add(obj)
c.Refresh()
}
func (c *Border) showMenu(from fyne.CanvasObject) {
name := c.title.Text
if len(name) > 25 {
name = name[:25] + "..."
}
title := fyne.NewMenuItem(name, func() {})
title.Disabled = true
max := fyne.NewMenuItem("Maximize", func() {
if c.win.Maximized() {
c.win.Unmaximize()
} else {
c.win.Maximize()
}
})
if c.win.Maximized() {
max.Checked = true
}
menu := fyne.NewMenu("",
title,
fyne.NewMenuItemSeparator(),
fyne.NewMenuItem("Minimize", func() {
c.win.Iconify()
}),
max,
fyne.NewMenuItemSeparator(),
fyne.NewMenuItem("Close", func() {
c.win.Close()
}))
pos := c.win.Position()
fynedesk.Instance().ShowMenuAt(menu, pos.Add(from.Position()))
}
// CreateRenderer creates a new renderer for this border
//
// Implements: fyne.Widget
func (c *Border) CreateRenderer() fyne.WidgetRenderer {
render := &coloredBoxRenderer{b: c, bg: canvas.NewRectangle(theme.BackgroundColor())}
return render
}
// SetIcon tells the border to change the icon that should be used
func (c *Border) SetIcon(icon fyne.Resource) {
if icon == nil {
return
}
c.appIcon.Icon = icon
}
// SetMaximized updates the state of the border maximize indicators and refreshes
func (c *Border) SetMaximized(isMax bool) {
if isMax {
c.max.Icon = theme.ViewRestoreIcon()
} else {
c.max.Icon = wmTheme.MaximizeIcon
}
}
// SetTitle updates the title portion of this border and refreshes.
func (c *Border) SetTitle(title string) {
c.title.Text = title
}
func newColoredHBox(focused bool, win fynedesk.Window, objs ...fyne.CanvasObject) *Border {
ret := &Border{win: win}
ret.content = container.NewHBox(objs...)
ret.ExtendBaseWidget(ret)
return ret
}
type coloredBoxRenderer struct {
b *Border
bg *canvas.Rectangle
}
func (r *coloredBoxRenderer) Destroy() {
}
func (r *coloredBoxRenderer) Layout(size fyne.Size) {
r.bg.Resize(size)
r.b.content.Resize(size)
}
func (r *coloredBoxRenderer) MinSize() fyne.Size {
return r.b.content.MinSize()
}
func (r *coloredBoxRenderer) Objects() []fyne.CanvasObject {
return []fyne.CanvasObject{r.bg, r.b.content}
}
func (r *coloredBoxRenderer) Refresh() {
r.bg.Resize(r.b.Size()) // Not sure why this resize is needed, but it is...
if r.b.win.Focused() {
r.bg.FillColor = theme.BackgroundColor()
} else {
r.bg.FillColor = theme.DisabledButtonColor()
}
r.bg.Refresh()
r.b.title.Color = theme.ForegroundColor()
r.b.content.Refresh()
}