| 1 | // The settings window's work: show what the app has, and keep each change |
| 2 | // the moment it is made. Everything it asks is a command in |
| 3 | // src/bridge.rs, answered only to this window. |
| 4 | (function () { |
| 5 | var SWITCHES = ["startHidden", "closeToTray", "notifications", "flash", "badge", "menuBar", "autoUpdate"]; |
| 6 | var current = null; |
| 7 | |
| 8 | function byId(id) { |
| 9 | return document.getElementById(id); |
| 10 | } |
| 11 | |
| 12 | function invoke(command, args) { |
| 13 | var core = window.__TAURI__ && window.__TAURI__.core; |
| 14 | if (core && typeof core.invoke === "function") return core.invoke(command, args || {}); |
| 15 | return Promise.reject(new Error("This page belongs inside the g1t desktop app.")); |
| 16 | } |
| 17 | |
| 18 | function fail(error) { |
| 19 | var box = byId("error"); |
| 20 | box.textContent = typeof error === "string" ? error : (error && error.message) || "That could not be changed."; |
| 21 | box.hidden = false; |
| 22 | } |
| 23 | |
| 24 | function hostOf(server) { |
| 25 | try { |
| 26 | return new URL(server).host; |
| 27 | } catch (error) { |
| 28 | return server; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // What changes between Windows, macOS and Linux. |
| 33 | function platform(name) { |
| 34 | byId("flash-label").textContent = |
| 35 | name === "macos" ? "Bounce the dock icon" : name === "windows" ? "Flash the taskbar icon" : "Ask for attention"; |
| 36 | // macOS keeps its menu bar at the top of the screen, always there. |
| 37 | byId("menu-bar-row").hidden = name === "macos"; |
| 38 | if (name === "macos") byId("zoom-note").textContent = "The View menu changes it too: ⌘+, ⌘− and ⌘0."; |
| 39 | else byId("zoom-note").textContent = "The View menu changes it too: Ctrl+=, Ctrl+− and Ctrl+0."; |
| 40 | } |
| 41 | |
| 42 | function render(view) { |
| 43 | current = view; |
| 44 | byId("error").hidden = true; |
| 45 | byId("openAtLogin").checked = !!view.openAtLogin; |
| 46 | SWITCHES.forEach(function (key) { |
| 47 | byId(key).checked = !!view[key]; |
| 48 | }); |
| 49 | // Starting in the tray only means something when g1t starts at sign-in. |
| 50 | byId("startHidden").disabled = !view.openAtLogin; |
| 51 | byId("startHidden").closest(".setting").classList.toggle("off", !view.openAtLogin); |
| 52 | var zoom = byId("zoom"); |
| 53 | zoom.value = String(view.zoom); |
| 54 | if (zoom.value === "") zoom.value = "1"; |
| 55 | byId("version").textContent = "g1t " + view.version; |
| 56 | if (view.updateReady) byId("update-status").textContent = "Version " + view.updateReady + " is downloaded. Restart g1t to use it."; |
| 57 | byId("server").textContent = hostOf(view.server); |
| 58 | } |
| 59 | |
| 60 | function settingsOf() { |
| 61 | var settings = {}; |
| 62 | SWITCHES.forEach(function (key) { |
| 63 | settings[key] = byId(key).checked; |
| 64 | }); |
| 65 | settings.zoom = Number(byId("zoom").value) || 1; |
| 66 | return settings; |
| 67 | } |
| 68 | |
| 69 | function save() { |
| 70 | invoke("save_settings", { settings: settingsOf() }).then(render, function (error) { |
| 71 | fail(error); |
| 72 | if (current) render(current); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | SWITCHES.forEach(function (key) { |
| 77 | byId(key).addEventListener("change", save); |
| 78 | }); |
| 79 | byId("zoom").addEventListener("change", save); |
| 80 | byId("openAtLogin").addEventListener("change", function (event) { |
| 81 | invoke("set_open_at_login", { on: event.target.checked }).then(render, function (error) { |
| 82 | fail(error); |
| 83 | if (current) render(current); |
| 84 | }); |
| 85 | }); |
| 86 | |
| 87 | byId("check").addEventListener("click", function () { |
| 88 | var button = byId("check"); |
| 89 | var status = byId("update-status"); |
| 90 | button.disabled = true; |
| 91 | status.textContent = "Checking…"; |
| 92 | invoke("check_for_updates") |
| 93 | .then(function (answer) { |
| 94 | if (answer === "current") status.textContent = "You have the newest version."; |
| 95 | else if (answer.indexOf("ready:") === 0) status.textContent = "Version " + answer.slice(6) + " is downloaded. Restart g1t to use it."; |
| 96 | else status.textContent = answer; |
| 97 | }) |
| 98 | .catch(function (error) { |
| 99 | status.textContent = "Could not check: " + (typeof error === "string" ? error : "try again later."); |
| 100 | }) |
| 101 | .then(function () { |
| 102 | button.disabled = false; |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | byId("change-server").addEventListener("click", function () { |
| 107 | invoke("change_server").catch(fail); |
| 108 | }); |
| 109 | |
| 110 | // Links out, in the system's browser (src/bridge.js does the same on the site). |
| 111 | document.addEventListener("click", function (event) { |
| 112 | var anchor = event.target && event.target.closest ? event.target.closest("a[href]") : null; |
| 113 | if (!anchor) return; |
| 114 | event.preventDefault(); |
| 115 | invoke("open_url", { url: anchor.href }).catch(function () {}); |
| 116 | }); |
| 117 | |
| 118 | invoke("get_settings") |
| 119 | .then(function (view) { |
| 120 | platform(view.platform); |
| 121 | render(view); |
| 122 | }) |
| 123 | .catch(fail); |
| 124 | })(); |