uicore.js 8.5 KB

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