core.js 7.1 KB

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