Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active May 26, 2025 15:40
Show Gist options
  • Save dkandalov/cd7e987ac171d18d79637f6d6c66bea0 to your computer and use it in GitHub Desktop.
Save dkandalov/cd7e987ac171d18d79637f6d6c66bea0 to your computer and use it in GitHub Desktop.
Mini-plugin with refactorings counter popup for the "47 refactorings in 45 minutes" talk (to be used via LivePlugin)
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.*
import com.intellij.openapi.util.Disposer
import com.intellij.ui.JBColor
import com.intellij.ui.awt.RelativePoint
import liveplugin.*
import java.awt.Point
// https://gist.github.com/dkandalov/cd7e987ac171d18d79637f6d6c66bea0
var counter = 0
var balloon: Balloon? = null
registerAction("Increment refactorings counter", keyStroke = "meta alt shift F12") {
balloon?.let(Disposer::dispose)
balloon = createBalloon(++counter).showIn(it.project)
}
registerAction("Decrement refactorings counter", keyStroke = "meta alt shift F11") {
balloon?.let(Disposer::dispose)
counter = (counter - 1).coerceAtLeast(0)
balloon = createBalloon(counter).showIn(it.project)
}
registerAction("Toggle refactorings counter", keyStroke = "meta alt shift F9") {
if (balloon == null || balloon!!.wasFadedOut()) {
balloon = createBalloon(counter).showIn(it.project)
} else {
balloon!!.hide()
}
}
fun createBalloon(counter: Int) =
JBPopupFactory.getInstance()
.createHtmlTextBalloonBuilder(
/* htmlContent = */ "<span style='font-size: 32pt; font-weight:bold'>Refactoring counter: $counter</span>",
/* icon = */ null,
/* fillColor = */ JBColor.background(),
/* listener = */ null
)
.setBorderColor(JBColor.gray)
.setFadeoutTime(3000) // We could use this to make popup disappear after a time-out
.setAnimationCycle(50)
.setCloseButtonEnabled(false)
.setHideOnClickOutside(false)
.setDisposable(pluginDisposable)
.setHideOnFrameResize(false)
.setHideOnKeyOutside(false)
.setBlockClicksThroughBalloon(true)
.setHideOnAction(false)
.setShowCallout(false)
.setCornerRadius(15)
.createBalloon()
fun Balloon.showIn(project: Project?) = apply {
val component = project?.currentEditor?.component ?: return@apply
show(
RelativePoint(component, Point(component.width, component.height - 100)),
Balloon.Position.above
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment