|
| 1 | +package com.kdroid.composetray.lib.mac |
| 2 | + |
| 3 | +import com.kdroid.composetray.utils.extractToTempIfDifferent |
| 4 | +import java.io.File |
| 5 | + |
| 6 | +/** |
| 7 | + * JNI bridge to the native macOS tray library (libMacTray.dylib). |
| 8 | + * Replaces the previous JNA direct-mapping approach. |
| 9 | + * All methods are static JNI calls into MacTrayBridge.m. |
| 10 | + */ |
| 11 | +internal object MacNativeBridge { |
| 12 | + |
| 13 | + init { |
| 14 | + loadNativeLibrary() |
| 15 | + } |
| 16 | + |
| 17 | + private fun loadNativeLibrary() { |
| 18 | + val arch = System.getProperty("os.arch") ?: "aarch64" |
| 19 | + val resourceDir = when { |
| 20 | + arch.contains("aarch64") || arch.contains("arm64") -> "darwin-aarch64" |
| 21 | + else -> "darwin-x86-64" |
| 22 | + } |
| 23 | + val resourcePath = "$resourceDir/libMacTray.dylib" |
| 24 | + |
| 25 | + // Try to find the dylib on the classpath (inside a JAR or on disk) |
| 26 | + val url = MacNativeBridge::class.java.classLoader?.getResource(resourcePath) |
| 27 | + if (url != null) { |
| 28 | + val protocol = url.protocol |
| 29 | + if (protocol == "jar") { |
| 30 | + // Extract from JAR to a temp file |
| 31 | + val tempFile = extractToTempIfDifferent(url.toString()) |
| 32 | + if (tempFile != null) { |
| 33 | + System.load(tempFile.absolutePath) |
| 34 | + return |
| 35 | + } |
| 36 | + } else if (protocol == "file") { |
| 37 | + // Direct file on disk (development mode) |
| 38 | + val file = File(url.toURI()) |
| 39 | + if (file.exists()) { |
| 40 | + System.load(file.absolutePath) |
| 41 | + return |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Fallback: let the JVM find it on java.library.path |
| 47 | + System.loadLibrary("MacTray") |
| 48 | + } |
| 49 | + |
| 50 | + // ── Tray lifecycle ────────────────────────────────────────────────── |
| 51 | + |
| 52 | + @JvmStatic external fun nativeCreateTray(iconPath: String, tooltip: String): Long |
| 53 | + @JvmStatic external fun nativeFreeTray(handle: Long) |
| 54 | + @JvmStatic external fun nativeSetTrayIcon(handle: Long, iconPath: String) |
| 55 | + @JvmStatic external fun nativeSetTrayTooltip(handle: Long, tooltip: String) |
| 56 | + @JvmStatic external fun nativeSetTrayCallback(handle: Long, callback: Runnable?) |
| 57 | + @JvmStatic external fun nativeSetTrayMenu(trayHandle: Long, menuHandle: Long) |
| 58 | + @JvmStatic external fun nativeClearTrayMenu(trayHandle: Long) |
| 59 | + @JvmStatic external fun nativeInitTray(handle: Long): Int |
| 60 | + @JvmStatic external fun nativeLoopTray(blocking: Int): Int |
| 61 | + @JvmStatic external fun nativeUpdateTray(handle: Long) |
| 62 | + @JvmStatic external fun nativeDisposeTray(handle: Long) |
| 63 | + @JvmStatic external fun nativeExitTray() |
| 64 | + |
| 65 | + // ── Menu items ────────────────────────────────────────────────────── |
| 66 | + |
| 67 | + @JvmStatic external fun nativeCreateMenuItems(count: Int): Long |
| 68 | + @JvmStatic external fun nativeSetMenuItem( |
| 69 | + menuHandle: Long, index: Int, |
| 70 | + text: String, iconPath: String?, |
| 71 | + disabled: Int, checked: Int |
| 72 | + ) |
| 73 | + @JvmStatic external fun nativeSetMenuItemCallback(menuHandle: Long, index: Int, callback: Runnable?) |
| 74 | + @JvmStatic external fun nativeSetMenuItemSubmenu(menuHandle: Long, index: Int, submenuHandle: Long) |
| 75 | + @JvmStatic external fun nativeFreeMenuItems(menuHandle: Long, count: Int) |
| 76 | + |
| 77 | + // ── Theme ─────────────────────────────────────────────────────────── |
| 78 | + |
| 79 | + @JvmStatic external fun nativeSetThemeCallback(callback: ThemeChangeCallback?) |
| 80 | + @JvmStatic external fun nativeIsMenuDark(): Int |
| 81 | + |
| 82 | + // ── Position ──────────────────────────────────────────────────────── |
| 83 | + |
| 84 | + /** Writes [x, y] into outXY. Returns 1 if precise, 0 if fallback. */ |
| 85 | + @JvmStatic external fun nativeGetStatusItemPosition(outXY: IntArray): Int |
| 86 | + @JvmStatic external fun nativeGetStatusItemRegion(): String |
| 87 | + @JvmStatic external fun nativeGetStatusItemPositionFor(handle: Long, outXY: IntArray): Int |
| 88 | + @JvmStatic external fun nativeGetStatusItemRegionFor(handle: Long): String |
| 89 | + |
| 90 | + // ── Appearance ────────────────────────────────────────────────────── |
| 91 | + |
| 92 | + @JvmStatic external fun nativeSetIconsForAppearance(handle: Long, lightIcon: String, darkIcon: String) |
| 93 | + |
| 94 | + // ── Window management ─────────────────────────────────────────────── |
| 95 | + |
| 96 | + @JvmStatic external fun nativeShowInDock(): Int |
| 97 | + @JvmStatic external fun nativeHideFromDock(): Int |
| 98 | + @JvmStatic external fun nativeSetMoveToActiveSpace() |
| 99 | + @JvmStatic external fun nativeSetMoveToActiveSpaceForWindow(viewPtr: Long): Int |
| 100 | + @JvmStatic external fun nativeIsFloatingWindowOnActiveSpace(): Int |
| 101 | + @JvmStatic external fun nativeBringFloatingWindowToFront(): Int |
| 102 | + @JvmStatic external fun nativeIsOnActiveSpaceForView(viewPtr: Long): Int |
| 103 | + |
| 104 | + // ── Mouse ─────────────────────────────────────────────────────────── |
| 105 | + |
| 106 | + @JvmStatic external fun nativeGetMouseButtonState(button: Int): Int |
| 107 | + |
| 108 | + // ── JAWT ──────────────────────────────────────────────────────────── |
| 109 | + |
| 110 | + /** Returns the NSView pointer for an AWT component, or 0 on failure. */ |
| 111 | + @JvmStatic external fun nativeGetAWTViewPtr(awtComponent: Any): Long |
| 112 | + |
| 113 | + // ── Callback interface ────────────────────────────────────────────── |
| 114 | + |
| 115 | + interface ThemeChangeCallback { |
| 116 | + fun onThemeChanged(isDark: Int) |
| 117 | + } |
| 118 | +} |
0 commit comments