settings.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. // Requirements
  2. const os = require('os')
  3. const semver = require('semver')
  4. const settingsState = {
  5. invalid: new Set()
  6. }
  7. /**
  8. * General Settings Functions
  9. */
  10. /**
  11. * Bind value validators to the settings UI elements. These will
  12. * validate against the criteria defined in the ConfigManager (if
  13. * and). If the value is invalid, the UI will reflect this and saving
  14. * will be disabled until the value is corrected. This is an automated
  15. * process. More complex UI may need to be bound separately.
  16. */
  17. function initSettingsValidators(){
  18. const sEls = document.getElementById('settingsContainer').querySelectorAll('[cValue]')
  19. Array.from(sEls).map((v, index, arr) => {
  20. const vFn = ConfigManager['validate' + v.getAttribute('cValue')]
  21. if(typeof vFn === 'function'){
  22. if(v.tagName === 'INPUT'){
  23. if(v.type === 'number' || v.type === 'text'){
  24. v.addEventListener('keyup', (e) => {
  25. const v = e.target
  26. if(!vFn(v.value)){
  27. settingsState.invalid.add(v.id)
  28. v.setAttribute('error', '')
  29. settingsSaveDisabled(true)
  30. } else {
  31. if(v.hasAttribute('error')){
  32. v.removeAttribute('error')
  33. settingsState.invalid.delete(v.id)
  34. if(settingsState.invalid.size === 0){
  35. settingsSaveDisabled(false)
  36. }
  37. }
  38. }
  39. })
  40. }
  41. }
  42. }
  43. })
  44. }
  45. /**
  46. * Load configuration values onto the UI. This is an automated process.
  47. */
  48. function initSettingsValues(){
  49. const sEls = document.getElementById('settingsContainer').querySelectorAll('[cValue]')
  50. Array.from(sEls).map((v, index, arr) => {
  51. const gFn = ConfigManager['get' + v.getAttribute('cValue')]
  52. if(typeof gFn === 'function'){
  53. if(v.tagName === 'INPUT'){
  54. if(v.type === 'number' || v.type === 'text'){
  55. // Special Conditions
  56. const cVal = v.getAttribute('cValue')
  57. if(cVal === 'JavaExecutable'){
  58. populateJavaExecDetails(v.value)
  59. v.value = gFn()
  60. } else if(cVal === 'JVMOptions'){
  61. v.value = gFn().join(' ')
  62. } else {
  63. v.value = gFn()
  64. }
  65. } else if(v.type === 'checkbox'){
  66. v.checked = gFn()
  67. }
  68. } else if(v.tagName === 'DIV'){
  69. if(v.classList.contains('rangeSlider')){
  70. // Special Conditions
  71. const cVal = v.getAttribute('cValue')
  72. if(cVal === 'MinRAM' || cVal === 'MaxRAM'){
  73. let val = gFn()
  74. if(val.endsWith('M')){
  75. val = Number(val.substring(0, val.length-1))/1000
  76. } else {
  77. val = Number.parseFloat(val)
  78. }
  79. v.setAttribute('value', val)
  80. } else {
  81. v.setAttribute('value', Number.parseFloat(gFn()))
  82. }
  83. }
  84. }
  85. }
  86. })
  87. }
  88. /**
  89. * Save the settings values.
  90. */
  91. function saveSettingsValues(){
  92. const sEls = document.getElementById('settingsContainer').querySelectorAll('[cValue]')
  93. Array.from(sEls).map((v, index, arr) => {
  94. const sFn = ConfigManager['set' + v.getAttribute('cValue')]
  95. if(typeof sFn === 'function'){
  96. if(v.tagName === 'INPUT'){
  97. if(v.type === 'number' || v.type === 'text'){
  98. // Special Conditions
  99. const cVal = v.getAttribute('cValue')
  100. if(cVal === 'JVMOptions'){
  101. sFn(v.value.split(' '))
  102. } else {
  103. sFn(v.value)
  104. }
  105. } else if(v.type === 'checkbox'){
  106. sFn(v.checked)
  107. // Special Conditions
  108. const cVal = v.getAttribute('cValue')
  109. if(cVal === 'AllowPrerelease'){
  110. changeAllowPrerelease(v.checked)
  111. }
  112. }
  113. } else if(v.tagName === 'DIV'){
  114. if(v.classList.contains('rangeSlider')){
  115. // Special Conditions
  116. const cVal = v.getAttribute('cValue')
  117. if(cVal === 'MinRAM' || cVal === 'MaxRAM'){
  118. let val = Number(v.getAttribute('value'))
  119. if(val%1 > 0){
  120. val = val*1000 + 'M'
  121. } else {
  122. val = val + 'G'
  123. }
  124. sFn(val)
  125. } else {
  126. sFn(v.getAttribute('value'))
  127. }
  128. }
  129. }
  130. }
  131. })
  132. }
  133. let selectedTab = 'settingsTabAccount'
  134. /**
  135. * Bind functionality for the settings navigation items.
  136. */
  137. function setupSettingsTabs(){
  138. Array.from(document.getElementsByClassName('settingsNavItem')).map((val) => {
  139. val.onclick = (e) => {
  140. if(val.hasAttribute('selected')){
  141. return
  142. }
  143. const navItems = document.getElementsByClassName('settingsNavItem')
  144. for(let i=0; i<navItems.length; i++){
  145. if(navItems[i].hasAttribute('selected')){
  146. navItems[i].removeAttribute('selected')
  147. }
  148. }
  149. val.setAttribute('selected', '')
  150. let prevTab = selectedTab
  151. selectedTab = val.getAttribute('rSc')
  152. $(`#${prevTab}`).fadeOut(250, () => {
  153. $(`#${selectedTab}`).fadeIn(250)
  154. })
  155. }
  156. })
  157. }
  158. const settingsNavDone = document.getElementById('settingsNavDone')
  159. /**
  160. * Set if the settings save (done) button is disabled.
  161. *
  162. * @param {boolean} v True to disable, false to enable.
  163. */
  164. function settingsSaveDisabled(v){
  165. settingsNavDone.disabled = v
  166. }
  167. /* Closes the settings view and saves all data. */
  168. settingsNavDone.onclick = () => {
  169. saveSettingsValues()
  170. ConfigManager.save()
  171. switchView(getCurrentView(), VIEWS.landing)
  172. }
  173. /**
  174. * Account Management Tab
  175. */
  176. // Bind the add account button.
  177. document.getElementById('settingsAddAccount').onclick = (e) => {
  178. switchView(getCurrentView(), VIEWS.login, 500, 500, () => {
  179. loginViewOnCancel = VIEWS.settings
  180. loginViewOnSuccess = VIEWS.settings
  181. loginCancelEnabled(true)
  182. })
  183. }
  184. /**
  185. * Bind functionality for the account selection buttons. If another account
  186. * is selected, the UI of the previously selected account will be updated.
  187. */
  188. function bindAuthAccountSelect(){
  189. Array.from(document.getElementsByClassName('settingsAuthAccountSelect')).map((val) => {
  190. val.onclick = (e) => {
  191. if(val.hasAttribute('selected')){
  192. return
  193. }
  194. const selectBtns = document.getElementsByClassName('settingsAuthAccountSelect')
  195. for(let i=0; i<selectBtns.length; i++){
  196. if(selectBtns[i].hasAttribute('selected')){
  197. selectBtns[i].removeAttribute('selected')
  198. selectBtns[i].innerHTML = 'Select Account'
  199. }
  200. }
  201. val.setAttribute('selected', '')
  202. val.innerHTML = 'Selected Account &#10004;'
  203. setSelectedAccount(val.closest('.settingsAuthAccount').getAttribute('uuid'))
  204. }
  205. })
  206. }
  207. /**
  208. * Bind functionality for the log out button. If the logged out account was
  209. * the selected account, another account will be selected and the UI will
  210. * be updated accordingly.
  211. */
  212. function bindAuthAccountLogOut(){
  213. Array.from(document.getElementsByClassName('settingsAuthAccountLogOut')).map((val) => {
  214. val.onclick = (e) => {
  215. let isLastAccount = false
  216. if(Object.keys(ConfigManager.getAuthAccounts()).length === 1){
  217. isLastAccount = true
  218. setOverlayContent(
  219. 'Warning<br>This is Your Last Account',
  220. 'In order to use the launcher you must be logged into at least one account. You will need to login again after.<br><br>Are you sure you want to log out?',
  221. 'I\'m Sure',
  222. 'Cancel'
  223. )
  224. setOverlayHandler(() => {
  225. processLogOut(val, isLastAccount)
  226. switchView(getCurrentView(), VIEWS.login)
  227. toggleOverlay(false)
  228. })
  229. setDismissHandler(() => {
  230. toggleOverlay(false)
  231. })
  232. toggleOverlay(true, true)
  233. } else {
  234. processLogOut(val, isLastAccount)
  235. }
  236. }
  237. })
  238. }
  239. /**
  240. * Process a log out.
  241. *
  242. * @param {Element} val The log out button element.
  243. * @param {boolean} isLastAccount If this logout is on the last added account.
  244. */
  245. function processLogOut(val, isLastAccount){
  246. const parent = val.closest('.settingsAuthAccount')
  247. const uuid = parent.getAttribute('uuid')
  248. const prevSelAcc = ConfigManager.getSelectedAccount()
  249. AuthManager.removeAccount(uuid).then(() => {
  250. if(!isLastAccount && uuid === prevSelAcc.uuid){
  251. const selAcc = ConfigManager.getSelectedAccount()
  252. refreshAuthAccountSelected(selAcc.uuid)
  253. updateSelectedAccount(selAcc)
  254. validateSelectedAccount()
  255. }
  256. })
  257. $(parent).fadeOut(250, () => {
  258. parent.remove()
  259. })
  260. }
  261. /**
  262. * Refreshes the status of the selected account on the auth account
  263. * elements.
  264. *
  265. * @param {string} uuid The UUID of the new selected account.
  266. */
  267. function refreshAuthAccountSelected(uuid){
  268. Array.from(document.getElementsByClassName('settingsAuthAccount')).map((val) => {
  269. const selBtn = val.getElementsByClassName('settingsAuthAccountSelect')[0]
  270. if(uuid === val.getAttribute('uuid')){
  271. selBtn.setAttribute('selected', '')
  272. selBtn.innerHTML = 'Selected Account &#10004;'
  273. } else {
  274. if(selBtn.hasAttribute('selected')){
  275. selBtn.removeAttribute('selected')
  276. }
  277. selBtn.innerHTML = 'Select Account'
  278. }
  279. })
  280. }
  281. const settingsCurrentAccounts = document.getElementById('settingsCurrentAccounts')
  282. /**
  283. * Add auth account elements for each one stored in the authentication database.
  284. */
  285. function populateAuthAccounts(){
  286. const authAccounts = ConfigManager.getAuthAccounts()
  287. const authKeys = Object.keys(authAccounts)
  288. const selectedUUID = ConfigManager.getSelectedAccount().uuid
  289. let authAccountStr = ``
  290. authKeys.map((val) => {
  291. const acc = authAccounts[val]
  292. authAccountStr += `<div class="settingsAuthAccount" uuid="${acc.uuid}">
  293. <div class="settingsAuthAccountLeft">
  294. <img class="settingsAuthAccountImage" alt="${acc.displayName}" src="https://crafatar.com/renders/body/${acc.uuid}?scale=3&default=MHF_Steve&overlay">
  295. </div>
  296. <div class="settingsAuthAccountRight">
  297. <div class="settingsAuthAccountDetails">
  298. <div class="settingsAuthAccountDetailPane">
  299. <div class="settingsAuthAccountDetailTitle">Username</div>
  300. <div class="settingsAuthAccountDetailValue">${acc.displayName}</div>
  301. </div>
  302. <div class="settingsAuthAccountDetailPane">
  303. <div class="settingsAuthAccountDetailTitle">${acc.displayName === acc.username ? 'UUID' : 'Email'}</div>
  304. <div class="settingsAuthAccountDetailValue">${acc.displayName === acc.username ? acc.uuid : acc.username}</div>
  305. </div>
  306. </div>
  307. <div class="settingsAuthAccountActions">
  308. <button class="settingsAuthAccountSelect" ${selectedUUID === acc.uuid ? 'selected>Selected Account &#10004;' : '>Select Account'}</button>
  309. <div class="settingsAuthAccountWrapper">
  310. <button class="settingsAuthAccountLogOut">Log Out</button>
  311. </div>
  312. </div>
  313. </div>
  314. </div>`
  315. })
  316. settingsCurrentAccounts.innerHTML = authAccountStr
  317. }
  318. /**
  319. * Prepare the accounts tab for display.
  320. */
  321. function prepareAccountsTab() {
  322. populateAuthAccounts()
  323. bindAuthAccountSelect()
  324. bindAuthAccountLogOut()
  325. }
  326. /**
  327. * Minecraft Tab
  328. */
  329. /**
  330. * Disable decimals, negative signs, and scientific notation.
  331. */
  332. document.getElementById('settingsGameWidth').addEventListener('keydown', (e) => {
  333. if(/[-\.eE]/.test(e.key)){
  334. e.preventDefault()
  335. }
  336. })
  337. document.getElementById('settingsGameHeight').addEventListener('keydown', (e) => {
  338. if(/[-\.eE]/.test(e.key)){
  339. e.preventDefault()
  340. }
  341. })
  342. /**
  343. * Java Tab
  344. */
  345. // DOM Cache
  346. const settingsMaxRAMRange = document.getElementById('settingsMaxRAMRange')
  347. const settingsMinRAMRange = document.getElementById('settingsMinRAMRange')
  348. const settingsMaxRAMLabel = document.getElementById('settingsMaxRAMLabel')
  349. const settingsMinRAMLabel = document.getElementById('settingsMinRAMLabel')
  350. const settingsMemoryTotal = document.getElementById('settingsMemoryTotal')
  351. const settingsMemoryAvail = document.getElementById('settingsMemoryAvail')
  352. const settingsJavaExecDetails = document.getElementById('settingsJavaExecDetails')
  353. const settingsJavaExecVal = document.getElementById('settingsJavaExecVal')
  354. const settingsJavaExecSel = document.getElementById('settingsJavaExecSel')
  355. // Store maximum memory values.
  356. const SETTINGS_MAX_MEMORY = ConfigManager.getAbsoluteMaxRAM()
  357. const SETTINGS_MIN_MEMORY = ConfigManager.getAbsoluteMinRAM()
  358. // Set the max and min values for the ranged sliders.
  359. settingsMaxRAMRange.setAttribute('max', SETTINGS_MAX_MEMORY)
  360. settingsMaxRAMRange.setAttribute('min', SETTINGS_MIN_MEMORY)
  361. settingsMinRAMRange.setAttribute('max', SETTINGS_MAX_MEMORY)
  362. settingsMinRAMRange.setAttribute('min', SETTINGS_MIN_MEMORY )
  363. // Bind on change event for min memory container.
  364. settingsMinRAMRange.onchange = (e) => {
  365. // Current range values
  366. const sMaxV = Number(settingsMaxRAMRange.getAttribute('value'))
  367. const sMinV = Number(settingsMinRAMRange.getAttribute('value'))
  368. // Get reference to range bar.
  369. const bar = e.target.getElementsByClassName('rangeSliderBar')[0]
  370. // Calculate effective total memory.
  371. const max = (os.totalmem()-1000000000)/1000000000
  372. // Change range bar color based on the selected value.
  373. if(sMinV >= max/2){
  374. bar.style.background = '#e86060'
  375. } else if(sMinV >= max/4) {
  376. bar.style.background = '#e8e18b'
  377. } else {
  378. bar.style.background = null
  379. }
  380. // Increase maximum memory if the minimum exceeds its value.
  381. if(sMaxV < sMinV){
  382. const sliderMeta = calculateRangeSliderMeta(settingsMaxRAMRange)
  383. updateRangedSlider(settingsMaxRAMRange, sMinV,
  384. ((sMinV-sliderMeta.min)/sliderMeta.step)*sliderMeta.inc)
  385. settingsMaxRAMLabel.innerHTML = sMinV.toFixed(1) + 'G'
  386. }
  387. // Update label
  388. settingsMinRAMLabel.innerHTML = sMinV.toFixed(1) + 'G'
  389. }
  390. // Bind on change event for max memory container.
  391. settingsMaxRAMRange.onchange = (e) => {
  392. // Current range values
  393. const sMaxV = Number(settingsMaxRAMRange.getAttribute('value'))
  394. const sMinV = Number(settingsMinRAMRange.getAttribute('value'))
  395. // Get reference to range bar.
  396. const bar = e.target.getElementsByClassName('rangeSliderBar')[0]
  397. // Calculate effective total memory.
  398. const max = (os.totalmem()-1000000000)/1000000000
  399. // Change range bar color based on the selected value.
  400. if(sMaxV >= max/2){
  401. bar.style.background = '#e86060'
  402. } else if(sMaxV >= max/4) {
  403. bar.style.background = '#e8e18b'
  404. } else {
  405. bar.style.background = null
  406. }
  407. // Decrease the minimum memory if the maximum value is less.
  408. if(sMaxV < sMinV){
  409. const sliderMeta = calculateRangeSliderMeta(settingsMaxRAMRange)
  410. updateRangedSlider(settingsMinRAMRange, sMaxV,
  411. ((sMaxV-sliderMeta.min)/sliderMeta.step)*sliderMeta.inc)
  412. settingsMinRAMLabel.innerHTML = sMaxV.toFixed(1) + 'G'
  413. }
  414. settingsMaxRAMLabel.innerHTML = sMaxV.toFixed(1) + 'G'
  415. }
  416. /**
  417. * Calculate common values for a ranged slider.
  418. *
  419. * @param {Element} v The range slider to calculate against.
  420. * @returns {Object} An object with meta values for the provided ranged slider.
  421. */
  422. function calculateRangeSliderMeta(v){
  423. const val = {
  424. max: Number(v.getAttribute('max')),
  425. min: Number(v.getAttribute('min')),
  426. step: Number(v.getAttribute('step')),
  427. }
  428. val.ticks = (val.max-val.min)/val.step
  429. val.inc = 100/val.ticks
  430. return val
  431. }
  432. /**
  433. * Binds functionality to the ranged sliders. They're more than
  434. * just divs now :').
  435. */
  436. function bindRangeSlider(){
  437. Array.from(document.getElementsByClassName('rangeSlider')).map((v) => {
  438. // Reference the track (thumb).
  439. const track = v.getElementsByClassName('rangeSliderTrack')[0]
  440. // Set the initial slider value.
  441. const value = v.getAttribute('value')
  442. const sliderMeta = calculateRangeSliderMeta(v)
  443. updateRangedSlider(v, value, ((value-sliderMeta.min)/sliderMeta.step)*sliderMeta.inc)
  444. // The magic happens when we click on the track.
  445. track.onmousedown = (e) => {
  446. // Stop moving the track on mouse up.
  447. document.onmouseup = (e) => {
  448. document.onmousemove = null
  449. document.onmouseup = null
  450. }
  451. // Move slider according to the mouse position.
  452. document.onmousemove = (e) => {
  453. // Distance from the beginning of the bar in pixels.
  454. const diff = e.pageX - v.offsetLeft - track.offsetWidth/2
  455. // Don't move the track off the bar.
  456. if(diff >= 0 && diff <= v.offsetWidth-track.offsetWidth/2){
  457. // Convert the difference to a percentage.
  458. const perc = (diff/v.offsetWidth)*100
  459. // Calculate the percentage of the closest notch.
  460. const notch = Number(perc/sliderMeta.inc).toFixed(0)*sliderMeta.inc
  461. // If we're close to that notch, stick to it.
  462. if(Math.abs(perc-notch) < sliderMeta.inc/2){
  463. updateRangedSlider(v, sliderMeta.min+(sliderMeta.step*(notch/sliderMeta.inc)), notch)
  464. }
  465. }
  466. }
  467. }
  468. })
  469. }
  470. /**
  471. * Update a ranged slider's value and position.
  472. *
  473. * @param {Element} element The ranged slider to update.
  474. * @param {string | number} value The new value for the ranged slider.
  475. * @param {number} notch The notch that the slider should now be at.
  476. */
  477. function updateRangedSlider(element, value, notch){
  478. const oldVal = element.getAttribute('value')
  479. const bar = element.getElementsByClassName('rangeSliderBar')[0]
  480. const track = element.getElementsByClassName('rangeSliderTrack')[0]
  481. element.setAttribute('value', value)
  482. const event = new MouseEvent('change', {
  483. target: element,
  484. type: 'change',
  485. bubbles: false,
  486. cancelable: true
  487. })
  488. let cancelled = !element.dispatchEvent(event)
  489. if(!cancelled){
  490. track.style.left = notch + '%'
  491. bar.style.width = notch + '%'
  492. } else {
  493. element.setAttribute('value', oldVal)
  494. }
  495. }
  496. /**
  497. * Display the total and available RAM.
  498. */
  499. function populateMemoryStatus(){
  500. settingsMemoryTotal.innerHTML = Number((os.totalmem()-1000000000)/1000000000).toFixed(1) + 'G'
  501. settingsMemoryAvail.innerHTML = Number(os.freemem()/1000000000).toFixed(1) + 'G'
  502. }
  503. // Bind the executable file input to the display text input.
  504. settingsJavaExecSel.onchange = (e) => {
  505. settingsJavaExecVal.value = settingsJavaExecSel.files[0].path
  506. populateJavaExecDetails(settingsJavaExecVal.value)
  507. }
  508. /**
  509. * Validate the provided executable path and display the data on
  510. * the UI.
  511. *
  512. * @param {string} execPath The executable path to populate against.
  513. */
  514. function populateJavaExecDetails(execPath){
  515. AssetGuard._validateJavaBinary(execPath).then(v => {
  516. if(v.valid){
  517. settingsJavaExecDetails.innerHTML = `Selected: Java ${v.version.major} Update ${v.version.update} (x${v.arch})`
  518. } else {
  519. settingsJavaExecDetails.innerHTML = 'Invalid Selection'
  520. }
  521. })
  522. }
  523. /**
  524. * Prepare the Java tab for display.
  525. */
  526. function prepareJavaTab(){
  527. bindRangeSlider()
  528. populateMemoryStatus()
  529. }
  530. /**
  531. * About Tab
  532. */
  533. const settingsAboutCurrentVersionCheck = document.getElementById('settingsAboutCurrentVersionCheck')
  534. const settingsAboutCurrentVersionTitle = document.getElementById('settingsAboutCurrentVersionTitle')
  535. const settingsAboutCurrentVersionValue = document.getElementById('settingsAboutCurrentVersionValue')
  536. const settingsChangelogTitle = document.getElementById('settingsChangelogTitle')
  537. const settingsChangelogText = document.getElementById('settingsChangelogText')
  538. const settingsChangelogButton = document.getElementById('settingsChangelogButton')
  539. // Bind the devtools toggle button.
  540. document.getElementById('settingsAboutDevToolsButton').onclick = (e) => {
  541. let window = remote.getCurrentWindow()
  542. window.toggleDevTools()
  543. }
  544. /**
  545. * Retrieve the version information and display it on the UI.
  546. */
  547. function populateVersionInformation(){
  548. const version = remote.app.getVersion()
  549. settingsAboutCurrentVersionValue.innerHTML = version
  550. const preRelComp = semver.prerelease(version)
  551. if(preRelComp != null && preRelComp.length > 0){
  552. settingsAboutCurrentVersionTitle.innerHTML = 'Pre-release'
  553. settingsAboutCurrentVersionTitle.style.color = '#ff886d'
  554. settingsAboutCurrentVersionCheck.style.background = '#ff886d'
  555. } else {
  556. settingsAboutCurrentVersionTitle.innerHTML = 'Stable Release'
  557. settingsAboutCurrentVersionTitle.style.color = null
  558. settingsAboutCurrentVersionCheck.style.background = null
  559. }
  560. }
  561. /**
  562. * Fetches the GitHub atom release feed and parses it for the release notes
  563. * of the current version. This value is displayed on the UI.
  564. */
  565. function populateReleaseNotes(){
  566. $.ajax({
  567. url: 'https://github.com/WesterosCraftCode/ElectronLauncher/releases.atom',
  568. success: (data) => {
  569. const version = 'v' + remote.app.getVersion()
  570. const entries = $(data).find('entry')
  571. for(let i=0; i<entries.length; i++){
  572. const entry = $(entries[i])
  573. let id = entry.find('id').text()
  574. id = id.substring(id.lastIndexOf('/')+1)
  575. if(id === version){
  576. settingsChangelogTitle.innerHTML = entry.find('title').text()
  577. settingsChangelogText.innerHTML = entry.find('content').text()
  578. settingsChangelogButton.href = entry.find('link').attr('href')
  579. }
  580. }
  581. },
  582. timeout: 2500
  583. }).catch(err => {
  584. settingsChangelogText.innerHTML = 'Failed to load release notes.'
  585. })
  586. }
  587. /**
  588. * Prepare account tab for display.
  589. */
  590. function prepareAboutTab(){
  591. populateVersionInformation()
  592. populateReleaseNotes()
  593. }
  594. /**
  595. * Settings preparation functions.
  596. */
  597. /**
  598. * Prepare the entire settings UI.
  599. *
  600. * @param {boolean} first Whether or not it is the first load.
  601. */
  602. function prepareSettings(first = false) {
  603. if(first){
  604. setupSettingsTabs()
  605. initSettingsValidators()
  606. }
  607. initSettingsValues()
  608. prepareAccountsTab()
  609. prepareJavaTab()
  610. prepareAboutTab()
  611. }
  612. // Prepare the settings UI on startup.
  613. prepareSettings(true)