From dca2281cd913ed537c745097dd6168185495edc5 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Mon, 22 Apr 2024 16:37:02 +0800 Subject: [PATCH] add side panel management --- scripts/GUI/sidepanel.js | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scripts/GUI/sidepanel.js diff --git a/scripts/GUI/sidepanel.js b/scripts/GUI/sidepanel.js new file mode 100644 index 0000000..84a1801 --- /dev/null +++ b/scripts/GUI/sidepanel.js @@ -0,0 +1,50 @@ +// Manage the sidepanel. +// The sidepanel is what I'll call the Sidebar. +export default class Sidebar { + /* + Create a new sidebar. + + @param {string} PATH the path of the file to be displayed by the sidebar + */ + constructor (PATH) { + // Set side panel's URL. + chrome.sidePanel.setOptions({ path: PATH }); + + // Grab the current tab ID. + chrome.tabs.query({ active: true, currentWindow: true }, function ([TAB]) { + chrome.sidePanel.open({windowId: TAB.id}); + this.root = PATH; + }); + + chrome.runtime.onConnect.addListener((CONNECTION) => { + if ((CONNECTION.name).includes(`view=${PATH}`)) { + this.focus(TRUE); + CONNECTION.onDisconnect.addListener(async () => { + this.focus(FALSE); + }); + } + }); + } + + /* + Set the focused state of the side panel. + + @param {boolean} STATE the focused state + */ + focus(STATE) { + if (STATE != null && (typeof STATE).includes(`bool`)) {this.state = STATE;} + else {chrome.sidePanel.setOptions({ path: this.root });} + } + + // Close the side panel. + async close() { + // Make sure that the panel is still active. + if (this.state) { + // First, disable it to close it. + await chrome.sidePanel.setOptions({enabled: false}); + + // Then, re-enable it. + await chrome.sidePanel.setOptions({enabled: true}); + }; + }; +} \ No newline at end of file