|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "os/exec" |
| 12 | + "os/user" |
| 13 | + "path/filepath" |
| 14 | + "runtime" |
| 15 | + "text/template" |
| 16 | + "time" |
| 17 | + |
| 18 | + _ "embed" |
| 19 | + |
| 20 | + "github.com/progrium/macdriver/cocoa" |
| 21 | + "github.com/progrium/macdriver/core" |
| 22 | + "github.com/progrium/macdriver/objc" |
| 23 | + "github.com/progrium/macdriver/webkit" |
| 24 | + "github.com/progrium/watcher" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + flagEdit bool |
| 29 | + flagAgent bool |
| 30 | + |
| 31 | + dir string |
| 32 | +) |
| 33 | + |
| 34 | +func init() { |
| 35 | + runtime.LockOSThread() |
| 36 | + flag.BoolVar(&flagEdit, "edit", false, "edit your topframe") |
| 37 | + flag.BoolVar(&flagAgent, "agent", false, "generate agent plist") |
| 38 | +} |
| 39 | + |
| 40 | +func edit() { |
| 41 | + editor := os.Getenv("EDITOR") |
| 42 | + if editor == "" { |
| 43 | + log.Println("unable to edit, no value for EDITOR") |
| 44 | + return |
| 45 | + } |
| 46 | + if err := exec.Command(editor, dir).Run(); err != nil { |
| 47 | + log.Println(err) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func main() { |
| 52 | + flag.Parse() |
| 53 | + |
| 54 | + var err error |
| 55 | + usr, err := user.Current() |
| 56 | + if err != nil { |
| 57 | + log.Fatal(err) |
| 58 | + } |
| 59 | + |
| 60 | + dir = filepath.Join(usr.HomeDir, ".topframe") |
| 61 | + os.MkdirAll(dir, 0755) |
| 62 | + |
| 63 | + //go:embed index.html |
| 64 | + var defaultIndex []byte |
| 65 | + if _, err := os.Stat(filepath.Join(dir, "index.html")); os.IsNotExist(err) { |
| 66 | + ioutil.WriteFile(filepath.Join(dir, "index.html"), defaultIndex, 0644) |
| 67 | + } |
| 68 | + |
| 69 | + if flagAgent { |
| 70 | + //go:embed agent.plist |
| 71 | + var plist string |
| 72 | + |
| 73 | + tmpl, err := template.New("plist").Parse(plist) |
| 74 | + if err != nil { |
| 75 | + log.Fatal(err) |
| 76 | + } |
| 77 | + bin, _ := filepath.Abs(os.Args[0]) |
| 78 | + err = tmpl.Execute(os.Stdout, struct { |
| 79 | + Dir, Bin string |
| 80 | + }{ |
| 81 | + Dir: dir, |
| 82 | + Bin: bin, |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + log.Fatal(err) |
| 86 | + } |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + if flagEdit { |
| 91 | + go edit() |
| 92 | + } |
| 93 | + |
| 94 | + srv := http.Server{ |
| 95 | + Handler: http.FileServer(http.Dir(dir)), |
| 96 | + } |
| 97 | + |
| 98 | + ln, err := net.Listen("tcp", ":0") |
| 99 | + if err != nil { |
| 100 | + log.Fatal(err) |
| 101 | + } |
| 102 | + |
| 103 | + go srv.Serve(ln) |
| 104 | + |
| 105 | + fw := watcher.New() |
| 106 | + if err := fw.AddRecursive(dir); err != nil { |
| 107 | + log.Fatal(err) |
| 108 | + } |
| 109 | + |
| 110 | + go fw.Start(400 * time.Millisecond) |
| 111 | + |
| 112 | + cocoa.TerminateAfterWindowsClose = false |
| 113 | + app := cocoa.NSApp_WithDidLaunch(func(notification objc.Object) { |
| 114 | + config := webkit.WKWebViewConfiguration_New() |
| 115 | + config.Preferences().SetValueForKey(core.True, core.String("developerExtrasEnabled")) |
| 116 | + |
| 117 | + wv := webkit.WKWebView_Init(cocoa.NSScreen_Main().Frame(), config) |
| 118 | + wv.SetOpaque(false) |
| 119 | + wv.SetBackgroundColor(cocoa.NSColor_Clear()) |
| 120 | + wv.SetValueForKey(core.False, core.String("drawsBackground")) |
| 121 | + |
| 122 | + url := core.URL(fmt.Sprintf("http://localhost:%d", ln.Addr().(*net.TCPAddr).Port)) |
| 123 | + req := core.NSURLRequest_Init(url) |
| 124 | + wv.LoadRequest(req) |
| 125 | + |
| 126 | + w := cocoa.NSWindow_Init(cocoa.NSScreen_Main().Frame(), cocoa.NSClosableWindowMask, |
| 127 | + cocoa.NSBackingStoreBuffered, false) |
| 128 | + w.SetContentView(wv) |
| 129 | + w.SetBackgroundColor(cocoa.NSColor_Clear()) |
| 130 | + w.SetOpaque(false) |
| 131 | + w.SetTitleVisibility(cocoa.NSWindowTitleHidden) |
| 132 | + w.SetTitlebarAppearsTransparent(true) |
| 133 | + w.SetIgnoresMouseEvents(true) |
| 134 | + w.SetLevel(cocoa.NSMainMenuWindowLevel + 2) |
| 135 | + w.MakeKeyAndOrderFront(w) |
| 136 | + w.SetCollectionBehavior(cocoa.NSWindowCollectionBehaviorCanJoinAllSpaces) |
| 137 | + |
| 138 | + statusBar := cocoa.NSStatusBar_System().StatusItemWithLength(cocoa.NSVariableStatusItemLength) |
| 139 | + statusBar.Retain() |
| 140 | + statusBar.Button().SetTitle("🔲") |
| 141 | + |
| 142 | + itemInteract := cocoa.NSMenuItem_New() |
| 143 | + itemInteract.Retain() |
| 144 | + itemInteract.SetTitle("Interactive") |
| 145 | + itemInteract.SetAction(objc.Sel("interact:")) |
| 146 | + cocoa.DefaultDelegateClass.AddMethod("interact:", func(_ objc.Object) { |
| 147 | + if w.IgnoresMouseEvents() { |
| 148 | + fmt.Println("Mouse events on") |
| 149 | + w.SetLevel(cocoa.NSMainMenuWindowLevel - 1) |
| 150 | + w.SetIgnoresMouseEvents(false) |
| 151 | + itemInteract.SetState(1) |
| 152 | + } else { |
| 153 | + fmt.Println("Mouse events off") |
| 154 | + w.SetIgnoresMouseEvents(true) |
| 155 | + w.SetLevel(cocoa.NSMainMenuWindowLevel + 2) |
| 156 | + itemInteract.SetState(0) |
| 157 | + } |
| 158 | + }) |
| 159 | + |
| 160 | + itemEnabled := cocoa.NSMenuItem_New() |
| 161 | + itemEnabled.Retain() |
| 162 | + itemEnabled.SetTitle("Enabled") |
| 163 | + itemEnabled.SetState(1) |
| 164 | + itemEnabled.SetAction(objc.Sel("enabled:")) |
| 165 | + cocoa.DefaultDelegateClass.AddMethod("enabled:", func(_ objc.Object) { |
| 166 | + if w.IsVisible() { |
| 167 | + w.Send("orderOut:", w) |
| 168 | + itemInteract.SetEnabled(false) |
| 169 | + itemEnabled.SetState(0) |
| 170 | + } else { |
| 171 | + w.Send("orderFront:", w) |
| 172 | + itemInteract.SetEnabled(true) |
| 173 | + itemEnabled.SetState(1) |
| 174 | + } |
| 175 | + }) |
| 176 | + |
| 177 | + itemQuit := cocoa.NSMenuItem_New() |
| 178 | + itemQuit.SetTitle("Quit") |
| 179 | + itemQuit.SetAction(objc.Sel("terminate:")) |
| 180 | + |
| 181 | + menu := cocoa.NSMenu_New() |
| 182 | + menu.SetAutoenablesItems(false) |
| 183 | + menu.AddItem(itemEnabled) |
| 184 | + menu.AddItem(itemInteract) |
| 185 | + |
| 186 | + if os.Getenv("EDITOR") != "" { |
| 187 | + itemEdit := cocoa.NSMenuItem_New() |
| 188 | + itemEdit.SetTitle("Edit Source...") |
| 189 | + itemEdit.SetAction(objc.Sel("edit:")) |
| 190 | + cocoa.DefaultDelegateClass.AddMethod("edit:", func(_ objc.Object) { |
| 191 | + edit() |
| 192 | + }) |
| 193 | + menu.AddItem(itemEdit) |
| 194 | + } |
| 195 | + |
| 196 | + menu.AddItem(cocoa.NSMenuItem_Separator()) |
| 197 | + menu.AddItem(itemQuit) |
| 198 | + statusBar.SetMenu(menu) |
| 199 | + |
| 200 | + go func() { |
| 201 | + for { |
| 202 | + select { |
| 203 | + case event := <-fw.Event: |
| 204 | + if event.IsDir() { |
| 205 | + continue |
| 206 | + } |
| 207 | + wv.Reload(nil) |
| 208 | + case <-fw.Closed: |
| 209 | + return |
| 210 | + } |
| 211 | + } |
| 212 | + }() |
| 213 | + }) |
| 214 | + app.ActivateIgnoringOtherApps(true) |
| 215 | + |
| 216 | + log.Printf("topframe 0.2.0 by progrium\n") |
| 217 | + app.Run() |
| 218 | +} |
0 commit comments