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

Adding tracking of mainWindow's visibility to ProtocolRouter, open main view on valid lens:// (#3026)

This commit is contained in:
Sebastian Malton 2021-06-14 08:55:45 -04:00 committed by GitHub
parent 5bd1d079de
commit fa5a54f271
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 19 deletions

View File

@ -251,19 +251,31 @@ let blockQuit = true;
autoUpdater.on("before-quit-for-update", () => blockQuit = false);
app.on("will-quit", (event) => {
// Quit app on Cmd+Q (MacOS)
// This is called when the close button of the main window is clicked
const lprm = LensProtocolRouterMain.getInstance(false);
logger.info("APP:QUIT");
appEventBus.emit({ name: "app", action: "close" });
ClusterManager.getInstance(false)?.stop(); // close cluster connections
KubeconfigSyncManager.getInstance(false)?.stopSync();
LensProtocolRouterMain.getInstance(false)?.cleanup();
cleanup();
if (lprm) {
// This is set to false here so that LPRM can wait to send future lens://
// requests until after it loads again
lprm.rendererLoaded = false;
}
if (blockQuit) {
// Quit app on Cmd+Q (MacOS)
event.preventDefault(); // prevent app's default shutdown (e.g. required for telemetry, etc.)
return; // skip exit to make tray work, to quit go to app's global menu or tray's menu
}
LensProtocolRouterMain.getInstance(false)?.cleanup();
});
app.on("open-url", (event, rawUrl) => {

View File

@ -21,17 +21,35 @@
import logger from "../logger";
import * as proto from "../../common/protocol-handler";
import Url from "url-parse";
import URLParse from "url-parse";
import type { LensExtension } from "../../extensions/lens-extension";
import { broadcastMessage } from "../../common/ipc";
import { observable, when, makeObservable } from "mobx";
import { ProtocolHandlerInvalid, RouteAttempt } from "../../common/protocol-handler";
import { disposer } from "../../common/utils";
import { disposer, noop } from "../../common/utils";
import { WindowManager } from "../window-manager";
export interface FallbackHandler {
(name: string): Promise<boolean>;
}
/**
* This function checks if the host part is valid
* @param host the URI host part
* @returns `true` if it should be routed internally to Lens, `false` if to an extension
* @throws if `host` is not valid
*/
function checkHost(url: URLParse): boolean {
switch (url.host) {
case "app":
return true;
case "extension":
return false;
default:
throw new proto.RoutingError(proto.RoutingErrorType.INVALID_HOST, url);
}
}
export class LensProtocolRouterMain extends proto.LensProtocolRouter {
private missingExtensionHandlers: FallbackHandler[] = [];
@ -58,25 +76,22 @@ export class LensProtocolRouterMain extends proto.LensProtocolRouter {
*/
public route(rawUrl: string) {
try {
const url = new Url(rawUrl, true);
const url = new URLParse(rawUrl, true);
if (url.protocol.toLowerCase() !== "lens:") {
throw new proto.RoutingError(proto.RoutingErrorType.INVALID_PROTOCOL, url);
}
const routeInternally = checkHost(url);
logger.info(`${proto.LensProtocolRouter.LoggingPrefix}: routing ${url.toString()}`);
WindowManager.getInstance(false)?.ensureMainWindow().catch(noop);
switch (url.host) {
case "app":
if (routeInternally) {
this._routeToInternal(url);
break;
case "extension":
} else {
this.disposers.push(when(() => this.extensionsLoaded, () => this._routeToExtension(url)));
break;
default:
throw new proto.RoutingError(proto.RoutingErrorType.INVALID_HOST, url);
}
} catch (error) {
broadcastMessage(ProtocolHandlerInvalid, error.toString(), rawUrl);
@ -98,7 +113,7 @@ export class LensProtocolRouterMain extends proto.LensProtocolRouter {
return false;
}
protected async _findMatchingExtensionByName(url: Url): Promise<LensExtension | string> {
protected async _findMatchingExtensionByName(url: URLParse): Promise<LensExtension | string> {
const firstAttempt = await super._findMatchingExtensionByName(url);
if (typeof firstAttempt !== "string") {
@ -112,7 +127,7 @@ export class LensProtocolRouterMain extends proto.LensProtocolRouter {
return "";
}
protected _routeToInternal(url: Url): RouteAttempt {
protected _routeToInternal(url: URLParse): RouteAttempt {
const rawUrl = url.toString(); // for sending to renderer
const attempt = super._routeToInternal(url);
@ -121,7 +136,7 @@ export class LensProtocolRouterMain extends proto.LensProtocolRouter {
return attempt;
}
protected async _routeToExtension(url: Url): Promise<RouteAttempt> {
protected async _routeToExtension(url: URLParse): Promise<RouteAttempt> {
const rawUrl = url.toString(); // for sending to renderer
/**
@ -131,7 +146,7 @@ export class LensProtocolRouterMain extends proto.LensProtocolRouter {
* Note: this needs to clone the url because _routeToExtension modifies its
* argument.
*/
const attempt = await super._routeToExtension(new Url(url.toString(), true));
const attempt = await super._routeToExtension(new URLParse(url.toString(), true));
this.disposers.push(when(() => this.rendererLoaded, () => broadcastMessage(proto.ProtocolHandlerExtension, rawUrl, attempt)));