forked from Blendman974/faceclaw
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;
|
||||
|
||||
Reference in new issue
Block a user