mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-10-27 01:53:45 +00:00
147 lines
4.1 KiB
TypeScript
147 lines
4.1 KiB
TypeScript
import process from "node:process";
|
|
|
|
import type {DndSettings} from "../../../../common/dnd-util";
|
|
import {html} from "../../../../common/html";
|
|
import type {NavItem} from "../../../../common/types";
|
|
import {ipcRenderer} from "../../typed-ipc-renderer";
|
|
|
|
import {initConnectedOrgSection} from "./connected-org-section";
|
|
import {initGeneralSection} from "./general-section";
|
|
import Nav from "./nav";
|
|
import {initNetworkSection} from "./network-section";
|
|
import {initServersSection} from "./servers-section";
|
|
import {initShortcutsSection} from "./shortcuts-section";
|
|
|
|
export class PreferenceView {
|
|
readonly $view: HTMLElement;
|
|
private readonly $shadow: ShadowRoot;
|
|
private readonly $settingsContainer: Element;
|
|
private readonly nav: Nav;
|
|
private navItem: NavItem = "General";
|
|
|
|
constructor() {
|
|
this.$view = document.createElement("div");
|
|
this.$shadow = this.$view.attachShadow({mode: "open"});
|
|
this.$shadow.innerHTML = html`
|
|
<link
|
|
rel="stylesheet"
|
|
href="${require.resolve("../../../css/fonts.css")}"
|
|
/>
|
|
<link
|
|
rel="stylesheet"
|
|
href="${require.resolve("../../../css/preference.css")}"
|
|
/>
|
|
<link
|
|
rel="stylesheet"
|
|
href="${require.resolve("@yaireo/tagify/dist/tagify.css")}"
|
|
/>
|
|
<!-- Initially hidden to prevent FOUC -->
|
|
<div id="content" hidden>
|
|
<div id="sidebar"></div>
|
|
<div id="settings-container"></div>
|
|
</div>
|
|
`.html;
|
|
|
|
const $sidebarContainer = this.$shadow.querySelector("#sidebar")!;
|
|
this.$settingsContainer = this.$shadow.querySelector(
|
|
"#settings-container",
|
|
)!;
|
|
|
|
this.nav = new Nav({
|
|
$root: $sidebarContainer,
|
|
onItemSelected: this.handleNavigation,
|
|
});
|
|
|
|
ipcRenderer.on("toggle-sidebar", this.handleToggleSidebar);
|
|
ipcRenderer.on("toggle-autohide-menubar", this.handleToggleMenubar);
|
|
ipcRenderer.on("toggle-dnd", this.handleToggleDnd);
|
|
|
|
this.handleNavigation(this.navItem);
|
|
}
|
|
|
|
handleNavigation = (navItem: NavItem): void => {
|
|
this.navItem = navItem;
|
|
this.nav.select(navItem);
|
|
switch (navItem) {
|
|
case "AddServer":
|
|
initServersSection({
|
|
$root: this.$settingsContainer,
|
|
});
|
|
break;
|
|
|
|
case "General":
|
|
initGeneralSection({
|
|
$root: this.$settingsContainer,
|
|
});
|
|
break;
|
|
|
|
case "Organizations":
|
|
initConnectedOrgSection({
|
|
$root: this.$settingsContainer,
|
|
});
|
|
break;
|
|
|
|
case "Network":
|
|
initNetworkSection({
|
|
$root: this.$settingsContainer,
|
|
});
|
|
break;
|
|
|
|
case "Shortcuts": {
|
|
initShortcutsSection({
|
|
$root: this.$settingsContainer,
|
|
});
|
|
break;
|
|
}
|
|
|
|
default:
|
|
((n: never) => n)(navItem);
|
|
}
|
|
|
|
window.location.hash = `#${navItem}`;
|
|
};
|
|
|
|
handleToggleTray(state: boolean) {
|
|
this.handleToggle("tray-option", state);
|
|
}
|
|
|
|
destroy(): void {
|
|
ipcRenderer.off("toggle-sidebar", this.handleToggleSidebar);
|
|
ipcRenderer.off("toggle-autohide-menubar", this.handleToggleMenubar);
|
|
ipcRenderer.off("toggle-dnd", this.handleToggleDnd);
|
|
}
|
|
|
|
// Handle toggling and reflect changes in preference page
|
|
private handleToggle(elementName: string, state = false): void {
|
|
const inputSelector = `#${elementName} .action .switch input`;
|
|
const input: HTMLInputElement = this.$shadow.querySelector(inputSelector)!;
|
|
if (input) {
|
|
input.checked = state;
|
|
}
|
|
}
|
|
|
|
private readonly handleToggleSidebar = (_event: Event, state: boolean) => {
|
|
this.handleToggle("sidebar-option", state);
|
|
};
|
|
|
|
private readonly handleToggleMenubar = (_event: Event, state: boolean) => {
|
|
this.handleToggle("menubar-option", state);
|
|
};
|
|
|
|
private readonly handleToggleDnd = (
|
|
_event: Event,
|
|
_state: boolean,
|
|
newSettings: Partial<DndSettings>,
|
|
) => {
|
|
this.handleToggle("show-notification-option", newSettings.showNotification);
|
|
this.handleToggle("silent-option", newSettings.silent);
|
|
|
|
if (process.platform === "win32") {
|
|
this.handleToggle(
|
|
"flash-taskbar-option",
|
|
newSettings.flashTaskbarOnMessage,
|
|
);
|
|
}
|
|
};
|
|
}
|