From 8da9e63c45418d1141f135db9dbc3ed69219c798 Mon Sep 17 00:00:00 2001 From: Uriel Date: Sat, 1 Jul 2023 16:59:21 -0700 Subject: [PATCH 1/2] Fix Dropdown not opening (#744) --- gui/src/components/commons/Dropdown.tsx | 46 ++++++++++++++----- gui/src/components/commons/LangSelector.tsx | 9 ++-- .../components/settings/SettingsLayout.tsx | 3 +- .../settings/SettingsPageLayout.tsx | 14 +++++- gui/src/components/settings/pages/Serial.tsx | 9 ++-- gui/tailwind.config.ts | 6 +++ 6 files changed, 66 insertions(+), 21 deletions(-) diff --git a/gui/src/components/commons/Dropdown.tsx b/gui/src/components/commons/Dropdown.tsx index 40c98374f..f3f856a92 100644 --- a/gui/src/components/commons/Dropdown.tsx +++ b/gui/src/components/commons/Dropdown.tsx @@ -1,6 +1,11 @@ import classNames from 'classnames'; import { useEffect, useState } from 'react'; -import { Control, Controller } from 'react-hook-form'; +import { + Control, + Controller, + UseFormGetValues, + useWatch, +} from 'react-hook-form'; import { a11yClick } from '../utils/a11y'; export interface DropdownItem { @@ -17,6 +22,7 @@ export function Dropdown({ display = 'fit', placeholder, control, + getValues, name, items = [], }: { @@ -26,13 +32,29 @@ export function Dropdown({ display?: 'fit' | 'block'; placeholder: string; control: Control; + getValues: UseFormGetValues; name: string; items: DropdownItem[]; }) { + const itemRefs: Record = {}; const [isOpen, setOpen] = useState(false); + const formValue = { + ...{ value: useWatch({ control, name }) as string }, + ...{ value: getValues(name) as string }, + }; useEffect(() => { if (!isOpen) return; + const curItem = itemRefs[formValue.value]; + const dropdownParent = curItem + ? (curItem.closest('.dropdown-scroll') as HTMLElement | null) + : null; + if (curItem && dropdownParent) { + dropdownParent.scroll({ + top: curItem.offsetTop - dropdownParent.offsetHeight / 2, + }); + } + function onWheelEvent() { if (isOpen && !document.querySelector('div.dropdown-scroll:hover')) { setOpen(false); @@ -55,9 +77,9 @@ export function Dropdown({ const isInDropdownScroll = document .querySelector('div.dropdown-scroll') ?.contains(event.target as HTMLDivElement); - const isInDropdown = document - .querySelector('div.dropdown') - ?.contains(event.target as HTMLDivElement); + const isInDropdown = !!(event.target as HTMLDivElement).closest( + '.dropdown' + ); if (isOpen && !isInDropdownScroll && !isInDropdown) { setOpen(false); } @@ -151,21 +173,19 @@ export function Dropdown({ alignment === 'left' && 'left-0' )} > -
    +
      {items.map((item) => (
    • { onChange(item.value); @@ -176,8 +196,10 @@ export function Dropdown({ onChange(item.value); setOpen(false); }} + ref={(ref) => (ref ? (itemRefs[item.value] = ref) : {})} key={item.value} tabIndex={0} + data-checked={item.value === value} > {item.label}
    • diff --git a/gui/src/components/commons/LangSelector.tsx b/gui/src/components/commons/LangSelector.tsx index b70299eb7..cb3cb7426 100644 --- a/gui/src/components/commons/LangSelector.tsx +++ b/gui/src/components/commons/LangSelector.tsx @@ -15,9 +15,11 @@ export function LangSelector({ const { changeLocales } = useContext(LangContext); const { l10n } = useLocalization(); const { config, setConfig } = useConfig(); - const { control, watch, handleSubmit } = useForm<{ lang: string }>({ - defaultValues: { lang: config?.lang || 'en' }, - }); + const { control, watch, handleSubmit, getValues } = useForm<{ lang: string }>( + { + defaultValues: { lang: config?.lang || 'en' }, + } + ); const languagesItems = useMemo( () => langs.map(({ key, name }) => ({ label: name, value: key })), @@ -37,6 +39,7 @@ export function LangSelector({ return ( ({ defaultValues: { link: links[0].value.url }, @@ -65,6 +65,7 @@ export function SettingSelectorMobile() {
      ({ label, diff --git a/gui/src/components/settings/SettingsPageLayout.tsx b/gui/src/components/settings/SettingsPageLayout.tsx index 74201d9b1..82fbfb666 100644 --- a/gui/src/components/settings/SettingsPageLayout.tsx +++ b/gui/src/components/settings/SettingsPageLayout.tsx @@ -17,9 +17,19 @@ export function SettingsPageLayout({ if (!pageRef.current || !typedState || !typedState.scrollTo) { return; } - const elem = pageRef.current.querySelector(`#${typedState.scrollTo}`); + const elem = pageRef.current.querySelector( + `#${typedState.scrollTo}` + ) as HTMLElement | null; if (elem) { - elem.scrollIntoView({ behavior: 'smooth', block: 'start' }); + // stupid way of doing this, just get the closest overflow-y-auto + // usually its just the parentElem + const closestScroll = elem.closest( + '.overflow-y-auto' + ) as HTMLElement | null; + if (closestScroll) { + // The 45 is just enough padding for making the scroll look perfect + closestScroll.scroll({ top: elem.offsetTop - 45, behavior: 'smooth' }); + } } }, [state]); diff --git a/gui/src/components/settings/pages/Serial.tsx b/gui/src/components/settings/pages/Serial.tsx index d6d57a625..61b82ff2b 100644 --- a/gui/src/components/settings/pages/Serial.tsx +++ b/gui/src/components/settings/pages/Serial.tsx @@ -51,9 +51,10 @@ export function Serial() { const [tryFactoryReset, setTryFactoryReset] = useState(false); - const { control, watch, handleSubmit, reset } = useForm({ - defaultValues: { port: 'Auto' }, - }); + const { control, watch, handleSubmit, reset, getValues } = + useForm({ + defaultValues: { port: 'Auto' }, + }); const { port } = watch(); @@ -234,6 +235,7 @@ export function Serial() { {isMobile && ( Date: Sun, 2 Jul 2023 02:29:31 +0200 Subject: [PATCH 2/2] mobile body proportions (#718) Co-authored-by: Uriel --- .../components/commons/icon/ArrowIcons.tsx | 22 +- .../body-proportions/BodyProportions.tsx | 510 +++++++++++------- .../body-proportions/ManualProportions.tsx | 19 +- gui/src/hooks/manual-proportions.ts | 29 +- gui/src/index.css | 11 + 5 files changed, 373 insertions(+), 218 deletions(-) diff --git a/gui/src/components/commons/icon/ArrowIcons.tsx b/gui/src/components/commons/icon/ArrowIcons.tsx index 3ab59e37b..faa9ff0e1 100644 --- a/gui/src/components/commons/icon/ArrowIcons.tsx +++ b/gui/src/components/commons/icon/ArrowIcons.tsx @@ -1,11 +1,27 @@ -export function ArrowDownIcon() { +export function ArrowDownIcon({ size = 24 }: { size?: number }) { return ( + + + ); +} + +export function ArrowUpIcon({ size = 24 }: { size: number }) { + return ( + diff --git a/gui/src/components/onboarding/pages/body-proportions/BodyProportions.tsx b/gui/src/components/onboarding/pages/body-proportions/BodyProportions.tsx index 2714895ad..6a52015f9 100644 --- a/gui/src/components/onboarding/pages/body-proportions/BodyProportions.tsx +++ b/gui/src/components/onboarding/pages/body-proportions/BodyProportions.tsx @@ -1,6 +1,14 @@ import { useLocalization } from '@fluent/react'; import classNames from 'classnames'; -import { MouseEventHandler, ReactNode, useEffect } from 'react'; +import { + MouseEventHandler, + ReactNode, + useEffect, + useRef, + UIEvent, + MouseEvent, + useMemo, +} from 'react'; import { LabelType, ProportionChangeType, @@ -8,7 +16,7 @@ import { } from '../../../../hooks/manual-proportions'; import { useLocaleConfig } from '../../../../i18n/config'; import { Typography } from '../../../commons/Typography'; -import { useBreakpoint } from '../../../../hooks/breakpoint'; +import { ArrowDownIcon, ArrowUpIcon } from '../../../commons/icon/ArrowIcons'; function IncrementButton({ children, @@ -21,7 +29,8 @@ function IncrementButton({
      @@ -40,25 +49,25 @@ export function BodyProportions({ type: 'linear' | 'ratio'; variant: 'onboarding' | 'alone'; }) { - const { isMobile } = useBreakpoint('mobile'); - - const [bodyParts, _ratioMode, currentSelection, dispatch, setRatioMode] = - useManualProportions(); - const { currentLocales } = useLocaleConfig(); + const { bodyParts, dispatch, state, setRatioMode } = useManualProportions(); const { l10n } = useLocalization(); + const { currentLocales } = useLocaleConfig(); + + const srcollerRef = useRef(null); + const cmFormat = Intl.NumberFormat(currentLocales, { style: 'unit', unit: 'centimeter', maximumFractionDigits: 1, }); - const configFormat = Intl.NumberFormat(currentLocales, { - signDisplay: 'always', - maximumFractionDigits: 1, - }); const percentageFormat = Intl.NumberFormat(currentLocales, { style: 'percent', maximumFractionDigits: 1, }); + const configFormat = Intl.NumberFormat(currentLocales, { + signDisplay: 'always', + maximumFractionDigits: 1, + }); useEffect(() => { if (type === 'linear') { @@ -68,131 +77,229 @@ export function BodyProportions({ } }, [type]); + useEffect(() => { + if (srcollerRef.current && bodyParts.length > 0) { + moveToIndex(1); + } + }, [srcollerRef, bodyParts.length]); + + const handleUIEvent = (e: UIEvent) => { + const target = e.target as HTMLDivElement; + + const itemHeight = target.offsetHeight / 3; + + const atSnappingPoint = target.scrollTop % itemHeight === 0; + + if (atSnappingPoint) { + const index = target.scrollTop / itemHeight; + const elem = srcollerRef.current?.childNodes[index + 1] as HTMLDivElement; + const id = elem.getAttribute('itemid'); + if (id) selectNew(id); + } + }; + + const clickPart = (id: string) => (e: MouseEvent) => { + const target = e.target as HTMLDivElement; + target.scrollIntoView({ behavior: 'smooth', block: 'center' }); + selectNew(id); + }; + + const moveToIndex = (index: number, smooth = true) => { + // We add one because of the offset placeholder + const elem = srcollerRef.current?.childNodes[index + 1] as HTMLDivElement; + elem?.scrollIntoView({ + behavior: smooth ? 'smooth' : 'auto', + block: 'center', + }); + + const id = elem.getAttribute('itemid'); + if (id) selectNew(id); + }; + + const selectNew = (id: string) => { + const part = bodyParts.find(({ label }) => label === id); + if (!part) return; + + const { value: originalValue, label, type, ...props } = part; + + const value = + 'index' in props && props.index !== undefined + ? props.bones[props.index].value + : originalValue; + + switch (type) { + case LabelType.Bone: { + if (!('bone' in props)) throw 'unreachable'; + dispatch({ + ...props, + label, + value, + type: ProportionChangeType.Bone, + }); + break; + } + case LabelType.Group: { + if (!('bones' in props)) throw 'unreachable'; + dispatch({ + ...props, + label, + value, + type: ProportionChangeType.Group, + index: undefined, + parentLabel: label, + }); + break; + } + case LabelType.GroupPart: { + if (!('index' in props)) throw 'unreachable'; + dispatch({ + ...props, + label, + // If this isn't done, we are replacing total + // with percentage value + value: originalValue, + type: ProportionChangeType.Group, + index: props.index, + }); + } + } + }; + + const seletedLabel = useMemo(() => { + return bodyParts.find(({ label }) => label === state.currentLabel); + }, [state]); + + const move = (action: 'next' | 'prev') => { + const elem = srcollerRef.current?.querySelector( + `div[itemid=${state.currentLabel}]` + ); + + const moveId = (id: string, elem: HTMLDivElement) => { + elem?.scrollIntoView({ + behavior: 'smooth', + block: 'center', + }); + selectNew(id); + }; + + if (action === 'prev') { + const prevElem = elem?.previousSibling as HTMLDivElement; + const prevId = prevElem.getAttribute('itemid'); + if (!prevId) return; + moveId(prevId, prevElem); + } + + if (action === 'next') { + const nextElem = elem?.nextSibling as HTMLDivElement; + const nextId = nextElem.getAttribute('itemid'); + if (!nextId) return; + moveId(nextId, nextElem); + } + }; + return ( -
      -
      - <> - {bodyParts.map(({ label, type, value: originalValue, ...props }) => { - const value = - 'index' in props && props.index !== undefined - ? props.bones[props.index].value - : originalValue; - const selected = isMobile || currentSelection.label === label; - - const selectNew = () => { - switch (type) { - case LabelType.Bone: { - if (!('bone' in props)) throw 'unreachable'; - dispatch({ - ...props, - label, - value, - type: ProportionChangeType.Bone, - }); - break; - } - case LabelType.Group: { - if (!('bones' in props)) throw 'unreachable'; - dispatch({ - ...props, - label, - value, - type: ProportionChangeType.Group, - index: undefined, - parentLabel: label, - }); - break; - } - case LabelType.GroupPart: { - if (!('index' in props)) throw 'unreachable'; - dispatch({ - ...props, - label, - // If this isn't done, we are replacing total - // with percentage value - value: originalValue, - type: ProportionChangeType.Group, - index: props.index, - }); + (bodyParts.length > 0 && ( +
      +
      + {!precise && ( +
      + + seletedLabel?.type === LabelType.GroupPart + ? dispatch({ + type: ProportionChangeType.Ratio, + value: -0.05, + }) + : dispatch({ + type: ProportionChangeType.Linear, + value: -5, + }) } + > + {configFormat.format(-5)} + +
      + )} +
      + + seletedLabel?.type === LabelType.GroupPart + ? dispatch({ + type: ProportionChangeType.Ratio, + value: -0.01, + }) + : dispatch({ + type: ProportionChangeType.Linear, + value: -1, + }) } - }; + > + {configFormat.format(-1)} + +
      + {precise && ( +
      + + seletedLabel?.type === LabelType.GroupPart + ? dispatch({ + type: ProportionChangeType.Ratio, + value: -0.005, + }) + : dispatch({ + type: ProportionChangeType.Linear, + value: -0.5, + }) + } + > + {configFormat.format(-0.5)} + +
      + )} +
      +
      +
      +
      move('prev')} + className={classNames( + 'h-12 w-32 rounded-lg bg-background-60 flex flex-col justify-center', + 'items-center fill-background-10', + srcollerRef?.current?.scrollTop ?? 0 > 0 + ? 'opacity-100 active:bg-accent-background-30' + : 'opacity-50' + )} + > + +
      +
      +
      +
      + {bodyParts.map((part) => { + const { label, value: originalValue, type, ...props } = part; + const value = + 'index' in props && props.index !== undefined + ? props.bones[props.index].value + : originalValue; - return ( -
      + const selected = state.currentLabel === label; + + return (
      - {!precise && ( - { - selectNew(); - return type === LabelType.GroupPart - ? dispatch({ - type: ProportionChangeType.Ratio, - value: -0.05, - }) - : dispatch({ - type: ProportionChangeType.Linear, - value: -5, - }); - }} - > - {configFormat.format(-5)} - - )} - { - selectNew(); - return type === LabelType.GroupPart - ? dispatch({ - type: ProportionChangeType.Ratio, - value: -0.01, - }) - : dispatch({ - type: ProportionChangeType.Linear, - value: -1, - }); - }} - > - {configFormat.format(-1)} - - {precise && ( - { - selectNew(); - return type === LabelType.GroupPart - ? dispatch({ - type: ProportionChangeType.Ratio, - value: -0.005, - }) - : dispatch({ - type: ProportionChangeType.Linear, - value: -0.5, - }); - }} - > - {configFormat.format(-0.5)} - - )} -
      -
      @@ -211,70 +318,87 @@ export function BodyProportions({
      -
      - {precise && ( - { - selectNew(); - return type === LabelType.GroupPart - ? dispatch({ - type: ProportionChangeType.Ratio, - value: 0.005, - }) - : dispatch({ - type: ProportionChangeType.Linear, - value: 0.5, - }); - }} - > - {configFormat.format(+0.5)} - - )} - { - selectNew(); - return type === LabelType.GroupPart - ? dispatch({ - type: ProportionChangeType.Ratio, - value: 0.01, - }) - : dispatch({ - type: ProportionChangeType.Linear, - value: 1, - }); - }} - > - {configFormat.format(+1)} - - {!precise && ( - { - selectNew(); - return type === LabelType.GroupPart - ? dispatch({ - type: ProportionChangeType.Ratio, - value: 0.05, - }) - : dispatch({ - type: ProportionChangeType.Linear, - value: 5, - }); - }} - > - {configFormat.format(+5)} - - )} -
      -
      - ); - })} - + ); + })} +
      +
      +
      +
      move('next')} + className={classNames( + 'h-12 w-32 rounded-lg bg-background-60 flex flex-col justify-center', + 'items-center fill-background-10', + srcollerRef?.current?.scrollTop !== + (srcollerRef?.current?.scrollHeight ?? 0) - + (srcollerRef?.current?.offsetHeight ?? 0) + ? 'opacity-100 active:bg-accent-background-30' + : 'opacity-50' + )} + > + +
      +
      +
      + +
      + {precise && ( +
      + + seletedLabel?.type === LabelType.GroupPart + ? dispatch({ + type: ProportionChangeType.Ratio, + value: 0.005, + }) + : dispatch({ + type: ProportionChangeType.Linear, + value: 0.5, + }) + } + > + {configFormat.format(+0.5)} + +
      + )} + +
      + + seletedLabel?.type === LabelType.GroupPart + ? dispatch({ + type: ProportionChangeType.Ratio, + value: 0.01, + }) + : dispatch({ + type: ProportionChangeType.Linear, + value: 1, + }) + } + > + {configFormat.format(+1)} + +
      + {!precise && ( +
      + + seletedLabel?.type === LabelType.GroupPart + ? dispatch({ + type: ProportionChangeType.Ratio, + value: 0.05, + }) + : dispatch({ + type: ProportionChangeType.Linear, + value: 5, + }) + } + > + {configFormat.format(+5)} + +
      + )} +
      -
      + )) || <> ); } diff --git a/gui/src/components/onboarding/pages/body-proportions/ManualProportions.tsx b/gui/src/components/onboarding/pages/body-proportions/ManualProportions.tsx index a3d2b4873..7d551b30e 100644 --- a/gui/src/components/onboarding/pages/body-proportions/ManualProportions.tsx +++ b/gui/src/components/onboarding/pages/body-proportions/ManualProportions.tsx @@ -66,8 +66,8 @@ export function ManualProportionsPage() { <>
      -
      -
      +
      +
      {l10n.getString('onboarding-manual_proportions-title')} @@ -92,19 +92,20 @@ export function ManualProportionsPage() {
      )}
      - - +
      + +
      {!isMobile && ( -
      +
      )} diff --git a/gui/src/hooks/manual-proportions.ts b/gui/src/hooks/manual-proportions.ts index a1d5e6534..7f4471169 100644 --- a/gui/src/hooks/manual-proportions.ts +++ b/gui/src/hooks/manual-proportions.ts @@ -64,7 +64,7 @@ export interface BoneState { type: BoneType.Single; bone: SkeletonBone; value: number; - label: string; + currentLabel: string; } export interface GroupState { @@ -77,7 +77,7 @@ export interface GroupState { value: number; }[]; value: number; - label: string; + currentLabel: string; index?: number; parentLabel: string; } @@ -87,6 +87,7 @@ function reducer(state: ProportionState, action: ProportionChange): ProportionSt case ProportionChangeType.Bone: { return { ...action, + currentLabel: action.label, type: BoneType.Single, }; } @@ -94,6 +95,7 @@ function reducer(state: ProportionState, action: ProportionChange): ProportionSt case ProportionChangeType.Group: { return { ...action, + currentLabel: action.label, type: BoneType.Group, }; } @@ -114,7 +116,7 @@ function reducer(state: ProportionState, action: ProportionChange): ProportionSt case ProportionChangeType.Ratio: { if (state.type === BoneType.Single || state.index === undefined) { - throw new Error(`Unexpected increase of bone ${state.label}`); + throw new Error(`Unexpected increase of bone ${state.currentLabel}`); } const newState: GroupState = JSON.parse(JSON.stringify(state)); @@ -192,16 +194,16 @@ export const INVALID_BONE: BoneState = { type: BoneType.Single, bone: SkeletonBone.NONE, value: 0, - label: 'invalid-bone', + currentLabel: 'invalid-bone', }; -export function useManualProportions(): [ - Label[], - boolean, - ProportionState, - (change: ProportionChange) => void, - (ratio: boolean) => void -] { +export function useManualProportions(): { + bodyParts: Label[]; + ratioMode: boolean; + state: ProportionState; + dispatch: (change: ProportionChange) => void; + setRatioMode: (ratio: boolean) => void; +} { const { useRPCPacket, sendRPCPacket } = useWebsocketAPI(); const [config, setConfig] = useState | null>( null @@ -278,6 +280,7 @@ export function useManualProportions(): [ useLayoutEffect(() => { dispatch({ ...INVALID_BONE, + label: INVALID_BONE.currentLabel, type: ProportionChangeType.Bone, }); }, [ratio]); @@ -315,7 +318,7 @@ export function useManualProportions(): [ const part = bodyParts.find( (it) => it.type === LabelType.Group && - (it.label === state.label || it.label === state.parentLabel) + (it.label === state.currentLabel || it.label === state.parentLabel) ) as GroupLabel | undefined; // Check if we found the group we were looking for @@ -345,7 +348,7 @@ export function useManualProportions(): [ setConfig(conf); }, [state]); - return [bodyParts, ratio, state, dispatch, setRatio]; + return { bodyParts, ratioMode: ratio, state, dispatch, setRatioMode: setRatio }; } function roundedStep(value: number, step: number, add: boolean): number { diff --git a/gui/src/index.css b/gui/src/index.css index f60c28247..86ff2d843 100644 --- a/gui/src/index.css +++ b/gui/src/index.css @@ -257,6 +257,17 @@ body { height: 100%; } +/* Hide scrollbar for Chrome, Safari and Opera */ +.no-scrollbar::-webkit-scrollbar { + display: none; +} + +/* Hide scrollbar for IE, Edge and Firefox */ +.no-scrollbar { + -ms-overflow-style: none; /* IE and Edge */ + scrollbar-width: none; /* Firefox */ +} + ::-webkit-scrollbar { width: 8px; height: 8px;