diff --git a/plugin-playground/src/main/kotlin/KondoKit/ReflectiveEditorView.kt b/plugin-playground/src/main/kotlin/KondoKit/ReflectiveEditorView.kt index 2e5409c..dacb58a 100644 --- a/plugin-playground/src/main/kotlin/KondoKit/ReflectiveEditorView.kt +++ b/plugin-playground/src/main/kotlin/KondoKit/ReflectiveEditorView.kt @@ -3,6 +3,7 @@ package KondoKit import KondoKit.Helpers.convertValue import KondoKit.Helpers.showToast import KondoKit.ScrollablePanel +import KondoKit.ToggleSwitch import KondoKit.plugin.Companion.TITLE_BAR_COLOR import KondoKit.plugin.Companion.TOOLTIP_BACKGROUND import KondoKit.plugin.Companion.VIEW_BACKGROUND_COLOR @@ -146,36 +147,18 @@ object ReflectiveEditorView { return panel } - private fun createToggleSwitch(plugin: Plugin, pluginInfo: PluginInfo): JPanel { - val switchPanel = JPanel(null) // null layout for precise positioning - switchPanel.background = WIDGET_COLOR - switchPanel.preferredSize = Dimension(50, 25) - switchPanel.maximumSize = Dimension(50, 25) - switchPanel.minimumSize = Dimension(50, 25) + private fun createToggleSwitch(plugin: Plugin, pluginInfo: PluginInfo): ToggleSwitch { + val toggleSwitch = ToggleSwitch() - // Track (background) - val track = JPanel() - track.background = if (isPluginEnabled(pluginInfo)) Color(0, 122, 255) else Color(142, 142, 147) - track.setBounds(0, 0, 50, 25) - track.border = RoundedBorder(12) + // Set initial state + toggleSwitch.setActivated(isPluginEnabled(pluginInfo)) - // Thumb (slider) - val thumb = JPanel() - thumb.background = Color.WHITE - thumb.setBounds(if (isPluginEnabled(pluginInfo)) 27 else 3, 3, 20, 20) - thumb.border = RoundedBorder(10) + // Add toggle listener + toggleSwitch.onToggleListener = { activated -> + togglePlugin(pluginInfo, plugin, toggleSwitch, activated) + } - switchPanel.add(track) - switchPanel.add(thumb) - - // Add click handler to toggle - switchPanel.addMouseListener(object : MouseAdapter() { - override fun mouseClicked(e: MouseEvent) { - togglePlugin(pluginInfo, plugin, track, thumb) - } - }) - - return switchPanel + return toggleSwitch } private fun isPluginEnabled(pluginInfo: PluginInfo): Boolean { @@ -184,25 +167,9 @@ object ReflectiveEditorView { return true } - private fun togglePlugin(pluginInfo: PluginInfo, plugin: Plugin, track: JPanel, thumb: JPanel) { - // Simple toggle animation - val isEnabled = track.background == Color(0, 122, 255) - - if (isEnabled) { - // Toggle off - track.background = Color(142, 142, 147) - thumb.setLocation(3, 3) - } else { - // Toggle on - track.background = Color(0, 122, 255) - thumb.setLocation(27, 3) - } - - track.repaint() - thumb.repaint() - + private fun togglePlugin(pluginInfo: PluginInfo, plugin: Plugin, toggleSwitch: ToggleSwitch, activated: Boolean) { // In a full implementation, you would disable/enable the plugin here - showToast(mainPanel, if (isEnabled) "Plugin disabled" else "Plugin enabled") + showToast(mainPanel, if (activated) "Plugin enabled" else "Plugin disabled") } private fun showPluginDetails(pluginInfo: PluginInfo, plugin: Plugin) { diff --git a/plugin-playground/src/main/kotlin/KondoKit/ToggleSwitch.kt b/plugin-playground/src/main/kotlin/KondoKit/ToggleSwitch.kt new file mode 100644 index 0000000..5784435 --- /dev/null +++ b/plugin-playground/src/main/kotlin/KondoKit/ToggleSwitch.kt @@ -0,0 +1,145 @@ +package KondoKit + +import java.awt.* +import java.awt.event.MouseAdapter +import java.awt.event.MouseEvent +import java.awt.image.BufferedImage +import javax.swing.JPanel + +class ToggleSwitch : JPanel() { + private var activated = false + private var switchColor = Color(200, 200, 200) + private var buttonColor = Color(255, 255, 255) + private var borderColor = Color(50, 50, 50) + private var activeSwitch = Color(0, 125, 255) + private var puffer: BufferedImage? = null + private var borderRadius = 10 + private var g: Graphics2D? = null + + var onToggleListener: ((Boolean) -> Unit)? = null + + init { + isVisible = true + addMouseListener(object : MouseAdapter() { + override fun mouseReleased(arg0: MouseEvent) { + activated = !activated + onToggleListener?.invoke(activated) + repaint() + } + }) + cursor = Cursor(Cursor.HAND_CURSOR) + preferredSize = Dimension(41, 21) + maximumSize = Dimension(41, 21) + minimumSize = Dimension(41, 21) + } + + override fun paint(gr: Graphics) { + if (g == null || puffer?.width != width || puffer?.height != height) { + puffer = BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) + g = puffer?.createGraphics() + val rh = RenderingHints( + RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON + ) + g?.setRenderingHints(rh) + } + + g?.color = if (activated) activeSwitch else switchColor + g?.fillRoundRect(0, 0, width - 1, height - 1, 5, borderRadius) + g?.color = borderColor + g?.drawRoundRect(0, 0, width - 1, height - 1, 5, borderRadius) + g?.color = buttonColor + + if (activated) { + g?.fillRoundRect( + width / 2, 1, + (width - 1) / 2 - 2, (height - 1) - 2, + borderRadius, borderRadius + ) + g?.color = borderColor + g?.drawRoundRect( + (width - 1) / 2, 0, + (width - 1) / 2, (height - 1), + borderRadius, borderRadius + ) + } else { + g?.fillRoundRect( + 1, 1, + (width - 1) / 2 - 2, (height - 1) - 2, + borderRadius, borderRadius + ) + g?.color = borderColor + g?.drawRoundRect( + 0, 0, + (width - 1) / 2, (height - 1), + borderRadius, borderRadius + ) + } + + gr.drawImage(puffer, 0, 0, null) + } + + fun isActivated(): Boolean { + return activated + } + + fun setActivated(activated: Boolean) { + this.activated = activated + repaint() + } + + fun getSwitchColor(): Color { + return switchColor + } + + /** + * Unactivated Background Color of switch + */ + fun setSwitchColor(switchColor: Color) { + this.switchColor = switchColor + } + + fun getButtonColor(): Color { + return buttonColor + } + + /** + * Switch-Button color + */ + fun setButtonColor(buttonColor: Color) { + this.buttonColor = buttonColor + } + + fun getBorderColor(): Color { + return borderColor + } + + /** + * Border-color of whole switch and switch-button + */ + fun setBorderColor(borderColor: Color) { + this.borderColor = borderColor + } + + fun getActiveSwitch(): Color { + return activeSwitch + } + + fun setActiveSwitch(activeSwitch: Color) { + this.activeSwitch = activeSwitch + } + + /** + * @return the borderRadius + */ + fun getBorderRadius(): Int { + return borderRadius + } + + /** + * @param borderRadius the borderRadius to set + */ + fun setBorderRadius(borderRadius: Int) { + this.borderRadius = borderRadius + } +} \ No newline at end of file