webview-zoom.ts 986 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. import { invoke } from "@tauri-apps/api/core"
  5. import { type as ostype } from "@tauri-apps/plugin-os"
  6. import { createSignal } from "solid-js"
  7. const OS_NAME = ostype()
  8. const [webviewZoom, setWebviewZoom] = createSignal(1)
  9. const MAX_ZOOM_LEVEL = 10
  10. const MIN_ZOOM_LEVEL = 0.2
  11. const clamp = (value: number) => Math.min(Math.max(value, MIN_ZOOM_LEVEL), MAX_ZOOM_LEVEL)
  12. const applyZoom = (next: number) => {
  13. setWebviewZoom(next)
  14. invoke("plugin:webview|set_webview_zoom", {
  15. value: next,
  16. })
  17. }
  18. window.addEventListener("keydown", (event) => {
  19. if (!(OS_NAME === "macos" ? event.metaKey : event.ctrlKey)) return
  20. let newZoom = webviewZoom()
  21. if (event.key === "-") newZoom -= 0.2
  22. if (event.key === "=" || event.key === "+") newZoom += 0.2
  23. if (event.key === "0") newZoom = 1
  24. applyZoom(clamp(newZoom))
  25. })
  26. export { webviewZoom }