app,app/internal/wm: implement ViewEvent for macOS

Move the deprecated setWantsBestResolutionOpenGLSurface to GL-specific
code while we're here.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-05-16 14:57:20 +02:00
parent 413bff8766
commit 1ec408280e
9 changed files with 46 additions and 5 deletions
+6
View File
@@ -9,6 +9,12 @@ import (
"gioui.org/app/internal/wm" "gioui.org/app/internal/wm"
) )
// ViewEvent carries the platform specific window handles for
// a Window.
//
// ViewEvent is implemented on Android and macOS.
type ViewEvent = wm.ViewEvent
// extraArgs contains extra arguments to append to // extraArgs contains extra arguments to append to
// os.Args. The arguments are separated with |. // os.Args. The arguments are separated with |.
// Useful for running programs on mobiles where the // Useful for running programs on mobiles where the
-2
View File
@@ -6,8 +6,6 @@ import (
"gioui.org/app/internal/wm" "gioui.org/app/internal/wm"
) )
type ViewEvent = wm.ViewEvent
// JavaVM returns the global JNI JavaVM. // JavaVM returns the global JNI JavaVM.
func JavaVM() uintptr { func JavaVM() uintptr {
return wm.JavaVM() return wm.JavaVM()
+1
View File
@@ -45,6 +45,7 @@ CFTypeRef gio_createGLContext(void) {
void gio_setContextView(CFTypeRef ctxRef, CFTypeRef viewRef) { void gio_setContextView(CFTypeRef ctxRef, CFTypeRef viewRef) {
GioGLContext *ctx = (__bridge GioGLContext *)ctxRef; GioGLContext *ctx = (__bridge GioGLContext *)ctxRef;
NSView *view = (__bridge NSView *)viewRef; NSView *view = (__bridge NSView *)viewRef;
[view setWantsBestResolutionOpenGLSurface:YES];
[ctx setView:view]; [ctx setView:view];
[[NSNotificationCenter defaultCenter] addObserver:ctx [[NSNotificationCenter defaultCenter] addObserver:ctx
selector:@selector(notifyUpdate:) selector:@selector(notifyUpdate:)
+4
View File
@@ -45,6 +45,8 @@ import (
"gioui.org/unit" "gioui.org/unit"
) )
type ViewEvent struct{}
type window struct { type window struct {
view C.CFTypeRef view C.CFTypeRef
w Callbacks w Callbacks
@@ -322,3 +324,5 @@ func Main() {
func gio_runMain() { func gio_runMain() {
runMain() runMain()
} }
func (_ ViewEvent) ImplementsEvent() {}
+6 -1
View File
@@ -4,7 +4,6 @@ package wm
import ( import (
"fmt" "fmt"
"gioui.org/internal/f32color"
"image" "image"
"image/color" "image/color"
"strings" "strings"
@@ -14,6 +13,8 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"gioui.org/internal/f32color"
"gioui.org/f32" "gioui.org/f32"
"gioui.org/io/clipboard" "gioui.org/io/clipboard"
"gioui.org/io/key" "gioui.org/io/key"
@@ -22,6 +23,8 @@ import (
"gioui.org/unit" "gioui.org/unit"
) )
type ViewEvent struct{}
type window struct { type window struct {
window js.Value window js.Value
document js.Value document js.Value
@@ -671,3 +674,5 @@ func translateKey(k string) (string, bool) {
} }
return n, true return n, true
} }
func (_ ViewEvent) ImplementsEvent() {}
+15
View File
@@ -51,6 +51,7 @@ __attribute__ ((visibility ("hidden"))) void gio_setSize(CFTypeRef windowRef, CG
__attribute__ ((visibility ("hidden"))) void gio_setMinSize(CFTypeRef windowRef, CGFloat width, CGFloat height); __attribute__ ((visibility ("hidden"))) void gio_setMinSize(CFTypeRef windowRef, CGFloat width, CGFloat height);
__attribute__ ((visibility ("hidden"))) void gio_setMaxSize(CFTypeRef windowRef, CGFloat width, CGFloat height); __attribute__ ((visibility ("hidden"))) void gio_setMaxSize(CFTypeRef windowRef, CGFloat width, CGFloat height);
__attribute__ ((visibility ("hidden"))) void gio_setTitle(CFTypeRef windowRef, const char *title); __attribute__ ((visibility ("hidden"))) void gio_setTitle(CFTypeRef windowRef, const char *title);
__attribute__ ((visibility ("hidden"))) CFTypeRef gio_layerForView(CFTypeRef viewRef);
*/ */
import "C" import "C"
@@ -59,6 +60,15 @@ func init() {
runtime.LockOSThread() runtime.LockOSThread()
} }
// ViewEvent notified the client of changes to the window AppKit handles.
// The handles are retained until another ViewEvent is sent.
type ViewEvent struct {
// View is a CFTypeRef for the NSView for the window.
View uintptr
// Layer is a CFTypeRef of the CALayer of View.
Layer uintptr
}
type window struct { type window struct {
view C.CFTypeRef view C.CFTypeRef
window C.CFTypeRef window C.CFTypeRef
@@ -335,6 +345,7 @@ func configFor(scale float32) unit.Metric {
func gio_onClose(view C.CFTypeRef) { func gio_onClose(view C.CFTypeRef) {
w := mustView(view) w := mustView(view)
w.displayLink.Close() w.displayLink.Close()
w.w.Event(ViewEvent{})
deleteView(view) deleteView(view)
w.w.Event(system.DestroyEvent{}) w.w.Event(system.DestroyEvent{})
C.CFRelease(w.view) C.CFRelease(w.view)
@@ -395,6 +406,8 @@ func NewWindow(win Callbacks, opts *Options) error {
} }
nextTopLeft = C.gio_cascadeTopLeftFromPoint(w.window, nextTopLeft) nextTopLeft = C.gio_cascadeTopLeftFromPoint(w.window, nextTopLeft)
C.gio_makeKeyAndOrderFront(w.window) C.gio_makeKeyAndOrderFront(w.window)
layer := C.gio_layerForView(w.view)
w.w.Event(ViewEvent{View: uintptr(w.view), Layer: uintptr(layer)})
}) })
return <-errch return <-errch
} }
@@ -510,3 +523,5 @@ func convertMods(mods C.NSUInteger) key.Modifiers {
} }
return kmods return kmods
} }
func (_ ViewEvent) ImplementsEvent() {}
+6 -2
View File
@@ -300,12 +300,16 @@ void gio_setTitle(CFTypeRef windowRef, const char *title) {
window.title = [NSString stringWithUTF8String: title]; window.title = [NSString stringWithUTF8String: title];
} }
CFTypeRef gio_layerForView(CFTypeRef viewRef) {
NSView *view = (__bridge NSView *)viewRef;
return (__bridge CFTypeRef)view.layer;
}
CFTypeRef gio_createView(void) { CFTypeRef gio_createView(void) {
@autoreleasepool { @autoreleasepool {
NSRect frame = NSMakeRect(0, 0, 0, 0); NSRect frame = NSMakeRect(0, 0, 0, 0);
GioView* view = [[GioView alloc] initWithFrame:frame]; GioView* view = [[GioView alloc] initWithFrame:frame];
[view setWantsBestResolutionOpenGLSurface:YES]; [view setWantsLayer:YES];
[view setWantsLayer:YES]; // The default in Mojave.
return CFBridgingRetain(view); return CFBridgingRetain(view);
} }
} }
+4
View File
@@ -8,6 +8,8 @@ import (
"errors" "errors"
) )
type ViewEvent struct{}
func Main() { func Main() {
select {} select {}
} }
@@ -37,3 +39,5 @@ func NewWindow(window Callbacks, opts *Options) error {
} }
return errors.New("app: no window driver available") return errors.New("app: no window driver available")
} }
func (_ ViewEvent) ImplementsEvent() {}
+4
View File
@@ -28,6 +28,8 @@ import (
"gioui.org/io/system" "gioui.org/io/system"
) )
type ViewEvent struct{}
type winConstraints struct { type winConstraints struct {
minWidth, minHeight int32 minWidth, minHeight int32
maxWidth, maxHeight int32 maxWidth, maxHeight int32
@@ -790,3 +792,5 @@ func configForDPI(dpi int) unit.Metric {
PxPerSp: ppdp, PxPerSp: ppdp,
} }
} }
func (_ ViewEvent) ImplementsEvent() {}