From b356aa89cd18addc77310179c4ac2b526a8250e2 Mon Sep 17 00:00:00 2001 From: JFronny Date: Sat, 20 Jul 2024 23:31:55 +0200 Subject: [PATCH] fix: prevent some more issues --- .../jfronny/globalmenu/GlobalMenuService.kt | 10 +------- .../jfronny/globalmenu/proxy/DbusmenuImpl.kt | 25 +++++++++++++++---- .../jfronny/globalmenu/proxy/SwingMenu.kt | 9 +++---- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/io/gitlab/jfronny/globalmenu/GlobalMenuService.kt b/src/main/kotlin/io/gitlab/jfronny/globalmenu/GlobalMenuService.kt index 7825c02..719c08d 100644 --- a/src/main/kotlin/io/gitlab/jfronny/globalmenu/GlobalMenuService.kt +++ b/src/main/kotlin/io/gitlab/jfronny/globalmenu/GlobalMenuService.kt @@ -1,12 +1,9 @@ package io.gitlab.jfronny.globalmenu import com.canonical.appmenu.Registrar -import com.intellij.ide.IdeEventQueue import com.intellij.openapi.Disposable -import com.intellij.openapi.actionSystem.impl.ActionMenu import com.intellij.openapi.application.Application import com.intellij.openapi.application.ApplicationActivationListener -import com.intellij.openapi.progress.runBlockingCancellable import com.intellij.openapi.util.Disposer import com.intellij.openapi.wm.IdeFrame import com.intellij.openapi.wm.impl.IdeFrameImpl @@ -14,16 +11,11 @@ import com.intellij.openapi.wm.impl.ProjectFrameHelper import com.intellij.platform.ide.menu.IdeJMenuBar import io.gitlab.jfronny.globalmenu.proxy.DbusmenuImpl import io.gitlab.jfronny.globalmenu.proxy.SwingMenuHolder -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.runBlocking import org.freedesktop.dbus.DBusPath import org.freedesktop.dbus.connections.impl.DBusConnection import org.freedesktop.dbus.connections.impl.DBusConnectionBuilder import org.freedesktop.dbus.types.UInt32 -import java.awt.Window -import java.awt.event.KeyEvent import javax.swing.JMenuBar -import javax.swing.SwingUtilities class GlobalMenuService(private val app: Application) : ApplicationActivationListener { override fun applicationActivated(ideFrame: IdeFrame) { @@ -55,7 +47,7 @@ class GlobalMenuService(private val app: Application) : ApplicationActivationLis menu.addUpdateGlobalMenuRootsListener { menuHolder.update(menu.rootMenuItems) } - menu.updateMenuActions(true) +// menu.updateMenuActions(true) } // IdeEventQueue.getInstance().addDispatcher({ e -> // if (e !is KeyEvent) false diff --git a/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/DbusmenuImpl.kt b/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/DbusmenuImpl.kt index 9367e7a..b5907e7 100644 --- a/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/DbusmenuImpl.kt +++ b/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/DbusmenuImpl.kt @@ -17,10 +17,15 @@ class DbusmenuImpl(windowId: Long, private val menuHolder: MenuHolder) : Dbusmen // object : Dbusmenu.PropertyIconThemePathType, List by listOf() {} override fun GetLayout(parentId: Int, recursionDepth: Int, propertyNames: MutableList?): DPair { - return DPair( - UInt32(parentId.toLong()), - getLayout(parentId, recursionDepth, propertyNames, menuHolder.find(parentId)!!) - ) + try { + return DPair( + UInt32(parentId.toUInt().toLong()), + getLayout(parentId, recursionDepth, propertyNames, menuHolder.find(parentId)!!) + ) + } catch (e: Exception) { + GlobalMenu.Log.error("Failed to get layout for menu $parentId", e) + throw e + } } private fun getLayout(parentId: Int, recursionDepth: Int, propertyNames: MutableList?, menu: Menu): GetLayoutStruct { @@ -80,6 +85,16 @@ class DbusmenuImpl(windowId: Long, private val menuHolder: MenuHolder) : Dbusmen } override fun EventGroup(events: MutableList?): MutableList? = null // not needed? - override fun AboutToShow(id: Int): Boolean = true // not needed? + override fun AboutToShow(id: Int): Boolean { + GlobalMenu.Log.warn("About to show menu $id") + try { + menuHolder.find(id)?.update() + return true + } catch (e: Exception) { + GlobalMenu.Log.error("Failed to update menu $id", e) + throw e + } + + } override fun AboutToShowGroup(ids: MutableList?): DPair, MutableList>? = null // not needed? } diff --git a/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/SwingMenu.kt b/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/SwingMenu.kt index 0664029..168af7a 100644 --- a/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/SwingMenu.kt +++ b/src/main/kotlin/io/gitlab/jfronny/globalmenu/proxy/SwingMenu.kt @@ -84,14 +84,11 @@ class SwingMenu(private val menuItem: JMenuItem?, private val holder: SwingMenuH override val children: List? get() = _children override fun onEvent() { - val event = ActionEvent(menuItem, ActionEvent.ACTION_PERFORMED, menuItem!!.actionCommand) - ApplicationManager.getApplication().invokeLater { - for (it in menuItem.actionListeners) it.actionPerformed(event) - } + if (menuItem == null) return ApplicationManager.getApplication().invokeLater(menuItem::doClick) - GlobalMenu.Log.warn("Event $event for menu $id (${menuItem.javaClass})") when (menuItem) { is ActionMenuItem -> {} + is ActionMenu -> {} is JCheckBoxMenuItem -> menuItem.isSelected = !menuItem.isSelected is JRadioButtonMenuItem -> menuItem.isSelected = true } @@ -116,7 +113,7 @@ class SwingMenu(private val menuItem: JMenuItem?, private val holder: SwingMenuH } fun syncChildren(deepness: Int) { - GlobalMenu.Log.warn("Syncing children for $label") +// GlobalMenu.Log.warn("Syncing children for $label") _children = when (menuItem) { is ActionMenu -> { val ch = mutableListOf()