From 5256ab1ddcb9c6f65d0cd817d952978faf4b31da Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sat, 24 Oct 2026 00:16:50 -0700 Subject: [PATCH] Add temperature-units config option --- app/apps/weather/temperature-format.ts | 17 +++++++++++++++++ app/apps/weather/weather-app.ts | 7 ++++++- app/apps/weather/weather-icons.ts | 3 ++- app/apps/weather/weather.ts | 7 ++++--- app/ui/dashboard-settings.ts | 11 +++++++++++ app/ui/dashboard/settings-menus.ts | 3 +++ tests/ios-location.test.cjs | 2 +- tests/weather.test.cjs | 4 ++++ 8 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 app/apps/weather/temperature-format.ts diff --git a/app/apps/weather/temperature-format.ts b/app/apps/weather/temperature-format.ts new file mode 100644 index 00000000..9fdadcc8 --- /dev/null +++ b/app/apps/weather/temperature-format.ts @@ -0,0 +1,17 @@ +import { temperatureUnitSetting } from "../../ui/dashboard-settings"; + +/** + * A temperature (weather data is kept in Fahrenheit) in the unit chosen in + * Settings > Customization > Temperature unit, rounded: "72°F" / "22°C", or + * "72°" without the unit letter. + */ +export function formatTemperature(fahrenheit: number, withUnit = true): string { + const celsius = temperatureUnitSetting.get() === "celsius"; + const value = Math.round(celsius ? (fahrenheit - 32) * 5 / 9 : fahrenheit); + return withUnit ? `${value}°${celsius ? "C" : "F"}` : `${value}°`; +} + +/** The widest temperature the forecast's column is sized for. */ +export function widestTemperatureSample(): string { + return temperatureUnitSetting.get() === "celsius" ? "-20°C" : "100°F"; +} diff --git a/app/apps/weather/weather-app.ts b/app/apps/weather/weather-app.ts index 7f75dcdf..96d2ec83 100644 --- a/app/apps/weather/weather-app.ts +++ b/app/apps/weather/weather-app.ts @@ -1,6 +1,6 @@ import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions"; import { weatherBridge } from "../../native/weather"; -import { toggleSettingMenuItem } from "../../ui/dashboard-settings"; +import { onAnySettingChanged, toggleSettingMenuItem } from "../../ui/dashboard-settings"; import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel"; import { WeatherLayer } from "./weather"; import { weatherShowInStatusBarSetting, weatherShowOnSystemCardSetting } from "./weather-settings"; @@ -19,6 +19,7 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW let closed = false; let requestingPermission = false; let unsubscribe: (() => void) | null = null; + let unsubscribeSettings: (() => void) | null = null; const requestUpdate = () => { if (closed || requestingPermission) return; @@ -81,11 +82,15 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW closed = true; unsubscribe?.(); unsubscribe = null; + unsubscribeSettings?.(); + unsubscribeSettings = null; weatherBridge.stop(); options.onClosed(); }, }); unsubscribe = weatherBridge.onStateChange(() => app.requestRender()); + // Repaint for a temperature-unit change made in the Settings app. + unsubscribeSettings = onAnySettingChanged(() => app.requestRender()); requestUpdate(); return app; } diff --git a/app/apps/weather/weather-icons.ts b/app/apps/weather/weather-icons.ts index 0c2a3008..0a21173f 100644 --- a/app/apps/weather/weather-icons.ts +++ b/app/apps/weather/weather-icons.ts @@ -1,6 +1,7 @@ import { renderSvgIcon } from "../../graphics/icons"; import { type GrayImage, type UiFont } from "../../graphics/image"; import { type WeatherCondition, type WeatherState } from "../../native/weather"; +import { formatTemperature } from "./temperature-format"; // Lucide weather icons (ISC licensed), kept verbatim so they can be diffed // against upstream. @@ -90,7 +91,7 @@ export function weatherReading(state: WeatherState, nowMs: number): WeatherReadi return { condition: current.condition, isDaytime: current.isDaytime, - temperature: current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°`, + temperature: current.temperatureF === null ? "--°" : formatTemperature(current.temperatureF, false), }; } diff --git a/app/apps/weather/weather.ts b/app/apps/weather/weather.ts index b62a22fe..d6c1e2af 100644 --- a/app/apps/weather/weather.ts +++ b/app/apps/weather/weather.ts @@ -7,6 +7,7 @@ import { type Layer, type LayerContext } from "../../ui/layers"; import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; import { renderWeatherIcon } from "./weather-icons"; +import { formatTemperature, widestTemperatureSample } from "./temperature-format"; const PAGE_X = 18; const HEADER_Y = 8; @@ -115,7 +116,7 @@ export class WeatherLayer implements Layer { const large = getDefaultLargeFont(); const medium = getDefaultMediumFont(); const small = getDefaultSmallFont(); - const temperature = current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°F`; + const temperature = current.temperatureF === null ? "--°" : formatTemperature(current.temperatureF); const temperatureY = CURRENT_TOP + 8; let temperatureX = PAGE_X; if (current.condition) { @@ -182,7 +183,7 @@ export class WeatherLayer implements Layer { ...forecast.map((period) => font.measureText(period.name)), ); const tempX = PAGE_X + nameWidth + FORECAST_COL_GAP; - const precipX = tempX + Math.max(font.measureText("Temp"), font.measureText("100°F")) + FORECAST_COL_GAP; + const precipX = tempX + Math.max(font.measureText("Temp"), font.measureText(widestTemperatureSample())) + FORECAST_COL_GAP; const summaryX = precipX + Math.max(font.measureText("Rain"), font.measureText("100%")) + FORECAST_COL_GAP; image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150); @@ -209,7 +210,7 @@ export class WeatherLayer implements Layer { const shade = selected ? 245 : 190; const textY = centeredTextY(font, y, height); image.drawText(font, x + columns.name, textY, period.name, shade); - image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade); + image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : formatTemperature(period.temperatureF), shade); image.drawText(font, x + columns.precip, textY, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155); // The summary stops PAGE_X short of the viewport's right edge, as the name starts PAGE_X in from the left. const summaryWidth = width - columns.summary - columns.name; diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 87450ba7..3472def5 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -35,6 +35,7 @@ export type BatteryDisplayMode = "icon" | "percentage" | "stacked" | "stacked-pe /** When a top-bar battery indicator is shown: always, only below 50%, or never. */ export type BatteryIndicatorVisibility = "always" | "low" | "never"; export type TimeFormat = "24h" | "12h"; +export type TemperatureUnit = "fahrenheit" | "celsius"; export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "3m" | "never"; // Auto runs Faceclaw's light curve. Numeric values use the calibrated firmware // scale, clamped to 2–100; 2 is also the endpoint for screen fades. @@ -330,6 +331,16 @@ export const timeFormatSetting = new ConfigSettingEnum({ description: "Whether the top-bar clock shows 24-hour or 12-hour time.", }); +export const temperatureUnitSetting = new ConfigSettingEnum({ + id: "temperatureUnit", + label: "Temperature unit", + storageKey: "display.temperatureUnit", + defaultValue: "fahrenheit", + values: ["fahrenheit", "celsius"], + formatValue: (value) => value === "celsius" ? "Celsius" : "Fahrenheit", + description: "Whether the Weather app, its top-bar indicator, and the Glanceboard's system card show temperatures in Fahrenheit or Celsius.", +}); + /** * Where the app switcher (the open-window icons) sits: a strip along the * left or right edge of the display, a row under the window, or a popup box diff --git a/app/ui/dashboard/settings-menus.ts b/app/ui/dashboard/settings-menus.ts index d9e8bbc1..efdc9670 100644 --- a/app/ui/dashboard/settings-menus.ts +++ b/app/ui/dashboard/settings-menus.ts @@ -76,6 +76,7 @@ import { saveVoiceRecordingsSetting, showBleBandwidthSetting, suspendEvenHubWhenScreenOffSetting, + temperatureUnitSetting, textSettingMenuItem, timeFormatSetting, toggleSettingMenuItem, @@ -289,6 +290,8 @@ function customizationRows(): () => MenuItem[] { animationsMenuItem(), // Controls the top-bar clock (24-hour vs 12-hour). enumSettingMenuItem(timeFormatSetting), + // Fahrenheit or Celsius, wherever weather temperatures show. + enumSettingMenuItem(temperatureUnitSetting), ]; const bottomRows = [font, position, depth, statusBar, ...rest]; const popupRows = [font, position, depth, statusBar, statusBarVisibility, windowBorder, ...rest]; diff --git a/tests/ios-location.test.cjs b/tests/ios-location.test.cjs index 226a154b..63ac50f6 100644 --- a/tests/ios-location.test.cjs +++ b/tests/ios-location.test.cjs @@ -118,7 +118,7 @@ test('closing Weather during permission prompt cannot restart polling', async () '../../native/weather': { weatherBridge: { start() { starts++; }, stop() { stops++; }, onStateChange: () => () => {}, snapshot() {} } }, './weather': { WeatherLayer: class {} }, './weather-settings': {}, - '../../ui/dashboard-settings': { toggleSettingMenuItem: () => ({}) }, + '../../ui/dashboard-settings': { toggleSettingMenuItem: () => ({}), onAnySettingChanged: () => () => {} }, '../../ui/dashboard/settings-panel': { openSettingsSubMenu() {} }, '../../ui/shell/in-process-window': { YieldAtRootLayer: class {}, createInProcessWindow: opts => { options = opts; return { requestRender() {} }; } }, })('app/apps/weather/weather-app.ts'); diff --git a/tests/weather.test.cjs b/tests/weather.test.cjs index 27b44bff..73e3ca1a 100644 --- a/tests/weather.test.cjs +++ b/tests/weather.test.cjs @@ -273,6 +273,7 @@ function indicatorHarness() { } }, '../../ui/dashboard-settings': { ConfigSettingBoolean: Setting, + temperatureUnitSetting: new Setting({ storageKey: 'display.temperatureUnit', defaultValue: 'fahrenheit' }), onAnySettingChanged: listener => { settingListeners.add(listener); return () => settingListeners.delete(listener); }, }, '../../ui/shell/shell': { shell: { setTrayIcon: (id, icon) => trays.push([id, icon]) } }, @@ -308,6 +309,9 @@ test('Weather tray icon follows the status-bar setting and hides a stale reading assert.equal(icon.height, 24); assert.deepEqual(icon.draws.map(draw => draw[0]), ['image', 'text']); assert.equal(icon.draws[1][3], '72°'); + h.setSetting('display.temperatureUnit', 'celsius'); + assert.equal(h.trays.at(-1)[1].draws[1][3], '22°', 'the temperature unit setting rebuilds the icon'); + h.setSetting('display.temperatureUnit', 'fahrenheit'); // An unchanged reading does not rebuild the icon. const count = h.trays.length;