improved tooltip

This commit is contained in:
Test 2024-08-22 16:59:20 -07:00
parent 4c1adbc734
commit 71de6c8d16
2 changed files with 48 additions and 16 deletions

View file

@ -2,6 +2,7 @@ import com.github.continuedev.continueintellijextension.actions.focusContinueInp
import com.github.continuedev.continueintellijextension.editor.openInlineEdit
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.ui.components.JBPanel
import java.awt.*
import java.awt.event.ActionEvent
@ -9,14 +10,14 @@ import java.util.*
import javax.swing.JButton
import java.awt.geom.RoundRectangle2D
class StyledButton(text: String) : JButton(text) {
class StyledButton(text: String, foregroundColor: Color) : JButton(text) {
init {
cursor = Cursor(Cursor.HAND_CURSOR)
isOpaque = false
isContentAreaFilled = false
isFocusPainted = false
border = null // Remove the border
foreground = Color.WHITE
foreground = foregroundColor
}
override fun paintComponent(g: Graphics) {
@ -31,11 +32,11 @@ class StyledButton(text: String) : JButton(text) {
val h = (height - 2 * padding)
// Draw flat background
g2.color = if (model.isPressed) Color(70, 70, 70) else Color(100, 100, 100)
// g2.color = if (model.isPressed) Color(70, 70, 70) else Color(100, 100, 100)
// g2.fill(RoundRectangle2D.Float(x, y, w, h, arc, arc))
// Draw border
g2.color = Color(120, 120, 120)
// g2.color = Color(120, 120, 120)
// g2.draw(RoundRectangle2D.Float(x, y, w - 1, h - 1, arc, arc))
g2.dispose()
@ -44,17 +45,28 @@ class StyledButton(text: String) : JButton(text) {
}
}
class ToolTipComponent(editor: Editor, line: Int, column: Int) :
class ToolTipComponent(editor: Editor, x: Int, y: Int) :
JBPanel<ToolTipComponent>() {
private lateinit var addToChatButton: StyledButton
private lateinit var editButton: StyledButton
private var addToChatButton: StyledButton
private var editButton: StyledButton
private var backgroundColor: Color
private var foregroundColor: Color
init {
layout = null // Remove the FlowLayout
val globalScheme = EditorColorsManager.getInstance().globalScheme
backgroundColor = globalScheme.defaultBackground
foregroundColor = Color(
globalScheme.defaultForeground.red,
globalScheme.defaultForeground.green,
globalScheme.defaultForeground.blue,
200
)
val cmdCtrlChar = if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("mac")) "⌘" else "Ctrl"
addToChatButton = StyledButton("${cmdCtrlChar}J Chat")
editButton = StyledButton("${cmdCtrlChar}I Edit")
addToChatButton = StyledButton("${cmdCtrlChar}J Chat", foregroundColor)
editButton = StyledButton("${cmdCtrlChar}I Edit", foregroundColor)
addToChatButton.addActionListener { e: ActionEvent? ->
focusContinueInput(editor.project)
@ -72,9 +84,6 @@ class ToolTipComponent(editor: Editor, line: Int, column: Int) :
add(addToChatButton)
add(editButton)
val pos = LogicalPosition(line, column)
val y: Int = editor.logicalPositionToXY(pos).y + editor.lineHeight
val x: Int = editor.logicalPositionToXY(pos).x
val totalWidth = addToChatButton.preferredSize.width + editButton.preferredSize.width
val totalHeight = addToChatButton.preferredSize.height
setBounds(x, y, totalWidth, totalHeight)
@ -91,13 +100,14 @@ class ToolTipComponent(editor: Editor, line: Int, column: Int) :
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
// Draw rounded rectangle background
g2.color = Color(70, 70, 70, 200)
g2.color = backgroundColor
g2.fill(RoundRectangle2D.Float(0f, 0f, width.toFloat(), height.toFloat(), arc, arc))
// Draw rounded rectangle border
g2.color = Color(120, 120, 120)
val strokeWidth = 1.0f
g2.stroke = BasicStroke(1.0f)
g2.draw(RoundRectangle2D.Float(0f, 0f, width.toFloat(), height.toFloat(), arc, arc))
g2.draw(RoundRectangle2D.Float(0f, 0f, width.toFloat() - strokeWidth, height.toFloat() - strokeWidth, arc, arc))
// Draw border between buttons
g2.color = Color(120, 120, 120)

View file

@ -6,6 +6,7 @@ import com.github.continuedev.continueintellijextension.services.ContinueExtensi
import com.github.continuedev.continueintellijextension.utils.Debouncer
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.service
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.SelectionModel
import com.intellij.openapi.editor.event.SelectionEvent
import com.intellij.openapi.editor.event.SelectionListener
@ -13,6 +14,8 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.DumbAware
import com.intellij.openapi.vfs.VirtualFile
import kotlinx.coroutines.CoroutineScope
import kotlin.math.max
import kotlin.math.min
class ContinuePluginSelectionListener(
private val ideProtocolClient: IdeProtocolClient,
@ -64,10 +67,29 @@ class ContinuePluginSelectionListener(
ApplicationManager.getApplication().invokeLater {
toolTipComponent?.let { editor.contentComponent.remove(it) }
toolTipComponent = ToolTipComponent(editor, startLine - 2, selectedText.split("\n")[0].length + 1)
editor.contentComponent.layout = null
editor.contentComponent.add(toolTipComponent)
val line = startLine - 2
if (line > 0) {
// Get the text on line number "line"
val text = document.getText(document.getLineStartOffset(line), document.getLineEndOffset(line))
val pos = LogicalPosition(line, selectedText.split("\n")[0].length + 1)
val y: Int = editor.logicalPositionToXY(pos).y + editor.lineHeight
var x: Int = editor.logicalPositionToXY(pos).x
// Check if x is out of bounds
val maxEditorWidth = editor.contentComponent.width
val maxToolTipWidth = 600
x = max(0, min(x, maxEditorWidth - maxToolTipWidth))
toolTipComponent = ToolTipComponent(editor, x, y)
editor.contentComponent.add(toolTipComponent)
} else {
toolTipComponent = null
}
editor.contentComponent.revalidate()
editor.contentComponent.repaint()
}