mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Add temperature-units config option
This commit is contained in:
1 parent
1780057f3c
commit
5256ab1ddc
8 files changed
+48
-6
No files matched your search
@@ -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";
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<TimeFormat>({
|
||||
description: "Whether the top-bar clock shows 24-hour or 12-hour time.",
|
||||
});
|
||||
|
||||
export const temperatureUnitSetting = new ConfigSettingEnum<TemperatureUnit>({
|
||||
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
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user