From 661e24116757c6f0c85d401e0a0378ecf7ead360 Mon Sep 17 00:00:00 2001 From: Nikita Prokopov Date: Fri, 21 May 2021 00:05:23 +0200 Subject: [PATCH] WindowMac::move, WindowMac::resize --- examples/Example.java | 3 +++ macos/cc/WindowMac.hh | 2 ++ macos/cc/WindowMac.mm | 27 ++++++++++++++++++++++++++- macos/java/WindowMac.java | 8 ++------ 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/examples/Example.java b/examples/Example.java index 4470f070..33975bd2 100644 --- a/examples/Example.java +++ b/examples/Example.java @@ -32,6 +32,9 @@ public Example() { _window = App.makeWindow(); _window.setEventListener(this); changeLayer(); + var scale = _window.getScale(); + _window.move((int) (100 * scale), (int) (100 * scale)); + _window.resize((int) (800 * scale), (int) (600 * scale)); _window.show(); _window.requestFrame(); } diff --git a/macos/cc/WindowMac.hh b/macos/cc/WindowMac.hh index af1eff77..be2e6a3c 100644 --- a/macos/cc/WindowMac.hh +++ b/macos/cc/WindowMac.hh @@ -13,6 +13,8 @@ namespace jwm { void show(); void reconfigure(); float getScale() const; + void move(int left, int top); + void resize(int width, int height); void requestFrame(); void close(); diff --git a/macos/cc/WindowMac.mm b/macos/cc/WindowMac.mm index 41ff87fd..c0ae03ee 100644 --- a/macos/cc/WindowMac.mm +++ b/macos/cc/WindowMac.mm @@ -88,6 +88,19 @@ static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt return (fNSWindow.screen ?: [NSScreen mainScreen]).backingScaleFactor; } +void jwm::WindowMac::move(int left, int top) { + auto screen = fNSWindow.screen ?: [NSScreen mainScreen]; + auto scale = screen.backingScaleFactor; + NSPoint point {(CGFloat) left / scale, screen.frame.size.height - (CGFloat) top / scale}; + [fNSWindow setFrameTopLeftPoint:point]; +} + +void jwm::WindowMac::resize(int width, int height) { + auto scale = getScale(); + NSSize size {(CGFloat) width / scale, (CGFloat) height / scale}; + [fNSWindow setContentSize:size]; +} + void jwm::WindowMac::requestFrame() { fFrameRequested = true; if (!CVDisplayLinkIsRunning(fDisplayLink)) @@ -149,10 +162,22 @@ static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt return instance->getScale(); } +extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowMac_move + (JNIEnv* env, jobject obj, int left, int top) { + jwm::WindowMac* instance = reinterpret_cast(jwm::classes::Native::fromJava(env, obj)); + instance->move(left, top); +} + +extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowMac_resize + (JNIEnv* env, jobject obj, int width, int height) { + jwm::WindowMac* instance = reinterpret_cast(jwm::classes::Native::fromJava(env, obj)); + instance->resize(width, height); +} + extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowMac_requestFrame (JNIEnv* env, jobject obj) { jwm::WindowMac* instance = reinterpret_cast(jwm::classes::Native::fromJava(env, obj)); - return instance->requestFrame(); + instance->requestFrame(); } extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_jwm_WindowMac__1nClose diff --git a/macos/java/WindowMac.java b/macos/java/WindowMac.java index 21913a66..2e24f6bc 100644 --- a/macos/java/WindowMac.java +++ b/macos/java/WindowMac.java @@ -31,14 +31,10 @@ public WindowMac() { public native float getScale(); @Override - public void move(int left, int top) { - throw new UnsupportedOperationException("Not implemented"); - } + public native void move(int left, int top); @Override - public void resize(int width, int height) { - throw new UnsupportedOperationException("Not implemented"); - } + public native void resize(int width, int height); @Override public native void requestFrame();