Support tg:// deeplinks in Electron (#3807)

This commit is contained in:
Alexander Zinchuk 2023-09-08 18:39:26 +02:00
parent c59d468c73
commit b806423831
6 changed files with 121 additions and 24 deletions

View File

@ -394,6 +394,12 @@ const Main: FC<OwnProps & StateProps> = ({
}
}, []);
useEffect(() => {
return window.electron?.on(ElectronEvent.DEEPLINK, (link: string) => {
processDeepLink(decodeURIComponent(link));
});
}, []);
useEffect(() => {
const parsedLocationHash = parseLocationHash();
if (!parsedLocationHash) return;

View File

@ -1,9 +1,9 @@
productName: "Telegram A"
artifactName: "${productName}-${arch}.${ext}"
appId: "org.telegram.TelegramA"
productName: Telegram A
artifactName: ${productName}-${arch}.${ext}
appId: org.telegram.TelegramA
extraMetadata:
main: "./dist/electron.js"
productName: "Telegram A"
main: ./dist/electron.js
productName: Telegram A
files:
- "dist"
- "package.json"
@ -14,40 +14,47 @@ files:
- "!dist/get"
- "!node_modules"
directories:
buildResources: "./public"
output: "./dist-electron"
buildResources: ./public
output: ./dist-electron
protocols:
- name: Tg
schemes:
- tg
publish:
provider: "github"
owner: "Ajaxy"
repo: "telegram-tt"
releaseType: "draft"
provider: github
owner: Ajaxy
repo: telegram-tt
releaseType: draft
win:
target: "nsis"
icon: "public/icon-electron-windows.ico"
target: nsis
icon: public/icon-electron-windows.ico
nsis:
oneClick: false
createDesktopShortcut: true
createStartMenuShortcut: true
mac:
target:
target: "default"
arch: ["x64", "arm64"]
entitlements: "public/electron-entitlements.mac.plist"
icon: "public/icon-electron-macos.icns"
target: default
arch:
- x64
- arm64
entitlements: public/electron-entitlements.mac.plist
icon: public/icon-electron-macos.icns
notarize:
teamId: "Y54Z4K69Z9"
teamId: Y54Z4K69Z9
dmg:
title: "Telegram A installer"
background: "public/background-electron-dmg.tiff"
title: Telegram A installer
background: public/background-electron-dmg.tiff
iconSize: 100
contents:
- x: 138
y: 225
type: "file"
type: file
- x: 402
y: 225
type: "link"
type: link
path: "/Applications"
linux:
category: "Community"
target: ["AppImage"]
category: Community
target:
- AppImage

78
src/electron/deeplink.ts Normal file
View File

@ -0,0 +1,78 @@
import { app } from 'electron';
import path from 'path';
import { ElectronEvent } from '../types/electron';
import {
IS_LINUX, IS_MAC_OS, IS_WINDOWS, windows,
} from './utils';
const TG_PROTOCOL = 'tg';
let deeplinkUrl: string | undefined;
export function initDeeplink() {
const currentWindow = Array.from(windows).pop();
if (process.defaultApp) {
if (process.argv.length >= 2) {
app.setAsDefaultProtocolClient(TG_PROTOCOL, process.execPath, [path.resolve(process.argv[1])]);
}
} else {
app.setAsDefaultProtocolClient(TG_PROTOCOL);
}
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
return;
}
app.on('will-finish-launching', () => {
app.on('open-url', (event: Event, url: string) => {
event.preventDefault();
deeplinkUrl = url;
processDeeplink();
});
});
if (IS_WINDOWS || IS_LINUX) {
deeplinkUrl = findDeeplink(process.argv);
}
app.on('second-instance', (_, argv: string[]) => {
if (IS_MAC_OS) {
deeplinkUrl = argv[0];
} else {
deeplinkUrl = findDeeplink(argv);
}
processDeeplink();
if (currentWindow) {
if (currentWindow.isMinimized()) {
currentWindow.restore();
}
currentWindow.focus();
}
});
}
export function processDeeplink() {
const currentWindow = Array.from(windows).pop();
if (!currentWindow || !deeplinkUrl) {
return;
}
currentWindow.webContents.send(ElectronEvent.DEEPLINK, deeplinkUrl);
deeplinkUrl = undefined;
}
function findDeeplink(args: string[]) {
return args.find((arg) => arg.startsWith(`${TG_PROTOCOL}://`));
}

View File

@ -4,9 +4,12 @@ import { app, nativeImage } from 'electron';
import contextMenu from 'electron-context-menu';
import path from 'path';
import { initDeeplink } from './deeplink';
import { createWindow, setupCloseHandlers, setupElectronActionHandlers } from './window';
import { IS_MAC_OS, IS_WINDOWS } from './utils';
initDeeplink();
contextMenu({
showLearnSpelling: false,
showLookUpSelection: false,

View File

@ -7,6 +7,7 @@ import path from 'path';
import { ElectronAction, ElectronEvent } from '../types/electron';
import type { TrafficLightPosition } from '../types/electron';
import setupAutoUpdates from './autoUpdates';
import { processDeeplink } from './deeplink';
import {
forceQuit, getAppTitle, IS_MAC_OS, TRAFFIC_LIGHT_POSITION, windows,
} from './utils';
@ -123,6 +124,7 @@ export function createWindow(url?: string) {
window.webContents.once('dom-ready', () => {
window.show();
processDeeplink();
if (process.env.APP_ENV === 'production') {
setupAutoUpdates(window, windowState);

View File

@ -2,6 +2,7 @@ export enum ElectronEvent {
FULLSCREEN_CHANGE = 'fullscreen-change',
UPDATE_ERROR = 'update-error',
UPDATE_DOWNLOADED = 'update-downloaded',
DEEPLINK = 'deeplink',
}
export enum ElectronAction {