Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion public-flavors/chrome/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>Edge Workspaces</h1>
<div id="workspaces-list" class="workspaces">
<!-- Whatever is here will be removed once the workspaces list loads -->
If you see this message, it means that the workspaces list is still loading.<br>
If this message persists, please file a bug report <a href="https://github.com/Elec0/chrome-edge-workspaces/issues/new">here</a>.
If this message persists, please file a bug report <a href="https://github.com/Elec0/chrome-edge-workspaces/issues/new?template=bug_report.md">here</a>.
</div>

</div>
Expand Down
10 changes: 6 additions & 4 deletions src/popup-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PopupActions {
private static async handleNewWorkspaceResponse(response: MessageResponse, windowId: number): Promise<void> {
if (response.message === MessageResponses.SUCCESS.message) {
console.debug("Workspace added successfully, refreshing list");
WorkspaceEntryLogic.listWorkspaces(await getWorkspaceStorage());
WorkspaceEntryLogic.listWorkspaces(await getWorkspaceStorage(), await Utils.getAllWindowIds());
}
else {
LogHelper.errorAlert("Workspace could not be added\n" + response.message);
Expand Down Expand Up @@ -108,6 +108,8 @@ export class PopupActions {
return;
}
// We don't need to do anything with the response, since all the data should now be in sync
// Update the workspace list
WorkspaceEntryLogic.listWorkspaces(await getWorkspaceStorage(), await Utils.getAllWindowIds());

});
}
Expand Down Expand Up @@ -174,7 +176,7 @@ export class PopupActions {
PopupMessageHelper.sendClearWorkspaces().then(async response => {
if (response.message === MessageResponses.SUCCESS.message) {
LogHelper.successAlert("Workspace data cleared.");
WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces());
WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces(), await Utils.getAllWindowIds());
}
else {
LogHelper.errorAlert("Error clearing workspace data. Check the console for more details.");
Expand All @@ -193,7 +195,7 @@ export class PopupActions {
PopupMessageHelper.sendDeleteWorkspace(workspace.uuid).then(async response => {
if (response.message === MessageResponses.SUCCESS.message) {
console.log("Workspace deleted", workspace);
WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces());
WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces(), await Utils.getAllWindowIds());
}
else {
LogHelper.errorAlert("Error deleting workspace. Check the console for more details.");
Expand All @@ -212,7 +214,7 @@ export class PopupActions {
PopupMessageHelper.sendRenameWorkspace(workspace.uuid, newName).then(async response => {
if (response.message === MessageResponses.SUCCESS.message) {
console.log("Workspace renamed", workspace);
WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces());
WorkspaceEntryLogic.listWorkspaces(await StorageHelper.getWorkspaces(), await Utils.getAllWindowIds());
}
else {
LogHelper.errorAlert("Error renaming workspace. Check the console for more details.");
Expand Down
2 changes: 1 addition & 1 deletion src/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ img#addIcon:hover {
background-color: var(--surface-container-color);
}

.workspace-current {
.workspace-open {
font-weight: 700;
}

Expand Down
53 changes: 3 additions & 50 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PageAddWorkspace } from "./pages/page-add-workspace";
import { PageSettings } from "./pages/page-settings";
import "./popup.css";
import { StorageHelper } from "./storage-helper";
import { Utils } from "./utils";
import { WorkspaceEntryLogic } from "./workspace-entry-logic";
import { WorkspaceStorage } from "./workspace-storage";

Expand All @@ -13,46 +14,13 @@ import { WorkspaceStorage } from "./workspace-storage";
* Setup the listeners for the buttons
*/
async function documentLoaded() {
chrome.tabs.onRemoved.addListener(WorkspaceEntryLogic.tabRemoved);
chrome.tabs.onUpdated.addListener(WorkspaceEntryLogic.tabUpdated);
chrome.windows.onRemoved.addListener(windowRemoved);
const workspaceStorage = await getWorkspaceStorage();
const curWindow = await chrome.windows.getCurrent();

// Check if the popup is opened from a workspace or not
const isWorkspace = isWindowWorkspace(curWindow.id, workspaceStorage);
console.debug("Window is workspace:", isWorkspace, curWindow);

if (isWorkspace) {
loadWorkspacePopup(workspaceStorage, curWindow.id);
}
else {
loadNonWorkspacePopup(workspaceStorage);
}
}

/**
* Load the popup page for a non0workspace window.
*/
async function loadNonWorkspacePopup(workspaceStorage) {
document.getElementById("addWorkspace").addEventListener("click", addWorkspaceButtonClicked);
document.getElementById("settings-button").addEventListener("click", settingsButtonClicked);

WorkspaceEntryLogic.listWorkspaces(workspaceStorage);
}

/**
* Load the popup page for a workspace window.
* Highlight the current workspace name, and do nothing if the workspace is clicked.
* @param {WorkspaceStorage} workspaceStorage
* @param {number} curWindowId
*/
async function loadWorkspacePopup(workspaceStorage, curWindowId) {
console.log("Loading workspace popup")
document.getElementById("addWorkspace").style.display = "none";
document.getElementById("settings-button").style.display = "none";

WorkspaceEntryLogic.listWorkspaces(workspaceStorage, workspaceStorage.get(curWindowId));
WorkspaceEntryLogic.listWorkspaces(workspaceStorage, await Utils.getAllWindowIds());
}

/**
Expand All @@ -64,31 +32,16 @@ async function addWorkspaceButtonClicked() {
pageAddWorkspace.open();
}

/**
* Check if the window is a workspace window.
* @param {number} windowId
* @param {WorkspaceStorage} workspaceStorage
* @returns {boolean}
*/
function isWindowWorkspace(windowId, workspaceStorage) {
return workspaceStorage.get(windowId) !== undefined;
}

/**
* Present a popup asking for confirmation, then clear all workspace data.
*/
async function settingsButtonClicked() {
const pageSettings = new PageSettings();
pageSettings.open();

// Open basic javascript ok cancel prompt
// if (confirm("Clear all workspace data?")) {
// PopupActions.clearWorkspaceData();
// }
}

/**
*
* When a window is added or removed, update the workspace list.
* @param {chrome.windows.window} window
*/
async function windowRemoved(window) {
Expand Down
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ export class Utils {
});
}

/**
* Retrieves all Chrome window IDs.
* @returns A Promise that resolves with an array of window IDs.
*/
public static async getAllWindowIds(): Promise<(number | undefined)[]> {
return new Promise((resolve) => {
chrome.windows.getAll().then((windows) => {
const windowIds = windows.map((window) => window.id);
resolve(windowIds);
})
// If the windows are not found, the promise will still resolve, just with an empty array.
// Catch the error to prevent the console from logging it.
.catch(() => {
resolve([]);
});
});
}

/**
* Focuses a Chrome window by its ID.
* @param windowId - The ID of the window to focus.
Expand Down
29 changes: 8 additions & 21 deletions src/workspace-entry-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { WorkspaceStorage } from "./workspace-storage";
*/
export class WorkspaceEntryLogic {

public static listWorkspaces(workspaces: WorkspaceStorage, curOpenWorkspace: Workspace | undefined = undefined) {
public static listWorkspaces(workspaces: WorkspaceStorage, allWindowIds: (number | undefined)[]) {
console.debug("listWorkspaces", workspaces)

const workspaceDiv = document.getElementById("workspaces-list");
Expand All @@ -19,7 +19,7 @@ export class WorkspaceEntryLogic {
return;
}
workspaceDiv.innerHTML = "";

// Add each workspace to the list, and add event listeners to the buttons.
for (const workspace of Array.from(workspaces.values())) {
const workspaceElement = this.addWorkspace(workspaceDiv, workspace);
Expand All @@ -28,20 +28,13 @@ export class WorkspaceEntryLogic {
const editWorkspace = workspaceElement.querySelector('#edit-button');
const deleteWorkspace = workspaceElement.querySelector('#delete-button');

// Add a class to the workspace if this window is the current workspace.
if (curOpenWorkspace?.uuid === workspace.uuid) {
workspaceElement.classList.add('workspace-current');
}
else {
// Don't allow the user to re-open the current workspace.
openWorkspace?.addEventListener('click', () => {
this.workspaceClicked(workspace);
});
// Add a class to the workspace if it is open in a window.
if (allWindowIds.includes(workspace.windowId)) {
workspaceElement.classList.add('workspace-open');
}

// settingsWorkspace?.addEventListener('click', () => {
// this.workspaceSettingsClicked(workspace);
// });
openWorkspace?.addEventListener('click', () => {
this.workspaceClicked(workspace);
});

editWorkspace?.addEventListener('click', (event) => {
event.stopPropagation();
Expand Down Expand Up @@ -126,10 +119,4 @@ export class WorkspaceEntryLogic {
}
PopupActions.deleteWorkspace(workspace);
}

public static async tabRemoved(tabId: number, removeInfo: chrome.tabs.TabRemoveInfo) {
}

public static async tabUpdated(tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) {
}
}