Introduce vibrateIfPossible into the utils package. This will check if the browser

has support for vibrating. This fixes a number of bugs for iOS safari & friends.
This commit is contained in:
Haze Booth
2023-08-24 12:02:22 -04:00
parent 6f02912d2e
commit 63d51e9119
12 changed files with 34 additions and 24 deletions

View File

@@ -12,7 +12,7 @@
import {cubicIn} from 'svelte/easing'; import {cubicIn} from 'svelte/easing';
import { flip } from 'svelte/animate'; import { flip } from 'svelte/animate';
import { type ContainerLayout, type WidgetLayout, type IDragItem } from "$lib/stores/layoutStates"; import { type ContainerLayout, type WidgetLayout, type IDragItem } from "$lib/stores/layoutStates";
import { startDrag, stopDrag } from "$lib/utils" import { startDrag, stopDrag, vibrateIfPossible } from "$lib/utils"
import { writable, type Writable } from "svelte/store"; import { writable, type Writable } from "svelte/store";
import { isHidden } from "$lib/widgets/utils"; import { isHidden } from "$lib/widgets/utils";
import { handleContainerConsider, handleContainerFinalize } from "./utils"; import { handleContainerConsider, handleContainerFinalize } from "./utils";
@@ -56,7 +56,7 @@
}; };
function handleClick(e: CustomEvent<boolean>) { function handleClick(e: CustomEvent<boolean>) {
navigator.vibrate(20) vibrateIfPossible(20)
$isOpen = e.detail $isOpen = e.detail
} }

View File

@@ -12,7 +12,7 @@
import {cubicIn} from 'svelte/easing'; import {cubicIn} from 'svelte/easing';
import { flip } from 'svelte/animate'; import { flip } from 'svelte/animate';
import { type ContainerLayout, type WidgetLayout, type IDragItem, type WritableLayoutStateStore } from "$lib/stores/layoutStates"; import { type ContainerLayout, type WidgetLayout, type IDragItem, type WritableLayoutStateStore } from "$lib/stores/layoutStates";
import { startDrag, stopDrag } from "$lib/utils" import { startDrag, stopDrag, vibrateIfPossible } from "$lib/utils"
import type { Writable } from "svelte/store"; import type { Writable } from "svelte/store";
import { isHidden } from "$lib/widgets/utils"; import { isHidden } from "$lib/widgets/utils";
import { handleContainerConsider, handleContainerFinalize } from "./utils"; import { handleContainerConsider, handleContainerFinalize } from "./utils";
@@ -62,7 +62,7 @@
} }
function handleSelect() { function handleSelect() {
navigator.vibrate(20) vibrateIfPossible(20)
} }
function _startDrag(e: MouseEvent | TouchEvent) { function _startDrag(e: MouseEvent | TouchEvent) {

View File

@@ -828,3 +828,9 @@ const MOBILE_USER_AGENTS = ["iPhone", "iPad", "Android", "BlackBerry", "WebOs"].
export function isMobileBrowser(userAgent: string): boolean { export function isMobileBrowser(userAgent: string): boolean {
return MOBILE_USER_AGENTS.some(a => userAgent.match(a)) return MOBILE_USER_AGENTS.some(a => userAgent.match(a))
} }
export function vibrateIfPossible(strength: number | Array<number>) {
if (window.navigator.vibrate) {
window.navigator.vibrate(strength);
}
}

View File

@@ -3,6 +3,7 @@
import { Button } from "@gradio/button"; import { Button } from "@gradio/button";
import { get, type Writable, writable } from "svelte/store"; import { get, type Writable, writable } from "svelte/store";
import { isDisabled } from "./utils" import { isDisabled } from "./utils"
import { vibrateIfPossible } from "$lib/utils";
import type { ComfyButtonNode } from "$lib/nodes/widgets"; import type { ComfyButtonNode } from "$lib/nodes/widgets";
export let widget: WidgetLayout | null = null; export let widget: WidgetLayout | null = null;
@@ -24,7 +25,7 @@
function onClick(e: MouseEvent) { function onClick(e: MouseEvent) {
node.onClick(); node.onClick();
navigator.vibrate(20) vibrateIfPossible(20)
} }
const style = { const style = {

View File

@@ -4,6 +4,7 @@
import { Checkbox } from "@gradio/form"; import { Checkbox } from "@gradio/form";
import { get, type Writable, writable } from "svelte/store"; import { get, type Writable, writable } from "svelte/store";
import { isDisabled } from "./utils" import { isDisabled } from "./utils"
import { vibrateIfPossible } from "$lib/utils";
import type { SelectData } from "@gradio/utils"; import type { SelectData } from "@gradio/utils";
import type { ComfyCheckboxNode } from "$lib/nodes/widgets"; import type { ComfyCheckboxNode } from "$lib/nodes/widgets";
@@ -11,7 +12,7 @@
export let isMobile: boolean = false; export let isMobile: boolean = false;
let node: ComfyCheckboxNode | null = null; let node: ComfyCheckboxNode | null = null;
let nodeValue: Writable<boolean> | null = null; let nodeValue: Writable<boolean> | null = null;
let attrsChanged: Writable<number> | null = null; let attrsChanged: Wrivtable<number> | null = null;
$: widget && setNodeValue(widget); $: widget && setNodeValue(widget);
@@ -25,7 +26,7 @@
function onSelect(e: CustomEvent<SelectData>) { function onSelect(e: CustomEvent<SelectData>) {
$nodeValue = e.detail.selected $nodeValue = e.detail.selected
navigator.vibrate(20) vibrateIfPossible(20)
} }
</script> </script>

View File

@@ -8,7 +8,7 @@
import { type WidgetLayout } from "$lib/stores/layoutStates"; import { type WidgetLayout } from "$lib/stores/layoutStates";
import { get, writable, type Writable } from "svelte/store"; import { get, writable, type Writable } from "svelte/store";
import { isDisabled } from "./utils" import { isDisabled } from "./utils"
import { clamp, getSafetensorsMetadata } from '$lib/utils'; import { clamp, getSafetensorsMetadata, vibrateIfPossible } from '$lib/utils';
export let widget: WidgetLayout | null = null; export let widget: WidgetLayout | null = null;
export let isMobile: boolean = false; export let isMobile: boolean = false;
let node: ComfyComboNode | null = null; let node: ComfyComboNode | null = null;
@@ -70,7 +70,7 @@
function onFocus() { function onFocus() {
// console.warn("FOCUS") // console.warn("FOCUS")
if (listOpen) { if (listOpen) {
navigator.vibrate(20) vibrateIfPossible(20)
} }
} }
@@ -86,7 +86,7 @@
function handleSelect(index: number) { function handleSelect(index: number) {
// console.warn("SEL", index) // console.warn("SEL", index)
navigator.vibrate(20) vibrateIfPossible(20)
const item = $valuesForCombo[index] const item = $valuesForCombo[index]
activeIndex = index; activeIndex = index;
$nodeValue = item.value $nodeValue = item.value

View File

@@ -3,7 +3,7 @@
import { type WidgetLayout } from "$lib/stores/layoutStates"; import { type WidgetLayout } from "$lib/stores/layoutStates";
import { Range } from "$lib/components/gradio/form"; import { Range } from "$lib/components/gradio/form";
import { get, type Writable } from "svelte/store"; import { get, type Writable } from "svelte/store";
import { debounce } from "$lib/utils"; import { debounce, vibrateIfPossible } from "$lib/utils";
import interfaceState from "$lib/stores/interfaceState"; import interfaceState from "$lib/stores/interfaceState";
import { isDisabled } from "./utils" import { isDisabled } from "./utils"
export let widget: WidgetLayout | null = null; export let widget: WidgetLayout | null = null;
@@ -96,7 +96,7 @@
lastDisplayValue = option; lastDisplayValue = option;
canVibrate = false; canVibrate = false;
setTimeout(() => { canVibrate = true }, 30) setTimeout(() => { canVibrate = true }, 30)
navigator.vibrate(10) vibrateIfPossible(10)
} }
} }
</script> </script>

View File

@@ -5,7 +5,7 @@
import { get, type Writable, writable } from "svelte/store"; import { get, type Writable, writable } from "svelte/store";
import { isDisabled } from "./utils" import { isDisabled } from "./utils"
import type { SelectData } from "@gradio/utils"; import type { SelectData } from "@gradio/utils";
import { clamp } from "$lib/utils"; import { clamp, vibrateIfPossible } from "$lib/utils";
import type { ComfyRadioNode } from "$lib/nodes/widgets"; import type { ComfyRadioNode } from "$lib/nodes/widgets";
export let widget: WidgetLayout | null = null; export let widget: WidgetLayout | null = null;
@@ -34,7 +34,7 @@
function onSelect(e: CustomEvent<SelectData>) { function onSelect(e: CustomEvent<SelectData>) {
node.setValue(e.detail.value) node.setValue(e.detail.value)
node.index = e.detail.index as number node.index = e.detail.index as number
navigator.vibrate(20) vibrateIfPossible(20)
} }
</script> </script>

View File

@@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import ComfyApp, { type SerializedAppState } from "$lib/components/ComfyApp"; import ComfyApp, { type SerializedAppState } from "$lib/components/ComfyApp";
import workflowState, { ComfyBoxWorkflow } from "$lib/stores/workflowState"; import workflowState, { ComfyBoxWorkflow } from "$lib/stores/workflowState";
import { vibrateIfPossible } from "$lib/utils";
import { Link, Toolbar } from "framework7-svelte" import { Link, Toolbar } from "framework7-svelte"
@@ -11,7 +12,7 @@
$: workflow = $workflowState.activeWorkflow; $: workflow = $workflowState.activeWorkflow;
function queuePrompt() { function queuePrompt() {
navigator.vibrate(20) vibrateIfPossible(20)
app.runDefaultQueueAction() app.runDefaultQueueAction()
} }
</script> </script>

View File

@@ -2,7 +2,7 @@
import ComfyApp, { type SerializedAppState } from "$lib/components/ComfyApp"; import ComfyApp, { type SerializedAppState } from "$lib/components/ComfyApp";
import queueState from "$lib/stores/queueState"; import queueState from "$lib/stores/queueState";
import workflowState, { ComfyBoxWorkflow } from "$lib/stores/workflowState"; import workflowState, { ComfyBoxWorkflow } from "$lib/stores/workflowState";
import { getNodeInfo } from "$lib/utils" import { getNodeInfo, vibrateIfPossible } from "$lib/utils"
import { LayoutTextSidebarReverse, Image, Grid } from "svelte-bootstrap-icons"; import { LayoutTextSidebarReverse, Image, Grid } from "svelte-bootstrap-icons";
import { Link, Toolbar } from "framework7-svelte" import { Link, Toolbar } from "framework7-svelte"
@@ -21,12 +21,12 @@
$: workflow = $workflowState.activeWorkflow; $: workflow = $workflowState.activeWorkflow;
function queuePrompt() { function queuePrompt() {
navigator.vibrate(20) vibrateIfPossible(20)
app.runDefaultQueueAction() app.runDefaultQueueAction()
} }
async function refreshCombos() { async function refreshCombos() {
navigator.vibrate(20) vibrateIfPossible(20)
await app.refreshComboInNodes() await app.refreshComboInNodes()
} }
@@ -34,7 +34,7 @@
if (!fileInput) if (!fileInput)
return; return;
navigator.vibrate(20) vibrateIfPossible(20)
app.querySave() app.querySave()
} }
@@ -42,7 +42,7 @@
if (!fileInput) if (!fileInput)
return; return;
navigator.vibrate(20) vibrateIfPossible(20)
fileInput.value = null; fileInput.value = null;
fileInput.click(); fileInput.click();
} }
@@ -52,7 +52,7 @@
} }
function doSaveLocal(): void { function doSaveLocal(): void {
navigator.vibrate(20) vibrateIfPossible(20)
app.saveStateToLocalStorage(); app.saveStateToLocalStorage();
} }

View File

@@ -34,12 +34,12 @@
} }
async function refreshCombos() { async function refreshCombos() {
navigator.vibrate(20) vibrateIfPossible(20)
await app.refreshComboInNodes() await app.refreshComboInNodes()
} }
function doSaveLocal(): void { function doSaveLocal(): void {
navigator.vibrate(20) vibrateIfPossible(20)
app.saveStateToLocalStorage(); app.saveStateToLocalStorage();
} }

View File

@@ -3,6 +3,7 @@
import workflowState, { ComfyBoxWorkflow, type WorkflowInstID } from "$lib/stores/workflowState"; import workflowState, { ComfyBoxWorkflow, type WorkflowInstID } from "$lib/stores/workflowState";
import { onMount } from "svelte"; import { onMount } from "svelte";
import interfaceState from "$lib/stores/interfaceState"; import interfaceState from "$lib/stores/interfaceState";
import { vibrateIfPossible } from "$lib/utils";
import { f7 } from 'framework7-svelte'; import { f7 } from 'framework7-svelte';
import { XCircle } from 'svelte-bootstrap-icons'; import { XCircle } from 'svelte-bootstrap-icons';
@@ -31,7 +32,7 @@
if (!fileInput) if (!fileInput)
return; return;
navigator.vibrate(20) vibrateIfPossible(20);
fileInput.value = null; fileInput.value = null;
fileInput.click(); fileInput.click();
} }