From 8232a9342abfb1b35d90281fe5281f31a71b20fd Mon Sep 17 00:00:00 2001 From: Uriel Date: Thu, 15 May 2025 20:43:05 -0400 Subject: [PATCH] start adding support for inches --- gui/package.json | 1 + gui/public/i18n/en/translation.ftl | 5 ++ gui/src/components/commons/Dropdown.tsx | 12 ++-- gui/src/components/commons/HeightDisplay.tsx | 71 +++++++++++++++++++ gui/src/components/commons/LangSelector.tsx | 4 +- gui/src/components/commons/NumberSelector.tsx | 16 ++--- gui/src/components/commons/UnitSelector.tsx | 54 ++++++++++++++ gui/src/components/onboarding/pages/Home.tsx | 4 +- .../scaled-steps/ManualHeightStep.tsx | 64 ++++++++++------- .../settings/pages/InterfaceSettings.tsx | 15 ++++ gui/src/hooks/config.ts | 17 ++++- gui/src/hooks/height.ts | 2 + gui/tailwind.config.ts | 4 +- pnpm-lock.yaml | 8 +++ 14 files changed, 231 insertions(+), 46 deletions(-) create mode 100644 gui/src/components/commons/HeightDisplay.tsx create mode 100644 gui/src/components/commons/UnitSelector.tsx diff --git a/gui/package.json b/gui/package.json index 138723e1b..d7a260d7b 100644 --- a/gui/package.json +++ b/gui/package.json @@ -25,6 +25,7 @@ "@twemoji/svg": "^15.0.0", "browser-fs-access": "^0.35.0", "classnames": "^2.5.1", + "convert": "^5.10.0", "flatbuffers": "22.10.26", "intl-pluralrules": "^2.0.1", "ip-num": "^1.5.1", diff --git a/gui/public/i18n/en/translation.ftl b/gui/public/i18n/en/translation.ftl index eb41704bc..057e1ca49 100644 --- a/gui/public/i18n/en/translation.ftl +++ b/gui/public/i18n/en/translation.ftl @@ -651,6 +651,11 @@ settings-interface-appearance-font_size-description = This affects the font size settings-interface-appearance-decorations = Use the system native decorations settings-interface-appearance-decorations-description = This will not render the top bar of the interface and will use the operating system's instead. settings-interface-appearance-decorations-label = Use native decorations +settings-interface-appearance-unit_system = Select unit system +settings-interface-appearance-unit_system-description = Change if you want to use the metric system or the imperial system for body measurements! +settings-interface-appearance-unit_system-placeholder = Select the unit system to use +settings-interface-appearance-unit_system-metric = ( cm - °C ) Metric units +settings-interface-appearance-unit_system-imperial = ( ' " - °F ) Imperial units ## Notification settings settings-interface-notifications = Notifications diff --git a/gui/src/components/commons/Dropdown.tsx b/gui/src/components/commons/Dropdown.tsx index ec6c5e8d1..c2c055969 100644 --- a/gui/src/components/commons/Dropdown.tsx +++ b/gui/src/components/commons/Dropdown.tsx @@ -72,7 +72,7 @@ export function DropdownItems({
{ diff --git a/gui/src/components/commons/HeightDisplay.tsx b/gui/src/components/commons/HeightDisplay.tsx new file mode 100644 index 000000000..e80755d7f --- /dev/null +++ b/gui/src/components/commons/HeightDisplay.tsx @@ -0,0 +1,71 @@ +import { defaultConfig, UnitType, useConfig } from '@/hooks/config'; +import { useLocaleConfig } from '@/i18n/config'; +import convert from 'convert'; +import { useMemo } from 'react'; + +export function HeightDisplay({ + height, + /** + * Unit type of only the input height + */ + heightUnit = 'm', + unitDisplay = 'short', + className, +}: { + heightUnit?: 'm' | 'cm' | 'in'; + height: number; + unitDisplay?: 'narrow' | 'short' | 'long'; + className?: string; +}) { + const { currentLocales } = useLocaleConfig(); + const { config } = useConfig(); + + const cmFormat = useMemo( + () => + new Intl.NumberFormat(currentLocales, { + style: 'unit', + unit: 'centimeter', + unitDisplay, + maximumFractionDigits: 2, + }), + [currentLocales, unitDisplay] + ); + + const [footFormat, inchesFormat] = useMemo( + () => [ + new Intl.NumberFormat(currentLocales, { + style: 'unit', + unit: 'foot', + unitDisplay: 'narrow', + maximumFractionDigits: 0, + roundingMode: 'trunc', + }), + new Intl.NumberFormat(currentLocales, { + style: 'unit', + unit: 'inch', + unitDisplay: 'narrow', + maximumFractionDigits: 1, + roundingMode: 'trunc', + }), + ], + [currentLocales] + ); + + const value = useMemo(() => { + const unitSystem = config?.unitSystem ?? defaultConfig.unitSystem; + + if (unitSystem === UnitType.Metric) { + return cmFormat.format(convert(height, heightUnit).to('cm')); + } else { + const totalInches = convert(height, heightUnit).to('inches'); + const feet = Math.trunc(totalInches / 12); + const remainingInches = totalInches % 12; + return ( + (feet ? footFormat.format(feet) : '') + + inchesFormat.format(remainingInches) + ); + } + }, [config?.unitSystem, height, heightUnit]); + + return {value}; +} diff --git a/gui/src/components/commons/LangSelector.tsx b/gui/src/components/commons/LangSelector.tsx index aaf6cfa8b..3cf36bbfb 100644 --- a/gui/src/components/commons/LangSelector.tsx +++ b/gui/src/components/commons/LangSelector.tsx @@ -1,7 +1,7 @@ import { useLocalization } from '@fluent/react'; import { useEffect, useMemo, useContext } from 'react'; import { useForm } from 'react-hook-form'; -import { useConfig } from '@/hooks/config'; +import { defaultConfig, useConfig } from '@/hooks/config'; import { LangContext } from '@/i18n/config'; import { langs } from '@/i18n/names'; import { Dropdown, DropdownDirection } from './Dropdown'; @@ -17,7 +17,7 @@ export function LangSelector({ const { l10n } = useLocalization(); const { config, setConfig } = useConfig(); const { control, watch, handleSubmit } = useForm<{ lang: string }>({ - defaultValues: { lang: config?.lang || 'en' }, + defaultValues: { lang: config?.lang ?? defaultConfig.lang }, }); const languagesItems = useMemo( diff --git a/gui/src/components/commons/NumberSelector.tsx b/gui/src/components/commons/NumberSelector.tsx index a21d35634..19a9bca5d 100644 --- a/gui/src/components/commons/NumberSelector.tsx +++ b/gui/src/components/commons/NumberSelector.tsx @@ -1,7 +1,7 @@ import { Control, Controller } from 'react-hook-form'; import { Button } from './Button'; import { Typography } from './Typography'; -import { useCallback, useMemo } from 'react'; +import { ReactNode, useCallback, useMemo } from 'react'; import { useLocaleConfig } from '@/i18n/config'; export function NumberSelector({ @@ -14,10 +14,10 @@ export function NumberSelector({ step, doubleStep, disabled = false, - showButtonWithNumber = false, + showButtonWithNumber, }: { label?: string; - valueLabelFormat?: (value: number) => string; + valueLabelFormat?: (value: number) => string | ReactNode; control: Control; name: string; min: number; @@ -25,7 +25,7 @@ export function NumberSelector({ step: number | ((value: number, add: boolean) => number); doubleStep?: number; disabled?: boolean; - showButtonWithNumber?: boolean; + showButtonWithNumber?: number; }) { const { currentLocales } = useLocaleConfig(); @@ -69,8 +69,8 @@ export function NumberSelector({ onClick={() => onChange(doubleStepFn(value, false))} disabled={doubleStepFn(value, false) < min || disabled} > - {showButtonWithNumber - ? decimalFormat.format(-doubleStep) + {showButtonWithNumber !== undefined + ? decimalFormat.format(-showButtonWithNumber) : '--'} )} @@ -102,8 +102,8 @@ export function NumberSelector({ onClick={() => onChange(doubleStepFn(value, true))} disabled={doubleStepFn(value, true) > max || disabled} > - {showButtonWithNumber - ? decimalFormat.format(doubleStep) + {showButtonWithNumber !== undefined + ? decimalFormat.format(showButtonWithNumber) : '++'} )} diff --git a/gui/src/components/commons/UnitSelector.tsx b/gui/src/components/commons/UnitSelector.tsx new file mode 100644 index 000000000..9f8c0003b --- /dev/null +++ b/gui/src/components/commons/UnitSelector.tsx @@ -0,0 +1,54 @@ +import { defaultConfig, UnitType, useConfig } from '@/hooks/config'; +import { Dropdown, DropdownDirection } from './Dropdown'; +import { useForm } from 'react-hook-form'; +import { useEffect, useMemo } from 'react'; +import { useLocalization } from '@fluent/react'; + +export function UnitSelector({ + direction = 'up', + alignment = 'right', +}: { + direction?: DropdownDirection; + alignment?: 'right' | 'left'; +}) { + const { l10n } = useLocalization(); + const { config, setConfig } = useConfig(); + const { control, watch, handleSubmit } = useForm<{ unitSystem: UnitType }>({ + defaultValues: { + unitSystem: config?.unitSystem ?? defaultConfig.unitSystem, + }, + }); + + const unitItems = useMemo( + () => + Object.values(UnitType).map((type) => ({ + label: l10n.getString( + `settings-interface-appearance-unit_system-${type}` + ), + value: type, + })), + [l10n] + ); + + useEffect(() => { + const subscription = watch(() => handleSubmit(onSubmit)()); + return () => subscription.unsubscribe(); + }, []); + + const onSubmit = (value: { unitSystem: UnitType }) => { + setConfig({ unitSystem: value.unitSystem }); + }; + + return ( + + ); +} diff --git a/gui/src/components/onboarding/pages/Home.tsx b/gui/src/components/onboarding/pages/Home.tsx index 3c33c21d3..1c5221363 100644 --- a/gui/src/components/onboarding/pages/Home.tsx +++ b/gui/src/components/onboarding/pages/Home.tsx @@ -4,6 +4,7 @@ import { Button } from '@/components/commons/Button'; import { SlimeVRIcon } from '@/components/commons/icon/SimevrIcon'; import { LangSelector } from '@/components/commons/LangSelector'; import { Typography } from '@/components/commons/Typography'; +import { UnitSelector } from '@/components/commons/UnitSelector'; export function HomePage() { const { l10n } = useLocalization(); @@ -23,8 +24,9 @@ export function HomePage() { {l10n.getString('onboarding-home-start')}
-
+
+
{ reset({ height: - (currentHeight && currentHeight / EYE_HEIGHT_TO_HEIGHT_RATIO) || - DEFAULT_FULL_HEIGHT, + (currentHeight && + convert(currentHeight / EYE_HEIGHT_TO_HEIGHT_RATIO, 'm').to( + currentUnit + )) || + currentUnit === 'm' + ? DEFAULT_FULL_HEIGHT + : DEFAULT_FULL_HEIGHT_INCHES, }); }, [currentHeight]); - const mFormat = useMemo( - () => - new Intl.NumberFormat(currentLocales, { - style: 'unit', - unit: 'meter', - maximumFractionDigits: 2, - }), - [currentLocales] - ); - const submitFullHeight = (values: HeightForm) => { - const newHeight = values.height * EYE_HEIGHT_TO_HEIGHT_RATIO; + const newHeight = + convert(values.height, currentUnit).to('m') * EYE_HEIGHT_TO_HEIGHT_RATIO; setHmdHeight(newHeight); const settingsRequest = new ChangeSettingsRequestT(); settingsRequest.modelSettings = new ModelSettingsT( @@ -104,17 +103,23 @@ export function ManualHeightStep({ 'onboarding-scaled_proportions-manual_height-height-v2' )} valueLabelFormat={(value) => - isNaN(value) - ? l10n.getString( - 'onboarding-scaled_proportions-manual_height-unknown' - ) - : mFormat.format(value) + isNaN(value) ? ( + l10n.getString( + 'onboarding-scaled_proportions-manual_height-unknown' + ) + ) : ( + + ) } - min={MIN_HEIGHT} - max={4} - step={0.01} - showButtonWithNumber - doubleStep={0.1} + min={convert(MIN_HEIGHT, 'm').to(currentUnit)} + max={convert(4, 'm').to(currentUnit)} + step={currentUnit === 'm' ? 0.01 : 1} + showButtonWithNumber={currentUnit === 'm' ? 10 : 12} + doubleStep={currentUnit === 'm' ? 0.1 : 12} />
@@ -124,7 +129,12 @@ export function ManualHeightStep({ )} - {mFormat.format(height * EYE_HEIGHT_TO_HEIGHT_RATIO)} +
diff --git a/gui/src/components/settings/pages/InterfaceSettings.tsx b/gui/src/components/settings/pages/InterfaceSettings.tsx index f8d9ebb7d..9393c2117 100644 --- a/gui/src/components/settings/pages/InterfaceSettings.tsx +++ b/gui/src/components/settings/pages/InterfaceSettings.tsx @@ -18,6 +18,7 @@ import { Range } from '@/components/commons/Range'; import { Dropdown } from '@/components/commons/Dropdown'; import { ArrowRightLeftIcon } from '@/components/commons/icon/ArrowIcons'; import { isTrayAvailable } from '@/utils/tauri'; +import { UnitSelector } from '@/components/commons/UnitSelector'; interface InterfaceSettingsForm { appearance: { @@ -521,6 +522,20 @@ export function InterfaceSettings() {
+ + + {l10n.getString('settings-interface-appearance-unit_system')} + +
+ + {l10n.getString( + 'settings-interface-appearance-unit_system-description' + )} + +
+
+ +
diff --git a/gui/src/hooks/config.ts b/gui/src/hooks/config.ts index 1a84ca329..ea1f9ca53 100644 --- a/gui/src/hooks/config.ts +++ b/gui/src/hooks/config.ts @@ -1,4 +1,4 @@ -import { createContext, useContext, useState } from 'react'; +import { createContext, useContext, useMemo, useState } from 'react'; import { DeveloperModeWidgetForm } from '@/components/widgets/DeveloperModeWidget'; import { error } from '@/utils/logging'; import { useDebouncedEffect } from './timeout'; @@ -22,6 +22,11 @@ export enum AssignMode { All = 'all', } +export enum UnitType { + Metric = 'metric', + Imperial = 'imperial', +} + export interface Config { debug: boolean; lang: string; @@ -42,6 +47,7 @@ export interface Config { decorations: boolean; showNavbarOnboarding: boolean; vrcMutedWarnings: string[]; + unitSystem: UnitType; } export interface ConfigContext { @@ -69,6 +75,7 @@ export const defaultConfig: Omit = { decorations: false, showNavbarOnboarding: true, vrcMutedWarnings: [], + unitSystem: UnitType.Metric, }; interface CrossStorage { @@ -195,3 +202,11 @@ export function useConfig() { } return context; } + +export function useUnit() { + const { config } = useConfig(); + return useMemo(() => { + const unitSystem = config?.unitSystem ?? defaultConfig.unitSystem; + return unitSystem === UnitType.Metric ? 'm' : 'in'; + }, [config?.unitSystem]); +} diff --git a/gui/src/hooks/height.ts b/gui/src/hooks/height.ts index 24c1b1a0f..6459614a3 100644 --- a/gui/src/hooks/height.ts +++ b/gui/src/hooks/height.ts @@ -2,6 +2,7 @@ import { createContext, useContext, useEffect, useMemo, useState } from 'react'; import { useWebsocketAPI } from './websocket-api'; import { RpcMessage, SettingsRequestT, SettingsResponseT } from 'solarxr-protocol'; import { MIN_HEIGHT } from './manual-proportions'; +import convert from 'convert'; export interface HeightContext { hmdHeight: number | null; @@ -73,4 +74,5 @@ export const EYE_HEIGHT_TO_HEIGHT_RATIO = 0.936; // Based on average human height (1.65m) // From https://ourworldindata.org/human-height (January 2024) export const DEFAULT_FULL_HEIGHT = 1.65; +export const DEFAULT_FULL_HEIGHT_INCHES = Math.round(convert(DEFAULT_FULL_HEIGHT, 'm').to('in')); export const DEFAULT_EYE_HEIGHT = DEFAULT_FULL_HEIGHT * EYE_HEIGHT_TO_HEIGHT_RATIO; diff --git a/gui/tailwind.config.ts b/gui/tailwind.config.ts index 34ab01a0b..84c4ad571 100644 --- a/gui/tailwind.config.ts +++ b/gui/tailwind.config.ts @@ -258,10 +258,12 @@ const config = { addUtilities({ '.text-main-title': textConfig('calc(var(--font-size-title) / 16)', 700), '.text-section-title': textConfig('calc(var(--font-size-vr) / 16)', 700), + '.text-standard': textConfig('calc(var(--font-size-standard) / 16)', 500), + '.text-standard-bold': textConfig('calc(var(--font-size-standard) / 16)', 700), + '.text-vr-accesible': textConfig('calc(var(--font-size-vr) / 16)', 500), '.text-vr-accesible-bold': textConfig('calc(var(--font-size-vr) / 16)', 700), - '.text-standard-bold': textConfig('calc(var(--font-size-standard) / 16)', 700), }); }), plugin(function ({ addVariant }) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c7d9bc83..5e896e3e3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -80,6 +80,9 @@ importers: classnames: specifier: ^2.5.1 version: 2.5.1 + convert: + specifier: ^5.10.0 + version: 5.10.0 flatbuffers: specifier: 22.10.26 version: 22.10.26 @@ -1861,6 +1864,9 @@ packages: resolution: {integrity: sha512-cj09EBuObp9gZNQCzc7hByQyrs6jVGE+o9kSJmeUoj+GiPiJvi5LYqEH/Hmme4+MTLHM+Ejtq+FChpjjEnsPdQ==} engines: {node: '>= 4'} + convert@5.10.0: + resolution: {integrity: sha512-1agZ7UULsa0AVUZefVDmfy7hdHDIm7hr/tB8scKwSy8HE8a+5c3EQsCTEQfx6/+8LGNa7Re2+4fir9mKxFsBuw==} + create-require@1.1.1: resolution: {integrity: sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==} @@ -6147,6 +6153,8 @@ snapshots: convert-to-spaces@1.0.2: {} + convert@5.10.0: {} + create-require@1.1.1: {} cross-env@7.0.3: