From 1ec408280e5358949c5de30bc988be6613594821 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 16 May 2021 14:57:20 +0200 Subject: [PATCH] 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 --- app/app.go | 6 ++++++ app/app_android.go | 2 -- app/internal/wm/gl_macos.m | 1 + app/internal/wm/os_ios.go | 4 ++++ app/internal/wm/os_js.go | 7 ++++++- app/internal/wm/os_macos.go | 15 +++++++++++++++ app/internal/wm/os_macos.m | 8 ++++++-- app/internal/wm/os_unix.go | 4 ++++ app/internal/wm/os_windows.go | 4 ++++ 9 files changed, 46 insertions(+), 5 deletions(-) diff --git a/app/app.go b/app/app.go index 611493b2..0c840cf7 100644 --- a/app/app.go +++ b/app/app.go @@ -9,6 +9,12 @@ import ( "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 // os.Args. The arguments are separated with |. // Useful for running programs on mobiles where the diff --git a/app/app_android.go b/app/app_android.go index e1c56b43..30a6c675 100644 --- a/app/app_android.go +++ b/app/app_android.go @@ -6,8 +6,6 @@ import ( "gioui.org/app/internal/wm" ) -type ViewEvent = wm.ViewEvent - // JavaVM returns the global JNI JavaVM. func JavaVM() uintptr { return wm.JavaVM() diff --git a/app/internal/wm/gl_macos.m b/app/internal/wm/gl_macos.m index e722407e..61943daf 100644 --- a/app/internal/wm/gl_macos.m +++ b/app/internal/wm/gl_macos.m @@ -45,6 +45,7 @@ CFTypeRef gio_createGLContext(void) { void gio_setContextView(CFTypeRef ctxRef, CFTypeRef viewRef) { GioGLContext *ctx = (__bridge GioGLContext *)ctxRef; NSView *view = (__bridge NSView *)viewRef; + [view setWantsBestResolutionOpenGLSurface:YES]; [ctx setView:view]; [[NSNotificationCenter defaultCenter] addObserver:ctx selector:@selector(notifyUpdate:) diff --git a/app/internal/wm/os_ios.go b/app/internal/wm/os_ios.go index 1730af2c..660119b0 100644 --- a/app/internal/wm/os_ios.go +++ b/app/internal/wm/os_ios.go @@ -45,6 +45,8 @@ import ( "gioui.org/unit" ) +type ViewEvent struct{} + type window struct { view C.CFTypeRef w Callbacks @@ -322,3 +324,5 @@ func Main() { func gio_runMain() { runMain() } + +func (_ ViewEvent) ImplementsEvent() {} diff --git a/app/internal/wm/os_js.go b/app/internal/wm/os_js.go index 3f7c6bdc..427be44d 100644 --- a/app/internal/wm/os_js.go +++ b/app/internal/wm/os_js.go @@ -4,7 +4,6 @@ package wm import ( "fmt" - "gioui.org/internal/f32color" "image" "image/color" "strings" @@ -14,6 +13,8 @@ import ( "unicode" "unicode/utf8" + "gioui.org/internal/f32color" + "gioui.org/f32" "gioui.org/io/clipboard" "gioui.org/io/key" @@ -22,6 +23,8 @@ import ( "gioui.org/unit" ) +type ViewEvent struct{} + type window struct { window js.Value document js.Value @@ -671,3 +674,5 @@ func translateKey(k string) (string, bool) { } return n, true } + +func (_ ViewEvent) ImplementsEvent() {} diff --git a/app/internal/wm/os_macos.go b/app/internal/wm/os_macos.go index 4ad04b4f..83f6edac 100644 --- a/app/internal/wm/os_macos.go +++ b/app/internal/wm/os_macos.go @@ -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_setMaxSize(CFTypeRef windowRef, CGFloat width, CGFloat height); __attribute__ ((visibility ("hidden"))) void gio_setTitle(CFTypeRef windowRef, const char *title); +__attribute__ ((visibility ("hidden"))) CFTypeRef gio_layerForView(CFTypeRef viewRef); */ import "C" @@ -59,6 +60,15 @@ func init() { 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 { view C.CFTypeRef window C.CFTypeRef @@ -335,6 +345,7 @@ func configFor(scale float32) unit.Metric { func gio_onClose(view C.CFTypeRef) { w := mustView(view) w.displayLink.Close() + w.w.Event(ViewEvent{}) deleteView(view) w.w.Event(system.DestroyEvent{}) C.CFRelease(w.view) @@ -395,6 +406,8 @@ func NewWindow(win Callbacks, opts *Options) error { } nextTopLeft = C.gio_cascadeTopLeftFromPoint(w.window, nextTopLeft) C.gio_makeKeyAndOrderFront(w.window) + layer := C.gio_layerForView(w.view) + w.w.Event(ViewEvent{View: uintptr(w.view), Layer: uintptr(layer)}) }) return <-errch } @@ -510,3 +523,5 @@ func convertMods(mods C.NSUInteger) key.Modifiers { } return kmods } + +func (_ ViewEvent) ImplementsEvent() {} diff --git a/app/internal/wm/os_macos.m b/app/internal/wm/os_macos.m index 649a6a4f..def7b6c6 100644 --- a/app/internal/wm/os_macos.m +++ b/app/internal/wm/os_macos.m @@ -300,12 +300,16 @@ void gio_setTitle(CFTypeRef windowRef, const char *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) { @autoreleasepool { NSRect frame = NSMakeRect(0, 0, 0, 0); GioView* view = [[GioView alloc] initWithFrame:frame]; - [view setWantsBestResolutionOpenGLSurface:YES]; - [view setWantsLayer:YES]; // The default in Mojave. + [view setWantsLayer:YES]; return CFBridgingRetain(view); } } diff --git a/app/internal/wm/os_unix.go b/app/internal/wm/os_unix.go index 8143100e..d17ef7f3 100644 --- a/app/internal/wm/os_unix.go +++ b/app/internal/wm/os_unix.go @@ -8,6 +8,8 @@ import ( "errors" ) +type ViewEvent struct{} + func Main() { select {} } @@ -37,3 +39,5 @@ func NewWindow(window Callbacks, opts *Options) error { } return errors.New("app: no window driver available") } + +func (_ ViewEvent) ImplementsEvent() {} diff --git a/app/internal/wm/os_windows.go b/app/internal/wm/os_windows.go index 5e6dbc6c..a393dd7d 100644 --- a/app/internal/wm/os_windows.go +++ b/app/internal/wm/os_windows.go @@ -28,6 +28,8 @@ import ( "gioui.org/io/system" ) +type ViewEvent struct{} + type winConstraints struct { minWidth, minHeight int32 maxWidth, maxHeight int32 @@ -790,3 +792,5 @@ func configForDPI(dpi int) unit.Metric { PxPerSp: ppdp, } } + +func (_ ViewEvent) ImplementsEvent() {}