core.js 8.7 KB

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