Skip to content

Commit

Permalink
fix: second instance lock, deep links
Browse files Browse the repository at this point in the history
  • Loading branch information
N0chteil committed Jun 17, 2023
1 parent 38c0921 commit 8bacede
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<div class="settings">
<div class="notes"></div>

<div class="settings_category">
<div class="settings_category" id="general">
<div class="category__header">
<h3 data-i18n="settings.category.general">
General
Expand Down Expand Up @@ -131,7 +131,7 @@ <h3 data-i18n="settings.category.general">
</div>
</div>

<div class="settings_category">
<div class="settings_category" id="rpc">
<div class="category__header">
<h3>Rich Presence</h3>
</div>
Expand Down Expand Up @@ -291,7 +291,7 @@ <h3>Rich Presence</h3>
</div>
</div>

<div class="settings_category">
<div class="settings_category" id="lastfm">
<div class="category__header">
<h3>Last.fm</h3>
</div>
Expand Down Expand Up @@ -330,7 +330,7 @@ <h3>Last.fm</h3>
</div>
</div>

<div class="settings_category">
<div class="settings_category" id="cache">
<div class="category__header">
<h3 data-i18n="settings.category.cache">
Cache Management
Expand Down Expand Up @@ -396,7 +396,7 @@ <h3 data-i18n="settings.category.cache">
</div>
</div>

<div class="settings_category">
<div class="settings_category" id="advanced">
<div class="category__header">
<h3 data-i18n="settings.category.advanced">
Advanced
Expand Down
3 changes: 2 additions & 1 deletion src/browser/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ contextBridge.exposeInMainWorld("api", {
"update-downloaded",
"get-current-track",
"open-modal",
"lastfm-connect"
"lastfm-connect",
"url"
];

if (validChannels.includes(channel))
Expand Down
9 changes: 9 additions & 0 deletions src/browser/renderer/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ export function init() {

new Modal(data.title, data.description, data.buttons);
});

window.api.receive("url", async (url: string) => {
console.log("[BROWSER][RENDERER]", "Received URL", url);

document.getElementById(url).scrollIntoView({
behavior: "smooth",
block: "center"
});
});
}
3 changes: 2 additions & 1 deletion src/browser/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { init as initLastFM } from "./lastFM.js";
import { init as initListeners } from "./listeners.js";
import { init as initEventSettings } from "./eventSettings.js";

initAPI();

declare global {
interface Window {
electron: any;
Expand All @@ -35,7 +37,6 @@ export let appVersion,

console.log("[BROWSER][RENDERER] Loading...");

initAPI();
initLastFM();
initEventSettings();
initListeners();
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { init as initAutoUpdater } from "./managers/updater";
import { init as initTheme } from "./utils/theme";
import { init as initMsStoreModal } from "./utils/msStoreModal";
import { init as initCrowdin } from "./utils/crowdin";
import { init as initProtocol } from "./utils/protocol";

import { checkAppDependency } from "./utils/checkAppDependency";

Expand Down Expand Up @@ -44,6 +45,7 @@ if (isRC) log.info("[READY]", "Detected release candidate build");
if (process.windowsStore) log.info("[READY]", "Detected Windows Store build");

initSentry();
initProtocol();

app.on("ready", async () => {
await initCrowdin().catch((err) => log.error("[READY][initCrowdin]", err));
Expand Down
17 changes: 9 additions & 8 deletions src/managers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,20 @@ export class Browser {
static awaitsSend: { channel: string; args: any[] }[] = [];
static instance: Browser;

constructor() {
constructor(url: string = "") {
if (Browser.instance) {
Browser.windowAction("show");
return;
}

initIPC();

this.initWindow();

this.isReady = true;

this.checkAwaits();
this.initWindow(undefined, url);

Browser.instance = this;
}

initWindow(show: boolean = false) {
initWindow(show: boolean = false, url: string = "") {
const windowState = getConfig("windowState");

this.window = new BrowserWindow({
Expand All @@ -55,7 +51,12 @@ export class Browser {
this.window = null;
});

if (show) this.window.show();
if (show || url) this.window.show();
if (url) {
this.window.webContents.on("did-finish-load", () => {
this.window.webContents.send("url", url);
});
}
}

async windowAction(action: string) {
Expand Down
40 changes: 40 additions & 0 deletions src/utils/protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { app } from "electron";

import * as path from "path";
import { Browser } from "../managers/browser";

export function init() {
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient("amrpc", process.execPath, [
path.resolve(process.argv[1])
]);
}
} else {
app.setAsDefaultProtocolClient("amrpc");
}

app.on("second-instance", (_e, commandLine) => {
let url = commandLine.pop()?.replace("amrpc://", "");

if (url && url.endsWith("/")) url = url.slice(0, -1);

if (url === "--allow-file-access-from-files") new Browser();
else {
new Browser(url.replace("settings/", ""));
}
});

app.on("open-url", (_e, url) => {
if (!url) return;

url = url.replace("amrpc://", "");

if (url && url.endsWith("/")) url = url.slice(0, -1);

if (url === "--allow-file-access-from-files") new Browser();
else {
new Browser(url.replace("settings/", ""));
}
});
}

0 comments on commit 8bacede

Please sign in to comment.