Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

chore: [Idle detection] Deal with users on systems that don't handle emoji (#2941)

Thomas Steiner1a67642fd11d
diff --git a/src/renderer/renderScene.ts b/src/renderer/renderScene.ts
index 70b5c1a260..6c9fd83701 100644
--- a/src/renderer/renderScene.ts
+++ b/src/renderer/renderScene.ts
@@ -47,9 +47,11 @@ import {
TransformHandles,
TransformHandleType,
} from "../element/transformHandles";
-import { viewportCoordsToSceneCoords } from "../utils";
+import { viewportCoordsToSceneCoords, supportsEmoji } from "../utils";
import { UserIdleState } from "../excalidraw-app/collab/types";
+const hasEmojiSupport = supportsEmoji();
+
const strokeRectWithRotation = (
context: CanvasRenderingContext2D,
x: number,
@@ -449,7 +451,7 @@ export const renderScene = (
const userState = sceneState.remotePointerUserStates[clientId];
if (isOutOfBounds || userState === UserIdleState.AWAY) {
- context.globalAlpha = 0.2;
+ context.globalAlpha = 0.48;
}
if (
@@ -481,13 +483,24 @@ export const renderScene = (
context.stroke();
const username = sceneState.remotePointerUsernames[clientId];
- const usernameAndIdleState = `${username ? `${username} ` : ""}${
- userState === UserIdleState.AWAY
- ? "⚫️"
- : userState === UserIdleState.IDLE
- ? "💤"
- : "🟢"
- }`;
+ let usernameAndIdleState;
+ if (hasEmojiSupport) {
+ usernameAndIdleState = `${username ? `${username} ` : ""}${
+ userState === UserIdleState.AWAY
+ ? "⚫️"
+ : userState === UserIdleState.IDLE
+ ? "💤"
+ : "🟢"
+ }`;
+ } else {
+ usernameAndIdleState = `${username ? `${username}` : ""}${
+ userState === UserIdleState.AWAY
+ ? ` (${UserIdleState.AWAY})`
+ : userState === UserIdleState.IDLE
+ ? ` (${UserIdleState.IDLE})`
+ : ""
+ }`;
+ }
if (!isOutOfBounds && usernameAndIdleState) {
const offsetX = x + width;
diff --git a/src/utils.ts b/src/utils.ts
index 936b2850c6..2532aefcca 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -369,3 +369,20 @@ export const getVersion = () => {
DEFAULT_VERSION
);
};
+
+// Adapted from https://github.com/Modernizr/Modernizr/blob/master/feature-detects/emoji.js
+export const supportsEmoji = () => {
+ const canvas = document.createElement("canvas");
+ const ctx = canvas.getContext("2d");
+ if (!ctx) {
+ return false;
+ }
+ const offset = 12;
+ ctx.fillStyle = "#f00";
+ ctx.textBaseline = "top";
+ ctx.font = "32px Arial";
+ // Modernizr used 🐨, but it is sort of supported on Windows 7.
+ // Luckily 😀 isn't supported.
+ ctx.fillText("😀", 0, 0);
+ return ctx.getImageData(offset, offset, 1, 1).data[0] !== 0;
+};