-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (70 loc) · 1.92 KB
/
main.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
package main
import (
"log"
"os"
"os/exec"
"runtime"
"github.com/progrium/macdriver/cocoa"
"github.com/progrium/macdriver/objc"
"github.com/sevlyar/go-daemon"
)
func main() {
home := os.Getenv("HOME")
cntxt := &daemon.Context{
PidFileName: home+"/docky.pid",
PidFilePerm: 0644,
LogFileName: home+"/docky.log",
LogFilePerm: 0640,
WorkDir: home,
Umask: 027,
Args: []string{"[go-daemon docky]"},
}
d, err := cntxt.Reborn()
if err != nil {
log.Fatal("Unable to run: ", err)
}
if d != nil {
return
}
defer cntxt.Release()
log.Print("- - - - - - - - - - - - - - -")
log.Print("daemon started")
runtime.LockOSThread()
cocoa.TerminateAfterWindowsClose = false
app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) {
obj := cocoa.NSStatusBar_System().StatusItemWithLength(cocoa.NSVariableStatusItemLength)
obj.Retain()
obj.Button().SetTitle("docky")
moveClicked := make(chan bool)
go func() {
for {
select {
case <-moveClicked:
log.Printf("clicked Move")
cmd1 := exec.Command("defaults", "write", "com.apple.dock", "orientation", "bottom")
log.Printf("Running command \"defaults write com.apple.dock orientation bottom\"...")
err1 := cmd1.Run()
log.Printf("Command finished with error: %v", err1)
cmd2 := exec.Command("killall", "Dock")
log.Printf("Running command \"killall Dock\"...")
err2 := cmd2.Run()
log.Printf("Command finished with error: %v", err2)
}
}
}()
itemMove := cocoa.NSMenuItem_New()
itemMove.SetTitle("Move dock to laptop screen")
itemMove.SetAction(objc.Sel("moveClicked:"))
cocoa.DefaultDelegateClass.AddMethod("moveClicked:", func(_ objc.Object) {
moveClicked <- true
})
itemQuit := cocoa.NSMenuItem_New()
itemQuit.SetTitle("Quit")
itemQuit.SetAction(objc.Sel("terminate:"))
menu := cocoa.NSMenu_New()
menu.AddItem(itemMove)
menu.AddItem(itemQuit)
obj.SetMenu(menu)
})
app.Run()
}