core.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. ; (function () {
  5. function uid() {
  6. return window.crypto.getRandomValues(new Uint32Array(1))[0]
  7. }
  8. if (!window.__TAURI__) {
  9. Object.defineProperty(window, '__TAURI__', {
  10. value: {}
  11. })
  12. }
  13. window.__TAURI__.transformCallback = function transformCallback(
  14. callback,
  15. once
  16. ) {
  17. const identifier = uid()
  18. const prop = `_${identifier}`
  19. Object.defineProperty(window, prop, {
  20. value: (result) => {
  21. if (once) {
  22. Reflect.deleteProperty(window, prop)
  23. }
  24. return callback && callback(result)
  25. },
  26. writable: false,
  27. configurable: true
  28. })
  29. return identifier
  30. }
  31. const ipcQueue = []
  32. let isWaitingForIpc = false
  33. function waitForIpc() {
  34. if ('__TAURI_IPC__' in window) {
  35. for (const action of ipcQueue) {
  36. action()
  37. }
  38. } else {
  39. setTimeout(waitForIpc, 50)
  40. }
  41. }
  42. window.__TAURI_INVOKE__ = function invoke(cmd, args = {}) {
  43. return new Promise(function (resolve, reject) {
  44. const callback = window.__TAURI__.transformCallback(function (r) {
  45. resolve(r)
  46. delete window[`_${error}`]
  47. }, true)
  48. const error = window.__TAURI__.transformCallback(function (e) {
  49. reject(e)
  50. delete window[`_${callback}`]
  51. }, true)
  52. if (typeof cmd === 'string') {
  53. args.cmd = cmd
  54. } else if (typeof cmd === 'object') {
  55. args = cmd
  56. } else {
  57. return reject(new Error('Invalid argument type.'))
  58. }
  59. const action = () => {
  60. window.__TAURI_IPC__({
  61. ...args,
  62. callback,
  63. error: error
  64. })
  65. }
  66. if (window.__TAURI_IPC__) {
  67. action()
  68. } else {
  69. ipcQueue.push(action)
  70. if (!isWaitingForIpc) {
  71. waitForIpc()
  72. isWaitingForIpc = true
  73. }
  74. }
  75. })
  76. }
  77. // open <a href="..."> links with the Tauri API
  78. function __openLinks() {
  79. document.querySelector('body').addEventListener(
  80. 'click',
  81. function (e) {
  82. let target = e.target
  83. const baseTarget = document.querySelector('head base')?.target
  84. while (target != null) {
  85. if (target.matches('a')) {
  86. if (
  87. target.href &&
  88. (['http://', 'https://', 'mailto:', 'tel:'].some(v => target.href.startsWith(v))) &&
  89. (target.target === '_blank' || (!target.target && baseTarget === "_blank"))
  90. ) {
  91. window.__TAURI_INVOKE__('tauri', {
  92. __tauriModule: 'Shell',
  93. message: {
  94. cmd: 'open',
  95. path: target.href
  96. }
  97. })
  98. e.preventDefault()
  99. }
  100. break
  101. }
  102. target = target.parentElement
  103. }
  104. }
  105. )
  106. }
  107. if (
  108. document.readyState === 'complete' ||
  109. document.readyState === 'interactive'
  110. ) {
  111. __openLinks()
  112. } else {
  113. window.addEventListener(
  114. 'DOMContentLoaded',
  115. function () {
  116. __openLinks()
  117. },
  118. true
  119. )
  120. }
  121. // drag region
  122. document.addEventListener('mousedown', (e) => {
  123. if (e.target.hasAttribute('data-tauri-drag-region') && e.buttons === 1) {
  124. // prevents text cursor
  125. e.preventDefault()
  126. // fix #2549: double click on drag region edge causes content to maximize without window sizing change
  127. // https://github.com/tauri-apps/tauri/issues/2549#issuecomment-1250036908
  128. e.stopImmediatePropagation()
  129. // start dragging if the element has a `tauri-drag-region` data attribute and maximize on double-clicking it
  130. window.__TAURI_INVOKE__('tauri', {
  131. __tauriModule: 'Window',
  132. message: {
  133. cmd: 'manage',
  134. data: {
  135. cmd: {
  136. type: e.detail === 2 ? '__toggleMaximize' : 'startDragging'
  137. }
  138. }
  139. }
  140. })
  141. }
  142. })
  143. let permissionSettable = false
  144. let permissionValue = 'default'
  145. function isPermissionGranted() {
  146. if (window.Notification.permission !== 'default') {
  147. return Promise.resolve(window.Notification.permission === 'granted')
  148. }
  149. return window.__TAURI_INVOKE__('tauri', {
  150. __tauriModule: 'Notification',
  151. message: {
  152. cmd: 'isNotificationPermissionGranted'
  153. }
  154. })
  155. }
  156. function setNotificationPermission(value) {
  157. permissionSettable = true
  158. window.Notification.permission = value
  159. permissionSettable = false
  160. }
  161. function requestPermission() {
  162. return window
  163. .__TAURI_INVOKE__('tauri', {
  164. __tauriModule: 'Notification',
  165. message: {
  166. cmd: 'requestNotificationPermission'
  167. }
  168. })
  169. .then(function (permission) {
  170. setNotificationPermission(permission)
  171. return permission
  172. })
  173. }
  174. function sendNotification(options) {
  175. if (typeof options === 'object') {
  176. Object.freeze(options)
  177. }
  178. return window.__TAURI_INVOKE__('tauri', {
  179. __tauriModule: 'Notification',
  180. message: {
  181. cmd: 'notification',
  182. options:
  183. typeof options === 'string'
  184. ? {
  185. title: options
  186. }
  187. : options
  188. }
  189. })
  190. }
  191. window.Notification = function (title, options) {
  192. const opts = options || {}
  193. sendNotification(
  194. Object.assign(opts, {
  195. title: title
  196. })
  197. )
  198. }
  199. window.Notification.requestPermission = requestPermission
  200. Object.defineProperty(window.Notification, 'permission', {
  201. enumerable: true,
  202. get: function () {
  203. return permissionValue
  204. },
  205. set: function (v) {
  206. if (!permissionSettable) {
  207. throw new Error('Readonly property')
  208. }
  209. permissionValue = v
  210. }
  211. })
  212. isPermissionGranted().then(function (response) {
  213. if (response === null) {
  214. setNotificationPermission('default')
  215. } else {
  216. setNotificationPermission(response ? 'granted' : 'denied')
  217. }
  218. })
  219. window.alert = function (message) {
  220. window.__TAURI_INVOKE__('tauri', {
  221. __tauriModule: 'Dialog',
  222. message: {
  223. cmd: 'messageDialog',
  224. message: message.toString()
  225. }
  226. })
  227. }
  228. window.confirm = function (message) {
  229. return window.__TAURI_INVOKE__('tauri', {
  230. __tauriModule: 'Dialog',
  231. message: {
  232. cmd: 'confirmDialog',
  233. message: message.toString()
  234. }
  235. })
  236. }
  237. // window.print works on Linux/Windows; need to use the API on macOS
  238. if (navigator.userAgent.includes('Mac')) {
  239. window.print = function () {
  240. return window.__TAURI_INVOKE__('tauri', {
  241. __tauriModule: 'Window',
  242. message: {
  243. cmd: 'manage',
  244. data: {
  245. cmd: {
  246. type: 'print'
  247. }
  248. }
  249. }
  250. })
  251. }
  252. }
  253. })()