Skip to content

Commit

Permalink
Merge pull request #204 from holochain/feat/open-settings-next-to-main
Browse files Browse the repository at this point in the history
Add logic to open settings window next to main window
  • Loading branch information
matthme authored Aug 21, 2024
2 parents 4fda016 + d03dbdf commit 6d6192d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
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.');
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

0 comments on commit 6d6192d

Please sign in to comment.