HeliosLauncher/app/assets/js/scripts/uicore.js

188 lines
7.1 KiB
JavaScript
Raw Normal View History

2023-11-25 15:34:04 -08:00
import { VIEWS } from './views.js'
/**
* Core UI functions are initialized in this file. This prevents
* unexpected errors from breaking the core features. Specifically,
* actions in this file should not require the usage of any internal
* modules, excluding dependencies.
*/
// Requirements
2023-11-25 15:34:04 -08:00
// const { ipcRenderer } = require('electron')
const isDev = await window.api.app.isDev()
// const { LoggerUtil } = require('helios-core')
// const Lang = require('./assets/js/langloader')
2018-04-12 15:13:09 -07:00
2023-11-25 15:34:04 -08:00
// const loggerUICore = LoggerUtil.getLogger('UICore')
// const loggerAutoUpdater = LoggerUtil.getLogger('AutoUpdater')
// Disable eval function.
// eslint-disable-next-line
2023-11-25 15:34:04 -08:00
window.eval = function () {
throw new Error('Sorry, this app does not support window.eval().')
}
// Initialize auto updates in production environments.
let updateCheckListener
if(!isDev){
ipcRenderer.on('autoUpdateNotification', (event, arg, info) => {
switch(arg){
case 'checking-for-update':
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.info('Checking for update..')
settingsUpdateButtonStatus(Lang.queryJS('uicore.autoUpdate.checkingForUpdateButton'), true)
break
case 'update-available':
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.info('New update available', info.version)
2023-11-25 15:34:04 -08:00
if(process.platform() === 'darwin'){
info.darwindownload = `https://github.com/dscalzi/HeliosLauncher/releases/download/v${info.version}/Helios-Launcher-setup-${info.version}${process.arch() === 'arm64' ? '-arm64' : '-x64'}.dmg`
showUpdateUI(info)
}
populateSettingsUpdateInformation(info)
break
case 'update-downloaded':
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.info('Update ' + info.version + ' ready to be installed.')
settingsUpdateButtonStatus(Lang.queryJS('uicore.autoUpdate.installNowButton'), false, () => {
if(!isDev){
ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
}
})
showUpdateUI(info)
break
case 'update-not-available':
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.info('No new update found.')
settingsUpdateButtonStatus(Lang.queryJS('uicore.autoUpdate.checkForUpdatesButton'))
break
case 'ready':
updateCheckListener = setInterval(() => {
ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
}, 1800000)
ipcRenderer.send('autoUpdateAction', 'checkForUpdate')
break
case 'realerror':
if(info != null && info.code != null){
if(info.code === 'ERR_UPDATER_INVALID_RELEASE_FEED'){
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.info('No suitable releases found.')
} else if(info.code === 'ERR_XML_MISSED_ELEMENT'){
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.info('No releases found.')
} else {
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.error('Error during update check..', info)
// loggerAutoUpdater.debug('Error Code:', info.code)
}
}
break
default:
2023-11-25 15:34:04 -08:00
// loggerAutoUpdater.info('Unknown argument', arg)
break
}
})
}
/**
* Send a notification to the main process changing the value of
* allowPrerelease. If we are running a prerelease version, then
* this will always be set to true, regardless of the current value
* of val.
*
* @param {boolean} val The new allow prerelease value.
*/
function changeAllowPrerelease(val){
ipcRenderer.send('autoUpdateAction', 'allowPrereleaseChange', val)
}
function showUpdateUI(info){
//TODO Make this message a bit more informative `${info.version}`
document.getElementById('image_seal_container').setAttribute('update', true)
document.getElementById('image_seal_container').onclick = () => {
/*setOverlayContent('Update Available', 'A new update for the launcher is available. Would you like to install now?', 'Install', 'Later')
setOverlayHandler(() => {
if(!isDev){
ipcRenderer.send('autoUpdateAction', 'installUpdateNow')
} else {
console.error('Cannot install updates in development environment.')
toggleOverlay(false)
}
})
setDismissHandler(() => {
toggleOverlay(false)
})
toggleOverlay(true, true)*/
switchView(getCurrentView(), VIEWS.settings, 500, 500, () => {
settingsNavItemListener(document.getElementById('settingsNavUpdate'), false)
})
}
}
/* jQuery Example
$(function(){
2022-12-24 19:17:30 -08:00
loggerUICore.info('UICore Initialized');
})*/
2023-11-25 15:34:04 -08:00
// loggerUICore.info('UICore Initializing..')
2023-11-25 15:34:04 -08:00
// Bind close button.
Array.from(document.getElementsByClassName('fCb')).map((val) => {
val.addEventListener('click', async e => {
await window.api.xwindow.close()
})
})
2023-11-25 15:34:04 -08:00
// Bind restore down button.
Array.from(document.getElementsByClassName('fRb')).map((val) => {
val.addEventListener('click', async e => {
if(await window.api.xwindow.isMaximized()){
await window.api.xwindow.unmaximize()
} else {
await window.api.xwindow.maximize()
}
document.activeElement.blur()
})
})
2023-11-25 15:34:04 -08:00
// Bind minimize button.
Array.from(document.getElementsByClassName('fMb')).map((val) => {
val.addEventListener('click', async e => {
console.log('hi')
await window.api.xwindow.minimize()
document.activeElement.blur()
})
})
2023-11-25 15:34:04 -08:00
// Remove focus from social media buttons once they're clicked.
Array.from(document.getElementsByClassName('mediaURL')).map(val => {
val.addEventListener('click', e => {
document.activeElement.blur()
})
})
2023-11-25 15:34:04 -08:00
//266.01
//170.8
//53.21
// Bind progress bar length to length of bot wrapper
//const targetWidth = document.getElementById("launch_content").getBoundingClientRect().width
//const targetWidth2 = document.getElementById("server_selection").getBoundingClientRect().width
//const targetWidth3 = document.getElementById("launch_button").getBoundingClientRect().width
2023-11-25 15:34:04 -08:00
document.getElementById('launch_details').style.maxWidth = 266.01
document.getElementById('launch_progress').style.width = 170.8
document.getElementById('launch_details_right').style.maxWidth = 170.8
document.getElementById('launch_progress_label').style.width = 53.21
/**
* Open web links in the user's default browser.
*/
2023-11-25 15:34:04 -08:00
$(document).on('click', 'a[href^="http"]', async (event) => {
event.preventDefault()
2023-11-25 15:34:04 -08:00
await window.api.shell.openExternal(this.href)
})
/**
* Opens DevTools window if you hold (ctrl + shift + i).
* This will crash the program if you are using multiple
* DevTools, for example the chrome debugger in VS Code.
*/
2023-11-25 15:34:04 -08:00
document.addEventListener('keydown', async (e) => {
if((e.key === 'I' || e.key === 'i') && e.ctrlKey && e.shiftKey){
2023-11-25 15:34:04 -08:00
await window.api.xwindow.toggleDevTools()
}
})