Skip to content

Commit

Permalink
feat(desktop): add width and height settings to database schema and a…
Browse files Browse the repository at this point in the history
…djust window dimensions
  • Loading branch information
Red-Asuka committed Dec 5, 2024
1 parent d9b275b commit 935439c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apps/desktop/src/database/schemas/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Settings['currentLang']>().notNull().default('en'),
autoCheck: integer({ mode: 'boolean' }).notNull().default(true),
autoResub: integer({ mode: 'boolean' }).notNull().default(true),
Expand Down
29 changes: 26 additions & 3 deletions apps/desktop/src/main/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'),
Expand All @@ -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' }
Expand Down

0 comments on commit 935439c

Please sign in to comment.