From 935439c86dc9f4d74480bbbbc0e8a5fcd3ef6901 Mon Sep 17 00:00:00 2001 From: Red-Asuka Date: Thu, 5 Dec 2024 17:36:24 +0800 Subject: [PATCH] feat(desktop): add width and height settings to database schema and adjust window dimensions --- apps/desktop/src/database/schemas/settings.ts | 2 ++ apps/desktop/src/main/index.ts | 29 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/apps/desktop/src/database/schemas/settings.ts b/apps/desktop/src/database/schemas/settings.ts index 33fc90cfd..16049b90c 100644 --- a/apps/desktop/src/database/schemas/settings.ts +++ b/apps/desktop/src/database/schemas/settings.ts @@ -3,6 +3,8 @@ import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core' export const settings = sqliteTable('settings', { id: integer().primaryKey({ autoIncrement: true }), + width: integer({ mode: 'number' }).notNull().default(1024), + height: integer({ mode: 'number' }).notNull().default(749), currentLang: text().$type().notNull().default('en'), autoCheck: integer({ mode: 'boolean' }).notNull().default(true), autoResub: integer({ mode: 'boolean' }).notNull().default(true), diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts index db0741649..a07ab2db7 100644 --- a/apps/desktop/src/main/index.ts +++ b/apps/desktop/src/main/index.ts @@ -1,5 +1,6 @@ import { join } from 'node:path' import { electronApp, is, optimizer } from '@electron-toolkit/utils' +import { eq } from 'drizzle-orm' import { app, BrowserWindow, ipcMain, shell } from 'electron' import icon from '../../resources/icon.png?asset' import { db, execute, runMigrate } from '../database/db.main' @@ -13,13 +14,27 @@ async function createWindow() { if (!data) { await db.insert(settings).values({}) } - data = await db.query.settings.findFirst() + data = await db.query.settings.findFirst() as SelectSettings + + const width = data.width || 1024 + const height = data.height || 749 + const currentTheme = data.currentTheme || 'light' + const bgColor = { + dark: '#232323', + night: '#212328', + light: '#ffffff', + } + const backgroundColor = bgColor[currentTheme] + // Create the browser window. const mainWindow = new BrowserWindow({ - width: 1024, - height: 749, + width, + height, + minHeight: 650, + minWidth: 997, show: false, autoHideMenuBar: true, + backgroundColor, ...(process.platform === 'linux' ? { icon } : {}), webPreferences: { preload: join(__dirname, '../preload/index.mjs'), @@ -36,6 +51,14 @@ async function createWindow() { } }) + mainWindow.on('resize', async () => { + const { width, height } = mainWindow.getBounds() + const data = await db.query.settings.findFirst() + if (data) { + await db.update(settings).set({ width, height }).where(eq(settings.id, data.id)) + } + }) + mainWindow.webContents.setWindowOpenHandler((details) => { shell.openExternal(details.url) return { action: 'deny' }