diff --git a/plugin-playground/src/main/kotlin/KondoKit/ComponentExtensions.kt b/plugin-playground/src/main/kotlin/KondoKit/ComponentExtensions.kt index ecb57ce..23d05f2 100644 --- a/plugin-playground/src/main/kotlin/KondoKit/ComponentExtensions.kt +++ b/plugin-playground/src/main/kotlin/KondoKit/ComponentExtensions.kt @@ -1,7 +1,11 @@ package KondoKit import java.awt.Component +import java.awt.Container import java.awt.Dimension +import java.awt.event.MouseAdapter +import java.awt.event.MouseEvent +import javax.swing.JPopupMenu fun T.setFixedSize(width: Int, height: Int): T = setFixedSize(Dimension(width, height)) @@ -12,3 +16,23 @@ fun T.setFixedSize(size: Dimension): T { maximumSize = size return this } + +fun Component.attachPopupMenu(popupMenu: JPopupMenu, includeChildren: Boolean = false) { + val listener = object : MouseAdapter() { + private fun maybeShow(e: MouseEvent) { + if (e.isPopupTrigger) { + popupMenu.show(e.component, e.x, e.y) + } + } + + override fun mousePressed(e: MouseEvent) = maybeShow(e) + + override fun mouseReleased(e: MouseEvent) = maybeShow(e) + } + + addMouseListener(listener) + + if (includeChildren && this is Container) { + Helpers.addMouseListenerToAll(this, listener) + } +} diff --git a/plugin-playground/src/main/kotlin/KondoKit/views/LootTrackerView.kt b/plugin-playground/src/main/kotlin/KondoKit/views/LootTrackerView.kt index f085307..d025106 100644 --- a/plugin-playground/src/main/kotlin/KondoKit/views/LootTrackerView.kt +++ b/plugin-playground/src/main/kotlin/KondoKit/views/LootTrackerView.kt @@ -1,12 +1,12 @@ package KondoKit.views import KondoKit.Helpers -import KondoKit.Helpers.addMouseListenerToAll import KondoKit.Helpers.formatHtmlLabelText import KondoKit.ImageCanvas import KondoKit.SpriteToBufferedImage.getBufferedImageFromSprite import KondoKit.ViewConstants import KondoKit.setFixedSize +import KondoKit.attachPopupMenu import KondoKit.views.XPTrackerView.wrappedWidget import KondoKit.components.PopupMenuComponent import KondoKit.components.ProgressBar @@ -139,23 +139,7 @@ object LootTrackerView : View, OnPostClientTickCallback, OnKillingBlowNPCCallbac val wrapped = wrappedWidget(totalTrackerWidget!!.container, padding = 0) val popupMenu = resetLootTrackerMenu() - - // Create a custom MouseListener - val rightClickListener = object : MouseAdapter() { - override fun mousePressed(e: MouseEvent) { - if (e.isPopupTrigger) { - popupMenu.show(e.component, e.x, e.y) - } - } - - override fun mouseReleased(e: MouseEvent) { - if (e.isPopupTrigger) { - popupMenu.show(e.component, e.x, e.y) - } - } - } - addMouseListenerToAll(wrapped,rightClickListener) - wrapped.addMouseListener(rightClickListener) + wrapped.attachPopupMenu(popupMenu, includeChildren = true) add(wrapped) add(Box.createVerticalStrut(8)) revalidate() @@ -504,23 +488,7 @@ object LootTrackerView : View, OnPostClientTickCallback, OnKillingBlowNPCCallbac childFramePanel.add(lootPanel) val popupMenu = removeLootFrameMenu(childFramePanel, npcName) - - // Create a custom MouseListener - val rightClickListener = object : MouseAdapter() { - override fun mousePressed(e: MouseEvent) { - if (e.isPopupTrigger) { - popupMenu.show(e.component, e.x, e.y) - } - } - - override fun mouseReleased(e: MouseEvent) { - if (e.isPopupTrigger) { - popupMenu.show(e.component, e.x, e.y) - } - } - } - - labelPanel.addMouseListener(rightClickListener) + labelPanel.attachPopupMenu(popupMenu) return childFramePanel } @@ -570,23 +538,7 @@ object LootTrackerView : View, OnPostClientTickCallback, OnKillingBlowNPCCallbac val wrapped = wrappedWidget(totalTrackerWidget!!.container, padding = 0) val _popupMenu = resetLootTrackerMenu() - - // Create a custom MouseListener - val rightClickListener = object : MouseAdapter() { - override fun mousePressed(e: MouseEvent) { - if (e.isPopupTrigger) { - _popupMenu.show(e.component, e.x, e.y) - } - } - - override fun mouseReleased(e: MouseEvent) { - if (e.isPopupTrigger) { - _popupMenu.show(e.component, e.x, e.y) - } - } - } - addMouseListenerToAll(wrapped,rightClickListener) - wrapped.addMouseListener(rightClickListener) + wrapped.attachPopupMenu(_popupMenu, includeChildren = true) lootTrackerView?.add(Box.createVerticalStrut(5)) lootTrackerView?.add(wrapped) lootTrackerView?.add(Box.createVerticalStrut(8)) diff --git a/plugin-playground/src/main/kotlin/KondoKit/views/ReflectiveEditorView.kt b/plugin-playground/src/main/kotlin/KondoKit/views/ReflectiveEditorView.kt index 1390852..f9f30a7 100644 --- a/plugin-playground/src/main/kotlin/KondoKit/views/ReflectiveEditorView.kt +++ b/plugin-playground/src/main/kotlin/KondoKit/views/ReflectiveEditorView.kt @@ -5,6 +5,7 @@ import KondoKit.components.* import KondoKit.ViewConstants import KondoKit.views.ViewLayoutHelpers.createSearchFieldSection import KondoKit.setFixedSize +import KondoKit.attachPopupMenu import KondoKit.ReflectiveEditorPlugin import KondoKit.pluginmanager.GitLabPlugin import KondoKit.pluginmanager.GitLabPluginFetcher @@ -22,8 +23,6 @@ import plugin.PluginInfo import plugin.PluginRepository import rt4.GlobalJsonConfig import java.awt.* -import java.awt.event.MouseAdapter -import java.awt.event.MouseEvent import java.io.File import java.util.* import javax.swing.* @@ -274,15 +273,7 @@ object ReflectiveEditorView : View { } private fun createPluginItemPanel(pluginInfo: PluginInfo, plugin: Plugin): JPanel { - val panel = WidgetPanel( - widgetWidth = ViewConstants.PLUGIN_LIST_ITEM_SIZE.width, - widgetHeight = ViewConstants.PLUGIN_LIST_ITEM_SIZE.height, - paddingLeft = 10, - paddingRight = 10, - paddingTop = 10, - paddingBottom = 10, - addDefaultPadding = false - ).apply { + val panel = createPluginWidgetPanel().apply { layout = BorderLayout() } @@ -336,22 +327,14 @@ object ReflectiveEditorView : View { if (!isKondoKit(packageName)) { val popupMenu = createPluginContextMenu(plugin, pluginInfo, packageName, false) - addContextMenuToPanel(panel, popupMenu) + panel.attachPopupMenu(popupMenu) } return panel } private fun createDisabledPluginItemPanel(pluginName: String): JPanel { - val panel = WidgetPanel( - widgetWidth = ViewConstants.PLUGIN_LIST_ITEM_SIZE.width, - widgetHeight = ViewConstants.PLUGIN_LIST_ITEM_SIZE.height, - paddingLeft = 10, - paddingRight = 10, - paddingTop = 10, - paddingBottom = 10, - addDefaultPadding = false - ).apply { + val panel = createPluginWidgetPanel().apply { layout = BorderLayout() } @@ -388,22 +371,14 @@ object ReflectiveEditorView : View { // Add right-click context menu (except for KondoKit itself) if (!isKondoKit(pluginName)) { val popupMenu = createPluginContextMenu(null, null, pluginName, true) - addContextMenuToPanel(panel, popupMenu) + panel.attachPopupMenu(popupMenu) } return panel } private fun createPluginStatusItemPanel(pluginStatus: PluginInfoWithStatus): JPanel { - val panel = WidgetPanel( - widgetWidth = ViewConstants.PLUGIN_LIST_ITEM_SIZE.width, - widgetHeight = ViewConstants.PLUGIN_LIST_ITEM_SIZE.height, - paddingLeft = 10, - paddingRight = 10, - paddingTop = 10, - paddingBottom = 10, - addDefaultPadding = false - ).apply { + val panel = createPluginWidgetPanel().apply { layout = BorderLayout() } @@ -557,12 +532,24 @@ object ReflectiveEditorView : View { if (!isKondoKit(pluginStatus.name) && pluginStatus.isInstalled) { val popupMenu = createPluginContextMenu(null, null, pluginStatus.name, !pluginStatus.needsUpdate) - addContextMenuToPanel(panel, popupMenu) + panel.attachPopupMenu(popupMenu) } return panel } + private fun createPluginWidgetPanel(): WidgetPanel { + return WidgetPanel( + widgetWidth = ViewConstants.PLUGIN_LIST_ITEM_SIZE.width, + widgetHeight = ViewConstants.PLUGIN_LIST_ITEM_SIZE.height, + addDefaultPadding = false, + paddingTop = 10, + paddingLeft = 10, + paddingBottom = 10, + paddingRight = 10 + ) + } + private fun buildInstallableTooltip(pluginStatus: PluginInfoWithStatus): String { val lines = mutableListOf() pluginStatus.remoteVersion?.takeIf { it.isNotBlank() }?.let { @@ -801,24 +788,6 @@ object ReflectiveEditorView : View { reflectiveEditorPlugin.startPluginDownload(pluginStatus, mainPanel ?: JPanel()) } - // Helper method to add context menu to a panel - private fun addContextMenuToPanel(panel: JPanel, popupMenu: JPopupMenu) { - val rightClickListener = object : MouseAdapter() { - override fun mousePressed(e: MouseEvent) { - if (e.isPopupTrigger) { - popupMenu.show(e.component, e.x, e.y) - } - } - - override fun mouseReleased(e: MouseEvent) { - if (e.isPopupTrigger) { - popupMenu.show(e.component, e.x, e.y) - } - } - } - panel.addMouseListener(rightClickListener) - } - // Helper method to create context menu for plugins private fun createPluginContextMenu(plugin: Plugin?, pluginInfo: PluginInfo?, pluginName: String, isDisabled: Boolean): JPopupMenu { val popupMenu = PopupMenuComponent() diff --git a/plugin-playground/src/main/kotlin/KondoKit/views/XPTrackerView.kt b/plugin-playground/src/main/kotlin/KondoKit/views/XPTrackerView.kt index 498647a..d207cd1 100644 --- a/plugin-playground/src/main/kotlin/KondoKit/views/XPTrackerView.kt +++ b/plugin-playground/src/main/kotlin/KondoKit/views/XPTrackerView.kt @@ -1,7 +1,6 @@ package KondoKit.views import KondoKit.Helpers -import KondoKit.Helpers.addMouseListenerToAll import KondoKit.Helpers.formatHtmlLabelText import KondoKit.Helpers.formatNumber import KondoKit.Helpers.getProgressBarColor @@ -9,6 +8,7 @@ import KondoKit.Helpers.getSpriteId import KondoKit.SpriteToBufferedImage.getBufferedImageFromSprite import KondoKit.ViewConstants import KondoKit.setFixedSize +import KondoKit.attachPopupMenu import KondoKit.XPTable import KondoKit.components.PopupMenuComponent import KondoKit.components.ProgressBar @@ -26,8 +26,6 @@ import KondoKit.plugin.StateManager.focusedView import plugin.api.API import java.awt.* import java.awt.image.BufferedImage -import java.awt.event.MouseAdapter -import java.awt.event.MouseEvent import javax.swing.* import javax.swing.SwingConstants @@ -66,22 +64,6 @@ object XPTrackerView : View, OnUpdateCallback, OnXPUpdateCallback { private val widgetFont = ViewConstants.FONT_RUNESCAPE_SMALL_16 - private fun createPopupListener(popupMenu: JPopupMenu) = object : MouseAdapter() { - override fun mousePressed(e: MouseEvent) { - if (e.isPopupTrigger) popupMenu.show(e.component, e.x, e.y) - } - - override fun mouseReleased(e: MouseEvent) { - if (e.isPopupTrigger) popupMenu.show(e.component, e.x, e.y) - } - } - - private fun attachPopup(component: Container, popupMenu: JPopupMenu) { - val listener = createPopupListener(popupMenu) - addMouseListenerToAll(component, listener) - component.addMouseListener(listener) - } - private fun BufferedImage.ensureOpaque(): BufferedImage { for (y in 0 until height) { for (x in 0 until width) { @@ -133,7 +115,9 @@ object XPTrackerView : View, OnUpdateCallback, OnXPUpdateCallback { private fun createTotalWidgetContainer(popupMenu: JPopupMenu): Container { totalXPWidget = createTotalXPWidget() - return wrappedWidget(totalXPWidget!!.container, padding = 0).also { attachPopup(it, popupMenu) } + return wrappedWidget(totalXPWidget!!.container, padding = 0).also { + it.attachPopupMenu(popupMenu, includeChildren = true) + } } override val panel: JPanel @@ -190,7 +174,7 @@ object XPTrackerView : View, OnUpdateCallback, OnXPUpdateCallback { val wrapped = wrappedWidget(xpWidget.container) val popupMenu = removeXPWidgetMenu(wrapped, skillId) - attachPopup(wrapped, popupMenu) + wrapped.attachPopupMenu(popupMenu, includeChildren = true) xpTrackerView?.add(wrapped) xpTrackerView?.add(Box.createVerticalStrut(5))