diff --git a/src/main/menu.ts b/src/main/menu.ts index 6585c5aa9..de42b904d 100644 --- a/src/main/menu.ts +++ b/src/main/menu.ts @@ -1,15 +1,23 @@ +import { name } from "@pkg" +import { dispatchEventOnWindow } from "@shared/event" import type { MenuItem, MenuItemConstructorOptions } from "electron" import { Menu } from "electron" import { revealLogFile } from "./logger" -import { createSettingWindow, createWindow } from "./window" +import { createSettingWindow, createWindow, getMainWindow } from "./window" export const registerAppMenu = () => { const menus: Array = [ { role: "appMenu", submenu: [ - { role: "about" }, + { + type: "normal", + label: `About ${name}`, + click: () => { + createSettingWindow("about") + }, + }, { type: "separator" }, { label: "Settings...", @@ -25,7 +33,37 @@ export const registerAppMenu = () => { { role: "quit" }, ], }, - { role: "fileMenu" }, + { + role: "fileMenu", + submenu: [ + { + type: "normal", + label: "Quick Add", + accelerator: "CmdOrCtrl+N", + click: () => { + const mainWindow = getMainWindow() + if (!mainWindow) return + mainWindow.show() + dispatchEventOnWindow(mainWindow, "QuickAdd") + }, + }, + + { + type: "normal", + label: "Discover", + accelerator: "CmdOrCtrl+T", + click: () => { + const mainWindow = getMainWindow() + if (!mainWindow) return + mainWindow.show() + dispatchEventOnWindow(mainWindow, "Discover") + }, + }, + + { type: "separator" }, + { role: "close" }, + ], + }, { role: "editMenu" }, { role: "viewMenu" }, { role: "windowMenu" }, diff --git a/src/main/window.ts b/src/main/window.ts index a464bfb71..422d4cb4c 100644 --- a/src/main/window.ts +++ b/src/main/window.ts @@ -76,22 +76,13 @@ export function createWindow( process.env["ELECTRON_RENDERER_URL"] + (options?.extraPath || ""), ) } else { - const openPath = path.resolve( - __dirname, - "../renderer/index.html", - ) - window.loadFile( - openPath, - { - hash: options?.extraPath, - }, - ) - logger.log( - openPath, - { - hash: options?.extraPath, - }, - ) + const openPath = path.resolve(__dirname, "../renderer/index.html") + window.loadFile(openPath, { + hash: options?.extraPath, + }) + logger.log(openPath, { + hash: options?.extraPath, + }) } const refererMatchs = [ @@ -229,12 +220,12 @@ export const createMainWindow = () => { return window } -export const createSettingWindow = () => { +export const createSettingWindow = (path?: string) => { // We need to open the setting modal in the main window when the main window exists, // if we open a new window then the state between the two windows will be out of sync. if (windows.mainWindow && windows.mainWindow.isVisible()) { windows.mainWindow.show() - callGlobalContextMethod(windows.mainWindow, "showSetting") + callGlobalContextMethod(windows.mainWindow, "showSetting", [path]) return } if (windows.settingWindow) { @@ -242,7 +233,7 @@ export const createSettingWindow = () => { return } const window = createWindow({ - extraPath: "#settings", + extraPath: `#settings/${path || ""}`, width: 700, height: 600, resizable: false, diff --git a/src/renderer/src/initialize/index.ts b/src/renderer/src/initialize/index.ts index ef2cb6366..34d39b8de 100644 --- a/src/renderer/src/initialize/index.ts +++ b/src/renderer/src/initialize/index.ts @@ -67,7 +67,7 @@ export const initializeApp = async () => { subscribeNetworkStatus() registerGlobalContext({ - showSetting: () => window.router.showSettings(), + showSetting: (path) => window.router.showSettings(path), getGeneralSettings, getUISettings, /** diff --git a/src/renderer/src/modules/feed-column/index.tsx b/src/renderer/src/modules/feed-column/index.tsx index 19166b804..fb4b744d5 100644 --- a/src/renderer/src/modules/feed-column/index.tsx +++ b/src/renderer/src/modules/feed-column/index.tsx @@ -19,6 +19,7 @@ import { clamp, cn } from "@renderer/lib/utils" import { Queries } from "@renderer/queries" import { useSubscriptionStore } from "@renderer/store/subscription" import { useFeedUnreadStore } from "@renderer/store/unread" +import { useSubscribeElectronEvent } from "@shared/event" import { useWheel } from "@use-gesture/react" import type { MotionValue } from "framer-motion" import { m, useSpring } from "framer-motion" @@ -171,6 +172,10 @@ export function FeedColumn({ children }: PropsWithChildren) { const showSidebarUnreadCount = useUISettingKey("sidebarShowUnreadCount") + useSubscribeElectronEvent("Discover", () => { + window.router.navigate(Routes.Discover) + }) + return ( { const { url } = form.getValues() const defaultView = getSidebarActiveView() as FeedViewType + + window.posthog?.capture("quick_add_feed", { url, defaultView }) + present({ title: "Add Feed", content: () => ( @@ -102,20 +106,23 @@ const CmdNPanel = () => { export const CmdNTrigger = () => { const { present } = useModalStack() - useHotkeys( - "meta+n,ctrl+n", - (e) => { - e.preventDefault() - present({ - title: "Quick Follow", - content: CmdNPanel, - CustomModalComponent: NoopChildren, - overlay: false, - clickOutsideToDismiss: true, - }) - }, - { scopes: HotKeyScopeMap.Home }, - ) + const handler = () => { + present({ + title: "Quick Follow", + content: CmdNPanel, + CustomModalComponent: NoopChildren, + overlay: false, + id: "quick-add", + clickOutsideToDismiss: true, + }) + } + + useSubscribeElectronEvent("QuickAdd", handler) + + useHotkeys("meta+n,ctrl+n", handler, { + scopes: HotKeyScopeMap.Home, + preventDefault: true, + }) return null } diff --git a/src/shared/src/bridge.ts b/src/shared/src/bridge.ts index 1df119eed..409666d2c 100644 --- a/src/shared/src/bridge.ts +++ b/src/shared/src/bridge.ts @@ -5,7 +5,7 @@ import type { GeneralSettings, UISettings } from "./interface/settings" const PREFIX = "__follow" interface RenderGlobalContext { - showSetting: () => void + showSetting: (path?: string) => void getGeneralSettings: () => GeneralSettings getUISettings: () => UISettings diff --git a/src/shared/src/event.ts b/src/shared/src/event.ts new file mode 100644 index 000000000..52da23e21 --- /dev/null +++ b/src/shared/src/event.ts @@ -0,0 +1,52 @@ +import type { BrowserWindow } from "electron" +import { useEffect, useRef } from "react" + +export const EventsMap = { + QuickAdd: "quick-add", + Discover: "discover", +} + +export const dispatchEventOnWindow = ( + window: BrowserWindow, + event: keyof typeof EventsMap, + ...args: any[] +) => { + window.webContents.executeJavaScript( + iife(` + ${function Call(event: string, ...args: any[]) { + globalThis.window.dispatchEvent(new CustomEvent(event, { detail: args })) + }} + Call('${EventsMap[event]}', ${args + .map((arg) => JSON.stringify(arg)) + .join(",")}); + `), + ) +} + +const iife = (code: string) => `!(() => {${code}})()` +const subscribeEvent = ( + event: keyof typeof EventsMap, + callback: (args: any) => void, +) => { + const handler = (e) => { + callback(e.detail) + } + window.addEventListener(EventsMap[event], handler) + + return () => { + window.removeEventListener(EventsMap[event], handler) + } +} + +export const useSubscribeElectronEvent = ( + event: keyof typeof EventsMap, + callback: (args: any) => void, +) => { + const eventCallbackRef = useRef(callback) + eventCallbackRef.current = callback + + useEffect(() => { + const unsubscribe = subscribeEvent(event, eventCallbackRef.current) + return unsubscribe + }, [event]) +}