UNPKG

tauri-plugin-polygon-api

Version:

A plugin for [tauri@v2](https://tauri.app/) to achieve click-through of the tauri main window by allowing developers to define polygons, thus customizing the mouse response area.

103 lines (99 loc) 2.99 kB
'use strict'; var core = require('@tauri-apps/api/core'); var event = require('@tauri-apps/api/event'); async function register(id) { return await core.invoke('plugin:polygon|register', { id, }).then((r) => r); } async function registerAll(ids) { return await core.invoke('plugin:polygon|register_all', { id: ids, }).then((r) => r); } async function remove(id) { return await core.invoke('plugin:polygon|remove', { id, }).then((r) => r); } async function clear() { return await core.invoke('plugin:polygon|clear', {}).then((r) => r); } async function show(id) { return await core.invoke('plugin:polygon|show', { id, }).then((r) => r); } async function hide(id) { return await core.invoke('plugin:polygon|hide', { id, }).then((r) => r); } async function update(id, points) { return await core.invoke('plugin:polygon|update', { id, points }).then((r) => r); } const POLYGON_LEFT_CLICK = "POLYGON_LEFT_CLICK"; const POLYGON_DOUBLE_CLICK = "POLYGON_DOUBLE_CLICK"; const POLYGON_RIGHT_CLICK = "POLYGON_RIGHT_CLICK"; const POLYGON_DRAG = "POLYGON_DRAG"; const POLYGON_MOUSE_MOVE = "POLYGON_MOUSE_MOVE"; const POLYGON_WHEEL = "POLYGON_WHEEL"; const POLYGON_ERROR = "POLYGON_ERROR"; const Events = ["LeftClick", "DoubleClick", "RightClick", "Drag", "MouseMove", "Wheel", "Error"]; const EventCallbacks = { LeftClick: [], DoubleClick: [], RightClick: [], Drag: [], Wheel: [], MouseMove: [], Error: [], }; function on(evt, callback) { if (!Events.includes(evt)) { throw new Error(`Event [${evt}] does not exist. Available event: ${Events.join(', ')}`); } EventCallbacks[evt].push(callback); } function off(evt, callback) { if (!Events.includes(evt)) { throw new Error(`Event [${evt}] does not exist. Available event: ${Events.join(', ')}`); } EventCallbacks[evt] = EventCallbacks[evt].filter((c) => c !== callback); } event.listen(POLYGON_LEFT_CLICK, async (ev) => { EventCallbacks.LeftClick.forEach(callback => callback(ev.payload)); }); event.listen(POLYGON_DOUBLE_CLICK, async (ev) => { EventCallbacks.DoubleClick.forEach(callback => callback(ev.payload)); }); event.listen(POLYGON_RIGHT_CLICK, async (ev) => { EventCallbacks.RightClick.forEach(callback => callback(ev.payload)); }); event.listen(POLYGON_DRAG, async (ev) => { EventCallbacks.Drag.forEach(callback => callback(ev.payload)); }); event.listen(POLYGON_WHEEL, async (ev) => { EventCallbacks.Wheel.forEach(callback => callback(ev.payload)); }); event.listen(POLYGON_MOUSE_MOVE, async (ev) => { EventCallbacks.MouseMove.forEach(callback => callback(ev.payload)); }); event.listen(POLYGON_ERROR, async (ev) => { EventCallbacks.Error.forEach(callback => callback(ev.payload)); }); const polygon = { register, registerAll, remove, clear, show, hide, update, on, off }; exports.polygon = polygon;