/
lib_darwin.go
125 lines (106 loc) · 2.05 KB
/
lib_darwin.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
//go:build darwin
// +build darwin
package frame
/*
#ifndef WEBVIEW_COCOA
#define WEBVIEW_COCOA
#endif
#import "c_darwin.h"
*/
import "C"
import (
"fmt"
"strings"
"sync"
"sync/atomic"
// "reflect"
)
// State struct
type State struct {
Hidden bool
Iconified bool
Maximized bool
Fullscreen bool
Focused bool
}
//export goAppActivated
func goAppActivated(ret C.AppMenu) {
appChan <- &App{
openedWns: sync.WaitGroup{},
shown: make(chan bool),
mainMenu: &Menu{
menu: ret.mainMenu,
},
appMenu: &Menu{
menu: ret.appMenu,
},
}
}
//export goPrint
func goPrint(text *C.char) {
fmt.Println(C.GoString(text))
}
//export goPrintInt
func goPrintInt(t C.int) {
fmt.Println(int(t))
}
func goBool(b C.bool) bool {
return C.bool(true) == b
}
func stateSender(win *Window, newState State) {
prevState := win.state
win.state = newState
if win.StateEvent != nil {
win.StateEvent(newState)
}
if prevState.Hidden && !newState.Hidden {
win.app.openedWns.Add(1)
select {
case win.app.shown <- true:
default:
}
} else if !prevState.Hidden && newState.Hidden {
win.app.openedWns.Done()
}
}
//export goWindowEvent
func goWindowEvent(windowID C.int, eventTitle *C.char, x C.int, y C.int, w C.int, h C.int) {
id := int(windowID)
title := C.GoString(eventTitle)
var win *Window
for i := range winds {
if int(winds[i].id) == id {
win = winds[i]
}
}
if win == nil {
return
}
if win.Invoke != nil && strings.HasPrefix(title, "invoke:") {
win.Invoke(strings.TrimPrefix(title, "invoke:"))
return
}
state := win.state
switch title {
case "windowDidBecomeKey":
state.Focused = true
state.Hidden = false
case "windowDidResignKey":
state.Focused = false
}
if !state.Hidden && (state.Focused != win.state.Focused) {
go stateSender(win, state)
}
}
func cRequest(fn func(id uint64)) interface{} {
id := atomic.AddUint64(&goRequestID, 1)
ch := make(chan interface{})
goRequests.Store(id, ch)
defer goRequests.Delete(id)
fn(id)
return <-ch
}
//export goScriptEvent
func goScriptEvent() {
fmt.Println("js...")
}