diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/tabs/ExternalInterfaceFields.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/tabs/ExternalInterfaceFields.kt index 5b0e728d..051f611c 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/tabs/ExternalInterfaceFields.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/tabs/ExternalInterfaceFields.kt @@ -18,10 +18,12 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.runtime.staticCompositionLocalOf import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import androidx.compose.ui.window.AwtWindow +import java.awt.Dialog import java.awt.FileDialog import java.awt.Frame import java.io.File @@ -125,34 +127,51 @@ internal fun FilePathRow( } } -/** - * The AWT file dialog, opened from Compose. The JavaFX chooser started in the user's - * home directory and a cancelled choice kept the stored path — both are preserved. - */ +/** The modal owner that keeps the native file chooser accessible when opened from the wizard. */ +val LocalFileDialogOwner = staticCompositionLocalOf { null } + +/** The AWT file dialog, opened from Compose. */ @Composable private fun FileChooserDialog(title: String, onResult: (String?) -> Unit) { + val owner = LocalFileDialogOwner.current AwtWindow( create = { - object : FileDialog(null as Frame?, title, LOAD) { - override fun setVisible(visible: Boolean) { - if (visible) { - directory = System.getProperty("user.home") - } - super.setVisible(visible) - if (visible) { - val chosenName = file - onResult( - if (chosenName == null) null - else File(directory ?: "", chosenName).absolutePath - ) - } - } - } + 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) { + if (visible) { + directory = System.getProperty("user.home") + } + super.setVisible(visible) + if (visible) { + val chosenName = file + onResult( + if (chosenName == null) null + else File(directory ?: "", chosenName).absolutePath + ) + } + } +} + /** The label column width the shared form controls use, so the tabs line up. */ @Composable private fun LabelledRow(label: String, field: @Composable () -> Unit) { diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt index 339edd9d..a9dbbd1b 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardContent.kt @@ -16,6 +16,10 @@ import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable 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.Modifier import androidx.compose.ui.semantics.Role @@ -23,6 +27,8 @@ import androidx.compose.ui.unit.dp import kst4contest.model.ChatCategory import kst4contest.view.compose.Density import kst4contest.view.compose.Form +import kst4contest.view.compose.LocalSettingsDraft +import kst4contest.view.compose.SettingsDraft import kst4contest.view.compose.tabs.FilePathRow import kst4contest.view.i18n.LanguageStore import kst4contest.view.i18n.LocalStrings @@ -60,8 +66,17 @@ fun SetupWizardContent( onSave: (connect: Boolean) -> Unit, ) { 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()) } - 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()) { Column(modifier = Modifier.padding(15.dp)) { Column( @@ -80,6 +95,9 @@ fun SetupWizardContent( } } + if (typedEntryRefusals.isNotEmpty()) { + Refusal(typedEntryRefusals.joinToString("\n")) + } Row( modifier = Modifier.fillMaxWidth().padding(top = 10.dp), horizontalArrangement = Arrangement.spacedBy(Density.BUTTON_GAP), @@ -90,22 +108,23 @@ fun SetupWizardContent( Form.button(strings.wizardBack, onClick = state::back) } 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( strings.wizardSaveAndConnect, enabled = state.mandatoryComplete, accented = true, - ) { onSave(true) } + ) { withAcceptedTypedEntries { onSave(true) } } } else { if (state.canFinish) { - Form.button(strings.wizardFinish, onClick = state::finish) + Form.button(strings.wizardFinish) { withAcceptedTypedEntries(state::finish) } } Form.button( strings.wizardNext, enabled = state.canGoNext, 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() }) { 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 } Form.decimal(strings.stationOwnTxPower, antenna.txPowerWatts, isValid = { it.isFinite() }) { diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt index c6297620..70932114 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/SetupWizardWindow.kt @@ -1,5 +1,6 @@ package kst4contest.view.compose.wizard +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -9,6 +10,7 @@ import kst4contest.model.ChatPreferences import kst4contest.view.compose.Kst4ContestTheme import kst4contest.view.compose.WindowPlacementOnScreen import kst4contest.view.compose.openOnEventThread +import kst4contest.view.compose.tabs.LocalFileDialogOwner import java.awt.Dialog import java.awt.Dimension import java.util.concurrent.atomic.AtomicReference @@ -96,21 +98,23 @@ object SetupWizardWindow { darkMode = prefs.isGUI_darkModeActiveByDefault(), baseFontSizeSp = DIALOG_FONT_SIZE_SP, ) { - SetupWizardContent( - state = state, - languageStore = languageStore, - saveFailed = saveFailed, - onLater = { dialog.dispose() }, - onSave = { connect -> - val saved = saveFromWizard(state, prefs, connect) - if (saved == null) { - saveFailed = true - } else { - outcome.set(saved) - dialog.dispose() - } - }, - ) + CompositionLocalProvider(LocalFileDialogOwner provides dialog) { + SetupWizardContent( + state = state, + languageStore = languageStore, + saveFailed = saveFailed, + onLater = { dialog.dispose() }, + onSave = { connect -> + val saved = saveFromWizard(state, prefs, connect) + if (saved == null) { + saveFailed = true + } else { + outcome.set(saved) + dialog.dispose() + } + }, + ) + } } } diff --git a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardAntenna.kt b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardAntenna.kt index 9b14357e..3e3c259f 100644 --- a/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardAntenna.kt +++ b/app-desktop/src/main/kotlin/kst4contest/view/compose/wizard/WizardAntenna.kt @@ -55,7 +55,7 @@ class WizardAntenna(prefs: ChatPreferences) { /** Whether every answer can be stored as it stands. */ val complete: Boolean 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]. */ fun writeTo(prefs: ChatPreferences) { diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt index 21a50d60..1783ef1b 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/SetupWizardContentScreenTest.kt @@ -2,8 +2,10 @@ package kst4contest.view.compose.wizard import androidx.compose.ui.test.ComposeUiTest import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.performTextReplacement import androidx.compose.ui.test.runComposeUiTest import kst4contest.model.ChatCategory import kst4contest.model.ChatPreferences @@ -168,6 +170,47 @@ class SetupWizardContentScreenTest { 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 fun theSummaryNamesWhatANewStationSwitchesOff() = runComposeUiTest { val state = SetupWizardState(firstInstallation()) diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardAntennaTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardAntennaTest.kt index 237e3e69..e5cd502a 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardAntennaTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardAntennaTest.kt @@ -79,6 +79,17 @@ class WizardAntennaTest { 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 fun writingPutsTheAnswersIntoThePreferences() { val target = prefs() diff --git a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt index 8dee2ba8..760d0526 100644 --- a/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt +++ b/app-desktop/src/test/kotlin/kst4contest/view/compose/wizard/WizardIntegrationsTest.kt @@ -301,6 +301,7 @@ class WizardIntegrationsTest { @Test fun loadingAgainPutsTheDetailsBackToTheStoredOnes() { val answers = WizardIntegrations(winTestStation()) + val storedFilePath = answers.filePath answers.udpPort = 1 answers.wintestPort = 2 answers.wintestStationName = "typed" @@ -316,6 +317,7 @@ class WizardIntegrationsTest { assertEquals(12060, answers.udpPort) assertEquals(9999, answers.wintestPort) assertEquals("KST", answers.wintestStationName) + assertEquals(storedFilePath, answers.filePath) assertEquals("127.0.0.1", answers.rotatorHost) assertEquals(12000, answers.rotatorPort) assertEquals(9872, answers.airScoutPort) @@ -326,7 +328,7 @@ class WizardIntegrationsTest { answers.rotatorPort = 3 answers.loadStored() - assertFalse(answers.filePath == "/typed", "the stored file name is back") + assertEquals(storedFilePath, answers.filePath) assertEquals(12000, answers.rotatorPort) }