mirror of
https://github.com/praktimarc/kst4contest.git
synced 2026-11-19 01:52:29 +01:00
Stop the wizard on unreadable numbers and own its file chooser
Written-by: Codex (GPT-6.2 Sol) Written-by: Claude Fable 5.2
This commit is contained in:
7 files changed
+116
-18
No files matched your search
+28
-9
@@ -18,10 +18,12 @@ import androidx.compose.runtime.getValue
|
|||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.runtime.staticCompositionLocalOf
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.window.AwtWindow
|
import androidx.compose.ui.window.AwtWindow
|
||||||
|
import java.awt.Dialog
|
||||||
import java.awt.FileDialog
|
import java.awt.FileDialog
|
||||||
import java.awt.Frame
|
import java.awt.Frame
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -125,15 +127,36 @@ internal fun FilePathRow(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** The modal owner that keeps the native file chooser accessible when opened from the wizard. */
|
||||||
* The AWT file dialog, opened from Compose. The JavaFX chooser started in the user's
|
val LocalFileDialogOwner = staticCompositionLocalOf<Dialog?> { null }
|
||||||
* home directory and a cancelled choice kept the stored path — both are preserved.
|
|
||||||
*/
|
/** The AWT file dialog, opened from Compose. */
|
||||||
@Composable
|
@Composable
|
||||||
private fun FileChooserDialog(title: String, onResult: (String?) -> Unit) {
|
private fun FileChooserDialog(title: String, onResult: (String?) -> Unit) {
|
||||||
|
val owner = LocalFileDialogOwner.current
|
||||||
AwtWindow(
|
AwtWindow(
|
||||||
create = {
|
create = {
|
||||||
object : FileDialog(null as Frame?, title, LOAD) {
|
if (owner == null) LogFileDialog(title, onResult) else LogFileDialog(owner, title, onResult)
|
||||||
|
},
|
||||||
|
dispose = FileDialog::dispose,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The JavaFX chooser started in the user's home directory and a cancelled choice kept the
|
||||||
|
* stored path. Both constructors share that behaviour.
|
||||||
|
*/
|
||||||
|
private class LogFileDialog : FileDialog {
|
||||||
|
private val onResult: (String?) -> Unit
|
||||||
|
|
||||||
|
constructor(title: String, onResult: (String?) -> Unit) : super(null as Frame?, title, LOAD) {
|
||||||
|
this.onResult = onResult
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(owner: Dialog, title: String, onResult: (String?) -> Unit) : super(owner, title, LOAD) {
|
||||||
|
this.onResult = onResult
|
||||||
|
}
|
||||||
|
|
||||||
override fun setVisible(visible: Boolean) {
|
override fun setVisible(visible: Boolean) {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
directory = System.getProperty("user.home")
|
directory = System.getProperty("user.home")
|
||||||
@@ -147,10 +170,6 @@ private fun FileChooserDialog(title: String, onResult: (String?) -> Unit) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
|
||||||
dispose = FileDialog::dispose,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The label column width the shared form controls use, so the tabs line up. */
|
/** The label column width the shared form controls use, so the tabs line up. */
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ import androidx.compose.material3.Surface
|
|||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.CompositionLocalProvider
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.semantics.Role
|
import androidx.compose.ui.semantics.Role
|
||||||
@@ -23,6 +27,8 @@ import androidx.compose.ui.unit.dp
|
|||||||
import kst4contest.model.ChatCategory
|
import kst4contest.model.ChatCategory
|
||||||
import kst4contest.view.compose.Density
|
import kst4contest.view.compose.Density
|
||||||
import kst4contest.view.compose.Form
|
import kst4contest.view.compose.Form
|
||||||
|
import kst4contest.view.compose.LocalSettingsDraft
|
||||||
|
import kst4contest.view.compose.SettingsDraft
|
||||||
import kst4contest.view.compose.tabs.FilePathRow
|
import kst4contest.view.compose.tabs.FilePathRow
|
||||||
import kst4contest.view.i18n.LanguageStore
|
import kst4contest.view.i18n.LanguageStore
|
||||||
import kst4contest.view.i18n.LocalStrings
|
import kst4contest.view.i18n.LocalStrings
|
||||||
@@ -60,8 +66,17 @@ fun SetupWizardContent(
|
|||||||
onSave: (connect: Boolean) -> Unit,
|
onSave: (connect: Boolean) -> Unit,
|
||||||
) {
|
) {
|
||||||
val strings = languageStore.strings
|
val strings = languageStore.strings
|
||||||
|
// The draft only registers typed entries. Wizard answers are never staged or applied through it.
|
||||||
|
val typedEntries = remember { SettingsDraft() }
|
||||||
|
var typedEntryRefusals by remember { mutableStateOf(emptyList<String>()) }
|
||||||
|
|
||||||
CompositionLocalProvider(LocalStrings provides strings) {
|
fun withAcceptedTypedEntries(action: () -> Unit) {
|
||||||
|
val refusals = typedEntries.commitTypedEntries()
|
||||||
|
typedEntryRefusals = refusals
|
||||||
|
if (refusals.isEmpty()) action()
|
||||||
|
}
|
||||||
|
|
||||||
|
CompositionLocalProvider(LocalStrings provides strings, LocalSettingsDraft provides typedEntries) {
|
||||||
Surface(modifier = Modifier.fillMaxSize()) {
|
Surface(modifier = Modifier.fillMaxSize()) {
|
||||||
Column(modifier = Modifier.padding(15.dp)) {
|
Column(modifier = Modifier.padding(15.dp)) {
|
||||||
Column(
|
Column(
|
||||||
@@ -80,6 +95,9 @@ fun SetupWizardContent(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typedEntryRefusals.isNotEmpty()) {
|
||||||
|
Refusal(typedEntryRefusals.joinToString("\n"))
|
||||||
|
}
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.fillMaxWidth().padding(top = 10.dp),
|
modifier = Modifier.fillMaxWidth().padding(top = 10.dp),
|
||||||
horizontalArrangement = Arrangement.spacedBy(Density.BUTTON_GAP),
|
horizontalArrangement = Arrangement.spacedBy(Density.BUTTON_GAP),
|
||||||
@@ -90,22 +108,23 @@ fun SetupWizardContent(
|
|||||||
Form.button(strings.wizardBack, onClick = state::back)
|
Form.button(strings.wizardBack, onClick = state::back)
|
||||||
}
|
}
|
||||||
if (state.page == WizardPage.SUMMARY) {
|
if (state.page == WizardPage.SUMMARY) {
|
||||||
Form.button(strings.wizardSave, enabled = state.mandatoryComplete) { onSave(false) }
|
Form.button(strings.wizardSave, enabled = state.mandatoryComplete) {
|
||||||
|
withAcceptedTypedEntries { onSave(false) }
|
||||||
|
}
|
||||||
Form.button(
|
Form.button(
|
||||||
strings.wizardSaveAndConnect,
|
strings.wizardSaveAndConnect,
|
||||||
enabled = state.mandatoryComplete,
|
enabled = state.mandatoryComplete,
|
||||||
accented = true,
|
accented = true,
|
||||||
) { onSave(true) }
|
) { withAcceptedTypedEntries { onSave(true) } }
|
||||||
} else {
|
} else {
|
||||||
if (state.canFinish) {
|
if (state.canFinish) {
|
||||||
Form.button(strings.wizardFinish, onClick = state::finish)
|
Form.button(strings.wizardFinish) { withAcceptedTypedEntries(state::finish) }
|
||||||
}
|
}
|
||||||
Form.button(
|
Form.button(
|
||||||
strings.wizardNext,
|
strings.wizardNext,
|
||||||
enabled = state.canGoNext,
|
enabled = state.canGoNext,
|
||||||
accented = true,
|
accented = true,
|
||||||
onClick = state::next,
|
) { withAcceptedTypedEntries(state::next) }
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -270,7 +289,7 @@ private fun AntennaPage(antenna: WizardAntenna, strings: Strings) {
|
|||||||
Form.decimal(strings.stationOwnAntennaGain, antenna.gainDbi, isValid = { it.isFinite() }) {
|
Form.decimal(strings.stationOwnAntennaGain, antenna.gainDbi, isValid = { it.isFinite() }) {
|
||||||
antenna.gainDbi = it
|
antenna.gainDbi = it
|
||||||
}
|
}
|
||||||
Form.decimal(strings.stationOwnAntennaHeight, antenna.heightMeters, isValid = { it.isFinite() }) {
|
Form.decimal(strings.stationOwnAntennaHeight, antenna.heightMeters, isValid = { it.isFinite() && it >= 0.0 }) {
|
||||||
antenna.heightMeters = it
|
antenna.heightMeters = it
|
||||||
}
|
}
|
||||||
Form.decimal(strings.stationOwnTxPower, antenna.txPowerWatts, isValid = { it.isFinite() }) {
|
Form.decimal(strings.stationOwnTxPower, antenna.txPowerWatts, isValid = { it.isFinite() }) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package kst4contest.view.compose.wizard
|
package kst4contest.view.compose.wizard
|
||||||
|
|
||||||
|
import androidx.compose.runtime.CompositionLocalProvider
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
@@ -9,6 +10,7 @@ import kst4contest.model.ChatPreferences
|
|||||||
import kst4contest.view.compose.Kst4ContestTheme
|
import kst4contest.view.compose.Kst4ContestTheme
|
||||||
import kst4contest.view.compose.WindowPlacementOnScreen
|
import kst4contest.view.compose.WindowPlacementOnScreen
|
||||||
import kst4contest.view.compose.openOnEventThread
|
import kst4contest.view.compose.openOnEventThread
|
||||||
|
import kst4contest.view.compose.tabs.LocalFileDialogOwner
|
||||||
import java.awt.Dialog
|
import java.awt.Dialog
|
||||||
import java.awt.Dimension
|
import java.awt.Dimension
|
||||||
import java.util.concurrent.atomic.AtomicReference
|
import java.util.concurrent.atomic.AtomicReference
|
||||||
@@ -96,6 +98,7 @@ object SetupWizardWindow {
|
|||||||
darkMode = prefs.isGUI_darkModeActiveByDefault(),
|
darkMode = prefs.isGUI_darkModeActiveByDefault(),
|
||||||
baseFontSizeSp = DIALOG_FONT_SIZE_SP,
|
baseFontSizeSp = DIALOG_FONT_SIZE_SP,
|
||||||
) {
|
) {
|
||||||
|
CompositionLocalProvider(LocalFileDialogOwner provides dialog) {
|
||||||
SetupWizardContent(
|
SetupWizardContent(
|
||||||
state = state,
|
state = state,
|
||||||
languageStore = languageStore,
|
languageStore = languageStore,
|
||||||
@@ -113,6 +116,7 @@ object SetupWizardWindow {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dialog.isVisible = true
|
dialog.isVisible = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ class WizardAntenna(prefs: ChatPreferences) {
|
|||||||
/** Whether every answer can be stored as it stands. */
|
/** Whether every answer can be stored as it stands. */
|
||||||
val complete: Boolean
|
val complete: Boolean
|
||||||
get() = beamWidthDeg.isFinite() && gainDbi.isFinite() && heightMeters.isFinite() &&
|
get() = beamWidthDeg.isFinite() && gainDbi.isFinite() && heightMeters.isFinite() &&
|
||||||
txPowerWatts.isFinite() && !heightAslRefused
|
heightMeters >= 0.0 && txPowerWatts.isFinite() && !heightAslRefused
|
||||||
|
|
||||||
/** Puts the answers into [prefs]. Call only while [complete]. */
|
/** Puts the answers into [prefs]. Call only while [complete]. */
|
||||||
fun writeTo(prefs: ChatPreferences) {
|
fun writeTo(prefs: ChatPreferences) {
|
||||||
|
|||||||
+43
@@ -2,8 +2,10 @@ package kst4contest.view.compose.wizard
|
|||||||
|
|
||||||
import androidx.compose.ui.test.ComposeUiTest
|
import androidx.compose.ui.test.ComposeUiTest
|
||||||
import androidx.compose.ui.test.ExperimentalTestApi
|
import androidx.compose.ui.test.ExperimentalTestApi
|
||||||
|
import androidx.compose.ui.test.hasSetTextAction
|
||||||
import androidx.compose.ui.test.onNodeWithText
|
import androidx.compose.ui.test.onNodeWithText
|
||||||
import androidx.compose.ui.test.performClick
|
import androidx.compose.ui.test.performClick
|
||||||
|
import androidx.compose.ui.test.performTextReplacement
|
||||||
import androidx.compose.ui.test.runComposeUiTest
|
import androidx.compose.ui.test.runComposeUiTest
|
||||||
import kst4contest.model.ChatCategory
|
import kst4contest.model.ChatCategory
|
||||||
import kst4contest.model.ChatPreferences
|
import kst4contest.model.ChatPreferences
|
||||||
@@ -168,6 +170,47 @@ class SetupWizardContentScreenTest {
|
|||||||
onNodeWithText("Take my own frequency from the log program").assertExists()
|
onNodeWithText("Take my own frequency from the log program").assertExists()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun nextRefusesAnUnreadableUdpPortAndAcceptsItsCorrection() = runComposeUiTest {
|
||||||
|
val state = SetupWizardState(firstInstallation())
|
||||||
|
state.walkToTheLogPage()
|
||||||
|
state.integrations.chooseLogProgram(WizardLogProgram.UDP)
|
||||||
|
val originalPort = state.integrations.udpPort
|
||||||
|
show(state)
|
||||||
|
|
||||||
|
val portField = onNode(hasSetTextAction())
|
||||||
|
val refusal = onNodeWithText(
|
||||||
|
"Shared UDP port for QSO and TRX messages [default 12060]:: \"abc\" is not a valid entry.",
|
||||||
|
)
|
||||||
|
portField.performTextReplacement("abc")
|
||||||
|
onNodeWithText("Next").performClick()
|
||||||
|
|
||||||
|
assertEquals(originalPort, state.integrations.udpPort)
|
||||||
|
assertEquals(WizardPage.LOG, state.page)
|
||||||
|
refusal.assertExists()
|
||||||
|
|
||||||
|
portField.performTextReplacement("12061")
|
||||||
|
refusal.assertExists()
|
||||||
|
onNodeWithText("Next").performClick()
|
||||||
|
|
||||||
|
assertEquals(WizardPage.GEAR, state.page)
|
||||||
|
assertEquals(12061, state.integrations.udpPort)
|
||||||
|
refusal.assertDoesNotExist()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun finishRefusesAnUnreadableUdpPort() = runComposeUiTest {
|
||||||
|
val state = SetupWizardState(firstInstallation())
|
||||||
|
state.walkToTheLogPage()
|
||||||
|
state.integrations.chooseLogProgram(WizardLogProgram.UDP)
|
||||||
|
show(state)
|
||||||
|
|
||||||
|
onNode(hasSetTextAction()).performTextReplacement("abc")
|
||||||
|
onNodeWithText("Finish").performClick()
|
||||||
|
|
||||||
|
assertEquals(WizardPage.LOG, state.page)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun theSummaryNamesWhatANewStationSwitchesOff() = runComposeUiTest {
|
fun theSummaryNamesWhatANewStationSwitchesOff() = runComposeUiTest {
|
||||||
val state = SetupWizardState(firstInstallation())
|
val state = SetupWizardState(firstInstallation())
|
||||||
|
|||||||
@@ -79,6 +79,17 @@ class WizardAntennaTest {
|
|||||||
assertFalse(antenna.complete)
|
assertFalse(antenna.complete)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun antennaHeightMustBeNonNegative() {
|
||||||
|
val antenna = WizardAntenna(stored())
|
||||||
|
|
||||||
|
antenna.heightMeters = -5.0
|
||||||
|
assertFalse(antenna.complete)
|
||||||
|
|
||||||
|
antenna.heightMeters = 0.0
|
||||||
|
assertTrue(antenna.complete)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun writingPutsTheAnswersIntoThePreferences() {
|
fun writingPutsTheAnswersIntoThePreferences() {
|
||||||
val target = prefs()
|
val target = prefs()
|
||||||
|
|||||||
+3
-1
@@ -301,6 +301,7 @@ class WizardIntegrationsTest {
|
|||||||
@Test
|
@Test
|
||||||
fun loadingAgainPutsTheDetailsBackToTheStoredOnes() {
|
fun loadingAgainPutsTheDetailsBackToTheStoredOnes() {
|
||||||
val answers = WizardIntegrations(winTestStation())
|
val answers = WizardIntegrations(winTestStation())
|
||||||
|
val storedFilePath = answers.filePath
|
||||||
answers.udpPort = 1
|
answers.udpPort = 1
|
||||||
answers.wintestPort = 2
|
answers.wintestPort = 2
|
||||||
answers.wintestStationName = "typed"
|
answers.wintestStationName = "typed"
|
||||||
@@ -316,6 +317,7 @@ class WizardIntegrationsTest {
|
|||||||
assertEquals(12060, answers.udpPort)
|
assertEquals(12060, answers.udpPort)
|
||||||
assertEquals(9999, answers.wintestPort)
|
assertEquals(9999, answers.wintestPort)
|
||||||
assertEquals("KST", answers.wintestStationName)
|
assertEquals("KST", answers.wintestStationName)
|
||||||
|
assertEquals(storedFilePath, answers.filePath)
|
||||||
assertEquals("127.0.0.1", answers.rotatorHost)
|
assertEquals("127.0.0.1", answers.rotatorHost)
|
||||||
assertEquals(12000, answers.rotatorPort)
|
assertEquals(12000, answers.rotatorPort)
|
||||||
assertEquals(9872, answers.airScoutPort)
|
assertEquals(9872, answers.airScoutPort)
|
||||||
@@ -326,7 +328,7 @@ class WizardIntegrationsTest {
|
|||||||
answers.rotatorPort = 3
|
answers.rotatorPort = 3
|
||||||
answers.loadStored()
|
answers.loadStored()
|
||||||
|
|
||||||
assertFalse(answers.filePath == "/typed", "the stored file name is back")
|
assertEquals(storedFilePath, answers.filePath)
|
||||||
assertEquals(12000, answers.rotatorPort)
|
assertEquals(12000, answers.rotatorPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user