core.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. //-----------------------//
  130. // data-tauri-drag-region
  131. //
  132. // drag on mousedown and maximize on double click on Windows and Linux
  133. // while macOS macos maximization should be on mouseup and if the mouse
  134. // moves after the double click, it should be cancelled (see https://github.com/tauri-apps/tauri/issues/8306)
  135. //-----------------------//
  136. const TAURI_DRAG_REGION_ATTR = 'data-tauri-drag-region';
  137. let x = 0, y = 0;
  138. document.addEventListener('mousedown', (e) => {
  139. if (
  140. // element has the magic data attribute
  141. e.target.hasAttribute(TAURI_DRAG_REGION_ATTR) &&
  142. // and was left mouse button
  143. e.button === 0 &&
  144. // and was normal click to drag or double click to maximize
  145. (e.detail === 1 || e.detail === 2)
  146. ) {
  147. // macOS maximization happens on `mouseup`,
  148. // so we save needed state and early return
  149. if (osName === 'macos' && e.detail == 2) {
  150. x = e.clientX
  151. y = e.clientY
  152. return
  153. }
  154. // prevents text cursor
  155. e.preventDefault()
  156. // fix #2549: double click on drag region edge causes content to maximize without window sizing change
  157. // https://github.com/tauri-apps/tauri/issues/2549#issuecomment-1250036908
  158. e.stopImmediatePropagation()
  159. window.__TAURI_INVOKE__('tauri', {
  160. __tauriModule: 'Window',
  161. message: {
  162. cmd: 'manage',
  163. data: {
  164. cmd: {
  165. type: e.detail === 2 ? '__toggleMaximize' : 'startDragging'
  166. }
  167. }
  168. }
  169. })
  170. }
  171. })
  172. // on macOS we maximze on mouseup instead, to match the system behavior where maximization can be canceled
  173. // if the mouse moves outside the data-tauri-drag-region
  174. if (osName === "macos") {
  175. document.addEventListener('mouseup', (e) => {
  176. if (
  177. // element has the magic data attribute
  178. e.target.hasAttribute(TAURI_DRAG_REGION_ATTR) &&
  179. // and was left mouse button
  180. e.button === 0 &&
  181. // and was double click
  182. e.detail === 2 &&
  183. // and the cursor hasn't moved from initial mousedown
  184. e.clientX === x && e.clientY === y
  185. ) {
  186. window.__TAURI_INVOKE__('tauri', {
  187. __tauriModule: 'Window',
  188. message: {
  189. cmd: 'manage',
  190. data: {
  191. cmd: {
  192. type: '__toggleMaximize'
  193. }
  194. }
  195. }
  196. })
  197. }
  198. })
  199. }
  200. let permissionSettable = false
  201. let permissionValue = 'default'
  202. function isPermissionGranted() {
  203. if (window.Notification.permission !== 'default') {
  204. return Promise.resolve(window.Notification.permission === 'granted')
  205. }
  206. return window.__TAURI_INVOKE__('tauri', {
  207. __tauriModule: 'Notification',
  208. message: {
  209. cmd: 'isNotificationPermissionGranted'
  210. }
  211. })
  212. }
  213. function setNotificationPermission(value) {
  214. permissionSettable = true
  215. window.Notification.permission = value
  216. permissionSettable = false
  217. }
  218. function requestPermission() {
  219. return window
  220. .__TAURI_INVOKE__('tauri', {
  221. __tauriModule: 'Notification',
  222. message: {
  223. cmd: 'requestNotificationPermission'
  224. }
  225. })
  226. .then(function (permission) {
  227. setNotificationPermission(permission)
  228. return permission
  229. })
  230. }
  231. function sendNotification(options) {
  232. if (typeof options === 'object') {
  233. Object.freeze(options)
  234. }
  235. return window.__TAURI_INVOKE__('tauri', {
  236. __tauriModule: 'Notification',
  237. message: {
  238. cmd: 'notification',
  239. options:
  240. typeof options === 'string'
  241. ? {
  242. title: options
  243. }
  244. : options
  245. }
  246. })
  247. }
  248. window.Notification = function (title, options) {
  249. const opts = options || {}
  250. sendNotification(
  251. Object.assign(opts, {
  252. title: title
  253. })
  254. )
  255. }
  256. window.Notification.requestPermission = requestPermission
  257. Object.defineProperty(window.Notification, 'permission', {
  258. enumerable: true,
  259. get: function () {
  260. return permissionValue
  261. },
  262. set: function (v) {
  263. if (!permissionSettable) {
  264. throw new Error('Readonly property')
  265. }
  266. permissionValue = v
  267. }
  268. })
  269. isPermissionGranted().then(function (response) {
  270. if (response === null) {
  271. setNotificationPermission('default')
  272. } else {
  273. setNotificationPermission(response ? 'granted' : 'denied')
  274. }
  275. })
  276. window.alert = function (message) {
  277. window.__TAURI_INVOKE__('tauri', {
  278. __tauriModule: 'Dialog',
  279. message: {
  280. cmd: 'messageDialog',
  281. message: message.toString()
  282. }
  283. })
  284. }
  285. window.confirm = function (message) {
  286. return window.__TAURI_INVOKE__('tauri', {
  287. __tauriModule: 'Dialog',
  288. message: {
  289. cmd: 'confirmDialog',
  290. message: message.toString()
  291. }
  292. })
  293. }
  294. // window.print works on Linux/Windows; need to use the API on macOS
  295. if (navigator.userAgent.includes('Mac')) {
  296. window.print = function () {
  297. return window.__TAURI_INVOKE__('tauri', {
  298. __tauriModule: 'Window',
  299. message: {
  300. cmd: 'manage',
  301. data: {
  302. cmd: {
  303. type: 'print'
  304. }
  305. }
  306. }
  307. })
  308. }
  309. }
  310. })()