Skip to content

Commit

Permalink
macOS: Window::getZOrder, Window::setZOrder #166
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Oct 26, 2021
1 parent 2ab911b commit ad060b3
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/dashboard/java/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public void accept(Event e) {
panelMouseCursors.accept(e);
panelRendering.accept(e);
panelEvents.accept(e);
panelTheme.accept(e);

float scale = window.getScreen().getScale();
if (e instanceof EventKey eventKey) {
Expand Down
1 change: 1 addition & 0 deletions examples/dashboard/java/PanelLegend.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public PanelLegend(Window window) {
shortcuts.put("N", "New Window");
shortcuts.put("W", "Close Window");
shortcuts.put("F", "Clipboard formats");
shortcuts.put("Z", "Toggle Z-order");
shortcuts.put("O", "Opacity");
shortcuts.put("1", "Minimize");
shortcuts.put("2", "Maximize");
Expand Down
12 changes: 12 additions & 0 deletions examples/dashboard/java/PanelTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ public PanelTheme(Window window) {
super(window);
}

@Override
public void accept(Event e) {
if (e instanceof EventKey ee && ee.isPressed() && ee.getKey() == Key.Z && ee.isModifierDown(Example.MODIFIER)) {
ZOrder zOrder = window.getZOrder();
ZOrder next = zOrder._values[(zOrder.ordinal() + 1) % zOrder._values.length];
window.setZOrder(next);
}
}

@Override
public void paintImpl(Canvas canvas, int width, int height, float scale) {
try (var paint = new Paint()) {
Expand All @@ -21,6 +30,9 @@ public void paintImpl(Canvas canvas, int width, int height, float scale) {

canvas.drawString("isInverted", Example.PADDING, Example.PADDING * 6, Example.FONT12, paint);
canvas.drawString("" + Theme.isInverted(), width / 2 + Example.PADDING / 2, Example.PADDING * 6, Example.FONT12, paint);

canvas.drawString("zOrder", Example.PADDING, Example.PADDING * 8, Example.FONT12, paint);
canvas.drawString("" + window.getZOrder(), width / 2 + Example.PADDING / 2, Example.PADDING * 8, Example.FONT12, paint);
}
}
}
13 changes: 13 additions & 0 deletions linux/java/WindowX11.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ public Window minimize() {
return this;
}

@Override
public ZOrder getZOrder() {
assert _onUIThread();
return ZOrder.NORMAL;
}

@Override
public Window setZOrder(ZOrder order) {
assert _onUIThread();
// TODO implement
return this;
}

@Override
public Window restore() {
_nRestore();
Expand Down
60 changes: 60 additions & 0 deletions macos/cc/WindowMac.mm
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <iostream>
#include <jni.h>
#include <sstream>
#include "impl/Library.hh"
#include "MainView.hh"
#include "MouseCursor.hh"
#include <memory>
#include "WindowMac.hh"
#include "WindowDelegate.hh"
#include "Util.hh"
#include "ZOrder.hh"

namespace jwm {
NSArray* kCursorCache;
Expand Down Expand Up @@ -323,6 +325,64 @@ static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt
}
}

extern "C" JNIEXPORT jint JNICALL Java_io_github_humbleui_jwm_WindowMac__1nGetZOrder
(JNIEnv* env, jobject obj) {
jwm::WindowMac* instance = reinterpret_cast<jwm::WindowMac*>(jwm::classes::Native::fromJava(env, obj));
NSWindow* nsWindow = instance->fNSWindow;
NSWindowLevel level = [nsWindow level];

if (level <= NSNormalWindowLevel)
return static_cast<jint>(jwm::ZOrder::NORMAL);
else if (level <= NSFloatingWindowLevel)
return static_cast<jint>(jwm::ZOrder::FLOATING);
else if (level <= NSModalPanelWindowLevel)
return static_cast<jint>(jwm::ZOrder::MODAL_PANEL);
else if (level <= NSMainMenuWindowLevel)
return static_cast<jint>(jwm::ZOrder::MAIN_MENU);
else if (level <= NSStatusWindowLevel)
return static_cast<jint>(jwm::ZOrder::STATUS);
else if (level <= NSPopUpMenuWindowLevel)
return static_cast<jint>(jwm::ZOrder::POP_UP_MENU);
else if (level <= NSScreenSaverWindowLevel)
return static_cast<jint>(jwm::ZOrder::SCREEN_SAVER);
else {
std::ostringstream oss;
oss << "Unexpected NSWindowLevel: " << level;
jwm::classes::Throwable::throwRuntimeException(env, oss.str().c_str());
return -1;
}
}

extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_WindowMac__1nSetZOrder
(JNIEnv* env, jobject obj, jint order) {
jwm::WindowMac* instance = reinterpret_cast<jwm::WindowMac*>(jwm::classes::Native::fromJava(env, obj));
NSWindow* nsWindow = instance->fNSWindow;
NSWindowLevel level;
switch (static_cast<jwm::ZOrder>(order)) {
case jwm::ZOrder::NORMAL:
level = NSNormalWindowLevel;
break;
case jwm::ZOrder::FLOATING:
level = NSFloatingWindowLevel;
break;
case jwm::ZOrder::MODAL_PANEL:
level = NSModalPanelWindowLevel;
break;
case jwm::ZOrder::MAIN_MENU:
level = NSMainMenuWindowLevel;
break;
case jwm::ZOrder::STATUS:
level = NSStatusWindowLevel;
break;
case jwm::ZOrder::POP_UP_MENU:
level = NSPopUpMenuWindowLevel;
break;
case jwm::ZOrder::SCREEN_SAVER:
level = NSScreenSaverWindowLevel;
break;
}
nsWindow.level = level;
}
extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_WindowMac__1nClose
(JNIEnv* env, jobject obj) {
jwm::WindowMac* instance = reinterpret_cast<jwm::WindowMac*>(jwm::classes::Native::fromJava(env, obj));
Expand Down
18 changes: 18 additions & 0 deletions macos/java/WindowMac.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,40 @@ public void requestFrame() {
_nRequestFrame();
}

@Override
public Window maximize() {
assert _onUIThread();
_nMaximize();
return this;
}

@Override
public Window minimize() {
assert _onUIThread();
_nMinimize();
return this;
}

@Override
public Window restore() {
assert _onUIThread();
_nRestore();
return this;
}

@Override
public ZOrder getZOrder() {
assert _onUIThread();
return ZOrder._values[_nGetZOrder()];
}

@Override
public Window setZOrder(ZOrder order) {
assert _onUIThread();
_nSetZOrder(order.ordinal());
return this;
}

@Override
public void close() {
assert _onUIThread();
Expand All @@ -145,5 +161,7 @@ public void close() {
@ApiStatus.Internal public native void _nMinimize();
@ApiStatus.Internal public native void _nMaximize();
@ApiStatus.Internal public native void _nRestore();
@ApiStatus.Internal public native int _nGetZOrder();
@ApiStatus.Internal public native void _nSetZOrder(int zOrder);
@ApiStatus.Internal public native void _nClose();
}
14 changes: 14 additions & 0 deletions shared/cc/ZOrder.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

namespace jwm {
// must be kept in sync with ZOrder.java
enum class ZOrder {
NORMAL,
FLOATING,
MODAL_PANEL,
MAIN_MENU,
STATUS,
POP_UP_MENU,
SCREEN_SAVER,
};
}
12 changes: 12 additions & 0 deletions shared/java/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,18 @@ public Window setMouseCursor(MouseCursor cursor) {
*/
public abstract Window restore();

/**
* @return current Z order
*/
public abstract ZOrder getZOrder();

/**
* <p>Makes window float always on top.</p>
*
* @return this
*/
public abstract Window setZOrder(ZOrder order);

/**
* <p>Close window and release its internal resources.</p>
* <p>Must be last window object method call. After this method window is in released state.</p>
Expand Down
28 changes: 28 additions & 0 deletions shared/java/ZOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.humbleui.jwm;

import org.jetbrains.annotations.*;

public enum ZOrder {
/** Default z-order */
NORMAL,

/** Default “Always on top” behavior */
FLOATING,

/** Windows/macOS only */
MODAL_PANEL,

/** Windows/macOS only */
MAIN_MENU,

/** Windows/macOS only */
STATUS,

/** Windows/macOS only */
POP_UP_MENU,

/** Windows/macOS only */
SCREEN_SAVER;

@ApiStatus.Internal public static final ZOrder[] _values = values();
}
13 changes: 13 additions & 0 deletions windows/java/WindowWin32.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ public Window restore() {
return this;
}

@Override
public ZOrder getZOrder() {
assert _onUIThread();
return ZOrder.NORMAL;
}

@Override
public Window setZOrder(ZOrder order) {
assert _onUIThread();
// TODO implement
return this;
}

@Override
public void close() {
assert _onUIThread();
Expand Down

0 comments on commit ad060b3

Please sign in to comment.