2022-07-28 01:47:53 +02:00
|
|
|
<!--
|
|
|
|
|
Copyright 2022 ModZero.
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
-->
|
2022-07-27 12:58:22 +02:00
|
|
|
<script type="ts">
|
|
|
|
|
import { onDestroy, onMount } from "svelte";
|
|
|
|
|
import type { UnlistenFn } from "@tauri-apps/api/event";
|
|
|
|
|
import { invoke } from "@tauri-apps/api";
|
|
|
|
|
import { listen } from "@tauri-apps/api/event";
|
2022-07-26 23:36:24 +02:00
|
|
|
|
2022-07-27 12:58:22 +02:00
|
|
|
let seconds = 5;
|
2022-07-28 01:47:53 +02:00
|
|
|
let timerTickUnlisten: Promise<UnlistenFn> | null = null;
|
|
|
|
|
let timerDoneUnlisten: Promise<UnlistenFn> | null = null;
|
2022-07-27 12:58:22 +02:00
|
|
|
|
|
|
|
|
type Timer = {
|
|
|
|
|
id: string;
|
|
|
|
|
elapsed: {
|
|
|
|
|
secs: number;
|
|
|
|
|
nsecs: number;
|
|
|
|
|
};
|
|
|
|
|
};
|
2022-07-26 23:36:24 +02:00
|
|
|
|
2022-07-27 12:58:22 +02:00
|
|
|
onMount(() => {
|
2022-07-28 01:47:53 +02:00
|
|
|
timerTickUnlisten = listen<Timer>("timer-tick", (event) => {
|
2022-07-27 12:58:22 +02:00
|
|
|
console.log("Tick!", event.payload.id, event.payload.elapsed);
|
|
|
|
|
});
|
2022-07-26 23:36:24 +02:00
|
|
|
|
2022-07-28 01:47:53 +02:00
|
|
|
timerDoneUnlisten = listen<Timer>("timer-done", (event) => {
|
2022-07-27 12:58:22 +02:00
|
|
|
console.log("Done!", event.payload.id);
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-07-26 23:36:24 +02:00
|
|
|
|
2022-07-27 12:58:22 +02:00
|
|
|
onDestroy(() => {
|
2022-07-28 01:47:53 +02:00
|
|
|
timerTickUnlisten?.then((ttu) => ttu());
|
|
|
|
|
timerDoneUnlisten?.then((tdu) => tdu());
|
2022-07-27 12:58:22 +02:00
|
|
|
});
|
|
|
|
|
|
2022-07-28 01:47:53 +02:00
|
|
|
async function startTimer() {
|
2022-07-28 02:52:31 +02:00
|
|
|
let timer = await invoke<Timer>("plugin:timers|make", {
|
2022-07-27 12:58:22 +02:00
|
|
|
duration: { secs: seconds, nanos: 0 },
|
|
|
|
|
message: "Hi!",
|
|
|
|
|
});
|
2022-07-28 02:52:31 +02:00
|
|
|
invoke("plugin:timers|start", { timerId: timer.id });
|
2022-07-26 23:36:24 +02:00
|
|
|
}
|
2022-07-26 05:03:28 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<main>
|
2022-07-28 01:47:53 +02:00
|
|
|
<div id="timer" />
|
|
|
|
|
<div id="controls">
|
|
|
|
|
<label>
|
|
|
|
|
Fire after
|
|
|
|
|
<input type="number" bind:value={seconds} />
|
|
|
|
|
</label>
|
|
|
|
|
<button on:click={startTimer}>Fire!</button>
|
|
|
|
|
</div>
|
2022-07-26 05:03:28 +02:00
|
|
|
</main>
|
2022-07-28 01:47:53 +02:00
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
main {
|
|
|
|
|
position: fixed;
|
|
|
|
|
top: 0px;
|
|
|
|
|
left: 0px;
|
|
|
|
|
right: 0px;
|
|
|
|
|
bottom: 0px;
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
}
|
|
|
|
|
#timer {
|
|
|
|
|
flex-grow: 1;
|
|
|
|
|
margin: 0.5em;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#controls {
|
|
|
|
|
margin: 0.5em;
|
|
|
|
|
padding: 0.5em;
|
|
|
|
|
}
|
|
|
|
|
</style>
|