Skip to content

Commit cf0aea3

Browse files
committed
Store Layer, EventListener and TextInputClient in Window. Automatically manage layers on window events
1 parent 8e1c4c2 commit cf0aea3

File tree

15 files changed

+236
-183
lines changed

15 files changed

+236
-183
lines changed

examples/dashboard/java/Example.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public class Example implements Consumer<Event> {
3232
public Window window;
3333

3434
public boolean paused = true;
35-
public boolean closed = false;
3635

3736
public Example() {
3837
window = App.makeWindow();
@@ -76,7 +75,7 @@ public Example() {
7675
}
7776

7877
public void paint(String reason) {
79-
if (closed)
78+
if (window.isClosed())
8079
return;
8180

8281
IRect contentRect = window.getContentRect();
@@ -98,7 +97,7 @@ public void paint(String reason) {
9897
FONT48.setSize(48 * scale);
9998
}
10099

101-
var canvas = panelRendering.layer.beforePaint();
100+
var canvas = ((SkijaLayer) window.getLayer()).beforePaint();
102101
canvas.clear(0xFF264653);
103102
int canvasCount = canvas.save();
104103

@@ -150,7 +149,7 @@ public void paint(String reason) {
150149
}
151150
canvas.restoreToCount(canvasCount);
152151

153-
panelRendering.layer.afterPaint();
152+
((SkijaLayer) window.getLayer()).afterPaint();
154153
}
155154

156155
@Override
@@ -195,7 +194,6 @@ public void accept(Event e) {
195194
if (!paused)
196195
window.requestFrame();
197196
} else if (e instanceof EventWindowCloseRequest) {
198-
closed = true;
199197
window.close();
200198
if (App._windows.size() == 0)
201199
App.terminate();

examples/dashboard/java/PanelRendering.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public class PanelRendering extends Panel {
2020
public Map<String, String> layersStatus = new HashMap<>();
2121
public String[] layers;
2222
public int layerIdx = 0;
23-
public SkijaLayer layer;
2423

2524
// Layer status displayed on the right side from the layer name
2625
public static final String CHECKED = "+";
@@ -48,57 +47,40 @@ public void bumpCounter(String reason) {
4847
}
4948

5049
public void changeLayer() {
51-
if (layer != null) {
52-
layer.close();
53-
layer = null;
54-
}
55-
5650
int attemptsCount = layers.length;
5751

58-
while (layer == null && attemptsCount > 0) {
52+
while (attemptsCount > 0) {
5953
attemptsCount -= 1;
6054
String layerName = layers[layerIdx];
6155
String className = "io.github.humbleui.jwm.examples." + layerName;
6256

6357
try {
64-
layer = (SkijaLayer) Example.class.forName(className).getDeclaredConstructor().newInstance();
58+
SkijaLayer layer = (SkijaLayer) Example.class.forName(className).getDeclaredConstructor().newInstance();
6559
layer.attach(window);
60+
break;
6661
} catch (Exception e) {
6762
System.err.println("Failed to create layer " + className);
6863
e.printStackTrace();
69-
layer = null;
7064
layersStatus.put(layerName, FAILED); // Update layer status
7165
nextLayerIdx();
7266
}
7367
}
7468

75-
if (layer == null)
69+
if (window._layer == null)
7670
throw new RuntimeException("No available layer to create");
7771

7872
layersStatus.put(layers[layerIdx], CHECKED); // Mark layer as checked
79-
layer.reconfigure();
80-
layer.resize(window.getContentRect().getWidth(), window.getContentRect().getHeight());
8173
}
8274

8375
@Override
8476
public void accept(Event e) {
85-
if (e instanceof EventWindowScreenChange) {
86-
layer.reconfigure();
87-
accept(new EventWindowResize(window.getWindowRect().getWidth(),
88-
window.getWindowRect().getHeight(),
89-
window.getContentRect().getWidth(),
90-
window.getContentRect().getHeight()));
91-
} else if (e instanceof EventWindowResize ee) {
92-
layer.resize(ee.getContentWidth(), ee.getContentHeight());
93-
} else if (e instanceof EventKey ee && ee.isPressed()) {
77+
if (e instanceof EventKey ee && ee.isPressed()) {
9478
Key key = ee.getKey();
9579
boolean modifier = ee.isModifierDown(Example.MODIFIER);
9680
if (Key.L == key && modifier) {
9781
nextLayerIdx();
9882
changeLayer();
9983
}
100-
} else if (e instanceof EventWindowCloseRequest) {
101-
layer.close();
10284
}
10385
}
10486

examples/dashboard/java/PanelTextInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public PanelTextInput(Window window) {
2525
timerTask = new TimerTask() {
2626
public void run() {
2727
cursorDraw = !cursorDraw;
28-
App.runOnUIThread(() -> { if (!window._closed) window.requestFrame(); });
28+
App.runOnUIThread(() -> { if (!window.isClosed()) window.requestFrame(); });
2929
}
3030
};
3131
timer.schedule(timerTask, 0, 500);

macos/cc/MainView.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ - (NSUInteger)characterIndexForPoint:(NSPoint)point {
516516

517517
- (NSRect)firstRectForCharacterRange:(NSRange)range
518518
actualRange:(NSRangePointer)actualRange {
519-
if (fWindow->fTextInputClient != nullptr) {
519+
jwm::IRect rect{};
520+
if (fWindow->getRectForMarkedRange(range.location, range.location + range.length, rect)) {
520521
JNIEnv* env = fWindow->fEnv;
521-
jwm::IRect rect = jwm::classes::TextInputClient::getRectForMarkedRange(env, fWindow->fTextInputClient, range.location, range.location + range.length);
522522
const NSRect frame = [fWindow->fNSWindow.contentView frame];
523523
CGFloat scale = fWindow->getScale();
524524
const NSRect res = NSMakeRect(

macos/java/LayerMetal.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ public LayerMetal() {
1414

1515
@Override
1616
public void attach(Window window) {
17+
// TODO extract
1718
assert _onUIThread();
19+
if (window._layer != null) {
20+
window._layer.close();
21+
window._layer = null;
22+
}
1823
_window = window;
1924
_nAttach(window);
25+
window._layer = this;
26+
reconfigure();
27+
resize(window.getContentRect().getWidth(), window.getContentRect().getHeight());
2028
}
2129

2230
@Override
@@ -54,6 +62,7 @@ public void swapBuffers() {
5462
@Override
5563
public void close() {
5664
assert _onUIThread();
65+
_window._layer = null;
5766
_nClose();
5867
_window = null;
5968
super.close();

shared/cc/Window.cc

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
#include <iostream>
22
#include <jni.h>
33
#include "Window.hh"
4+
#include "impl/JNILocal.hh"
45
#include "impl/Library.hh"
56

67
jwm::Window::~Window() {
7-
if (fEventListener)
8-
fEnv->DeleteGlobalRef(fEventListener);
8+
fEnv->DeleteGlobalRef(fWindow);
99
}
1010

1111
void jwm::Window::dispatch(jobject event) {
12-
if (fEventListener)
13-
jwm::classes::Consumer::accept(fEnv, fEventListener, event);
12+
jwm::classes::Consumer::accept(fEnv, fWindow, event);
1413
}
1514

16-
extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_Window__1nSetEventListener
17-
(JNIEnv* env, jobject obj, jobject listener) {
18-
jwm::Window* instance = reinterpret_cast<jwm::Window*>(jwm::classes::Native::fromJava(env, obj));
19-
if (instance->fEventListener)
20-
env->DeleteGlobalRef(instance->fEventListener);
21-
instance->fEventListener = listener ? env->NewGlobalRef(listener) : nullptr;
15+
bool jwm::Window::getRectForMarkedRange(jint selectionStart, jint selectionEnd, jwm::IRect& rect) {
16+
JNILocal<jobject> client(fEnv, fEnv->GetObjectField(fWindow, jwm::classes::Window::kTextInputClient));
17+
jwm::classes::Throwable::exceptionThrown(fEnv);
18+
if (client.get()) {
19+
rect = jwm::classes::TextInputClient::getRectForMarkedRange(fEnv, client.get(), selectionStart, selectionEnd);
20+
return true;
21+
} else
22+
return false;
2223
}
2324

24-
extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_Window__1nSetTextInputClient
25-
(JNIEnv* env, jobject obj, jobject client) {
25+
extern "C" JNIEXPORT void JNICALL Java_io_github_humbleui_jwm_Window__1nInit
26+
(JNIEnv* env, jobject obj) {
2627
jwm::Window* instance = reinterpret_cast<jwm::Window*>(jwm::classes::Native::fromJava(env, obj));
27-
if (instance->fTextInputClient)
28-
env->DeleteGlobalRef(instance->fTextInputClient);
29-
instance->fTextInputClient = client ? env->NewGlobalRef(client) : nullptr;
30-
}
28+
instance->fWindow = env->NewGlobalRef(obj);
29+
}

shared/cc/Window.hh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include <jni.h>
3+
#include "impl/Library.hh"
34
#include "impl/RefCounted.hh"
45

56
namespace jwm {
@@ -12,8 +13,9 @@ namespace jwm {
1213

1314
void dispatch(jobject event);
1415

16+
bool getRectForMarkedRange(jint selectionStart, jint selectionEnd, jwm::IRect& rect);
17+
1518
JNIEnv* fEnv = nullptr;
16-
jobject fEventListener = nullptr;
17-
jobject fTextInputClient = nullptr;
19+
jobject fWindow = nullptr;
1820
};
1921
}

0 commit comments

Comments
 (0)