Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to open settings window next to main window #204

Merged
merged 2 commits into from
Aug 21, 2024
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
22 changes: 16 additions & 6 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import {
} from '$shared/const';
import { getErrorMessage } from '$shared/helpers';
import type {
AdminWindow,
DistributionInfoV1,
HolochainDataRoot,
HolochainPartition,
Screen,
WindowInfoRecord,
} from '$shared/types';
import {
Expand Down Expand Up @@ -87,7 +87,7 @@ import {
throwTRPCErrorError,
validateWithZod,
} from './utils';
import { createHappWindow, focusVisibleWindow, loadOrServe, setupAppWindows } from './windows';
import { createHappWindow, loadOrServe, setupAppWindows } from './windows';

const t = initTRPC.create({ isServer: true });

Expand Down Expand Up @@ -179,11 +179,15 @@ if (!isFirstInstance && app.isPackaged && !VALIDATED_CLI_ARGS.profile) {
}

app.on('second-instance', () => {
focusVisibleWindow(PRIVILEDGED_LAUNCHER_WINDOWS);
if (PRIVILEDGED_LAUNCHER_WINDOWS[MAIN_WINDOW]) {
PRIVILEDGED_LAUNCHER_WINDOWS[MAIN_WINDOW].show();
}
});

app.on('activate', () => {
focusVisibleWindow(PRIVILEDGED_LAUNCHER_WINDOWS);
if (PRIVILEDGED_LAUNCHER_WINDOWS[MAIN_WINDOW]) {
PRIVILEDGED_LAUNCHER_WINDOWS[MAIN_WINDOW].show();
}
});

protocol.registerSchemesAsPrivileged([
Expand Down Expand Up @@ -213,7 +217,7 @@ let APP_PORT: number | undefined;
let DEFAULT_HOLOCHAIN_DATA_ROOT: HolochainDataRoot | undefined;
const HOLOCHAIN_MANAGERS: Record<string, HolochainManager> = {}; // holochain managers sorted by HolochainDataRoot.name
let LAIR_HANDLE: ChildProcessWithoutNullStreams | undefined;
let PRIVILEDGED_LAUNCHER_WINDOWS: Record<Screen, BrowserWindow>; // Admin windows with special zome call signing priviledges
let PRIVILEDGED_LAUNCHER_WINDOWS: Record<AdminWindow, BrowserWindow>; // Admin windows with special zome call signing priviledges
const WINDOW_INFO_MAP: WindowInfoRecord = {}; // WindowInfo by webContents.id - used to verify origin of zome call requests
let IS_LAUNCHED = false;
let IS_QUITTING = false;
Expand Down Expand Up @@ -270,6 +274,7 @@ app.whenReady().then(async () => {

const mainWindow = PRIVILEDGED_LAUNCHER_WINDOWS[MAIN_WINDOW];
const settingsWindow = PRIVILEDGED_LAUNCHER_WINDOWS[SETTINGS_WINDOW];

mainWindow.on('close', (e) => {
if (IS_QUITTING) return;
// If launcher has already launched, i.e. not "Enter Password" screen anymore, only hide the window
Expand Down Expand Up @@ -535,7 +540,12 @@ const getDevhubAppClient = async () => {

const router = t.router({
openSettings: t.procedure.mutation(() => {
PRIVILEDGED_LAUNCHER_WINDOWS[SETTINGS_WINDOW].show();
const mainWindow = PRIVILEDGED_LAUNCHER_WINDOWS[MAIN_WINDOW];
const [xMain, yMain] = mainWindow.getPosition();
const settingsWindow = PRIVILEDGED_LAUNCHER_WINDOWS[SETTINGS_WINDOW];
if (!settingsWindow) throw new Error('Settings window is undefined.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, the settingsWindow remains undefined until the show method is called right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it should already be defined here as it is being created as part of the setupAppWindows function (in this PR again, this was not the case in #201) .

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.show() only makes it be visible and come to front on the screen. The BrowserWindow javascript object is already defined before that.

settingsWindow.setPosition(xMain + 50, yMain - 50);
settingsWindow.show();
}),
hideApp: t.procedure.mutation(() => {
PRIVILEDGED_LAUNCHER_WINDOWS[MAIN_WINDOW].hide();
Expand Down
26 changes: 9 additions & 17 deletions src/main/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import path from 'path';
import url from 'url';

import { MAIN_WINDOW, MIN_HEIGH, SETTINGS_SIZE, SETTINGS_WINDOW, WINDOW_SIZE } from '$shared/const';
import type { ExtendedAppInfo, Screen } from '$shared/types';
import type { AdminWindow, ExtendedAppInfo } from '$shared/types';
import { LAUNCHER_ERROR } from '$shared/types';

import type { LauncherFileSystem } from './filesystem';
Expand Down Expand Up @@ -73,17 +73,6 @@ const createAdminWindow = ({
},
});

export const focusVisibleWindow = (launcherWindows: Record<Screen, BrowserWindow>) => {
const windows = Object.values(launcherWindows);
const anyVisible = windows.some((window) => !window.isMinimized() && window.isVisible());

if (!anyVisible) {
launcherWindows[MAIN_WINDOW].show();
} else {
windows.find((window) => !window.isMinimized() && window.isVisible())?.focus();
}
};

export const setupAppWindows = (launcherEmitter: LauncherEmitter) => {
// Create the browser window.
const mainIcon = nativeImage.createFromPath(path.join(ICONS_DIRECTORY, '../icon.png'));
Expand All @@ -105,7 +94,7 @@ export const setupAppWindows = (launcherEmitter: LauncherEmitter) => {

loadOrServe(mainWindow, { screen: MAIN_WINDOW });

const windows: Record<Screen, BrowserWindow> = {
const windows: Record<AdminWindow, BrowserWindow> = {
[MAIN_WINDOW]: mainWindow,
[SETTINGS_WINDOW]: settingsWindow,
};
Expand All @@ -116,9 +105,9 @@ export const setupAppWindows = (launcherEmitter: LauncherEmitter) => {
type: 'normal',
click() {
try {
focusVisibleWindow(windows);
mainWindow.show();
} catch (e) {
launcherEmitter.emit(LAUNCHER_ERROR, `Failed to focus visible window: ${e}`);
launcherEmitter.emit(LAUNCHER_ERROR, `Failed to focus main window: ${e}`);
}
},
},
Expand Down Expand Up @@ -152,8 +141,11 @@ export const setupAppWindows = (launcherEmitter: LauncherEmitter) => {
tray.setContextMenu(trayContextMenu);

globalShortcut.register('CommandOrControl+Shift+L', () => {
mainWindow.setSize(WINDOW_SIZE, MIN_HEIGH);
focusVisibleWindow(windows);
if (mainWindow.isFocused()) {
mainWindow.hide();
} else {
mainWindow.show();
}
});

mainWindow.once('ready-to-show', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/types/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type SETTINGS_WINDOW,
} from '../const';

export type Screen = typeof MAIN_WINDOW | typeof SETTINGS_WINDOW;
export type AdminWindow = typeof MAIN_WINDOW | typeof SETTINGS_WINDOW;

export const MainScreenRouteSchema = z.union([z.literal(APP_STORE), z.literal(APPS_VIEW)]);

Expand Down