From 53ba9dffd9048dc64397c6fe33321dad9ea40f69 Mon Sep 17 00:00:00 2001 From: Zsolt Viczian Date: Sun, 20 Mar 2022 18:14:47 +0100 Subject: [PATCH] setDeviceType --- src/components/Actions.tsx | 5 ++++- src/components/App.tsx | 15 +++++++++------ src/components/LayerUI.tsx | 11 ++++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/components/Actions.tsx b/src/components/Actions.tsx index b29207207..48ef943c4 100644 --- a/src/components/Actions.tsx +++ b/src/components/Actions.tsx @@ -14,7 +14,7 @@ import { hasText, } from "../scene"; import { SHAPES } from "../shapes"; -import { AppState, Zoom } from "../types"; +import { AppState, DeviceType, Zoom } from "../types"; import { capitalizeString, isTransparent, setCursorForShape } from "../utils"; import Stack from "./Stack"; import { ToolButton } from "./ToolButton"; @@ -187,6 +187,7 @@ export const ShapesSwitcher = ({ onImageAction, appState, penDetected, + setDeviceType, }: { canvas: HTMLCanvasElement | null; elementType: AppState["elementType"]; @@ -194,6 +195,7 @@ export const ShapesSwitcher = ({ onImageAction: (data: { pointerType: PointerType | null }) => void; appState: AppState; penDetected: boolean; + setDeviceType: (obj: Partial) => void; }) => { return ( <> @@ -219,6 +221,7 @@ export const ShapesSwitcher = ({ onChange={({ pointerType }) => { if (!penDetected && pointerType === "pen") { setAppState({ penMode: true }); + setDeviceType({ penDetected: true }); } setAppState({ elementType: value, diff --git a/src/components/App.tsx b/src/components/App.tsx index 136905a4d..f61d58e92 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -497,6 +497,7 @@ class App extends React.Component { appState={this.state} files={this.files} setAppState={this.setAppState} + setDeviceType={this.setDeviceType} actionManager={this.actionManager} elements={this.scene.getElements()} onCollabButtonClick={onCollabButtonClick} @@ -902,7 +903,7 @@ class App extends React.Component { // --------------------------------------------------------------------- const { width, height } = this.excalidrawContainerRef.current!.getBoundingClientRect(); - this.deviceType = updateObject(this.deviceType, { + this.setDeviceType({ isMobile: width < MQ_MAX_WIDTH_PORTRAIT || (height < MQ_MAX_HEIGHT_LANDSCAPE && @@ -918,9 +919,7 @@ class App extends React.Component { `(max-width: ${MQ_MAX_WIDTH_PORTRAIT}px), (max-height: ${MQ_MAX_HEIGHT_LANDSCAPE}px) and (max-width: ${MQ_MAX_WIDTH_LANDSCAPE}px)`, ); const handler = () => { - this.deviceType = updateObject(this.deviceType, { - isMobile: mediaQuery.matches, - }); + this.setDeviceType({ isMobile: mediaQuery.matches }); }; mediaQuery.addListener(handler); this.detachIsMobileMqHandler = () => mediaQuery.removeListener(handler); @@ -1562,6 +1561,10 @@ class App extends React.Component { this.setState(obj); }; + setDeviceType = (obj: Partial): void => { + this.deviceType = updateObject(this.deviceType, obj); + }; + removePointer = (event: React.PointerEvent | PointerEvent) => { if (touchTimeout) { this.resetContextMenuTimer(); @@ -2866,7 +2869,7 @@ class App extends React.Component { //fires only once, if pen is detected, penMode is enabled //the user can disable this by toggling the penMode button if (!this.deviceType.penDetected && event.pointerType === "pen") { - this.deviceType = updateObject(this.deviceType, { penDetected: true }); + this.setDeviceType({ penDetected: true }); this.setState({ penMode: true }); } @@ -2874,7 +2877,7 @@ class App extends React.Component { !this.deviceType.isTouchScreen && ["pen", "touch"].includes(event.pointerType) ) { - this.deviceType = updateObject(this.deviceType, { isTouchScreen: true }); + this.setDeviceType({ isTouchScreen: true }); } if (isPanning) { diff --git a/src/components/LayerUI.tsx b/src/components/LayerUI.tsx index 276b228aa..b549a9e0a 100644 --- a/src/components/LayerUI.tsx +++ b/src/components/LayerUI.tsx @@ -8,7 +8,13 @@ import { NonDeletedExcalidrawElement } from "../element/types"; import { Language, t } from "../i18n"; import { calculateScrollCenter, getSelectedElements } from "../scene"; import { ExportType } from "../scene/types"; -import { AppProps, AppState, ExcalidrawProps, BinaryFiles } from "../types"; +import { + AppProps, + AppState, + ExcalidrawProps, + BinaryFiles, + DeviceType, +} from "../types"; import { muteFSAbortError } from "../utils"; import { SelectedShapeActions, ShapesSwitcher, ZoomActions } from "./Actions"; import { BackgroundPickerAndDarkModeToggle } from "./BackgroundPickerAndDarkModeToggle"; @@ -44,6 +50,7 @@ interface LayerUIProps { files: BinaryFiles; canvas: HTMLCanvasElement | null; setAppState: React.Component["setState"]; + setDeviceType: (obj: Partial) => void; elements: readonly NonDeletedExcalidrawElement[]; onCollabButtonClick?: () => void; onLockToggle: () => void; @@ -74,6 +81,7 @@ const LayerUI = ({ appState, files, setAppState, + setDeviceType, canvas, elements, onCollabButtonClick, @@ -353,6 +361,7 @@ const LayerUI = ({ }); }} penDetected={deviceType.penDetected} + setDeviceType={setDeviceType} />