Skip to content

Commit

Permalink
Add arrow to jump indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
romainguy committed May 3, 2024
1 parent dbc81f7 commit 64c3fbc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
25 changes: 22 additions & 3 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/CodeTextArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ package dev.romainguy.kotlin.explorer

import dev.romainguy.kotlin.explorer.jump.JumpDetector
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea
import java.awt.BasicStroke
import java.awt.Graphics
import java.awt.Graphics2D
import java.awt.Polygon
import java.awt.RenderingHints
import java.awt.geom.GeneralPath
import javax.swing.event.CaretEvent
import javax.swing.event.CaretListener

Expand All @@ -31,6 +35,7 @@ open class CodeTextArea(
private var jumpOffsets: JumpOffsets? = null
private var fullText = ""

var presentationMode: Boolean = false

init {
addCaretListener(::caretUpdate)
Expand All @@ -54,7 +59,9 @@ open class CodeTextArea(
override fun paintComponent(g: Graphics?) {
super.paintComponent(g)
jumpOffsets?.let { jump ->
val padding = 6
val scale = if (presentationMode) 2 else 1
val padding = 6 * scale
val triangleSize = 8 * scale

val bounds1 = modelToView2D(jump.src)
val bounds2 = modelToView2D(jump.dst)
Expand All @@ -65,11 +72,23 @@ open class CodeTextArea(
val x2 = bounds2.x.toInt() - padding
val y2 = (bounds2.y + lineHeight / 2).toInt()

val x0 = modelToView2D(2).x.toInt()
val x0 = modelToView2D(4).x.toInt()

val g2 = g as Graphics2D
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2.stroke = BasicStroke(scale.toFloat())
g2.drawLine(x1, y1, x0, y1)
g2.drawLine(x0, y1, x0, y2)
g2.drawLine(x0, y2, x2, y2)
g2.drawLine(x0, y2, x2 - triangleSize / 2, y2)

g2.fill(GeneralPath().apply {
val fx = x2.toFloat()
val fy = y2.toFloat() + 0.5f
val fs = triangleSize.toFloat()
moveTo(fx, fy)
lineTo(fx - fs, fy - fs / 2.0f)
lineTo(fx - fs, fy + fs / 2.0f)
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ import org.jetbrains.jewel.intui.window.decoratedWindow
import org.jetbrains.jewel.intui.window.styling.dark
import org.jetbrains.jewel.intui.window.styling.light
import org.jetbrains.jewel.ui.ComponentStyling
import org.jetbrains.jewel.ui.component.DefaultButton
import org.jetbrains.jewel.ui.component.Icon
import org.jetbrains.jewel.ui.component.Text
import org.jetbrains.jewel.ui.component.TextField
import org.jetbrains.jewel.ui.component.*
import org.jetbrains.jewel.window.DecoratedWindow
import org.jetbrains.jewel.window.TitleBar
import org.jetbrains.jewel.window.newFullscreenControls
Expand Down Expand Up @@ -197,7 +194,7 @@ private fun SourcePanel(sourceTextArea: RSyntaxTextArea, explorerState: Explorer
},
update = {
sourceTextArea.text = explorerState.sourceCode
sourceTextArea.setFont(explorerState)
sourceTextArea.updateStyle(explorerState)
}
)
}
Expand All @@ -210,7 +207,7 @@ private fun TextPanel(title: String, textArea: RSyntaxTextArea, explorerState: E
SwingPanel(
modifier = Modifier.fillMaxSize(),
factory = { RTextScrollPane(textArea) },
update = { textArea.setFont(explorerState) })
update = { textArea.updateStyle(explorerState) })
}
}

Expand Down Expand Up @@ -286,10 +283,11 @@ private fun FrameWindowScope.MainMenu(
val onShowPanelChanged: (Boolean) -> Unit = { onPanelsUpdated() }
MenuCheckboxItem("Show DEX", Ctrl(D), explorerState::showDex, onShowPanelChanged)
MenuCheckboxItem("Show OAT", Ctrl(O), explorerState::showOat, onShowPanelChanged)
MenuCheckboxItem("Presentation Mode", CtrlShift(P), explorerState::presentationMode)
MenuCheckboxItem("Show Line Numbers", CtrlShift(L), explorerState::showLineNumbers) {
onDexUpdate(null)
}
Separator()
MenuCheckboxItem("Presentation Mode", CtrlShift(P), explorerState::presentationMode)
}
Menu("Compilation") {
MenuCheckboxItem("Optimize with R8", CtrlShift(O), explorerState::optimize)
Expand Down Expand Up @@ -402,9 +400,12 @@ private fun Settings(
}
}

private fun RSyntaxTextArea.setFont(explorerState: ExplorerState) {
private fun RSyntaxTextArea.updateStyle(explorerState: ExplorerState) {
val presentation = explorerState.presentationMode
font = font.deriveFont(if (presentation) FontSizePresentationMode else FontSizeEditingMode)
if (this is CodeTextArea) {
presentationMode = presentation
}
}

private fun updateTextArea(textArea: RSyntaxTextArea, text: String) {
Expand Down

0 comments on commit 64c3fbc

Please sign in to comment.