Toggle Switches

This commit is contained in:
downthecrop 2025-08-13 03:02:56 -07:00
parent ac2478d10f
commit f386c464f4
2 changed files with 157 additions and 45 deletions

View file

@ -3,6 +3,7 @@ package KondoKit
import KondoKit.Helpers.convertValue import KondoKit.Helpers.convertValue
import KondoKit.Helpers.showToast import KondoKit.Helpers.showToast
import KondoKit.ScrollablePanel import KondoKit.ScrollablePanel
import KondoKit.ToggleSwitch
import KondoKit.plugin.Companion.TITLE_BAR_COLOR import KondoKit.plugin.Companion.TITLE_BAR_COLOR
import KondoKit.plugin.Companion.TOOLTIP_BACKGROUND import KondoKit.plugin.Companion.TOOLTIP_BACKGROUND
import KondoKit.plugin.Companion.VIEW_BACKGROUND_COLOR import KondoKit.plugin.Companion.VIEW_BACKGROUND_COLOR
@ -146,36 +147,18 @@ object ReflectiveEditorView {
return panel return panel
} }
private fun createToggleSwitch(plugin: Plugin, pluginInfo: PluginInfo): JPanel { private fun createToggleSwitch(plugin: Plugin, pluginInfo: PluginInfo): ToggleSwitch {
val switchPanel = JPanel(null) // null layout for precise positioning val toggleSwitch = ToggleSwitch()
switchPanel.background = WIDGET_COLOR
switchPanel.preferredSize = Dimension(50, 25)
switchPanel.maximumSize = Dimension(50, 25)
switchPanel.minimumSize = Dimension(50, 25)
// Track (background) // Set initial state
val track = JPanel() toggleSwitch.setActivated(isPluginEnabled(pluginInfo))
track.background = if (isPluginEnabled(pluginInfo)) Color(0, 122, 255) else Color(142, 142, 147)
track.setBounds(0, 0, 50, 25)
track.border = RoundedBorder(12)
// Thumb (slider) // Add toggle listener
val thumb = JPanel() toggleSwitch.onToggleListener = { activated ->
thumb.background = Color.WHITE togglePlugin(pluginInfo, plugin, toggleSwitch, activated)
thumb.setBounds(if (isPluginEnabled(pluginInfo)) 27 else 3, 3, 20, 20) }
thumb.border = RoundedBorder(10)
switchPanel.add(track) return toggleSwitch
switchPanel.add(thumb)
// Add click handler to toggle
switchPanel.addMouseListener(object : MouseAdapter() {
override fun mouseClicked(e: MouseEvent) {
togglePlugin(pluginInfo, plugin, track, thumb)
}
})
return switchPanel
} }
private fun isPluginEnabled(pluginInfo: PluginInfo): Boolean { private fun isPluginEnabled(pluginInfo: PluginInfo): Boolean {
@ -184,25 +167,9 @@ object ReflectiveEditorView {
return true return true
} }
private fun togglePlugin(pluginInfo: PluginInfo, plugin: Plugin, track: JPanel, thumb: JPanel) { private fun togglePlugin(pluginInfo: PluginInfo, plugin: Plugin, toggleSwitch: ToggleSwitch, activated: Boolean) {
// 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()
// In a full implementation, you would disable/enable the plugin here // 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) { private fun showPluginDetails(pluginInfo: PluginInfo, plugin: Plugin) {

View file

@ -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
}
}