core.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2019-2022 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. var identifier = uid()
  18. var 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. var callback = window.__TAURI__.transformCallback(function (r) {
  45. resolve(r)
  46. delete window[`_${error}`]
  47. }, true)
  48. var 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. var target = e.target
  83. while (target != null) {
  84. if (target.matches('a')) {
  85. if (
  86. target.href &&
  87. (['http://', 'https://', 'mailto:', 'tel:'].some(v => target.href.startsWith(v))) &&
  88. target.target === '_blank'
  89. ) {
  90. window.__TAURI_INVOKE__('tauri', {
  91. __tauriModule: 'Shell',
  92. message: {
  93. cmd: 'open',
  94. path: target.href
  95. }
  96. })
  97. e.preventDefault()
  98. }
  99. break
  100. }
  101. target = target.parentElement
  102. }
  103. }
  104. )
  105. }
  106. if (
  107. document.readyState === 'complete' ||
  108. document.readyState === 'interactive'
  109. ) {
  110. __openLinks()
  111. } else {
  112. window.addEventListener(
  113. 'DOMContentLoaded',
  114. function () {
  115. __openLinks()
  116. },
  117. true
  118. )
  119. }
  120. // drag region
  121. document.addEventListener('mousedown', (e) => {
  122. if (e.target.hasAttribute('data-tauri-drag-region') && e.buttons === 1) {
  123. // prevents text cursor
  124. e.preventDefault()
  125. // fix #2549: double click on drag region edge causes content to maximize without window sizing change
  126. // https://github.com/tauri-apps/tauri/issues/2549#issuecomment-1250036908
  127. e.stopImmediatePropagation()
  128. // start dragging if the element has a `tauri-drag-region` data attribute and maximize on double-clicking it
  129. window.__TAURI_INVOKE__('tauri', {
  130. __tauriModule: 'Window',
  131. message: {
  132. cmd: 'manage',
  133. data: {
  134. cmd: {
  135. type: e.detail === 2 ? '__toggleMaximize' : 'startDragging'
  136. }
  137. }
  138. }
  139. })
  140. }
  141. })
  142. let permissionSettable = false
  143. let permissionValue = 'default'
  144. function isPermissionGranted() {
  145. if (window.Notification.permission !== 'default') {
  146. return Promise.resolve(window.Notification.permission === 'granted')
  147. }
  148. return window.__TAURI_INVOKE__('tauri', {
  149. __tauriModule: 'Notification',
  150. message: {
  151. cmd: 'isNotificationPermissionGranted'
  152. }
  153. })
  154. }
  155. function setNotificationPermission(value) {
  156. permissionSettable = true
  157. window.Notification.permission = value
  158. permissionSettable = false
  159. }
  160. function requestPermission() {
  161. return window
  162. .__TAURI_INVOKE__('tauri', {
  163. __tauriModule: 'Notification',
  164. message: {
  165. cmd: 'requestNotificationPermission'
  166. }
  167. })
  168. .then(function (permission) {
  169. setNotificationPermission(permission)
  170. return permission
  171. })
  172. }
  173. function sendNotification(options) {
  174. if (typeof options === 'object') {
  175. Object.freeze(options)
  176. }
  177. return window.__TAURI_INVOKE__('tauri', {
  178. __tauriModule: 'Notification',
  179. message: {
  180. cmd: 'notification',
  181. options:
  182. typeof options === 'string'
  183. ? {
  184. title: options
  185. }
  186. : options
  187. }
  188. })
  189. }
  190. window.Notification = function (title, options) {
  191. var opts = options || {}
  192. sendNotification(
  193. Object.assign(opts, {
  194. title: title
  195. })
  196. )
  197. }
  198. window.Notification.requestPermission = requestPermission
  199. Object.defineProperty(window.Notification, 'permission', {
  200. enumerable: true,
  201. get: function () {
  202. return permissionValue
  203. },
  204. set: function (v) {
  205. if (!permissionSettable) {
  206. throw new Error('Readonly property')
  207. }
  208. permissionValue = v
  209. }
  210. })
  211. isPermissionGranted().then(function (response) {
  212. if (response === null) {
  213. setNotificationPermission('default')
  214. } else {
  215. setNotificationPermission(response ? 'granted' : 'denied')
  216. }
  217. })
  218. window.alert = function (message) {
  219. window.__TAURI_INVOKE__('tauri', {
  220. __tauriModule: 'Dialog',
  221. message: {
  222. cmd: 'messageDialog',
  223. message: message.toString()
  224. }
  225. })
  226. }
  227. window.confirm = function (message) {
  228. return window.__TAURI_INVOKE__('tauri', {
  229. __tauriModule: 'Dialog',
  230. message: {
  231. cmd: 'confirmDialog',
  232. message: message.toString()
  233. }
  234. })
  235. }
  236. // window.print works on Linux/Windows; need to use the API on macOS
  237. if (navigator.userAgent.includes('Mac')) {
  238. window.print = function () {
  239. return window.__TAURI_INVOKE__('tauri', {
  240. __tauriModule: 'Window',
  241. message: {
  242. cmd: 'manage',
  243. data: {
  244. cmd: {
  245. type: 'print'
  246. }
  247. }
  248. }
  249. })
  250. }
  251. }
  252. })()