core.js 7.1 KB

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