refactor: remove minapp.html
This commit is contained in:
+7
-1
@@ -50,7 +50,13 @@ app.whenReady().then(() => {
|
||||
ipcMain.handle('save-file', saveFile)
|
||||
|
||||
ipcMain.handle('minapp', (_, args) => {
|
||||
createMinappWindow(args)
|
||||
createMinappWindow({
|
||||
url: args.url,
|
||||
windowOptions: {
|
||||
...mainWindow.getBounds(),
|
||||
...args.windowOptions
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
ipcMain.handle('set-theme', (_, theme: 'light' | 'dark') => {
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/**
|
||||
* 将 JavaScript 对象转换为 URL 查询参数字符串
|
||||
* @param obj - 要转换的对象
|
||||
* @param options - 配置选项
|
||||
* @returns 转换后的查询参数字符串
|
||||
*/
|
||||
export function objectToQueryParams(
|
||||
obj: Record<string, string | number | boolean | null | undefined | object>,
|
||||
options: {
|
||||
skipNull?: boolean
|
||||
skipUndefined?: boolean
|
||||
} = {}
|
||||
): string {
|
||||
const { skipNull = false, skipUndefined = false } = options
|
||||
|
||||
const params = new URLSearchParams()
|
||||
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
if (skipNull && value === null) continue
|
||||
if (skipUndefined && value === undefined) continue
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach((item) => params.append(key, String(item)))
|
||||
} else if (typeof value === 'object' && value !== null) {
|
||||
params.append(key, JSON.stringify(value))
|
||||
} else if (value !== undefined && value !== null) {
|
||||
params.append(key, String(value))
|
||||
}
|
||||
}
|
||||
|
||||
return params.toString()
|
||||
}
|
||||
+8
-38
@@ -1,11 +1,10 @@
|
||||
import { is } from '@electron-toolkit/utils'
|
||||
import { app, BrowserView, BrowserWindow, Menu, MenuItem, shell } from 'electron'
|
||||
import { BrowserWindow, Menu, MenuItem, shell } from 'electron'
|
||||
import windowStateKeeper from 'electron-window-state'
|
||||
import { join } from 'path'
|
||||
|
||||
import icon from '../../build/icon.png?asset'
|
||||
import { appConfig, titleBarOverlayDark, titleBarOverlayLight } from './config'
|
||||
import { objectToQueryParams } from './utils'
|
||||
|
||||
export function createMainWindow() {
|
||||
// Load the previous state with fallback to defaults
|
||||
@@ -62,14 +61,7 @@ export function createMainWindow() {
|
||||
})
|
||||
|
||||
mainWindow.webContents.setWindowOpenHandler((details) => {
|
||||
const websiteReg = /accounts.google.com/i
|
||||
|
||||
if (websiteReg.test(details.url)) {
|
||||
createMinappWindow({ url: details.url, windowOptions: { width: 1000, height: 680 } })
|
||||
} else {
|
||||
shell.openExternal(details.url)
|
||||
}
|
||||
|
||||
shell.openExternal(details.url)
|
||||
return { action: 'deny' }
|
||||
})
|
||||
|
||||
@@ -107,45 +99,23 @@ export function createMinappWindow({
|
||||
url: string
|
||||
windowOptions?: Electron.BrowserWindowConstructorOptions
|
||||
}) {
|
||||
const width = 1000
|
||||
const height = 680
|
||||
const headerHeight = 40
|
||||
const width = windowOptions?.width || 1000
|
||||
const height = windowOptions?.height || 680
|
||||
|
||||
const minappWindow = new BrowserWindow({
|
||||
width,
|
||||
height,
|
||||
autoHideMenuBar: true,
|
||||
alwaysOnTop: true,
|
||||
titleBarOverlay: titleBarOverlayDark,
|
||||
titleBarStyle: 'hidden',
|
||||
title: 'Cherry Studio',
|
||||
...windowOptions,
|
||||
webPreferences: {
|
||||
preload: join(__dirname, '../preload/minapp.js'),
|
||||
sandbox: false
|
||||
sandbox: false,
|
||||
contextIsolation: false
|
||||
}
|
||||
})
|
||||
|
||||
const view = new BrowserView()
|
||||
view.setBounds({ x: 0, y: headerHeight, width, height: height - headerHeight })
|
||||
view.webContents.loadURL(url)
|
||||
|
||||
const minappWindowParams = {
|
||||
title: windowOptions?.title || 'CherryStudio'
|
||||
}
|
||||
|
||||
const appPath = app.getAppPath()
|
||||
const minappHtmlPath = appPath + '/resources/minapp.html'
|
||||
|
||||
minappWindow.loadURL('file://' + minappHtmlPath + '?' + objectToQueryParams(minappWindowParams))
|
||||
minappWindow.setBrowserView(view)
|
||||
minappWindow.on('resize', () => {
|
||||
view.setBounds({
|
||||
x: 0,
|
||||
y: headerHeight,
|
||||
width: minappWindow.getBounds().width,
|
||||
height: minappWindow.getBounds().height - headerHeight
|
||||
})
|
||||
})
|
||||
minappWindow.loadURL(url)
|
||||
|
||||
return minappWindow
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -14,7 +14,7 @@ declare global {
|
||||
setProxy: (proxy: string | undefined) => void
|
||||
saveFile: (path: string, content: string) => void
|
||||
setTheme: (theme: 'light' | 'dark') => void
|
||||
minApp: (url: string) => void
|
||||
minApp: (options: { url: string; windowOptions?: Electron.BrowserWindowConstructorOptions }) => void
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { contextBridge } from 'electron'
|
||||
|
||||
const api = {}
|
||||
|
||||
if (process.contextIsolated) {
|
||||
try {
|
||||
contextBridge.exposeInMainWorld('api', api)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore (define in dts)
|
||||
window.api = api
|
||||
}
|
||||
@@ -354,7 +354,7 @@ export const PROVIDER_CONFIG = {
|
||||
},
|
||||
app: {
|
||||
name: 'Groq',
|
||||
url: 'https://groq.com/',
|
||||
url: 'https://chat.groq.com/',
|
||||
logo: GroqProviderLogo
|
||||
}
|
||||
},
|
||||
|
||||
@@ -12,6 +12,13 @@ const App: FC<Props> = ({ app }) => {
|
||||
const { theme } = useTheme()
|
||||
|
||||
const onClick = () => {
|
||||
const websiteReg = /claude|chatgpt|groq/i
|
||||
|
||||
if (websiteReg.test(app.url)) {
|
||||
window.api.minApp({ url: app.url, windowOptions: { title: app.name } })
|
||||
return
|
||||
}
|
||||
|
||||
MinApp.start(app)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user