-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Saves and Restores the Focus Owner Before and After a Dialog is Shown (…
…#39) * 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 I'm pretty sure this fixes it. I was able to repro very frequently before, and now I can't repro. That said, I was never able to get a 100% repro case. * Remove print statement --------- Co-authored-by: Romain Guy <[email protected]>
- Loading branch information
1 parent
e1708ee
commit 8b12f46
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/jvmMain/kotlin/dev/romainguy/kotlin/explorer/DialogState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters