feat: electron about redirect to app setting about, and add some other action

Signed-off-by: Innei <i@innei.in>
This commit is contained in:
Innei
2024-08-02 15:50:30 +08:00
parent e115b6de66
commit 159a11da67
7 changed files with 131 additions and 38 deletions
+41 -3
View File
@@ -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<MenuItemConstructorOptions | MenuItem> = [
{
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" },
+10 -19
View File
@@ -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,
+1 -1
View File
@@ -67,7 +67,7 @@ export const initializeApp = async () => {
subscribeNetworkStatus()
registerGlobalContext({
showSetting: () => window.router.showSettings(),
showSetting: (path) => window.router.showSettings(path),
getGeneralSettings,
getUISettings,
/**
@@ -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 (
<Vibrancy
className="relative flex h-full flex-col space-y-3 rounded-l-[12px] pt-2.5"
+21 -14
View File
@@ -13,6 +13,7 @@ import { HotKeyScopeMap } from "@renderer/constants"
import { tipcClient } from "@renderer/lib/client"
import type { FeedViewType } from "@renderer/lib/enum"
import { cn } from "@renderer/lib/utils"
import { useSubscribeElectronEvent } from "@shared/event"
import { useLayoutEffect } from "react"
import { useForm } from "react-hook-form"
import { useHotkeys } from "react-hotkeys-hook"
@@ -46,6 +47,9 @@ const CmdNPanel = () => {
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
}
+1 -1
View File
@@ -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
+52
View File
@@ -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])
}