From 21ad48b7dee6f4cd10761e7c2b6c791309a63a57 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 21 Jul 2019 17:34:12 +0200 Subject: [PATCH] ui/app: (macOS) use consistent scale for pointer events Instead of querying the backing store scale for every pointer event, use the scale from the last window draw. It might not make any visible difference because we redraw on scale changes, but it is more correct. Signed-off-by: Elias Naur --- ui/app/gl_macos.m | 3 +-- ui/app/os_macos.go | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ui/app/gl_macos.m b/ui/app/gl_macos.m index 3cadd0a9..7a2aad72 100644 --- a/ui/app/gl_macos.m +++ b/ui/app/gl_macos.m @@ -13,13 +13,12 @@ static void handleMouse(NSView *view, NSEvent *event, int typ, CGFloat dx, CGFloat dy) { NSPoint p = [view convertPoint:[event locationInWindow] fromView:nil]; - CGFloat scale = view.window.backingScaleFactor; if (!event.hasPreciseScrollingDeltas) { // dx and dy are in rows and columns. dx *= 10; dy *= 10; } - gio_onMouse((__bridge CFTypeRef)view, typ, p.x*scale, p.y*scale, dx*scale, dy*scale, [event timestamp]); + gio_onMouse((__bridge CFTypeRef)view, typ, p.x, p.y, dx, dy, [event timestamp]); } static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp *inNow, const CVTimeStamp *inOutputTime, CVOptionFlags flagsIn, CVOptionFlags *flagsOut, void *displayLinkContext) { diff --git a/ui/app/os_macos.go b/ui/app/os_macos.go index 49b86d87..a01f5a54 100644 --- a/ui/app/os_macos.go +++ b/ui/app/os_macos.go @@ -34,6 +34,7 @@ type window struct { w *Window stage Stage ppdp float32 + scale float32 } type viewCmd struct { @@ -157,12 +158,14 @@ func gio_onMouse(view C.CFTypeRef, cdir C.int, x, y, dx, dy C.CGFloat, ti C.doub t := time.Duration(float64(ti)*float64(time.Second) + .5) viewDo(view, func(views viewMap, view C.CFTypeRef) { w := views[view] + x, y := float32(x)*w.scale, float32(y)*w.scale + dx, dy := float32(dx)*w.scale, float32(dy)*w.scale 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)}, + Position: f32.Point{X: x, Y: y}, + Scroll: f32.Point{X: dx, Y: dy}, }) }) } @@ -185,14 +188,14 @@ func gio_onFocus(view C.CFTypeRef, focus C.BOOL) { } func (w *window) draw(sync bool) { + w.scale = float32(C.gio_getViewBackingScale(w.view)) wf, hf := float32(C.gio_viewWidth(w.view)), float32(C.gio_viewHeight(w.view)) if wf == 0 || hf == 0 { return } - scale := float32(C.gio_getViewBackingScale(w.view)) - width := int(wf*scale + .5) - height := int(hf*scale + .5) - cfg := configFor(w.ppdp, scale) + width := int(wf*w.scale + .5) + height := int(hf*w.scale + .5) + cfg := configFor(w.ppdp, w.scale) cfg.now = time.Now() w.setStage(StageRunning) w.w.event(DrawEvent{ @@ -252,8 +255,9 @@ func gio_onCreate(view C.CFTypeRef) { viewDo(view, func(views viewMap, view C.CFTypeRef) { scale := float32(C.gio_getBackingScale()) w := &window{ - view: view, - ppdp: getPixelsPerDp(scale), + view: view, + ppdp: getPixelsPerDp(scale), + scale: scale, } wopts := <-mainWindow.out w.w = wopts.window