@@ -29,18 +29,6 @@ interface Foundation : Library {
2929
3030class MacOSWindowManager {
3131
32- companion object {
33- // Constants for NSApplication activation policies
34- const val NSApplicationActivationPolicyRegular = 0L
35- const val NSApplicationActivationPolicyAccessory = 1L
36- const val NSApplicationActivationPolicyProhibited = 2L
37-
38- // Constants for window levels
39- const val NSNormalWindowLevel = 0L
40- const val NSFloatingWindowLevel = 3L
41- const val NSModalPanelWindowLevel = 8L
42- }
43-
4432 // Detect platform once
4533 private val isMacOs: Boolean = getOperatingSystem() == OperatingSystem .MACOS
4634
@@ -206,5 +194,51 @@ class MacOSWindowManager {
206194 return getNSApplication() != null
207195 }
208196
197+ /* *
198+ * Configure an AWT window so that macOS moves it to the active Space
199+ * when it is ordered front, instead of switching back to the Space
200+ * where the window was originally created.
201+ */
202+ fun setMoveToActiveSpace (awtWindow : java.awt.Window ): Boolean {
203+ if (! isMacOs) return false
204+ val localObjc = objc ? : return false
205+ return try {
206+ val viewPtr = Native .getComponentID(awtWindow)
207+ if (viewPtr == 0L ) return false
208+
209+ val nsView = Pointer (viewPtr)
210+ val windowSel = localObjc.sel_registerName(" window" )
211+ val nsWindow = localObjc.objc_msgSend(nsView, windowSel)
212+ if (nsWindow == Pointer .NULL ) return false
213+
214+ // Read current collectionBehavior and add moveToActiveSpace (1 << 1)
215+ val getCollSel = localObjc.sel_registerName(" collectionBehavior" )
216+ val current = Pointer .nativeValue(localObjc.objc_msgSend(nsWindow, getCollSel))
217+ val setCollSel = localObjc.sel_registerName(" setCollectionBehavior:" )
218+ localObjc.objc_msgSend(nsWindow, setCollSel, current or NSWindowCollectionBehaviorMoveToActiveSpace )
219+
220+ debugln { " Window configured to move to active Space" }
221+ true
222+ } catch (e: Throwable ) {
223+ debugln { " Failed to set moveToActiveSpace: ${e.message} " }
224+ false
225+ }
226+ }
227+
228+ companion object {
229+ // Constants for NSApplication activation policies
230+ const val NSApplicationActivationPolicyRegular = 0L
231+ const val NSApplicationActivationPolicyAccessory = 1L
232+ const val NSApplicationActivationPolicyProhibited = 2L
233+
234+ // Constants for window levels
235+ const val NSNormalWindowLevel = 0L
236+ const val NSFloatingWindowLevel = 3L
237+ const val NSModalPanelWindowLevel = 8L
238+
239+ // NSWindowCollectionBehavior
240+ const val NSWindowCollectionBehaviorMoveToActiveSpace = 2L // 1 << 1
241+ }
242+
209243}
210244
0 commit comments