diff --git a/shell/plugins/panels/weather/Model.js b/shell/plugins/panels/weather/Model.js index 1a6a8e6b..6d919396 100644 --- a/shell/plugins/panels/weather/Model.js +++ b/shell/plugins/panels/weather/Model.js @@ -69,6 +69,18 @@ function parseGeocodingResults(raw) { } } +function locationCommit(text, suggestions, selectedIndex) { + var name = String(text || "").replace(/^\s+|\s+$/g, "") + if (name === "") return { name: "", latitude: null, longitude: null } + + var choices = suggestions || [] + var index = Math.max(0, Math.min(parseInt(selectedIndex, 10) || 0, choices.length - 1)) + var suggestion = choices[index] + if (suggestion) return suggestion + + return { name: name, latitude: null, longitude: null } +} + function isFutureForecastDate(dateString, todayString) { if (!dateString) return false return String(dateString).slice(0, 10) > String(todayString || "") @@ -250,6 +262,7 @@ if (typeof module !== "undefined") { parseLocationFile: parseLocationFile, wttrLocationQuery: wttrLocationQuery, parseGeocodingResults: parseGeocodingResults, + locationCommit: locationCommit, isFutureForecastDate: isFutureForecastDate, roundedTemp: roundedTemp, celsiusToFahrenheit: celsiusToFahrenheit, diff --git a/shell/plugins/panels/weather/Panel.qml b/shell/plugins/panels/weather/Panel.qml index eae1416c..a9669822 100644 --- a/shell/plugins/panels/weather/Panel.qml +++ b/shell/plugins/panels/weather/Panel.qml @@ -183,11 +183,13 @@ Panel { } function commitLocation() { - if (locationField.text.trim() === "") { + var location = Model.locationCommit(locationField.text, locationSuggestions, suggestionIndex) + if (location.name === "") { clearLocation() return } - pickSuggestion(locationSuggestions[Math.min(suggestionIndex, locationSuggestions.length - 1)]) + persistLocation(location.name, location.latitude, location.longitude) + cancelEditingLocation() } function clearLocation() { @@ -203,8 +205,10 @@ Panel { } function persistLocation(name, latitude, longitude) { - if (name) + if (name && latitude !== null && longitude !== null) Quickshell.execDetached(["omarchy-weather-location", "--set", name, latitude + "," + longitude]) + else if (name) + Quickshell.execDetached(["omarchy-weather-location", "--set", name]) else Quickshell.execDetached(["omarchy-weather-location", "--clear"]) } diff --git a/test/shell.d/weather-test.sh b/test/shell.d/weather-test.sh index a06788b4..a93568bd 100644 --- a/test/shell.d/weather-test.sh +++ b/test/shell.d/weather-test.sh @@ -20,6 +20,14 @@ assertDeepEqual(weather.parseLocationFile('{"name": "Malibu", "latitude": 34.025 assertDeepEqual(weather.parseLocationFile('not json'), { name: '', latitude: null, longitude: null }, 'weather treats an unparseable weather.json as auto-detect') assertDeepEqual(weather.parseLocationFile(''), { name: '', latitude: null, longitude: null }, 'weather treats a missing weather.json as auto-detect') +assertDeepEqual(weather.locationCommit(' Pasadena ', [], 0), { name: 'Pasadena', latitude: null, longitude: null }, 'weather commits typed locations before suggestions load') +assertDeepEqual(weather.locationCommit('', [], 0), { name: '', latitude: null, longitude: null }, 'weather commits an empty location as auto-detect') +assertDeepEqual( + weather.locationCommit('mal', [{ name: 'Malibu', latitude: 34.02577, longitude: -118.7804 }], 0), + { name: 'Malibu', latitude: 34.02577, longitude: -118.7804 }, + 'weather commits the selected geocoding suggestion when available' +) + assertEqual(weather.wttrLocationQuery('Malibu', 34.02577, -118.7804), '34.02577,-118.7804', 'weather prefers coordinates for the wttr query') assertEqual(weather.wttrLocationQuery('Malibu', '34.02577', '-118.7804'), '34.02577,-118.7804', 'weather accepts string coordinates') assertEqual(weather.wttrLocationQuery('New York', null, null), 'New%20York', 'weather URL-encodes a name-only location')