Skip to content

Commit

Permalink
Merge pull request #148 from haze/hb/vibrateIfPossible
Browse files Browse the repository at this point in the history
`vibrateIfPossible`: fixes iOS safari uncaught exceptions & etc
  • Loading branch information
space-nuko authored Aug 26, 2023
2 parents 6f02912 + 2f48f96 commit abd3140
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/AccordionContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import {cubicIn} from 'svelte/easing';
import { flip } from 'svelte/animate';
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 { isHidden } from "$lib/widgets/utils";
import { handleContainerConsider, handleContainerFinalize } from "./utils";
Expand Down Expand Up @@ -56,7 +56,7 @@
};
function handleClick(e: CustomEvent<boolean>) {
navigator.vibrate(20)
vibrateIfPossible(20)
$isOpen = e.detail
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/TabsContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import {cubicIn} from 'svelte/easing';
import { flip } from 'svelte/animate';
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 { isHidden } from "$lib/widgets/utils";
import { handleContainerConsider, handleContainerFinalize } from "./utils";
Expand Down Expand Up @@ -62,7 +62,7 @@
}
function handleSelect() {
navigator.vibrate(20)
vibrateIfPossible(20)
}
function _startDrag(e: MouseEvent | TouchEvent) {
Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,9 @@ const MOBILE_USER_AGENTS = ["iPhone", "iPad", "Android", "BlackBerry", "WebOs"].
export function isMobileBrowser(userAgent: string): boolean {
return MOBILE_USER_AGENTS.some(a => userAgent.match(a))
}

export function vibrateIfPossible(strength: number | Array<number>) {
if (window.navigator.vibrate) {
window.navigator.vibrate(strength);
}
}
3 changes: 2 additions & 1 deletion src/lib/widgets/ButtonWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Button } from "@gradio/button";
import { get, type Writable, writable } from "svelte/store";
import { isDisabled } from "./utils"
import { vibrateIfPossible } from "$lib/utils";
import type { ComfyButtonNode } from "$lib/nodes/widgets";
export let widget: WidgetLayout | null = null;
Expand All @@ -24,7 +25,7 @@
function onClick(e: MouseEvent) {
node.onClick();
navigator.vibrate(20)
vibrateIfPossible(20)
}
const style = {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/widgets/CheckboxWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { Checkbox } from "@gradio/form";
import { get, type Writable, writable } from "svelte/store";
import { isDisabled } from "./utils"
import { vibrateIfPossible } from "$lib/utils";
import type { SelectData } from "@gradio/utils";
import type { ComfyCheckboxNode } from "$lib/nodes/widgets";
Expand All @@ -25,7 +26,7 @@
function onSelect(e: CustomEvent<SelectData>) {
$nodeValue = e.detail.selected
navigator.vibrate(20)
vibrateIfPossible(20)
}
</script>

Expand Down
6 changes: 3 additions & 3 deletions src/lib/widgets/ComboWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { type WidgetLayout } from "$lib/stores/layoutStates";
import { get, writable, type Writable } from "svelte/store";
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 isMobile: boolean = false;
let node: ComfyComboNode | null = null;
Expand Down Expand Up @@ -70,7 +70,7 @@
function onFocus() {
// console.warn("FOCUS")
if (listOpen) {
navigator.vibrate(20)
vibrateIfPossible(20)
}
}
Expand All @@ -86,7 +86,7 @@
function handleSelect(index: number) {
// console.warn("SEL", index)
navigator.vibrate(20)
vibrateIfPossible(20)
const item = $valuesForCombo[index]
activeIndex = index;
$nodeValue = item.value
Expand Down
4 changes: 2 additions & 2 deletions src/lib/widgets/NumberWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { type WidgetLayout } from "$lib/stores/layoutStates";
import { Range } from "$lib/components/gradio/form";
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 { isDisabled } from "./utils"
export let widget: WidgetLayout | null = null;
Expand Down Expand Up @@ -96,7 +96,7 @@
lastDisplayValue = option;
canVibrate = false;
setTimeout(() => { canVibrate = true }, 30)
navigator.vibrate(10)
vibrateIfPossible(10)
}
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/widgets/RadioWidget.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { get, type Writable, writable } from "svelte/store";
import { isDisabled } from "./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";
export let widget: WidgetLayout | null = null;
Expand Down Expand Up @@ -34,7 +34,7 @@
function onSelect(e: CustomEvent<SelectData>) {
node.setValue(e.detail.value)
node.index = e.detail.index as number
navigator.vibrate(20)
vibrateIfPossible(20)
}
</script>

Expand Down
3 changes: 2 additions & 1 deletion src/mobile/GenToolbar.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import ComfyApp, { type SerializedAppState } from "$lib/components/ComfyApp";
import workflowState, { ComfyBoxWorkflow } from "$lib/stores/workflowState";
import { vibrateIfPossible } from "$lib/utils";
import { Link, Toolbar } from "framework7-svelte"
Expand All @@ -11,7 +12,7 @@
$: workflow = $workflowState.activeWorkflow;
function queuePrompt() {
navigator.vibrate(20)
vibrateIfPossible(20)
app.runDefaultQueueAction()
}
</script>
Expand Down
12 changes: 6 additions & 6 deletions src/mobile/MainToolbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import ComfyApp, { type SerializedAppState } from "$lib/components/ComfyApp";
import queueState from "$lib/stores/queueState";
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 { Link, Toolbar } from "framework7-svelte"
Expand All @@ -21,28 +21,28 @@
$: workflow = $workflowState.activeWorkflow;
function queuePrompt() {
navigator.vibrate(20)
vibrateIfPossible(20)
app.runDefaultQueueAction()
}
async function refreshCombos() {
navigator.vibrate(20)
vibrateIfPossible(20)
await app.refreshComboInNodes()
}
function doSave(): void {
if (!fileInput)
return;
navigator.vibrate(20)
vibrateIfPossible(20)
app.querySave()
}
function doLoad(): void {
if (!fileInput)
return;
navigator.vibrate(20)
vibrateIfPossible(20)
fileInput.value = null;
fileInput.click();
}
Expand All @@ -52,7 +52,7 @@
}
function doSaveLocal(): void {
navigator.vibrate(20)
vibrateIfPossible(20)
app.saveStateToLocalStorage();
}
Expand Down
4 changes: 2 additions & 2 deletions src/mobile/routes/workflow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
}
async function refreshCombos() {
navigator.vibrate(20)
vibrateIfPossible(20)
await app.refreshComboInNodes()
}
function doSaveLocal(): void {
navigator.vibrate(20)
vibrateIfPossible(20)
app.saveStateToLocalStorage();
}
Expand Down
3 changes: 2 additions & 1 deletion src/mobile/routes/workflows.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import workflowState, { ComfyBoxWorkflow, type WorkflowInstID } from "$lib/stores/workflowState";
import { onMount } from "svelte";
import interfaceState from "$lib/stores/interfaceState";
import { vibrateIfPossible } from "$lib/utils";
import { f7 } from 'framework7-svelte';
import { XCircle } from 'svelte-bootstrap-icons';
Expand Down Expand Up @@ -31,7 +32,7 @@
if (!fileInput)
return;
navigator.vibrate(20)
vibrateIfPossible(20);
fileInput.value = null;
fileInput.click();
}
Expand Down

0 comments on commit abd3140

Please sign in to comment.