~eliasnaur/gio

ref: e3ba84758c1c4709c8670cf20f44ba8524180a31 gio/ui/app/os_macos.go -rw-r--r-- 5.0 KiB
e3ba8475Elias Naur cmd/gophers: use default scale for avatar images 2 years ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

// +build darwin,!ios

package app

/*
#cgo CFLAGS: -DGL_SILENCE_DEPRECATION -Werror -fmodules -fobjc-arc -x objective-c

#include <AppKit/AppKit.h>
#include "os_macos.h"
*/
import "C"
import (
	"errors"
	"image"
	"runtime"
	"sync"
	"time"
	"unsafe"

	"gioui.org/ui"
	"gioui.org/ui/f32"
	"gioui.org/ui/key"
	"gioui.org/ui/pointer"
)

func init() {
	// Darwin requires that UI operations happen on the main thread only.
	runtime.LockOSThread()
}

type window struct {
	view  C.CFTypeRef
	w     *Window
	stage Stage
}

// Only support one main window for now.
var singleWindow struct {
	mu      sync.Mutex
	hasOpts bool
	opts    *WindowOptions
}

var viewFactory func() C.CFTypeRef

var views = make(map[C.CFTypeRef]*window)

func (w *window) contextView() C.CFTypeRef {
	return w.view
}

func (w *window) setTextInput(s key.TextInputState) {}

func (w *window) setAnimating(anim bool) {
	var animb C.BOOL
	if anim {
		animb = 1
	}
	C.gio_setAnimating(w.view, animb)
}

func (w *window) setStage(stage Stage) {
	if stage == w.stage {
		return
	}
	w.stage = stage
	w.w.event(StageEvent{stage})
}

//export gio_onFrameCallback
func gio_onFrameCallback(view C.CFTypeRef) {
	w := views[view]
	w.draw(false)
}

//export gio_onKeys
func gio_onKeys(view C.CFTypeRef, cstr *C.char, ti C.double, mods C.NSUInteger) {
	str := C.GoString(cstr)
	var kmods key.Modifiers
	if mods&C.NSEventModifierFlagCommand != 0 {
		kmods |= key.ModCommand
	}
	if mods&C.NSEventModifierFlagShift != 0 {
		kmods |= key.ModShift
	}
	w := views[view]
	for _, k := range str {
		if n, ok := convertKey(k); ok {
			w.w.event(key.ChordEvent{Name: n, Modifiers: kmods})
		}
	}
}

//export gio_onText
func gio_onText(view C.CFTypeRef, cstr *C.char) {
	str := C.GoString(cstr)
	w := views[view]
	w.w.event(key.EditEvent{Text: str})
}

//export gio_onMouse
func gio_onMouse(view C.CFTypeRef, cdir C.int, x, y, dx, dy C.CGFloat, ti C.double) {
	var typ pointer.Type
	switch cdir {
	case C.GIO_MOUSE_MOVE:
		typ = pointer.Move
	case C.GIO_MOUSE_UP:
		typ = pointer.Release
	case C.GIO_MOUSE_DOWN:
		typ = pointer.Press
	default:
		panic("invalid direction")
	}
	t := time.Duration(float64(ti)*float64(time.Second) + .5)
	w := views[view]
	w.w.event(pointer.Event{
		Type:     typ,
		Source:   pointer.Mouse,
		Time:     t,
		Position: f32.Point{X: float32(x), Y: float32(y)},
		Scroll:   f32.Point{X: float32(dx), Y: float32(dy)},
	})
}

//export gio_onDraw
func gio_onDraw(view C.CFTypeRef) {
	w := views[view]
	w.draw(true)
}

//export gio_onFocus
func gio_onFocus(view C.CFTypeRef, focus C.BOOL) {
	w := views[view]
	w.w.event(key.FocusEvent{Focus: focus == C.YES})
}

func (w *window) draw(sync bool) {
	width, height := int(C.gio_viewWidth(w.view)+.5), int(C.gio_viewHeight(w.view)+.5)
	if width == 0 || height == 0 {
		return
	}
	cfg := getConfig()
	cfg.Now = time.Now()
	w.setStage(StageRunning)
	w.w.event(DrawEvent{
		Size: image.Point{
			X: width,
			Y: height,
		},
		Config: cfg,
		sync:   sync,
	})
}

func getConfig() ui.Config {
	ppdp := float32(C.gio_getPixelsPerDP())
	ppdp *= monitorScale
	if ppdp < minDensity {
		ppdp = minDensity
	}
	return ui.Config{
		PxPerDp: ppdp,
		PxPerSp: ppdp,
	}
}

//export gio_onTerminate
func gio_onTerminate(view C.CFTypeRef) {
	w := views[view]
	delete(views, view)
	w.setStage(StageDead)
	close(windows)
}

//export gio_onHide
func gio_onHide(view C.CFTypeRef) {
	w := views[view]
	w.setStage(StagePaused)
}

//export gio_onShow
func gio_onShow(view C.CFTypeRef) {
	w := views[view]
	w.setStage(StageRunning)
}

//export gio_onCreate
func gio_onCreate(view C.CFTypeRef) {
	w := &window{
		view: view,
	}
	ow := newWindow(w)
	w.w = ow
	views[view] = w
	windows <- ow
}

func createWindow(opts *WindowOptions) error {
	singleWindow.mu.Lock()
	defer singleWindow.mu.Unlock()
	if singleWindow.hasOpts {
		panic("only one window supported")
	}
	singleWindow.opts = opts
	singleWindow.hasOpts = true
	return nil
}

func Main() {
	view := viewFactory()
	if view == 0 {
		// TODO: return this error from CreateWindow.
		panic(errors.New("CreateWindow: failed to create view"))
	}
	cfg := getConfig()
	opts := singleWindow.opts
	w := cfg.Val(opts.Width)
	h := cfg.Val(opts.Height)
	title := C.CString(opts.Title)
	defer C.free(unsafe.Pointer(title))
	C.gio_main(view, title, C.CGFloat(w), C.CGFloat(h))
}

func convertKey(k rune) (rune, bool) {
	if '0' <= k && k <= '9' || 'A' <= k && k <= 'Z' {
		return k, true
	}
	if 'a' <= k && k <= 'z' {
		return k - 0x20, true
	}
	var n rune
	switch k {
	case 0x1b:
		n = key.NameEscape
	case C.NSLeftArrowFunctionKey:
		n = key.NameLeftArrow
	case C.NSRightArrowFunctionKey:
		n = key.NameRightArrow
	case C.NSUpArrowFunctionKey:
		n = key.NameUpArrow
	case C.NSDownArrowFunctionKey:
		n = key.NameDownArrow
	case 0xd:
		n = key.NameReturn
	case C.NSHomeFunctionKey:
		n = key.NameHome
	case C.NSEndFunctionKey:
		n = key.NameEnd
	case 0x7f:
		n = key.NameDeleteBackward
	case C.NSDeleteFunctionKey:
		n = key.NameDeleteForward
	case C.NSPageUpFunctionKey:
		n = key.NamePageUp
	case C.NSPageDownFunctionKey:
		n = key.NamePageDown
	default:
		return 0, false
	}
	return n, true
}