[PyCharm] Jupyter (feat): "graphics.create() as Graphics2D"... replaced to short and proper useG2D. #PY-81847 Ready For Merge

GitOrigin-RevId: 7d7e4c5c51dfdc43de4bd3eb6f99029aa40f9902
This commit is contained in:
Nikita Pavlenko
2025-06-11 12:57:16 +02:00
committed by intellij-monorepo-bot
parent 8658a3d49b
commit 8425f70f1a
5 changed files with 26 additions and 21 deletions

View File

@@ -45,7 +45,7 @@ class NotebookBelowLastCellPanel(
}
private inner class HighlightableTopBorder(private val borderHeight: Int) : JBEmptyBorder(borderHeight, 0, 0, 0) {
override fun paintBorder(c: java.awt.Component?, g: Graphics?, x: Int, y: Int, width: Int, height: Int) {
override fun paintBorder(c: java.awt.Component?, g: Graphics, x: Int, y: Int, width: Int, height: Int) {
super.paintBorder(c, g, x, y, width, height)
if (isHighlighted) {
val g2d = g as Graphics2D

View File

@@ -10,6 +10,7 @@ import com.intellij.util.SmartList
import com.intellij.util.containers.ContainerUtil
import com.intellij.util.keyFMap.KeyFMap
import java.awt.Graphics
import java.awt.Graphics2D
import javax.swing.JComponent
import kotlin.math.max
import kotlin.reflect.KProperty
@@ -17,6 +18,16 @@ import kotlin.reflect.KProperty
infix fun IntRange.hasIntersectionWith(other: IntRange): Boolean =
!(first > other.last || last < other.first)
inline fun Graphics.useG2D(handler: (Graphics2D) -> Unit) {
val g2d = this.create() as Graphics2D
try {
handler(g2d)
}
finally {
dispose()
}
}
inline fun <T, G : Graphics> G.use(handler: (g: G) -> T): T =
try {
handler(this)

View File

@@ -2,6 +2,7 @@
package com.intellij.notebooks.visualization.ui
import com.intellij.notebooks.visualization.ui.providers.bounds.JupyterBoundsChangeHandler
import com.intellij.notebooks.visualization.useG2D
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.util.Key
@@ -67,19 +68,15 @@ class EditorComponentWrapper private constructor(private val editor: EditorImpl)
layer.setLayerEventMask(0)
}
override fun paint(graphics: Graphics, component: JComponent) {
super.paint(graphics, component)
override fun paint(g: Graphics, c: JComponent) {
super.paint(g, c)
val g2d = graphics.create() as Graphics2D
try {
g.useG2D { g2d ->
for ((line, color) in overlayLines) {
g2d.color = color
g2d.draw(line)
}
}
finally {
g2d.dispose()
}
}
override fun processMouseEvent(e: MouseEvent, layer: JLayer<out Component>) {

View File

@@ -2,6 +2,7 @@
package com.intellij.notebooks.visualization.ui.jupyterToolbars
import com.intellij.notebooks.ui.SelectClickedCellEventHelper
import com.intellij.notebooks.visualization.useG2D
import com.intellij.openapi.actionSystem.ActionGroup
import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.AnAction
@@ -47,19 +48,15 @@ abstract class JupyterAbstractAboveCellToolbar(
}
override fun paintComponent(g: Graphics) {
val g2 = g.create() as Graphics2D
try {
g2.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ALPHA)
g2.color = this.background
fillRect(g2)
}
finally {
g2.dispose()
g.useG2D { g2d ->
g2d.composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ALPHA)
g2d.color = this.background
fillRect(g2d)
}
}
protected open fun fillRect(g2: Graphics2D) {
g2.fillRoundRect(0, 0, width, height, getArcSize(), getArcSize())
protected open fun fillRect(g2d: Graphics2D) {
g2d.fillRoundRect(0, 0, width, height, getArcSize(), getArcSize())
}
override fun updateUI() {

View File

@@ -23,7 +23,7 @@ class JupyterCellActionsToolbar(
background = EditorColorsManager.getInstance().globalScheme.defaultBackground
}
override fun fillRect(g2: Graphics2D) {
override fun fillRect(g2d: Graphics2D) {
val arcSize = getArcSize()
val shape = RoundRectangle2D.Float(
TOOLBAR_BORDER_THICKNESS / 2f, TOOLBAR_BORDER_THICKNESS / 2f,
@@ -31,8 +31,8 @@ class JupyterCellActionsToolbar(
arcSize.toFloat(), arcSize.toFloat()
)
g2.clip(shape)
g2.fill(shape)
g2d.clip(shape)
g2d.fill(shape)
}
override fun getArcSize(): Int = JBUI.scale(8)