core.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright 2019-2021 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. window.__TAURI__ = {}
  10. }
  11. window.__TAURI__.transformCallback = function transformCallback(
  12. callback,
  13. once
  14. ) {
  15. var identifier = uid()
  16. window[identifier] = function (result) {
  17. if (once) {
  18. delete window[identifier]
  19. }
  20. return callback && callback(result)
  21. }
  22. return identifier
  23. }
  24. window.__TAURI_INVOKE__ = function invoke(cmd, args = {}, key = null) {
  25. return new Promise(function (resolve, reject) {
  26. var callback = window.__TAURI__.transformCallback(function (r) {
  27. resolve(r)
  28. delete window[error]
  29. }, true)
  30. var error = window.__TAURI__.transformCallback(function (e) {
  31. reject(e)
  32. delete window[callback]
  33. }, true)
  34. if (typeof cmd === 'string') {
  35. args.cmd = cmd
  36. } else if (typeof cmd === 'object') {
  37. args = cmd
  38. } else {
  39. return reject(new Error('Invalid argument type.'))
  40. }
  41. if (
  42. document.readyState === 'complete' ||
  43. document.readyState === 'interactive'
  44. ) {
  45. window.__TAURI_POST_MESSAGE__(cmd, {
  46. ...args,
  47. callback: callback,
  48. error: error,
  49. __invokeKey: key || __TAURI_INVOKE_KEY__
  50. })
  51. } else {
  52. window.addEventListener('DOMContentLoaded', function () {
  53. window.__TAURI_POST_MESSAGE__(cmd, {
  54. ...args,
  55. callback: callback,
  56. error: error,
  57. __invokeKey: key || __TAURI_INVOKE_KEY__
  58. })
  59. })
  60. }
  61. })
  62. }
  63. // open <a href="..."> links with the Tauri API
  64. function __openLinks() {
  65. document.querySelector('body').addEventListener(
  66. 'click',
  67. function (e) {
  68. var target = e.target
  69. while (target != null) {
  70. if (target.matches('a')) {
  71. if (
  72. target.href &&
  73. target.href.startsWith('http') &&
  74. target.target === '_blank'
  75. ) {
  76. window.__TAURI_INVOKE__(
  77. 'tauri',
  78. {
  79. __tauriModule: 'Shell',
  80. message: {
  81. cmd: 'open',
  82. path: target.href
  83. }
  84. },
  85. _KEY_VALUE_
  86. )
  87. e.preventDefault()
  88. }
  89. break
  90. }
  91. target = target.parentElement
  92. }
  93. },
  94. true
  95. )
  96. }
  97. if (
  98. document.readyState === 'complete' ||
  99. document.readyState === 'interactive'
  100. ) {
  101. __openLinks()
  102. } else {
  103. window.addEventListener(
  104. 'DOMContentLoaded',
  105. function () {
  106. __openLinks()
  107. },
  108. true
  109. )
  110. }
  111. // drag region
  112. document.addEventListener('mousedown', (e) => {
  113. if (e.target.hasAttribute('data-tauri-drag-region') && e.buttons === 1) {
  114. // start dragging if the element has a `tauri-drag-region` data attribute and maximize on double-clicking it
  115. window.__TAURI_INVOKE__(
  116. 'tauri',
  117. {
  118. __tauriModule: 'Window',
  119. message: {
  120. cmd: 'manage',
  121. data: {
  122. cmd: {
  123. type: e.detail === 2 ? '__toggleMaximize' : 'startDragging'
  124. }
  125. }
  126. }
  127. },
  128. _KEY_VALUE_
  129. )
  130. }
  131. })
  132. window.__TAURI_INVOKE__(
  133. 'tauri',
  134. {
  135. __tauriModule: 'Event',
  136. message: {
  137. cmd: 'listen',
  138. event: 'tauri://window-created',
  139. handler: window.__TAURI__.transformCallback(function (event) {
  140. if (event.payload) {
  141. var windowLabel = event.payload.label
  142. window.__TAURI__.__windows.push({
  143. label: windowLabel
  144. })
  145. }
  146. })
  147. }
  148. },
  149. _KEY_VALUE_
  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__(
  158. 'tauri',
  159. {
  160. __tauriModule: 'Notification',
  161. message: {
  162. cmd: 'isNotificationPermissionGranted'
  163. }
  164. },
  165. _KEY_VALUE_
  166. )
  167. }
  168. function setNotificationPermission(value) {
  169. permissionSettable = true
  170. window.Notification.permission = value
  171. permissionSettable = false
  172. }
  173. function requestPermission() {
  174. return window
  175. .__TAURI_INVOKE__(
  176. 'tauri',
  177. {
  178. __tauriModule: 'Notification',
  179. message: {
  180. cmd: 'requestNotificationPermission'
  181. }
  182. },
  183. _KEY_VALUE_
  184. )
  185. .then(function (permission) {
  186. setNotificationPermission(permission)
  187. return permission
  188. })
  189. }
  190. function sendNotification(options) {
  191. if (typeof options === 'object') {
  192. Object.freeze(options)
  193. }
  194. return window.__TAURI_INVOKE__(
  195. 'tauri', {
  196. __tauriModule: 'Notification',
  197. message: {
  198. cmd: 'notification',
  199. options: typeof options === 'string' ?
  200. {
  201. title: options
  202. } :
  203. options
  204. }
  205. },
  206. _KEY_VALUE_
  207. )
  208. }
  209. window.Notification = function (title, options) {
  210. var opts = options || {}
  211. sendNotification(
  212. Object.assign(opts, {
  213. title: title
  214. })
  215. )
  216. }
  217. window.Notification.requestPermission = requestPermission
  218. Object.defineProperty(window.Notification, 'permission', {
  219. enumerable: true,
  220. get: function () {
  221. return permissionValue
  222. },
  223. set: function (v) {
  224. if (!permissionSettable) {
  225. throw new Error('Readonly property')
  226. }
  227. permissionValue = v
  228. }
  229. })
  230. isPermissionGranted().then(function (response) {
  231. if (response === null) {
  232. setNotificationPermission('default')
  233. } else {
  234. setNotificationPermission(response ? 'granted' : 'denied')
  235. }
  236. })
  237. window.alert = function (message) {
  238. window.__TAURI_INVOKE__(
  239. 'tauri',
  240. {
  241. __tauriModule: 'Dialog',
  242. message: {
  243. cmd: 'messageDialog',
  244. message: message
  245. }
  246. },
  247. _KEY_VALUE_
  248. )
  249. }
  250. window.confirm = function (message) {
  251. return window.__TAURI_INVOKE__(
  252. 'tauri',
  253. {
  254. __tauriModule: 'Dialog',
  255. message: {
  256. cmd: 'confirmDialog',
  257. message: message
  258. }
  259. },
  260. _KEY_VALUE_
  261. )
  262. }
  263. // window.print works on Linux/Windows; need to use the API on macOS
  264. if (navigator.userAgent.includes('Mac')) {
  265. window.print = function () {
  266. return window.__TAURI_INVOKE__(
  267. 'tauri',
  268. {
  269. __tauriModule: 'Window',
  270. message: {
  271. cmd: 'manage',
  272. data: {
  273. cmd: {
  274. type: 'print'
  275. }
  276. }
  277. }
  278. },
  279. _KEY_VALUE_
  280. )
  281. }
  282. }
  283. })()