-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdesk.go
427 lines (360 loc) · 11.3 KB
/
desk.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package ui
import (
"math"
"os/exec"
"strconv"
"fyshos.com/fynedesk/internal/notify"
"github.com/FyshOS/appie"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
deskDriver "fyne.io/fyne/v2/driver/desktop"
"fyshos.com/fynedesk"
wmtheme "fyshos.com/fynedesk/theme"
"fyshos.com/fynedesk/wm"
)
const (
// RootWindowName is the base string that all root windows will have in their title and is used to identify root windows.
RootWindowName = "Fyne Desktop"
// SkipTaskbarHint should be added to the title of normal windows that should be skipped like the X11 SkipTaskbar hint.
SkipTaskbarHint = "FyneDesk:skip"
)
type desktop struct {
wm.ShortcutHandler
app fyne.App
wm fynedesk.WindowManager
icons appie.Provider
recent []appie.AppData
screens fynedesk.ScreenList
settings fynedesk.DeskSettings
run func()
showMenu func(*fyne.Menu, fyne.Position)
moduleCache []fynedesk.Module
bar *bar
widgets *widgetPanel
mouse fyne.CanvasObject
root fyne.Window
desk int
}
func (l *desktop) Desktop() int {
return l.desk
}
func (l *desktop) SetDesktop(id int) {
diff := id - l.desk
l.desk = id
_, height := l.RootSizePixels()
offPix := float32(diff * -int(height))
wins := l.wm.Windows()
starts := make([]fyne.Position, len(wins))
deltas := make([]fyne.Delta, len(wins))
for i, win := range wins {
starts[i] = win.Position()
display := l.Screens().ScreenForWindow(win)
off := offPix / display.Scale
deltas[i] = fyne.NewDelta(0, off)
}
fyne.NewAnimation(canvas.DurationStandard, func(f float32) {
for i, item := range l.wm.Windows() {
if item.Pinned() {
continue
}
newX := starts[i].X + deltas[i].DX*f
newY := starts[i].Y + deltas[i].DY*f
item.Move(fyne.NewPos(newX, newY))
}
}).Start()
for _, m := range l.Modules() {
if desk, ok := m.(notify.DesktopNotify); ok {
desk.DesktopChangeNotify(id)
}
}
}
func (l *desktop) Layout(objects []fyne.CanvasObject, size fyne.Size) {
bg := objects[0].(*background)
bg.Resize(size)
if l.Settings().NarrowLeftLauncher() {
l.bar.Resize(size)
l.bar.Move(fyne.NewPos(0, 0))
} else {
barHeight := l.bar.MinSize().Height
l.bar.Resize(fyne.NewSize(size.Width, barHeight+1)) // add 1 so rounding cannot trigger mouse out on bottom edge
l.bar.Move(fyne.NewPos(0, size.Height-barHeight))
}
l.bar.Refresh()
widgetsWidth := l.widgets.MinSize().Width
l.widgets.Resize(fyne.NewSize(widgetsWidth, size.Height))
l.widgets.Move(fyne.NewPos(size.Width-widgetsWidth, 0))
l.widgets.Refresh()
}
func (l *desktop) MinSize(_ []fyne.CanvasObject) fyne.Size {
return fyne.NewSize(640, 480) // tiny - window manager will scale up to screen size
}
func (l *desktop) ShowMenuAt(menu *fyne.Menu, pos fyne.Position) {
l.showMenu(menu, pos)
}
func (l *desktop) updateBackgrounds(path string) {
root := l.root.Content().(*fyne.Container).Objects[0]
if back, ok := root.(*background); ok {
back.updateBackground(path)
} else { // embed mode has another container
root.(*fyne.Container).Objects[0].(*background).updateBackground(path)
}
}
func (l *desktop) createPrimaryContent() fyne.CanvasObject {
l.bar = newBar(l)
l.widgets = newWidgetPanel(l)
l.mouse = newMouse()
l.mouse.Hide()
return container.New(l, newBackground(), l.bar, l.widgets, l.mouse)
}
func (l *desktop) createRoot(screens fynedesk.ScreenList) fyne.Window {
win := l.newDesktopWindowFull()
win.SetContent(l.createPrimaryContent())
return win
}
func (l *desktop) setupRoot() {
if l.root == nil {
l.root = l.createRoot(l.screens)
}
scale := l.screens.Primary().CanvasScale()
l.root.Resize(fyne.NewSize(float32(l.screens.Primary().Width)/scale, float32(l.screens.Primary().Height)/scale))
}
func (l *desktop) RecentApps() []appie.AppData {
return l.recent
}
func (l *desktop) Run() {
go l.wm.Run()
go l.watchScreenActivity()
l.run() // use the configured run method
}
func (l *desktop) RunApp(app appie.AppData) error {
vars := l.scaleVars(l.Screens().Active().CanvasScale())
err := app.Run(vars)
if err == nil {
l.recent = append([]appie.AppData{app}, l.recent...)
// remove if it was already on the list
for i := 1; i < len(l.recent); i++ {
if l.recent[i] == app {
if i == len(l.recent)-1 {
l.recent = l.recent[:i]
} else {
l.recent = append(l.recent[:i], l.recent[i+1:]...)
}
break
}
}
// limit to 5 items
if len(l.recent) > 5 {
l.recent = l.recent[:5]
}
l.settings.(*deskSettings).saveRecents()
}
return err
}
func (l *desktop) Settings() fynedesk.DeskSettings {
return l.settings
}
func (l *desktop) ContentBoundsPixels(screen *fynedesk.Screen) (x, y, w, h uint32) {
screenW := uint32(screen.Width)
screenH := uint32(screen.Height)
pad := wmtheme.WidgetPanelWidth
if fynedesk.Instance().Settings().NarrowWidgetPanel() {
pad = wmtheme.NarrowBarWidth
}
if l.screens.Primary() == screen {
bar := uint32(0)
if l.Settings().NarrowLeftLauncher() {
bar = uint32(wmtheme.NarrowBarWidth * screen.CanvasScale())
}
wid := uint32(pad * screen.CanvasScale())
return bar, 0, screenW - bar - wid, screenH
}
return 0, 0, screenW, screenH
}
func (l *desktop) RootSizePixels() (w, h uint32) {
for _, screen := range l.Screens().Screens() {
right := uint32(screen.X + screen.Width)
bottom := uint32(screen.Y + screen.Height)
if right > w {
w = right
}
if bottom > h {
h = bottom
}
}
return w, h
}
func (l *desktop) IconProvider() appie.Provider {
return l.icons
}
func (l *desktop) WindowManager() fynedesk.WindowManager {
return l.wm
}
func (l *desktop) clearModuleCache() {
for _, mod := range l.moduleCache {
mod.Destroy()
}
l.moduleCache = nil
}
func (l *desktop) Modules() []fynedesk.Module {
if l.moduleCache != nil {
return l.moduleCache
}
var mods []fynedesk.Module
for _, meta := range fynedesk.AvailableModules() {
if !isModuleEnabled(meta.Name, l.settings) {
continue
}
instance := meta.NewInstance()
mods = append(mods, instance)
if bind, ok := instance.(fynedesk.KeyBindModule); ok {
for sh, f := range bind.Shortcuts() {
l.AddShortcut(sh, f)
}
}
}
l.moduleCache = mods
return mods
}
func (l *desktop) qtScreenScales() string {
screenScales := ""
for i, screen := range l.Screens().Screens() {
if i > 0 {
screenScales += ";"
}
// Qt toolkit cannot handle scale < 1
positiveScale := math.Max(1.0, float64(screen.CanvasScale()))
screenScales += screen.Name + "=" + strconv.FormatFloat(positiveScale, 'f', 1, 32)
}
return screenScales
}
func (l *desktop) scaleVars(scale float32) []string {
intScale := int(math.Round(float64(scale)))
return []string{
"QT_SCREEN_SCALE_FACTORS=" + l.qtScreenScales(),
"GDK_SCALE=" + strconv.Itoa(intScale),
"ELM_SCALE=" + strconv.FormatFloat(float64(scale), 'f', 1, 32),
}
}
// MouseInNotify can be called by the window manager to alert the desktop that the cursor has entered the canvas
func (l *desktop) MouseInNotify(pos fyne.Position) {
if l.bar == nil {
return
}
mouseX, mouseY := pos.X, pos.Y
barX, barY := l.bar.Position().X, l.bar.Position().Y
barWidth, barHeight := l.bar.Size().Width, l.bar.Size().Height
if mouseX >= barX && mouseX <= barX+barWidth {
if mouseY >= barY && mouseY <= barY+barHeight {
l.bar.MouseIn(&deskDriver.MouseEvent{PointEvent: fyne.PointEvent{AbsolutePosition: pos, Position: pos}})
}
}
}
// MouseOutNotify can be called by the window manager to alert the desktop that the cursor has left the canvas
func (l *desktop) MouseOutNotify() {
if l.bar == nil {
return
}
l.bar.MouseOut()
}
func (l *desktop) fireSettingsChangeListener(s fynedesk.DeskSettings) {
l.clearModuleCache()
l.updateBackgrounds(s.Background())
l.widgets.reloadModules(l.Modules())
l.bar.iconSize = l.Settings().LauncherIconSize()
l.bar.iconScale = l.Settings().LauncherZoomScale()
l.bar.disableZoom = l.Settings().LauncherDisableZoom()
l.bar.updateIcons()
l.bar.updateIconOrder()
l.bar.updateTaskbar()
}
func (l *desktop) addSettingsChangeListener() {
l.Settings().AddChangeListener(l.fireSettingsChangeListener)
l.app.Settings().AddListener(func(_ fyne.Settings) {
l.updateBackgrounds(l.Settings().Background())
})
}
func (l *desktop) registerShortcuts() {
l.AddShortcut(fynedesk.NewShortcut("Show Launcher", fyne.KeySpace, fynedesk.UserModifier),
ShowAppLauncher)
l.AddShortcut(fynedesk.NewShortcut("Switch App Next", fyne.KeyTab, fynedesk.UserModifier),
func() {
// dummy - the wm handles app switcher
})
l.AddShortcut(fynedesk.NewShortcut("Switch App Previous", fyne.KeyTab, fynedesk.UserModifier|fyne.KeyModifierShift),
func() {
// dummy - the wm handles app switcher
})
l.AddShortcut(fynedesk.NewShortcut("Print Window", deskDriver.KeyPrintScreen, fyne.KeyModifierShift),
l.screenshotWindow)
l.AddShortcut(fynedesk.NewShortcut("Print Screen", deskDriver.KeyPrintScreen, 0),
l.screenshot)
l.AddShortcut(fynedesk.NewShortcut("Calculator", fynedesk.KeyCalculator, 0),
l.calculator)
l.AddShortcut(fynedesk.NewShortcut("Lock screen", fyne.KeyL, fynedesk.UserModifier),
func() {
l.TriggerScreenSaver()
})
}
// Screens returns the screens provider of the current desktop environment for access to screen functionality.
func (l *desktop) Screens() fynedesk.ScreenList {
return l.screens
}
// NewDesktop creates a new desktop in fullscreen for main usage.
// The WindowManager passed in will be used to manage the screen it is loaded on.
// An ApplicationProvider is used to lookup application icons from the operating system.
func NewDesktop(app fyne.App, mgr fynedesk.WindowManager, icons appie.Provider, screenProvider fynedesk.ScreenList) fynedesk.Desktop {
desk := newDesktop(app, mgr, icons)
desk.run = desk.runFull
screenProvider.AddChangeListener(desk.setupRoot)
desk.screens = screenProvider
desk.setupRoot()
wm.StartAuthAgent()
if desk.Settings().ScreenSaverType() == "XScreensaver" {
go desk.startXscreensaver()
}
return desk
}
// NewEmbeddedDesktop creates a new windowed desktop for test purposes.
// An ApplicationProvider is used to lookup application icons from the operating system.
// If run during CI for testing it will return an in-memory window using the
// fyne/test package.
func NewEmbeddedDesktop(app fyne.App, icons appie.Provider) fynedesk.Desktop {
wm := &embededWM{}
desk := newDesktop(app, wm, icons)
desk.run = desk.runEmbed
desk.showMenu = desk.showMenuEmbed
desk.root = desk.newDesktopWindowEmbed()
over := wm.setWindow(desk.root)
desk.root.SetContent(container.NewStack(desk.createPrimaryContent(), over))
return desk
}
func newDesktop(app fyne.App, wm fynedesk.WindowManager, icons appie.Provider) *desktop {
desk := &desktop{app: app, wm: wm, icons: icons, screens: newEmbeddedScreensProvider()}
desk.showMenu = desk.showMenuFull
fynedesk.SetInstance(desk)
desk.settings = newDeskSettings()
desk.addSettingsChangeListener()
desk.registerShortcuts()
return desk
}
func (l *desktop) calculator() {
err := exec.Command("calculator").Start()
if err != nil {
fyne.LogError("Failed to open calculator", err)
}
}
//func (l *desktop) runCommand() {
// w := l.app.NewWindow("Run Command")
// input := widget.NewEntry()
// // TODO add history etc...
// run := widget.NewButton("Run", func() {
//
// })
// run.Importance = widget.HighImportance
//
// w.SetContent(container.NewVBox(widget.NewLabel("Enter command to run:"),
// container.NewBorder(nil, nil, nil, run, input)))
// w.Resize(fyne.NewSize(250, 40))
// w.Show()
//}