From cd187b6bee299357ea04c8cd1d64dc91a14cb860 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 21 Aug 2024 08:44:45 -0700 Subject: [PATCH] better tooltip styling --- .../editor/ToolTipComponent.kt | 85 ++++++++++++------- .../ContinuePluginSelectionListener.kt | 13 ++- 2 files changed, 66 insertions(+), 32 deletions(-) diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToolTipComponent.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToolTipComponent.kt index 9f6197892..3212eb27f 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToolTipComponent.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/editor/ToolTipComponent.kt @@ -1,24 +1,12 @@ -import com.github.continuedev.continueintellijextension.actions.FocusContinueInputWithoutClearAction import com.github.continuedev.continueintellijextension.actions.focusContinueInput import com.github.continuedev.continueintellijextension.editor.openInlineEdit -import com.intellij.openapi.actionSystem.ActionManager -import com.intellij.openapi.actionSystem.AnActionEvent -import com.intellij.openapi.actionSystem.DataContext -import com.intellij.openapi.actionSystem.ex.ActionUtil.invokeAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.LogicalPosition -import com.intellij.openapi.project.Project import com.intellij.ui.components.JBPanel +import java.awt.* import java.awt.event.ActionEvent import java.util.* import javax.swing.JButton -import java.awt.Cursor - -import javax.swing.BorderFactory -import java.awt.Color -import java.awt.Graphics -import java.awt.Graphics2D -import java.awt.RenderingHints import java.awt.geom.RoundRectangle2D class StyledButton(text: String) : JButton(text) { @@ -27,7 +15,7 @@ class StyledButton(text: String) : JButton(text) { isOpaque = false isContentAreaFilled = false isFocusPainted = false - border = BorderFactory.createEmptyBorder(5, 10, 5, 10) + border = null // Remove the border foreground = Color.WHITE } @@ -35,17 +23,20 @@ class StyledButton(text: String) : JButton(text) { val g2 = g.create() as Graphics2D g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) - val width = width - val height = height - val arc = 15 + val arc = 24f // Corner radius + val padding = 0f + val x = padding + val y = padding + val w = (width - 2 * padding) + val h = (height - 2 * padding) // Draw flat background g2.color = if (model.isPressed) Color(70, 70, 70) else Color(100, 100, 100) - g2.fill(RoundRectangle2D.Float(0f, 0f, width.toFloat(), height.toFloat(), arc.toFloat(), arc.toFloat())) +// g2.fill(RoundRectangle2D.Float(x, y, w, h, arc, arc)) // Draw border g2.color = Color(120, 120, 120) - g2.draw(RoundRectangle2D.Float(0f, 0f, width.toFloat() - 1, height.toFloat() - 1, arc.toFloat(), arc.toFloat())) +// g2.draw(RoundRectangle2D.Float(x, y, w - 1, h - 1, arc, arc)) g2.dispose() @@ -53,23 +44,28 @@ class StyledButton(text: String) : JButton(text) { } } - class ToolTipComponent(editor: Editor, line: Int, column: Int) : JBPanel() { + private lateinit var addToChatButton: StyledButton + private lateinit var editButton: StyledButton + init { layout = null // Remove the FlowLayout val cmdCtrlChar = if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("mac")) "⌘" else "Ctrl" - val addToChatButton = StyledButton("${cmdCtrlChar}J Add to chat") - val editButton = StyledButton("${cmdCtrlChar}I Edit") + addToChatButton = StyledButton("${cmdCtrlChar}J Add to chat") + editButton = StyledButton("${cmdCtrlChar}I Edit") - addToChatButton.addActionListener { e: ActionEvent? -> focusContinueInput(editor.project) } - editButton.addActionListener { e: ActionEvent? -> openInlineEdit(editor.project, editor) } + addToChatButton.addActionListener { e: ActionEvent? -> + focusContinueInput(editor.project) + editor.contentComponent.remove(this) + } + editButton.addActionListener { e: ActionEvent? -> + openInlineEdit(editor.project, editor) + editor.contentComponent.remove(this) + } - // Calculate total width of both buttons - val totalWidth = addToChatButton.preferredSize.width + editButton.preferredSize.width - - // Set bounds for buttons + // Set bounds for buttons to remove spacing addToChatButton.setBounds(0, 0, addToChatButton.preferredSize.width, addToChatButton.preferredSize.height) editButton.setBounds(addToChatButton.preferredSize.width, 0, editButton.preferredSize.width, editButton.preferredSize.height) @@ -79,10 +75,37 @@ class ToolTipComponent(editor: Editor, line: Int, column: Int) : val pos = LogicalPosition(line, column) val y: Int = editor.logicalPositionToXY(pos).y + editor.lineHeight val x: Int = editor.logicalPositionToXY(pos).x - setBounds(x, y, totalWidth, addToChatButton.preferredSize.height) + val totalWidth = addToChatButton.preferredSize.width + editButton.preferredSize.width + val totalHeight = addToChatButton.preferredSize.height + setBounds(x, y, totalWidth, totalHeight) // Make the background transparent isOpaque = false - background = java.awt.Color(0, 0, 0, 0) + background = Color(0, 0, 0, 0) } -} \ No newline at end of file + + override fun paintComponent(g: Graphics) { + val arc = 12f + + val g2 = g.create() as Graphics2D + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) + + // Draw rounded rectangle background + g2.color = Color(70, 70, 70, 200) + g2.fill(RoundRectangle2D.Float(0f, 0f, width.toFloat(), height.toFloat(), arc, arc)) + + // Draw rounded rectangle border + g2.color = Color(120, 120, 120) + g2.stroke = BasicStroke(1.0f) + g2.draw(RoundRectangle2D.Float(0f, 0f, width.toFloat(), height.toFloat(), arc, arc)) + + // Draw border between buttons + g2.color = Color(120, 120, 120) + g2.stroke = BasicStroke(1.0f) + val middleX = addToChatButton.width.toFloat() + g2.drawLine(middleX.toInt(), 0, middleX.toInt(), height) + + g2.dispose() + super.paintComponent(g) + } +} diff --git a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt index 7628bf669..5f3d687de 100644 --- a/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt +++ b/extensions/intellij/src/main/kotlin/com/github/continuedev/continueintellijextension/listeners/ContinuePluginSelectionListener.kt @@ -27,7 +27,18 @@ class ContinuePluginSelectionListener( ApplicationManager.getApplication().runReadAction { val editor = e.editor val model: SelectionModel = editor.selectionModel - val selectedText = model.selectedText ?: return@runReadAction + val selectedText = model.selectedText + + // If selected text is empty, remove the tooltip + if (selectedText.isNullOrEmpty()) { + ApplicationManager.getApplication().invokeLater { + toolTipComponent?.let { editor.contentComponent.remove(it) } + toolTipComponent = null + editor.contentComponent.revalidate() + editor.contentComponent.repaint() + } + return@runReadAction + } val document = editor.document val startOffset = model.selectionStart