1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fix menu/navigation

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-08-10 21:25:18 +03:00
parent 312fdfa4d3
commit a57be8eb01
5 changed files with 68 additions and 32 deletions

View File

@ -18,7 +18,6 @@ import { userStore } from "../common/user-store";
import { workspaceStore } from "../common/workspace-store"; import { workspaceStore } from "../common/workspace-store";
import { tracker } from "../common/tracker"; import { tracker } from "../common/tracker";
import logger from "./logger" import logger from "./logger"
import { initMenu } from "./menu";
let windowManager: WindowManager; let windowManager: WindowManager;
let clusterManager: ClusterManager; let clusterManager: ClusterManager;
@ -31,7 +30,6 @@ if (app.commandLine.getSwitchValue("proxy-server") !== "") {
async function main() { async function main() {
await shellSync(); await shellSync();
initMenu();
const workingDir = path.join(app.getPath("appData"), appName); const workingDir = path.join(app.getPath("appData"), appName);
app.setName(appName); app.setName(appName);

View File

@ -1,35 +1,36 @@
import { app, BrowserWindow, dialog, ipcMain, Menu, MenuItem, MenuItemConstructorOptions, shell, webContents } from "electron" import { app, BrowserWindow, dialog, Menu, MenuItem, MenuItemConstructorOptions, shell } from "electron"
import { autorun, observable } from "mobx"; import { autorun } from "mobx";
import { broadcastIpc } from "../common/ipc"; import { WindowManager } from "./window-manager";
import { appName, isMac, issuesTrackerUrl, isWindows, slackUrl } from "../common/vars"; import { appName, isMac, issuesTrackerUrl, isWindows, slackUrl } from "../common/vars";
import { ClusterId, clusterStore } from "../common/cluster-store";
import { addClusterURL } from "../renderer/components/+add-cluster/add-cluster.route"; import { addClusterURL } from "../renderer/components/+add-cluster/add-cluster.route";
import { preferencesURL } from "../renderer/components/+preferences/preferences.route"; import { preferencesURL } from "../renderer/components/+preferences/preferences.route";
import { whatsNewURL } from "../renderer/components/+whats-new/whats-new.route"; import { whatsNewURL } from "../renderer/components/+whats-new/whats-new.route";
import { clusterSettingsURL } from "../renderer/components/+cluster-settings/cluster-settings.route"; import { clusterSettingsURL } from "../renderer/components/+cluster-settings/cluster-settings.route";
import logger from "./logger"; import logger from "./logger";
const activeClusterView = observable.box<ClusterId>(); export function initMenu(windowManager: WindowManager) {
autorun(() => buildMenu(windowManager), {
export function initMenu() { delay: 100
autorun(buildMenu);
ipcMain.on("menu:refresh", (evt, activeClusterId: ClusterId) => {
activeClusterView.set(activeClusterId);
}); });
} }
export function buildMenu() { export function buildMenu(windowManager: WindowManager) {
function macOnly(menuItems: MenuItemConstructorOptions[]): MenuItemConstructorOptions[] { function macOnly(menuItems: MenuItemConstructorOptions[]) {
if (!isMac) return []; if (!isMac) return [];
return menuItems; return menuItems;
} }
function activeClusterOnly(menuItems: MenuItemConstructorOptions[]) {
if (!windowManager.activeClusterId) return [];
return menuItems;
}
function navigate(url: string, toClusterView = false) { function navigate(url: string, toClusterView = false) {
logger.info(`[MENU]: navigating to ${url}`); logger.info(`[MENU]: navigating to ${url}`);
const clusterId = activeClusterView.get(); windowManager.navigate({
broadcastIpc({ channel: "menu:navigate",
channel: "menu:navigate" + (toClusterView ? `:${clusterId}` : ""), url: url,
args: [url], clusterId: toClusterView ? windowManager.activeClusterId : null,
}) })
} }
@ -42,12 +43,14 @@ export function buildMenu() {
navigate(addClusterURL()) navigate(addClusterURL())
} }
}, },
...(activeClusterView.get() ? [{ ...activeClusterOnly([
label: 'Cluster Settings', {
click() { label: 'Cluster Settings',
navigate(clusterSettingsURL(), true) click() {
navigate(clusterSettingsURL(), true)
}
} }
}] : []), ]),
{ type: 'separator' }, { type: 'separator' },
{ {
label: 'Preferences', label: 'Preferences',
@ -90,24 +93,33 @@ export function buildMenu() {
label: 'Back', label: 'Back',
accelerator: 'CmdOrCtrl+[', accelerator: 'CmdOrCtrl+[',
click() { click() {
webContents.getFocusedWebContents()?.goBack(); // fixme: works until second cluster selected windowManager.getActiveClusterView()?.goBack();
} }
}, },
{ {
label: 'Forward', label: 'Forward',
accelerator: 'CmdOrCtrl+]', accelerator: 'CmdOrCtrl+]',
click() { click() {
webContents.getFocusedWebContents()?.goForward(); windowManager.getActiveClusterView()?.goForward();
} }
}, },
{ {
label: 'Reload', label: 'Reload',
accelerator: 'CmdOrCtrl+R', accelerator: 'CmdOrCtrl+R',
click() { click() {
webContents.getFocusedWebContents()?.reload(); windowManager.getActiveClusterView()?.reload();
} }
}, },
{ role: 'toggleDevTools' }, { role: 'toggleDevTools' },
...activeClusterOnly([
{
accelerator: "CmdOrCtrl+Shift+I",
label: "Toggle Dashboard DevTools",
click() {
windowManager.getActiveClusterView()?.toggleDevTools();
}
}
]),
{ type: 'separator' }, { type: 'separator' },
{ role: 'resetZoom' }, { role: 'resetZoom' },
{ role: 'zoomIn' }, { role: 'zoomIn' },

View File

@ -1,11 +1,16 @@
import { BrowserWindow, shell } from "electron" import type { ClusterId } from "../common/cluster-store";
import { BrowserWindow, ipcMain, shell, WebContents, webContents } from "electron"
import windowStateKeeper from "electron-window-state" import windowStateKeeper from "electron-window-state"
import { observable } from "mobx";
import { initMenu } from "./menu";
export class WindowManager { export class WindowManager {
protected mainView: BrowserWindow; protected mainView: BrowserWindow;
protected splashWindow: BrowserWindow; protected splashWindow: BrowserWindow;
protected windowState: windowStateKeeper.State; protected windowState: windowStateKeeper.State;
@observable activeClusterId: ClusterId;
constructor(protected proxyPort: number) { constructor(protected proxyPort: number) {
// Manage main window size and position with state persistence // Manage main window size and position with state persistence
this.windowState = windowStateKeeper({ this.windowState = windowStateKeeper({
@ -35,8 +40,32 @@ export class WindowManager {
shell.openExternal(url); shell.openExternal(url);
}); });
// track visible cluster from ui
ipcMain.on("cluster-view:change", (event, clusterId: ClusterId) => {
this.activeClusterId = clusterId;
});
// load & show app // load & show app
this.showMain(); this.showMain();
initMenu(this);
}
navigate({ url, channel, clusterId }: { url: string, channel: string, clusterId?: ClusterId }) {
if (clusterId) {
this.getClusterView(clusterId)?.send(channel, url);
} else {
this.mainView.webContents.send(channel, url);
}
}
getActiveClusterView() {
return this.getClusterView(this.activeClusterId)
}
getClusterView(clusterId: ClusterId): WebContents {
return webContents.getAllWebContents().find(view => {
return new URL(view.getURL()).host.split(".")[0] === clusterId;
})
} }
async showMain() { async showMain() {

View File

@ -33,7 +33,7 @@ if (ipcRenderer) {
const isMainView = !getHostedClusterId(); const isMainView = !getHostedClusterId();
if (isMainView) { if (isMainView) {
reaction(() => getMatchedClusterId(), clusterId => { reaction(() => getMatchedClusterId(), clusterId => {
ipcRenderer.send("menu:refresh", clusterId); ipcRenderer.send("cluster-view:change", clusterId);
}, { }, {
fireImmediately: true fireImmediately: true
}) })

View File

@ -4,16 +4,13 @@ import { ipcRenderer } from "electron";
import { compile } from "path-to-regexp" import { compile } from "path-to-regexp"
import { createBrowserHistory, createMemoryHistory, Location, LocationDescriptor } from "history"; import { createBrowserHistory, createMemoryHistory, Location, LocationDescriptor } from "history";
import { createObservableHistory } from "mobx-observable-history"; import { createObservableHistory } from "mobx-observable-history";
import { getHostedClusterId } from "../common/cluster-store";
export const history = typeof window !== "undefined" ? createBrowserHistory() : createMemoryHistory(); export const history = typeof window !== "undefined" ? createBrowserHistory() : createMemoryHistory();
export const navigation = createObservableHistory(history); export const navigation = createObservableHistory(history);
// handle navigation from global menu // handle navigation from global menu
if (ipcRenderer) { if (ipcRenderer) {
const clusterId = getHostedClusterId(); ipcRenderer.on("menu:navigate", (event, path: string) => {
const channel = "menu:navigate" + (clusterId ? `:${clusterId}` : "");
ipcRenderer.on(channel, (event, path: string) => {
navigate(path); navigate(path);
}) })
} }