Commit 168b0b51 authored by Roman Sedaikin's avatar Roman Sedaikin

Improved NSWindow search by CALayer on MacOS.

parent 544b4b76
...@@ -3,17 +3,32 @@ package SkijaInjectSample ...@@ -3,17 +3,32 @@ package SkijaInjectSample
import java.awt.BorderLayout import java.awt.BorderLayout
import java.awt.Color import java.awt.Color
import java.awt.Dimension import java.awt.Dimension
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.awt.event.ComponentAdapter import java.awt.event.ComponentAdapter
import java.awt.event.ComponentEvent import java.awt.event.ComponentEvent
import java.awt.event.MouseEvent import java.awt.event.MouseEvent
import java.awt.event.MouseMotionAdapter import java.awt.event.MouseMotionAdapter
import javax.swing.* import javax.swing.*
private val state = State()
private var mouseX = 0
private var mouseY = 0
fun Button(
text: String,
action: (() -> Unit)? = null
): JButton {
val button = JButton(text)
button.setToolTipText("Tooltip for $text button.")
button.setPreferredSize(Dimension(100, 100))
button.addActionListener(object : ActionListener {
public override fun actionPerformed(e: ActionEvent) {
action?.invoke()
}
})
fun Button(text: String): JButton { return button
val btn = JButton(text)
btn.setPreferredSize(Dimension(100, 100))
return btn
} }
fun SwingSkia() = SwingUtilities.invokeLater { fun SwingSkia() = SwingUtilities.invokeLater {
...@@ -22,30 +37,66 @@ fun SwingSkia() = SwingUtilities.invokeLater { ...@@ -22,30 +37,66 @@ fun SwingSkia() = SwingUtilities.invokeLater {
window.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE window.defaultCloseOperation = WindowConstants.DISPOSE_ON_CLOSE
window.title = "SwingSkiaWindow" window.title = "SwingSkiaWindow"
val panel = SkiaPanel() state.text = window.title
window.contentPane.add(Button("North"), BorderLayout.NORTH) var panel = getSkiaPanel()
window.contentPane.add(Button("West"), BorderLayout.WEST)
window.contentPane.add(Button("East"), BorderLayout.EAST) window.contentPane.add(
window.contentPane.add(Button("South"), BorderLayout.SOUTH) Button(
text = "Fullscreen",
action = {
if (panel != null) {
panel.layer.fullscreen = !panel.layer.fullscreen
}
}
),
BorderLayout.NORTH
)
window.contentPane.add(
Button(
text = "Add SP",
action = {
window.contentPane.remove(panel)
panel = getSkiaPanel()
window.contentPane.add(panel, BorderLayout.CENTER)
window.setSize(window.width + 1, window.height)
}
),
BorderLayout.WEST
)
window.contentPane.add(
Button(
text = "Remove SP",
action = {
window.contentPane.remove(panel)
}
),
BorderLayout.EAST
)
window.contentPane.add(
Button(
text = "New Window",
action = {
createWindow("ComposeWindow")
}
),
BorderLayout.SOUTH
)
window.contentPane.add(panel, BorderLayout.CENTER) window.contentPane.add(panel, BorderLayout.CENTER)
window.setSize(800, 600)
window.setVisible(true)
}
private fun getSkiaPanel(): SkiaPanel {
val panel = SkiaPanel()
val btnPanelOK = JPanel() val btnPanelOK = JPanel()
btnPanelOK.setLayout(BorderLayout(0, 0)) btnPanelOK.setLayout(BorderLayout(0, 0))
btnPanelOK.setBackground(Color.white) btnPanelOK.setBackground(Color.white)
btnPanelOK.add(JButton("OK")) btnPanelOK.add(JButton("OK"))
val btnCancel = JButton("Cancel") val btnCancel = JButton("Cancel")
panel.add(btnPanelOK) panel.add(btnPanelOK)
panel.add(btnCancel) panel.add(btnCancel)
val state = State()
state.text = window.title
var mouseX = 0
var mouseY = 0
panel.addComponentListener(object : ComponentAdapter() { panel.addComponentListener(object : ComponentAdapter() {
override fun componentResized(e: ComponentEvent) { override fun componentResized(e: ComponentEvent) {
btnPanelOK.setBounds(panel.width - 200, panel.height - 100, 200, 40) btnPanelOK.setBounds(panel.width - 200, panel.height - 100, 200, 40)
...@@ -55,16 +106,12 @@ fun SwingSkia() = SwingUtilities.invokeLater { ...@@ -55,16 +106,12 @@ fun SwingSkia() = SwingUtilities.invokeLater {
panel.repaint() panel.repaint()
} }
}) })
panel.layer.renderer = Renderer(panel.layer) { renderer, w, h, nanoTime -> displayScene(renderer, w, h, nanoTime, mouseX, mouseY, state) } panel.layer.renderer = Renderer(panel.layer) { renderer, w, h, nanoTime -> displayScene(renderer, w, h, nanoTime, mouseX, mouseY, state) }
panel.layer.addMouseMotionListener(object : MouseMotionAdapter() { panel.layer.addMouseMotionListener(object : MouseMotionAdapter() {
override fun mouseMoved(event: MouseEvent) { override fun mouseMoved(event: MouseEvent) {
mouseX = event.x mouseX = event.x
mouseY = event.y mouseY = event.y
} }
}) })
return panel
window.setSize(800, 600)
window.setVisible(true)
} }
...@@ -82,6 +82,29 @@ LayerHandler * findByObject(JNIEnv *env, jobject object) ...@@ -82,6 +82,29 @@ LayerHandler * findByObject(JNIEnv *env, jobject object)
extern "C" extern "C"
{ {
NSWindow *recoursiveSearch(NSView *rootView, CALayer *layer) {
if (rootView.subviews == nil || rootView.subviews.count == 0) {
return nil;
}
for (NSView* child in rootView.subviews) {
if (child.layer == layer) {
return rootView.window;
}
NSWindow* recOut = recoursiveSearch(child, layer);
if (recOut != nil) {
return recOut;
}
}
return nil;
}
NSWindow *findCALayerWindow(NSView *rootView, CALayer *layer) {
if (rootView.layer == layer) {
return rootView.window;
}
return recoursiveSearch(rootView, layer);
}
JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_nativeInit(JNIEnv *env, jobject canvas, jlong platformInfoPtr) JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_nativeInit(JNIEnv *env, jobject canvas, jlong platformInfoPtr)
{ {
if (layerStorage == nil) if (layerStorage == nil)
...@@ -98,20 +121,13 @@ JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_nativeInit(JNIEnv ...@@ -98,20 +121,13 @@ JNIEXPORT void JNICALL Java_org_jetbrains_skiko_HardwareLayer_nativeInit(JNIEnv
[layersSet setCanvasGlobalRef: canvasGlobalRef]; [layersSet setCanvasGlobalRef: canvasGlobalRef];
NSMutableArray<NSWindow *> *windows = [NSMutableArray arrayWithArray: [[NSApplication sharedApplication] windows]]; NSMutableArray<NSWindow *> *windows = [NSMutableArray arrayWithArray: [[NSApplication sharedApplication] windows]];
for (NSWindow* window in windows) {
for (LayerHandler* value in layerStorage) layersSet.window = findCALayerWindow(window.contentView, layersSet.container);
{ if (layersSet.window != nil) {
if (layersSet.container == value.container) break;
{
layersSet.window = value.window;
} }
} }
if (layersSet.window == NULL)
{
layersSet.window = [windows lastObject];
}
[layerStorage addObject: layersSet]; [layerStorage addObject: layersSet];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment