Add profile > new window menu

This commit is contained in:
Labhansh Agrawal 2023-07-01 00:37:04 +05:30
parent 354c0aa7dd
commit fd20ce660a
9 changed files with 38 additions and 17 deletions

View file

@ -147,6 +147,9 @@ const commands: Record<string, (focusedWindow?: BrowserWindow) => void> = {
//Profile specific commands //Profile specific commands
getConfig().profiles.forEach((profile) => { getConfig().profiles.forEach((profile) => {
commands[`window:new:${profile.name}`] = () => {
setTimeout(() => app.createWindow(undefined, undefined, profile.name), 0);
};
commands[`tab:new:${profile.name}`] = (focusedWindow) => { commands[`tab:new:${profile.name}`] = (focusedWindow) => {
focusedWindow?.rpc.emit('termgroup add req', {profile: profile.name}); focusedWindow?.rpc.emit('termgroup add req', {profile: profile.name});
}; };

View file

@ -10,7 +10,8 @@ declare global {
windowCallback?: (win: BrowserWindow) => void; windowCallback?: (win: BrowserWindow) => void;
createWindow: ( createWindow: (
fn?: (win: BrowserWindow) => void, fn?: (win: BrowserWindow) => void,
options?: {size?: [number, number]; position?: [number, number]} options?: {size?: [number, number]; position?: [number, number]},
profileName?: string
) => BrowserWindow; ) => BrowserWindow;
setVersion: (version: string) => void; setVersion: (version: string) => void;
} }
@ -22,6 +23,7 @@ declare global {
focusTime: number; focusTime: number;
clean: () => void; clean: () => void;
rpc: Server; rpc: Server;
profileName: string;
} }
} }
} }

View file

@ -90,9 +90,10 @@ app.on('ready', () =>
.then(() => { .then(() => {
function createWindow( function createWindow(
fn?: (win: BrowserWindow) => void, fn?: (win: BrowserWindow) => void,
options: {size?: [number, number]; position?: [number, number]} = {} options: {size?: [number, number]; position?: [number, number]} = {},
profileName: string = config.getDefaultProfile()
) { ) {
const cfg = plugins.getDecoratedConfig(config.getDefaultProfile()); const cfg = plugins.getDecoratedConfig(profileName);
const winSet = config.getWin(); const winSet = config.getWin();
let [startX, startY] = winSet.position; let [startX, startY] = winSet.position;
@ -134,7 +135,7 @@ app.on('ready', () =>
[startX, startY] = config.windowDefaults.windowPosition; [startX, startY] = config.windowDefaults.windowPosition;
} }
const hwin = newWindow({width, height, x: startX, y: startY}, cfg, fn); const hwin = newWindow({width, height, x: startX, y: startY}, cfg, fn, profileName);
windowSet.add(hwin); windowSet.add(hwin);
void hwin.loadURL(url); void hwin.loadURL(url);

View file

@ -54,6 +54,12 @@ export default (
execCommand(`tab:new:${profile}`, focusedWindow); execCommand(`tab:new:${profile}`, focusedWindow);
} }
}, },
{
label: 'New Window',
click(item, focusedWindow) {
execCommand(`window:new:${profile}`, focusedWindow);
}
},
{ {
type: 'separator' type: 'separator'
}, },

View file

@ -425,10 +425,6 @@ export const getDecoratedEnv = (baseEnv: Record<string, string>) => {
return decorateObject(baseEnv, 'decorateEnv'); return decorateObject(baseEnv, 'decorateEnv');
}; };
export const getDefaultProfile = () => {
return config.getDefaultProfile();
};
export const getDecoratedConfig = (profile: string) => { export const getDecoratedConfig = (profile: string) => {
const baseConfig = config.getProfileConfig(profile); const baseConfig = config.getProfileConfig(profile);
const decoratedConfig = decorateObject(baseConfig, 'decorateConfig'); const decoratedConfig = decorateObject(baseConfig, 'decorateConfig');

View file

@ -25,7 +25,8 @@ import {getDefaultProfile} from '../config';
export function newWindow( export function newWindow(
options_: BrowserWindowConstructorOptions, options_: BrowserWindowConstructorOptions,
cfg: configOptions, cfg: configOptions,
fn?: (win: BrowserWindow) => void fn?: (win: BrowserWindow) => void,
profileName: string = getDefaultProfile()
): BrowserWindow { ): BrowserWindow {
const classOpts = Object.assign({uid: uuidv4()}); const classOpts = Object.assign({uid: uuidv4()});
app.plugins.decorateWindowClass(classOpts); app.plugins.decorateWindowClass(classOpts);
@ -51,6 +52,8 @@ export function newWindow(
}; };
const window = new BrowserWindow(app.plugins.getDecoratedBrowserOptions(winOpts)); const window = new BrowserWindow(app.plugins.getDecoratedBrowserOptions(winOpts));
window.profileName = profileName;
// Enable remote module on this window // Enable remote module on this window
remoteEnable(window.webContents); remoteEnable(window.webContents);
@ -63,13 +66,13 @@ export function newWindow(
const sessions = new Map<string, Session>(); const sessions = new Map<string, Session>();
const updateBackgroundColor = () => { const updateBackgroundColor = () => {
const cfg_ = app.plugins.getDecoratedConfig(getDefaultProfile()); const cfg_ = app.plugins.getDecoratedConfig(profileName);
window.setBackgroundColor(toElectronBackgroundColor(cfg_.backgroundColor || '#000')); window.setBackgroundColor(toElectronBackgroundColor(cfg_.backgroundColor || '#000'));
}; };
// config changes // config changes
const cfgUnsubscribe = app.config.subscribe(() => { const cfgUnsubscribe = app.config.subscribe(() => {
const cfg_ = app.plugins.getDecoratedConfig(getDefaultProfile()); const cfg_ = app.plugins.getDecoratedConfig(profileName);
// notify renderer // notify renderer
window.webContents.send('config change'); window.webContents.send('config change');
@ -119,7 +122,7 @@ export function newWindow(
if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key]; if (extraOptions[key] !== undefined) extraOptionsFiltered[key] = extraOptions[key];
}); });
const profile = extraOptionsFiltered.profile || getDefaultProfile(); const profile = extraOptionsFiltered.profile || profileName;
const activeSession = extraOptionsFiltered.activeUid ? sessions.get(extraOptionsFiltered.activeUid) : undefined; const activeSession = extraOptionsFiltered.activeUid ? sessions.get(extraOptionsFiltered.activeUid) : undefined;
let cwd = ''; let cwd = '';
if (cfg.preserveCWD !== false && activeSession && activeSession.profile === profile) { if (cfg.preserveCWD !== false && activeSession && activeSession.profile === profile) {
@ -161,7 +164,7 @@ export function newWindow(
}, },
extraOptionsFiltered, extraOptionsFiltered,
{ {
profile: extraOptionsFiltered.profile || getDefaultProfile(), profile: extraOptionsFiltered.profile || profileName,
uid uid
} }
); );

View file

@ -20,7 +20,7 @@ function requestSplit(direction: 'VERTICAL' | 'HORIZONTAL') {
effect: () => { effect: () => {
const {ui, sessions} = getState(); const {ui, sessions} = getState();
const activeUid = _activeUid ? _activeUid : sessions.activeUid; const activeUid = _activeUid ? _activeUid : sessions.activeUid;
const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : undefined; const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : window.profileName;
rpc.emit('new', { rpc.emit('new', {
splitDirection: direction, splitDirection: direction,
cwd: ui.cwd, cwd: ui.cwd,
@ -51,7 +51,7 @@ export function requestTermGroup(_activeUid: string | undefined, _profile: strin
const {ui, sessions} = getState(); const {ui, sessions} = getState();
const {cwd} = ui; const {cwd} = ui;
const activeUid = _activeUid ? _activeUid : sessions.activeUid; const activeUid = _activeUid ? _activeUid : sessions.activeUid;
const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : undefined; const profile = _profile ? _profile : activeUid ? sessions.sessions[activeUid].profile : window.profileName;
rpc.emit('new', { rpc.emit('new', {
isNewGroup: true, isNewGroup: true,
cwd, cwd,

1
lib/hyper.d.ts vendored
View file

@ -6,6 +6,7 @@ declare global {
__rpcId: string; __rpcId: string;
rpc: Client; rpc: Client;
focusActiveTerm: (uid?: string) => void; focusActiveTerm: (uid?: string) => void;
profileName: string;
} }
const snapshotResult: { const snapshotResult: {

View file

@ -1,11 +1,20 @@
import {ipcRenderer} from 'electron'; import {ipcRenderer} from 'electron';
import {require as remoteRequire} from '@electron/remote'; import {require as remoteRequire, getCurrentWindow} from '@electron/remote';
// TODO: Should be updates to new async API https://medium.com/@nornagon/electrons-remote-module-considered-harmful-70d69500f31 // TODO: Should be updates to new async API https://medium.com/@nornagon/electrons-remote-module-considered-harmful-70d69500f31
const plugins = remoteRequire('./plugins') as typeof import('../../app/plugins'); const plugins = remoteRequire('./plugins') as typeof import('../../app/plugins');
Object.defineProperty(window, 'profileName', {
get() {
return getCurrentWindow().profileName;
},
set() {
throw new Error('profileName is readonly');
}
});
export function getConfig() { export function getConfig() {
return plugins.getDecoratedConfig(plugins.getDefaultProfile()); return plugins.getDecoratedConfig(window.profileName);
} }
export function subscribe(fn: (event: Electron.IpcRendererEvent, ...args: any[]) => void) { export function subscribe(fn: (event: Electron.IpcRendererEvent, ...args: any[]) => void) {