uicore.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // @ts-nocheck
  2. import $ from 'jquery'
  3. import { ipcRenderer, remote, shell, webFrame } from 'electron'
  4. import { LoggerUtil } from '../loggerutil'
  5. import isDev from '../isdev'
  6. /**
  7. * Core UI functions are initialized in this file. This prevents
  8. * unexpected errors from breaking the core features. Specifically,
  9. * actions in this file should not require the usage of any internal
  10. * modules, excluding dependencies.
  11. */
  12. const loggerUICore = new LoggerUtil('%c[UICore]', 'color: #000668; font-weight: bold')
  13. const loggerAutoUpdater = new LoggerUtil('%c[AutoUpdater]', 'color: #000668; font-weight: bold')
  14. const loggerAutoUpdaterSuccess = new LoggerUtil('%c[AutoUpdater]', 'color: #209b07; font-weight: bold')
  15. // Log deprecation and process warnings.
  16. process.traceProcessWarnings = true
  17. process.traceDeprecation = true
  18. // Disable eval function.
  19. // eslint-disable-next-line
  20. window.eval = global.eval = function () {
  21. throw new Error('Sorry, this app does not support window.eval().')
  22. }
  23. // Display warning when devtools window is opened.
  24. remote.getCurrentWebContents().on('devtools-opened', () => {
  25. console.log('%cThe console is dark and full of terrors.', 'color: white; -webkit-text-stroke: 4px #a02d2a; font-size: 60px; font-weight: bold')
  26. console.log('%cIf you\'ve been told to paste something here, you\'re being scammed.', 'font-size: 16px')
  27. console.log('%cUnless you know exactly what you\'re doing, close this window.', 'font-size: 16px')
  28. })
  29. // Disable zoom, needed for darwin.
  30. webFrame.setZoomLevel(0)
  31. webFrame.setVisualZoomLevelLimits(1, 1)
  32. webFrame.setLayoutZoomLevelLimits(0, 0)
  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-${info.version}.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. // DONE
  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. // DONE
  137. Array.from(document.getElementsByClassName('fRb')).map((val) => {
  138. val.addEventListener('click', e => {
  139. const window = remote.getCurrentWindow()
  140. if(window.isMaximized()){
  141. window.unmaximize()
  142. } else {
  143. window.maximize()
  144. }
  145. (document.activeElement as HTMLElement).blur()
  146. })
  147. })
  148. // Bind minimize button.
  149. // DONE
  150. Array.from(document.getElementsByClassName('fMb')).map((val) => {
  151. val.addEventListener('click', e => {
  152. const window = remote.getCurrentWindow()
  153. window.minimize();
  154. (document.activeElement as HTMLElement).blur()
  155. })
  156. })
  157. // Remove focus from social media buttons once they're clicked.
  158. Array.from(document.getElementsByClassName('mediaURL')).map(val => {
  159. val.addEventListener('click', e => {
  160. (document.activeElement as HTMLElement).blur()
  161. })
  162. })
  163. } else if(document.readyState === 'complete'){
  164. //266.01
  165. //170.8
  166. //53.21
  167. // Bind progress bar length to length of bot wrapper
  168. //const targetWidth = document.getElementById("launch_content").getBoundingClientRect().width
  169. //const targetWidth2 = document.getElementById("server_selection").getBoundingClientRect().width
  170. //const targetWidth3 = document.getElementById("launch_button").getBoundingClientRect().width
  171. document.getElementById('launch_details').style.maxWidth = '266.01'
  172. document.getElementById('launch_progress').style.width = '170.8'
  173. document.getElementById('launch_details_right').style.maxWidth = '170.8'
  174. document.getElementById('launch_progress_label').style.width = '53.21'
  175. }
  176. }, false)
  177. /**
  178. * Open web links in the user's default browser.
  179. */
  180. $(document).on('click', 'a[href^="http"]', function(event) {
  181. event.preventDefault()
  182. shell.openExternal(this.href)
  183. })
  184. /**
  185. * Opens DevTools window if you hold (ctrl + shift + i).
  186. * This will crash the program if you are using multiple
  187. * DevTools, for example the chrome debugger in VS Code.
  188. */
  189. document.addEventListener('keydown', function (e) {
  190. if((e.key === 'I' || e.key === 'i') && e.ctrlKey && e.shiftKey){
  191. let window = remote.getCurrentWindow()
  192. window.webContents.toggleDevTools()
  193. }
  194. })