Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (78 loc) · 2.97 KB
/
Copy pathmain.go
File metadata and controls
89 lines (78 loc) · 2.97 KB
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
package main
import (
"errors"
"fmt"
"os"
"runtime"
qt "github.com/mappu/miqt/qt6"
"github.com/mappu/miqt/qt6/mainthread"
"github.com/nomad-software/triggerapi/internal/instance"
"github.com/nomad-software/triggerapi/internal/settings"
"github.com/nomad-software/triggerapi/internal/ui"
"github.com/nomad-software/triggerapi/internal/workspace"
)
var files ui.Files
func main() {
// One copy runs at a time: one started while another runs, as from the
// desktop, has that one's window shown and leaves, passing on the
// desktop's leave to take the focus. Nothing else is done before, so
// that it leaves at once.
var claim *instance.Instance
if dir, err := instance.Dir(); err == nil {
claim, err = instance.Claim(dir, os.Getenv("XDG_ACTIVATION_TOKEN"))
switch {
case errors.Is(err, instance.ErrRunning):
return
case errors.Is(err, instance.ErrNoAnswer):
fmt.Fprintln(os.Stderr, "triggerapi:", err)
os.Exit(1)
case err != nil:
// The program runs, as it would without the check.
fmt.Fprintln(os.Stderr, "triggerapi: another copy could be started beside this one:", err)
default:
defer claim.Close()
}
} else if !errors.Is(err, errors.ErrUnsupported) {
fmt.Fprintln(os.Stderr, "triggerapi: another copy could be started beside this one:", err)
}
// The settings are kept where the system keeps the configuration of
// programs, or in the current directory if it has no such place.
files.Settings = "settings.json"
if path, err := settings.Path(); err != nil {
fmt.Fprintln(os.Stderr, "triggerapi: the settings are kept in the current directory:", err)
} else if err := settings.Prepare(path); err != nil {
fmt.Fprintln(os.Stderr, "triggerapi: the settings are kept in the current directory:", err)
} else {
files.Settings = path
}
// The workspaces are kept where the system keeps the data of programs,
// unless the settings name another folder.
files.Workspaces = "workspaces"
if dir, err := workspace.DefaultDir(); err != nil {
fmt.Fprintln(os.Stderr, "triggerapi: the workspaces are kept in the current directory:", err)
} else {
files.Workspaces = dir
}
qt.NewQApplication(os.Args)
// Under Wayland this is the name a window goes by, which a desktop
// matches to the desktop file of that name for its icon and its place
// in the task bar. It is the name of packaging/triggerapi.desktop.
qt.QGuiApplication_SetDesktopFileName("triggerapi")
// On Linux the desktop is given an entry for the program, which starts
// the copy that is running now, wherever it is and whatever it is
// called, so that it can be started from the desktop's own menus and
// kept in a dock or sidebar.
if runtime.GOOS == "linux" {
if err := ui.InstallDesktopEntry(); err != nil {
fmt.Fprintln(os.Stderr, "triggerapi: the desktop entry could not be written:", err)
}
}
window := ui.NewWindow(files)
window.Show()
if claim != nil {
claim.Serve(func(token string) {
mainthread.Start(func() { window.Activate(token) })
})
}
qt.QApplication_Exec()
}