Skip to content

Comments

Replace JNA with JNI for macOS native bridge#375

Open
kdroidFilter wants to merge 1 commit intomasterfrom
refactor/macos-jna-to-jni
Open

Replace JNA with JNI for macOS native bridge#375
kdroidFilter wants to merge 1 commit intomasterfrom
refactor/macos-jna-to-jni

Conversation

@kdroidFilter
Copy link
Owner

Summary

  • Replace all JNA (Java Native Access) usage with JNI (Java Native Interface) for macOS, reducing the dependency surface. JNA remains for Windows/Linux.
  • Create an Objective-C JNI bridge (MacTrayBridge.m) that wraps the existing Swift C API with proper callback trampolines, GlobalRef lifecycle management, and dynamic JAWT resolution.
  • Add new native Swift functions for dock visibility, Space management, and mouse button state — previously done via JNA ObjC runtime calls from Kotlin.
  • Rewrite MacTrayManager, MacOsWindowManager, MacOutsideClickWatcher, and related files to use opaque Long handles instead of JNA Structure objects.

Changed files

File Action
maclib/MacTrayBridge.m New — JNI bridge (33 native methods)
MacNativeBridge.kt New — Kotlin JNI binding with auto library loading
maclib/tray.h Added 8 C function declarations
maclib/tray.swift Added 7 @_cdecl functions (~80 lines)
maclib/build.sh Rewritten to compile ObjC bridge with JNI headers
MacTrayManager.kt Rewritten — opaque handles, no JNA
MacOsWindowManager.kt Rewritten — native bridge instead of ObjC runtime
MacOutsideClickWatcher.kt Simplified — removed ApplicationServices JNA
MacOSMenuBarThemeDetector.kt Adapted to use MacNativeBridge
MacTrayInitializer.kt getNativeTrayStruct()getNativeTrayHandle()
TrayPosition.kt IntByReferenceIntArray, uses MacNativeBridge
TrayApp.kt One-line change: MacNativeBridge.nativeSetMoveToActiveSpace()
MacTrayLoader.kt Deleted

Test plan

  • cd maclib && sh build.sh — both architectures compile
  • nm -g libMacTray.dylib | grep Java_ — all 33 JNI symbols exported
  • ./gradlew compileKotlinJvm — no compilation errors
  • Run demo app on macOS and verify:
    • Tray icon appears with tooltip
    • Left-click callback works
    • Context menu with items, checkables, submenus
    • Menu item callbacks fire correctly
    • Theme change detection (dark/light)
    • Outside click dismiss
    • Show/hide from dock
    • Window positioning near tray icon
    • Space switching works
    • Multi-tray instances

Migrate all macOS JNA (Java Native Access) bindings to JNI (Java Native
Interface) to reduce dependency surface on macOS. JNA remains for
Windows/Linux.

- Create MacTrayBridge.m: Objective-C JNI bridge (~420 lines) with
  callback trampolines, GlobalRef lifecycle management, and JAWT support
- Create MacNativeBridge.kt: Kotlin JNI binding with auto library loading
- Add 7 new Swift @_cdecl functions for dock, space, and mouse APIs
- Rewrite MacTrayManager to use opaque Long handles instead of JNA Structures
- Rewrite MacOsWindowManager to use native bridge instead of ObjC runtime
- Simplify MacOutsideClickWatcher (remove ApplicationServices JNA binding)
- Update build.sh to compile ObjC bridge with JNI headers
- Delete MacTrayLoader.kt (no longer needed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant