Skip to content

Commit

Permalink
Better focus handling
Browse files Browse the repository at this point in the history
  • Loading branch information
romainguy committed May 8, 2024
1 parent e72e23b commit 9978bed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
46 changes: 30 additions & 16 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/KotlinExplorer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

package dev.romainguy.kotlin.explorer

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
Expand Down Expand Up @@ -70,6 +69,7 @@ import org.jetbrains.jewel.window.DecoratedWindow
import org.jetbrains.jewel.window.TitleBar
import org.jetbrains.jewel.window.newFullscreenControls
import org.jetbrains.jewel.window.styling.TitleBarStyle
import java.awt.Container
import java.awt.event.FocusEvent
import java.awt.event.FocusListener
import java.io.IOException
Expand All @@ -84,6 +84,7 @@ private fun FrameWindowScope.KotlinExplorer(
) {
// TODO: Move all those remembers to an internal private state object
var activeTextArea by remember { mutableStateOf<RSyntaxTextArea?>(null) }
var previousActiveTextArea by remember { mutableStateOf<RSyntaxTextArea?>(null) }
var status by remember { mutableStateOf("Ready") }
var progress by remember { mutableStateOf(1f) }
var logs by remember { mutableStateOf("") }
Expand Down Expand Up @@ -118,14 +119,24 @@ private fun FrameWindowScope.KotlinExplorer(
}}
val focusTracker = remember { object : FocusListener {
override fun focusGained(e: FocusEvent?) {
previousActiveTextArea = activeTextArea
activeTextArea = e?.component as RSyntaxTextArea
}

override fun focusLost(e: FocusEvent?) {
// TODO: Bit of a hack to keep focus on the text areas. Without this, clicking
// the background loses focus, so does opening/closing panels
if (e?.oppositeComponent !is RSyntaxTextArea) {
if (activeTextArea?.isShowing == true) {
activeTextArea?.requestFocusInWindow()
} else {
previousActiveTextArea?.requestFocusInWindow()
}
}
}
}}

val sourceTextArea = remember { sourceTextArea(focusTracker, explorerState) }
val sourceTextArea = remember { sourceTextArea(focusTracker, explorerState).apply { requestFocusInWindow() } }
val byteCodeTextArea = remember { byteCodeTextArea(explorerState, focusTracker) }
val dexTextArea = remember { dexTextArea(explorerState, focusTracker) }
val oatTextArea = remember { oatTextArea(explorerState, focusTracker) }
Expand Down Expand Up @@ -200,17 +211,20 @@ private fun FrameWindowScope.KotlinExplorer(
private fun LogsPanel(logs: String) {
Column {
Title("Logs")
Text(
text = logs,
fontFamily = FontFamily.Monospace,
modifier = Modifier
.weight(1.0f)
.fillMaxSize()
.background(Color.White)
.verticalScroll(rememberScrollState())
.border(1.dp, JewelTheme.globalColors.borders.normal)
.padding(8.dp)
)
SelectionContainer {
Text(
text = logs,
fontFamily = FontFamily.Monospace,
modifier = Modifier
.weight(1.0f)
.fillMaxSize()
.background(Color.White)
.verticalScroll(rememberScrollState())
.border(1.dp, JewelTheme.globalColors.borders.normal)
.padding(8.dp)
.focusable(false)
)
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ private const val ShowLineNumbers = "SHOW_LINE_NUMBERS"
private const val ShowByteCode = "SHOW_BYTE_CODE"
private const val ShowDex = "SHOW_DEX"
private const val ShowOat = "SHOW_OAT"
private const val ShowLogs = "SHOW_LOGS"
private const val Indent = "Indent"
private const val LineNumberWidth = "LINE_NUMBER_WIDTH"
private const val WindowPosX = "WINDOW_X"
Expand All @@ -57,7 +56,7 @@ class ExplorerState {
var showByteCode by BooleanState(ShowByteCode, false)
var showDex by BooleanState(ShowDex, true)
var showOat by BooleanState(ShowOat, true)
var showLogs by BooleanState(ShowLogs, false)
var showLogs by mutableStateOf(false)
var lineNumberWidth by IntState(LineNumberWidth, 4)
var indent by IntState(Indent, 4)
var sourceCode: String = readSourceCode(toolPaths)
Expand Down

0 comments on commit 9978bed

Please sign in to comment.