Skip to content

Commit

Permalink
feat: pre-emptively inject the tradingView script for desktop & disab…
Browse files Browse the repository at this point in the history
…le search and numeric shortcuts in developer mode & allow navigation to swap interfaces with unsupported tokens. (#6344)
  • Loading branch information
huhuanming authored Dec 13, 2024
1 parent 1977cd1 commit 41fba93
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 87 deletions.
13 changes: 13 additions & 0 deletions apps/desktop/src-electron/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,19 @@ function createMainWindow() {
safelyBrowserWindow?.reload();
});

ipcMain.on(
ipcMessageKeys.APP_UPDATE_DISABLE_SHORTCUTS,
(
event,
params: {
disableNumberShortcuts: boolean;
disableSearchAndAccountSelectorShortcuts: boolean;
},
) => {
store.setDisableKeyboardShortcuts(params);
},
);

ipcMain.on(
ipcMessageKeys.APP_GET_MEDIA_ACCESS_STATUS,
(event, prefType: IMediaType) => {
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-electron/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const ipcMessageKeys = {
APP_GET_BUNDLE_INFO: 'app/getBundleInfo',
APP_OPEN_LOGGER_FILE: 'app/openLoggerFile',
APP_TEST_CRASH: 'app/testCrash',
APP_UPDATE_DISABLE_SHORTCUTS: 'app/updateDisableShortcuts',

// Theme
THEME_UPDATE: 'theme/update',
Expand Down
77 changes: 56 additions & 21 deletions apps/desktop/src-electron/libs/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,65 @@
import { app, globalShortcut } from 'electron';

import type { EShortcutEvents } from '@onekeyhq/shared/src/shortcuts/shortcuts.enum';
import { shortcutsMap } from '@onekeyhq/shared/src/shortcuts/shortcuts.enum';
import {
EShortcutEvents,
shortcutsMap,
} from '@onekeyhq/shared/src/shortcuts/shortcuts.enum';
import { shortcutsKeys } from '@onekeyhq/shared/src/shortcuts/shortcutsKeys.enum';

export function registerShortcuts(callback: (event: EShortcutEvents) => void) {
import * as store from './store';

export function registerShortcuts(
callback: (eventName: EShortcutEvents) => void,
) {
void app.whenReady().then(() => {
Object.entries(shortcutsMap).forEach(([event, { keys }]) => {
const { disableNumberShortcuts, disableSearchAndAccountSelectorShortcuts } =
store.getDisableKeyboardShortcuts();
Object.entries(shortcutsMap).forEach(([eventName, { keys }]) => {
if (disableNumberShortcuts && keys?.includes(shortcutsKeys.CmdOrCtrl)) {
return;
}
if (
disableSearchAndAccountSelectorShortcuts &&
[
EShortcutEvents.SearchInPage,
EShortcutEvents.AccountSelector,
].includes(eventName as EShortcutEvents)
) {
return;
}

if (
disableNumberShortcuts &&
[
EShortcutEvents.TabWallet,
EShortcutEvents.TabEarn,
EShortcutEvents.TabSwap,
EShortcutEvents.TabMarket,
EShortcutEvents.TabBrowser,
EShortcutEvents.TabPin6,
EShortcutEvents.TabPin7,
EShortcutEvents.TabPin8,
EShortcutEvents.TabPin9,
].includes(eventName as EShortcutEvents)
) {
return;
}
if (keys?.length) {
globalShortcut.register(
keys
.map((key) => {
switch (key) {
case shortcutsKeys.CmdOrCtrl:
return 'CmdOrCtrl';
case shortcutsKeys.Shift:
return 'Shift';
default:
return key;
}
})
.join('+'),
() => {
callback(event as EShortcutEvents);
},
);
const shortcutsKey = keys
.map((key) => {
switch (key) {
case shortcutsKeys.CmdOrCtrl:
return 'CmdOrCtrl';
case shortcutsKeys.Shift:
return 'Shift';
default:
return key;
}
})
.join('+');
globalShortcut.register(shortcutsKey, () => {
callback(eventName as EShortcutEvents);
});
}
});
});
Expand Down
20 changes: 20 additions & 0 deletions apps/desktop/src-electron/libs/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const configKeys = {
Theme: 'theme',
EncryptedData: 'EncryptedData',
Language: 'language',
DisableKeyboardShortcuts: 'disableKeyboardShortcuts',
};

export const getUpdateSettings = (): IUpdateSettings =>
Expand All @@ -40,6 +41,25 @@ export const setDevTools = (devTools: boolean) => {
store.set(configKeys.DevTools, devTools);
};

export const getDisableKeyboardShortcuts = () =>
store.get(configKeys.DisableKeyboardShortcuts, {
disableNumberShortcuts: false,
disableSearchAndAccountSelectorShortcuts: false,
}) as {
disableNumberShortcuts: boolean;
disableSearchAndAccountSelectorShortcuts: boolean;
};

export const setDisableKeyboardShortcuts = (config: {
disableNumberShortcuts: boolean;
disableSearchAndAccountSelectorShortcuts: boolean;
}) => {
store.set(configKeys.DisableKeyboardShortcuts, {
...getDisableKeyboardShortcuts(),
...config,
});
};

export const getTheme = () => store.get(configKeys.Theme, 'system') as string;

export const setTheme = (theme: string) => store.set(configKeys.Theme, theme);
Expand Down
9 changes: 9 additions & 0 deletions apps/desktop/src-electron/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ export type IDesktopAPI = {

// Updater
checkForUpdates: (isManual?: boolean) => void;
disableShortcuts: (params: {
disableNumberShortcuts?: boolean;
disableSearchAndAccountSelectorShortcuts?: boolean;
}) => void;
downloadUpdate: () => void;
verifyUpdate: (event: IVerifyUpdateParams) => void;
installUpdate: (event: IInstallUpdateParams) => void;
Expand Down Expand Up @@ -168,6 +172,7 @@ const validChannels = [
ipcMessageKeys.TOUCH_UPDATE_RES_SUCCESS,
ipcMessageKeys.TOUCH_UPDATE_PROGRESS,
ipcMessageKeys.SHOW_ABOUT_WINDOW,
ipcMessageKeys.APP_UPDATE_DISABLE_SHORTCUTS,
];

const getChannel = () => {
Expand Down Expand Up @@ -302,6 +307,10 @@ const desktopApi = Object.freeze({
// Updater
checkForUpdates: (isManual?: boolean) =>
ipcRenderer.send(ipcMessageKeys.UPDATE_CHECK, isManual),
disableShortcuts: (params: {
disableNumberShortcuts?: boolean;
disableSearchAndAccountSelectorShortcuts?: boolean;
}) => ipcRenderer.send(ipcMessageKeys.APP_UPDATE_DISABLE_SHORTCUTS, params),
downloadUpdate: () => ipcRenderer.send(ipcMessageKeys.UPDATE_DOWNLOAD),
verifyUpdate: (params: IVerifyUpdateParams) =>
ipcRenderer.send(ipcMessageKeys.UPDATE_VERIFY, params),
Expand Down
7 changes: 4 additions & 3 deletions packages/kit-bg/src/states/jotai/atoms/devSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export interface IDevSettings {
alwaysSignOnlySendTx?: boolean;
// show dev export private key
showDevExportPrivateKey?: boolean;
// show trading view
showTradingView?: boolean;
// disable Solana priority fee
disableSolanaPriorityFee?: boolean;
disableNumberShortcuts?: boolean;
disableSearchAndAccountSelectorShortcuts?: boolean;
}

export type IDevSettingsKeys = keyof IDevSettings;
Expand All @@ -40,8 +40,9 @@ export const {
settings: {
enableTestEndpoint: !!platformEnv.isDev || !!platformEnv.isE2E,
showDevOverlayWindow: platformEnv.isE2E ? true : undefined,
showTradingView: false,
disableSolanaPriorityFee: false,
disableNumberShortcuts: false,
disableSearchAndAccountSelectorShortcuts: false,
},
},
});
Expand Down
16 changes: 7 additions & 9 deletions packages/kit/src/components/TradingView/WebView.desktop.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from 'react';
import { useLayoutEffect, useRef } from 'react';

import { Stack } from '@onekeyhq/components';

Expand All @@ -22,28 +22,26 @@ export function WebView({
}: IWebViewProps): JSX.Element | null {
const ref = useRef<HTMLWebViewElement | null>(null);

useEffect(() => {
useLayoutEffect(() => {
const webview = ref.current;
if (webview) {
webview.addEventListener('dom-ready', () => {
webview.addEventListener('did-attach', () => {
(
webview as unknown as {
executeJavaScript: (code: string, userGesture: boolean) => void;
}
).executeJavaScript(injectedJavaScript, true);
});
webview.addEventListener('did-finish-load', () => {
setTimeout(() => {
onLoadEnd();
}, 200);
}, 100);
});
webview.addEventListener('will-navigate', (event) => {
event.preventDefault();
});
}

setTimeout(() => {
onLoadEnd();
}, 5500);
}, [injectedJavaScript, onLoadEnd]);
}, [injectedJavaScript, onLoadEnd, uri]);

return uri ? (
<Stack style={style}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function MarketMdColumn({
items: [
{
icon: 'SwitchHorOutline' as const,
label: intl.formatMessage({ id: ETranslations.global_swap }),
label: intl.formatMessage({ id: ETranslations.global_trade }),
onPress: tradeActions.onSwapLazyModal,
},
canStaking && {
Expand Down
15 changes: 2 additions & 13 deletions packages/kit/src/views/Market/components/MarketTradeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function MarketTradeButton({
const network = useMarketTradeNetwork(token);
const networkId = useMarketTradeNetworkId(network, token.symbol);
const [disabled, setDisabled] = useState({
trade: true,
buy: true,
sell: true,
});
Expand Down Expand Up @@ -60,11 +59,7 @@ export function MarketTradeButton({
}) || {};
const contractAddress = isNative ? '' : network?.contract_address || '';

const [swapResult, buyResult, sellResult] = await Promise.all([
backgroundApiProxy.serviceSwap.checkSupportSwap({
networkId,
contractAddress: isNative ? realContractAddress : contractAddress,
}),
const [buyResult, sellResult] = await Promise.all([
backgroundApiProxy.serviceFiatCrypto.isTokenSupported({
networkId,
tokenAddress: contractAddress,
Expand All @@ -77,7 +72,6 @@ export function MarketTradeButton({
}),
]);
setDisabled({
trade: !swapResult.isSupportCrossChain && !swapResult.isSupportSwap,
buy: !buyResult,
sell: !sellResult,
});
Expand All @@ -95,12 +89,7 @@ export function MarketTradeButton({
return (
<XStack $gtMd={{ mt: '$6' }} ai="center" gap="$4">
<XStack gap="$2.5" flex={1}>
<Button
flex={1}
variant="primary"
onPress={handleSwap}
disabled={disabled.trade}
>
<Button flex={1} variant="primary" onPress={handleSwap}>
{intl.formatMessage({ id: ETranslations.global_trade })}
</Button>
{canStaking ? (
Expand Down
Loading

0 comments on commit 41fba93

Please sign in to comment.