From f59ac961b5de6ab664cf60aa68d615d39d7d235b Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 3 Oct 2024 17:29:43 -0700 Subject: [PATCH 1/3] jb: make hint tooltip more subtle --- .../editor/ToolTipComponent.kt | 145 ++++++++++++------ .../ContinuePluginSelectionListener.kt | 33 ++-- 2 files changed, 115 insertions(+), 63 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 7d5ba7c08..a974daf39 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 @@ -2,49 +2,95 @@ 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.EditorColors import com.intellij.openapi.editor.colors.EditorColorsManager +import com.intellij.openapi.editor.colors.EditorFontType import com.intellij.ui.components.JBPanel import java.awt.* import java.awt.event.ActionEvent +import java.awt.event.MouseAdapter +import java.awt.event.MouseEvent import java.util.* import javax.swing.JButton import java.awt.geom.RoundRectangle2D -class StyledButton(text: String, foregroundColor: Color) : JButton(text) { +class StyledButton(text: String, foregroundColor: Color, backgroundColor: Color) : JButton(text) { + private var isHovered = false + private val borderColor: Color + private val hoverBackgroundColor: Color + private val hoverForegroundColor: Color + private val originalForegroundColor: Color + init { cursor = Cursor(Cursor.HAND_CURSOR) isOpaque = false isContentAreaFilled = false isFocusPainted = false - border = null // Remove the border + border = null foreground = foregroundColor + background = Color(backgroundColor.red, backgroundColor.green, backgroundColor.blue, 0) + + // Store the original foreground color + originalForegroundColor = foregroundColor + + // Calculate colors for different states + borderColor = Color(foregroundColor.red, foregroundColor.green, foregroundColor.blue, 40) // More subtle border + hoverBackgroundColor = Color(backgroundColor.red, backgroundColor.green, backgroundColor.blue, 40) + hoverForegroundColor = Color( + foregroundColor.red, + foregroundColor.green, + foregroundColor.blue, + 160 + ) + + // Get the editor font and its size + val scheme = EditorColorsManager.getInstance().globalScheme + val editorFont = scheme.getFont(EditorFontType.PLAIN) + val editorFontSize = editorFont.size + + // Set the button's font to be slightly smaller than the editor + font = font.deriveFont(editorFontSize.toFloat() * 0.75f) + + addMouseListener(object : MouseAdapter() { + override fun mouseEntered(e: MouseEvent) { + isHovered = true + repaint() + } + + override fun mouseExited(e: MouseEvent) { + isHovered = false + repaint() + } + }) } + override fun paintComponent(g: Graphics) { val g2 = g.create() as Graphics2D g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON) - val arc = 24f // Corner radius - val padding = 0f - val x = padding - val y = padding - val w = (width - 2 * padding) - val h = (height - 2 * padding) + val width = width.toFloat() + val height = height.toFloat() + val arc = 4f - // Draw flat background -// 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 button background + g2.color = if (isHovered) hoverBackgroundColor else background + g2.fill(RoundRectangle2D.Float(0f, 0f, width, height, arc, arc)) // Draw border -// g2.color = Color(120, 120, 120) -// g2.draw(RoundRectangle2D.Float(x, y, w - 1, h - 1, arc, arc)) + g2.color = if (isHovered) hoverForegroundColor else borderColor + g2.draw(RoundRectangle2D.Float(0.5f, 0.5f, width - 1f, height - 1f, arc, arc)) g2.dispose() + // Set text color + foreground = if (isHovered) hoverForegroundColor else originalForegroundColor + super.paintComponent(g) } } + class ToolTipComponent(editor: Editor, x: Int, y: Int) : JBPanel() { private var addToChatButton: StyledButton @@ -56,7 +102,11 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) : layout = null // Remove the FlowLayout val globalScheme = EditorColorsManager.getInstance().globalScheme - backgroundColor = globalScheme.defaultBackground + + // Get the selection background color + backgroundColor = globalScheme.getColor(EditorColors.SELECTION_BACKGROUND_COLOR) + ?: globalScheme.defaultBackground + foregroundColor = Color( globalScheme.defaultForeground.red, globalScheme.defaultForeground.green, @@ -64,9 +114,19 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) : 200 ) - val cmdCtrlChar = if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("mac")) "⌘" else "Ctrl" - addToChatButton = StyledButton("${cmdCtrlChar}J Chat", foregroundColor) - editButton = StyledButton("${cmdCtrlChar}I Edit", foregroundColor) + val cmdCtrlChar = + if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("mac")) "⌘" else "Ctrl" + + val buttonHeight = 16 // Reduced height + val buttonHorizontalPadding = 2 // Reduced horizontal padding inside buttons + val buttonVerticalPadding = 2 // Vertical padding above and below buttons + val componentHorizontalPadding = 4 // Padding on the left and right of the component + val buttonMargin = 4 // New margin between buttons + + + + addToChatButton = StyledButton("Chat (${cmdCtrlChar}J)", foregroundColor, backgroundColor) + editButton = StyledButton("Edit (${cmdCtrlChar}I)", foregroundColor, backgroundColor) addToChatButton.addActionListener { e: ActionEvent? -> focusContinueInput(editor.project) @@ -77,45 +137,32 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) : editor.contentComponent.remove(this) } - // 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) + + // Calculate button widths + val addToChatWidth = addToChatButton.preferredSize.width + (2 * buttonHorizontalPadding) + val editWidth = editButton.preferredSize.width + (2 * buttonHorizontalPadding) + + // Set bounds for buttons + addToChatButton.setBounds(componentHorizontalPadding, buttonVerticalPadding, addToChatWidth, buttonHeight) + editButton.setBounds( + componentHorizontalPadding + addToChatWidth + buttonMargin, + buttonVerticalPadding, + editWidth, + buttonHeight + ) add(addToChatButton) add(editButton) - val totalWidth = addToChatButton.preferredSize.width + editButton.preferredSize.width - val totalHeight = addToChatButton.preferredSize.height - setBounds(x, y, totalWidth, totalHeight) + val totalWidth = addToChatWidth + editWidth + buttonMargin + (2 * componentHorizontalPadding) + val totalHeight = buttonHeight + (2 * buttonVerticalPadding) + + // Center the component on the provided y coordinate + val yPosition = y - (totalHeight / 2) + setBounds(x, yPosition, totalWidth, totalHeight) // Make the background transparent isOpaque = false background = Color(0, 0, 0, 0) } - - 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 = 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() - strokeWidth, height.toFloat() - strokeWidth, 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 8c4b2f8b7..db046ad5d 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 @@ -14,6 +14,7 @@ import com.intellij.openapi.editor.event.SelectionEvent import com.intellij.openapi.editor.event.SelectionListener import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.openapi.project.DumbAware +import com.intellij.openapi.util.TextRange import com.intellij.openapi.vfs.VirtualFile import kotlinx.coroutines.CoroutineScope import kotlin.math.max @@ -30,7 +31,7 @@ class ContinuePluginSelectionListener( private var toolTipComponents: ArrayList = ArrayList() - private fun removeExistingTooltips(editor: Editor, onComplete : () -> Unit = {}) { + private fun removeExistingTooltips(editor: Editor, onComplete: () -> Unit = {}) { ApplicationManager.getApplication().invokeLater { toolTipComponents.forEach { editor.contentComponent.remove(it) @@ -71,32 +72,36 @@ class ContinuePluginSelectionListener( val endOffset = model.selectionEnd val startLine = document.getLineNumber(startOffset) val endLine = document.getLineNumber(endOffset) - val startCharacter = - startOffset - document.getLineStartOffset(startLine) - val endCharacter = endOffset - document.getLineStartOffset(endLine) - val virtualFile: VirtualFile? = FileDocumentManager.getInstance().getFile(document) - val filepath = virtualFile?.path ?: "Unknown path" removeExistingTooltips(editor) { ApplicationManager.getApplication().invokeLater { editor.contentComponent.layout = null - val line = startLine - 2 - if (line > 0 && startLine < endLine) { - // Get the text on line number "line" -// val text = document.getText(document.getLineStartOffset(line), document.getLineEndOffset(line)) + val topLine = startLine - 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 + if (topLine > 0 && startLine < endLine) { + // Get the text on the top line + val lineStartOffset = document.getLineStartOffset(topLine) + val lineEndOffset = document.getLineEndOffset(topLine) + val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset)) + + // Calculate the position 20px to the right of the end of the line + val endOfLinePos = LogicalPosition(topLine, lineText.length) + val endOfLineX = editor.logicalPositionToXY(endOfLinePos).x + val tooltipX = endOfLineX + 40 + + // Calculate the Y position (vertically centered on the line) + val lineTopY = editor.logicalPositionToXY(LogicalPosition(topLine, 0)).y + val lineHeight = editor.lineHeight + val y = lineTopY + (lineHeight / 2) // Check if x is out of bounds val maxEditorWidth = editor.contentComponent.width val maxToolTipWidth = 600 - x = max(0, min(x, maxEditorWidth - maxToolTipWidth)) + val x = max(0, min(tooltipX, maxEditorWidth - maxToolTipWidth)) val toolTipComponent = ToolTipComponent(editor, x, y) toolTipComponents.add(toolTipComponent) From 0db901a27913e878b777339d40c6082981d4c43e Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 3 Oct 2024 17:36:52 -0700 Subject: [PATCH 2/3] dont render on empty lines --- .../editor/ToolTipComponent.kt | 13 +++++-------- .../listeners/ContinuePluginSelectionListener.kt | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 10 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 a974daf39..3700cc6a2 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 @@ -103,7 +103,6 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) : val globalScheme = EditorColorsManager.getInstance().globalScheme - // Get the selection background color backgroundColor = globalScheme.getColor(EditorColors.SELECTION_BACKGROUND_COLOR) ?: globalScheme.defaultBackground @@ -117,13 +116,11 @@ class ToolTipComponent(editor: Editor, x: Int, y: Int) : val cmdCtrlChar = if (System.getProperty("os.name").lowercase(Locale.getDefault()).contains("mac")) "⌘" else "Ctrl" - val buttonHeight = 16 // Reduced height - val buttonHorizontalPadding = 2 // Reduced horizontal padding inside buttons - val buttonVerticalPadding = 2 // Vertical padding above and below buttons - val componentHorizontalPadding = 4 // Padding on the left and right of the component - val buttonMargin = 4 // New margin between buttons - - + val buttonHeight = 16 + val buttonHorizontalPadding = 2 + val buttonVerticalPadding = 2 + val componentHorizontalPadding = 4 + val buttonMargin = 4 addToChatButton = StyledButton("Chat (${cmdCtrlChar}J)", foregroundColor, backgroundColor) editButton = StyledButton("Edit (${cmdCtrlChar}I)", foregroundColor, backgroundColor) 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 db046ad5d..bc0636307 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 @@ -80,9 +80,21 @@ class ContinuePluginSelectionListener( ApplicationManager.getApplication().invokeLater { editor.contentComponent.layout = null - val topLine = startLine + var topLine = startLine + var hasText = false + + for (line in startLine until endLine) { + val lineStartOffset = document.getLineStartOffset(line) + val lineEndOffset = document.getLineEndOffset(line) + val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset)).trim() + if (lineText.isNotEmpty()) { + topLine = line + hasText = true + break + } + } - if (topLine > 0 && startLine < endLine) { + if (hasText && topLine < endLine) { // Get the text on the top line val lineStartOffset = document.getLineStartOffset(topLine) val lineEndOffset = document.getLineEndOffset(topLine) From ccaf060f4a59ec6cf9aaaf757b2162473d834b5b Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 3 Oct 2024 17:51:07 -0700 Subject: [PATCH 3/3] Update ContinuePluginSelectionListener.kt --- .../ContinuePluginSelectionListener.kt | 66 ++++++++++--------- 1 file changed, 36 insertions(+), 30 deletions(-) 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 bc0636307..23b0647cf 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 @@ -80,46 +80,52 @@ class ContinuePluginSelectionListener( ApplicationManager.getApplication().invokeLater { editor.contentComponent.layout = null - var topLine = startLine - var hasText = false - - for (line in startLine until endLine) { + val startOffset = model.selectionStart + val endOffset = model.selectionEnd + var startLine = document.getLineNumber(startOffset) + val endLine = document.getLineNumber(endOffset) + + // Find the first non-empty line + var nonEmptyLine = -1 + for (line in startLine..endLine) { val lineStartOffset = document.getLineStartOffset(line) val lineEndOffset = document.getLineEndOffset(line) val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset)).trim() if (lineText.isNotEmpty()) { - topLine = line - hasText = true + nonEmptyLine = line break } } - if (hasText && topLine < endLine) { - // Get the text on the top line - val lineStartOffset = document.getLineStartOffset(topLine) - val lineEndOffset = document.getLineEndOffset(topLine) - val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset)) - - // Calculate the position 20px to the right of the end of the line - val endOfLinePos = LogicalPosition(topLine, lineText.length) - val endOfLineX = editor.logicalPositionToXY(endOfLinePos).x - val tooltipX = endOfLineX + 40 - - // Calculate the Y position (vertically centered on the line) - val lineTopY = editor.logicalPositionToXY(LogicalPosition(topLine, 0)).y - val lineHeight = editor.lineHeight - val y = lineTopY + (lineHeight / 2) - - // Check if x is out of bounds - val maxEditorWidth = editor.contentComponent.width - val maxToolTipWidth = 600 - val x = max(0, min(tooltipX, maxEditorWidth - maxToolTipWidth)) - - val toolTipComponent = ToolTipComponent(editor, x, y) - toolTipComponents.add(toolTipComponent) - editor.contentComponent.add(toolTipComponent) + // If no non-empty line found, don't render anything + if (nonEmptyLine == -1) { + return@invokeLater } + // Get the text of the first non-empty line + val lineStartOffset = document.getLineStartOffset(nonEmptyLine) + val lineEndOffset = document.getLineEndOffset(nonEmptyLine) + val lineText = document.getText(TextRange(lineStartOffset, lineEndOffset)) + + // Calculate the position 20px to the right of the end of the line + val endOfLinePos = LogicalPosition(nonEmptyLine, lineText.length) + val endOfLineX = editor.logicalPositionToXY(endOfLinePos).x + val tooltipX = endOfLineX + 40 + + // Calculate the Y position (vertically centered on the line) + val lineTopY = editor.logicalPositionToXY(LogicalPosition(nonEmptyLine, 0)).y + val lineHeight = editor.lineHeight + val y = lineTopY + (lineHeight / 2) + + // Check if x is out of bounds + val maxEditorWidth = editor.contentComponent.width + val maxToolTipWidth = 600 + val x = max(0, min(tooltipX, maxEditorWidth - maxToolTipWidth)) + + val toolTipComponent = ToolTipComponent(editor, x, y) + toolTipComponents.add(toolTipComponent) + editor.contentComponent.add(toolTipComponent) + editor.contentComponent.revalidate() editor.contentComponent.repaint() }