uicore.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Core UI functions are initialized in this file. This prevents
  3. * unexpected errors from breaking the core features. Specifically,
  4. * actions in this file should not require the usage of any internal
  5. * modules, excluding dependencies.
  6. */
  7. // Requirements
  8. const $ = require('jquery')
  9. const {ipcRenderer, shell, webFrame} = require('electron')
  10. const remote = require('@electron/remote')
  11. const isDev = require('./assets/js/isdev')
  12. const LoggerUtil = require('./assets/js/loggerutil')
  13. const loggerUICore = LoggerUtil('%c[UICore]', 'color: #000668; font-weight: bold')
  14. const loggerAutoUpdater = LoggerUtil('%c[AutoUpdater]', 'color: #000668; font-weight: bold')
  15. const loggerAutoUpdaterSuccess = LoggerUtil('%c[AutoUpdater]', 'color: #209b07; font-weight: bold')
  16. // Log deprecation and process warnings.
  17. process.traceProcessWarnings = true
  18. process.traceDeprecation = true
  19. // Disable eval function.
  20. // eslint-disable-next-line
  21. window.eval = global.eval = function () {
  22. throw new Error('Sorry, this app does not support window.eval().')
  23. }
  24. // Display warning when devtools window is opened.
  25. remote.getCurrentWebContents().on('devtools-opened', () => {
  26. console.log('%cThe console is dark and full of terrors.', 'color: white; -webkit-text-stroke: 4px #a02d2a; font-size: 60px; font-weight: bold')
  27. console.log('%cIf you\'ve been told to paste something here, you\'re being scammed.', 'font-size: 16px')
  28. console.log('%cUnless you know exactly what you\'re doing, close this window.', 'font-size: 16px')
  29. })
  30. // Disable zoom, needed for darwin.
  31. webFrame.setZoomLevel(0)
  32. webFrame.setVisualZoomLevelLimits(1, 1)
  33. // Initialize auto updates in production environments.
  34. let updateCheckListener
  35. if(!isDev){
  36. ipcRenderer.on('autoUpdateNotification', (event, arg, info) => {
  37. switch(arg){
  38. case 'checking-for-update':
  39. loggerAutoUpdater.log('Checking for update..')
  40. settingsUpdateButtonStatus('Checking for Updates..', true)
  41. break
  42. case 'update-available':
  43. loggerAutoUpdaterSuccess.log('New update available', info.version)
  44. if(process.platform === 'darwin'){
  45. info.darwindownload = `https://github.com/dscalzi/HeliosLauncher/releases/download/v${info.version}/helioslauncher-setup-${info.version}${process.arch === 'arm64' ? '-arm64' : ''}.dmg`
  46. showUpdateUI(info)
  47. }
  48. populateSettingsUpdateInformation(info)
  49. break
  50. case 'update-downloaded':
  51. loggerAutoUpdaterSuccess.log('Update ' + info.version + ' ready to be installed.')
  52. settingsUpdateButtonStatus('Install Now', false, () => {
  53. if(!isDev){
  54. ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
  55. }
  56. })
  57. showUpdateUI(info)
  58. break
  59. case 'update-not-available':
  60. loggerAutoUpdater.log('No new update found.')
  61. settingsUpdateButtonStatus('Check for Updates')
  62. break
  63. case 'ready':
  64. updateCheckListener = setInterval(() => {
  65. ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
  66. }, 1800000)
  67. ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
  68. break
  69. case 'realerror':
  70. if(info != null && info.code != null){
  71. if(info.code === 'ERR_UPDATER_INVALID_RELEASE_FEED'){
  72. loggerAutoUpdater.log('No suitable releases found.')
  73. } else if(info.code === 'ERR_XML_MISSED_ELEMENT'){
  74. loggerAutoUpdater.log('No releases found.')
  75. } else {
  76. loggerAutoUpdater.error('Error during update check..', info)
  77. loggerAutoUpdater.debug('Error Code:', info.code)
  78. }
  79. }
  80. break
  81. default:
  82. loggerAutoUpdater.log('Unknown argument', arg)
  83. break
  84. }
  85. })
  86. }
  87. /**
  88. * Send a notification to the main process changing the value of
  89. * allowPrerelease. If we are running a prerelease version, then
  90. * this will always be set to true, regardless of the current value
  91. * of val.
  92. *
  93. * @param {boolean} val The new allow prerelease value.
  94. */
  95. function changeAllowPrerelease(val){
  96. ipcRenderer.send('autoUpdateAction', 'allowPrereleaseChange', val)
  97. }
  98. function showUpdateUI(info){
  99. //TODO Make this message a bit more informative `${info.version}`
  100. document.getElementById('image_seal_container').setAttribute('update', true)
  101. document.getElementById('image_seal_container').onclick = () => {
  102. /*setOverlayContent('Update Available', 'A new update for the launcher is available. Would you like to install now?', 'Install', 'Later')
  103. setOverlayHandler(() => {
  104. if(!isDev){
  105. ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
  106. } else {
  107. console.error('Cannot install updates in development environment.')
  108. toggleOverlay(false)
  109. }
  110. })
  111. setDismissHandler(() => {
  112. toggleOverlay(false)
  113. })
  114. toggleOverlay(true, true)*/
  115. switchView(getCurrentView(), VIEWS.settings, 500, 500, () => {
  116. settingsNavItemListener(document.getElementById('settingsNavUpdate'), false)
  117. })
  118. }
  119. }
  120. /* jQuery Example
  121. $(function(){
  122. loggerUICore.log('UICore Initialized');
  123. })*/
  124. document.addEventListener('readystatechange', function () {
  125. if (document.readyState === 'interactive'){
  126. loggerUICore.log('UICore Initializing..')
  127. // Bind close button.
  128. Array.from(document.getElementsByClassName('fCb')).map((val) => {
  129. val.addEventListener('click', e => {
  130. const window = remote.getCurrentWindow()
  131. window.close()
  132. })
  133. })
  134. // Bind restore down button.
  135. Array.from(document.getElementsByClassName('fRb')).map((val) => {
  136. val.addEventListener('click', e => {
  137. const window = remote.getCurrentWindow()
  138. if(window.isMaximized()){
  139. window.unmaximize()
  140. } else {
  141. window.maximize()
  142. }
  143. document.activeElement.blur()
  144. })
  145. })
  146. // Bind minimize button.
  147. Array.from(document.getElementsByClassName('fMb')).map((val) => {
  148. val.addEventListener('click', e => {
  149. const window = remote.getCurrentWindow()
  150. window.minimize()
  151. document.activeElement.blur()
  152. })
  153. })
  154. // Remove focus from social media buttons once they're clicked.
  155. Array.from(document.getElementsByClassName('mediaURL')).map(val => {
  156. val.addEventListener('click', e => {
  157. document.activeElement.blur()
  158. })
  159. })
  160. } else if(document.readyState === 'complete'){
  161. //266.01
  162. //170.8
  163. //53.21
  164. // Bind progress bar length to length of bot wrapper
  165. //const targetWidth = document.getElementById("launch_content").getBoundingClientRect().width
  166. //const targetWidth2 = document.getElementById("server_selection").getBoundingClientRect().width
  167. //const targetWidth3 = document.getElementById("launch_button").getBoundingClientRect().width
  168. document.getElementById('launch_details').style.maxWidth = 266.01
  169. document.getElementById('launch_progress').style.width = 170.8
  170. document.getElementById('launch_details_right').style.maxWidth = 170.8
  171. document.getElementById('launch_progress_label').style.width = 53.21
  172. }
  173. }, false)
  174. /**
  175. * Open web links in the user's default browser.
  176. */
  177. $(document).on('click', 'a[href^="http"]', function(event) {
  178. event.preventDefault()
  179. shell.openExternal(this.href)
  180. })
  181. /**
  182. * Opens DevTools window if you hold (ctrl + shift + i).
  183. * This will crash the program if you are using multiple
  184. * DevTools, for example the chrome debugger in VS Code.
  185. */
  186. document.addEventListener('keydown', function (e) {
  187. if((e.key === 'I' || e.key === 'i') && e.ctrlKey && e.shiftKey){
  188. let window = remote.getCurrentWindow()
  189. window.toggleDevTools()
  190. }
  191. })