Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saves and Restores the Focus Owner Before and After a Dialog is Shown #39

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/DialogState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2023 Romain Guy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.romainguy.kotlin.explorer

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import java.awt.Component
import javax.swing.FocusManager

/**
* A [MutableState]<[Boolean]> that saves and restores the focus owner before and after a dialog is shown.
*
* This is probably related to [DialogSupportingSwingPanel] and sometimes causes a crash on Linux when:
* * Click in a code panel to set focus
* * Open Settings
* * Close Settings
* * Press any key
*
* TODO: Remove all this code
* See https://github.com/JetBrains/compose-multiplatform-core/pull/915
*/
class DialogState(initial: Boolean) : MutableState<Boolean> {
private var state = mutableStateOf(initial)
private var focusOwner: Component? = null
override var value: Boolean
get() = state.value
set(value) {
if (value) {
focusOwner = FocusManager.getCurrentManager().focusOwner
} else {
focusOwner?.requestFocus()
}
state.value = value
}

override fun component1() = state.component1()

override fun component2() = state.component2()
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private fun FrameWindowScope.KotlinExplorer(
val oatTextArea = remember { oatTextArea(explorerState, focusTracker) }

val findDialog = remember { FindDialog(window, searchListener).apply { searchContext.searchWrap = true } }
var showSettings by remember { mutableStateOf(!explorerState.toolPaths.isValid) }
var showSettings by remember { DialogState(!explorerState.toolPaths.isValid) }

val sourcePanel: @Composable () -> Unit = { SourcePanel(sourceTextArea, explorerState, showSettings) }
val byteCodePanel: @Composable () -> Unit = { TextPanel("Byte Code", byteCodeTextArea, explorerState, showSettings) }
Expand Down