Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Store window size in app metadata table

Hakan Shehu7c0b0e093d50
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 3aaade84..e9bd869e 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -3,6 +3,8 @@ import started from 'electron-squirrel-startup';
import { app, BrowserWindow, ipcMain, protocol } from 'electron';
import path from 'path';
+import { metadataService } from '@/main/services/metadata-service';
+import { WindowSize } from '@/shared/types/metadata';
import { createDebugger } from '@/main/debugger';
import { bootstrapper } from '@/main/bootstrapper';
import { assetService } from '@/main/services/asset-service';
@@ -27,18 +29,54 @@ if (started) {
app.quit();
}
-const createWindow = () => {
- bootstrapper.init();
+const createWindow = async () => {
+ await bootstrapper.init();
// Create the browser window.
+ let windowSize = await metadataService.get<WindowSize>('window_size');
const mainWindow = new BrowserWindow({
- fullscreen: true,
+ width: windowSize?.width ?? 1200,
+ height: windowSize?.height ?? 800,
+ fullscreen: windowSize?.fullscreen ?? false,
+ fullscreenable: true,
+ minWidth: 800,
+ minHeight: 600,
icon: getAppIconPath(),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});
+ mainWindow.on('resized', () => {
+ windowSize = {
+ width: mainWindow.getBounds().width,
+ height: mainWindow.getBounds().height,
+ fullscreen: false,
+ };
+
+ metadataService.set('window_size', windowSize);
+ });
+
+ mainWindow.on('enter-full-screen', () => {
+ windowSize = {
+ width: windowSize?.width ?? mainWindow.getBounds().width,
+ height: windowSize?.height ?? mainWindow.getBounds().height,
+ fullscreen: true,
+ };
+
+ metadataService.set('window_size', windowSize);
+ });
+
+ mainWindow.on('leave-full-screen', () => {
+ windowSize = {
+ width: windowSize?.width ?? mainWindow.getBounds().width,
+ height: windowSize?.height ?? mainWindow.getBounds().height,
+ fullscreen: false,
+ };
+
+ metadataService.set('window_size', windowSize);
+ });
+
// and load the index.html of the app.
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
diff --git a/apps/desktop/src/main/bootstrapper.ts b/apps/desktop/src/main/bootstrapper.ts
index 3f2cbe0f..3a3fd5c9 100644
--- a/apps/desktop/src/main/bootstrapper.ts
+++ b/apps/desktop/src/main/bootstrapper.ts
@@ -35,7 +35,7 @@ class Bootstrapper {
radarService.init();
if (!this.eventLoop) {
- this.eventLoop = setTimeout(this.executeEventLoop, 20);
+ this.eventLoop = setTimeout(this.executeEventLoop, 50);
}
}
diff --git a/apps/desktop/src/main/data/app/migrations.ts b/apps/desktop/src/main/data/app/migrations.ts
index de9c1749..fa94f418 100644
--- a/apps/desktop/src/main/data/app/migrations.ts
+++ b/apps/desktop/src/main/data/app/migrations.ts
@@ -95,9 +95,25 @@ const createDeletedTokensTable: Migration = {
},
};
+const createMetadataTable: Migration = {
+ up: async (db) => {
+ await db.schema
+ .createTable('metadata')
+ .addColumn('key', 'text', (col) => col.notNull().primaryKey())
+ .addColumn('value', 'text', (col) => col.notNull())
+ .addColumn('created_at', 'text', (col) => col.notNull())
+ .addColumn('updated_at', 'text')
+ .execute();
+ },
+ down: async (db) => {
+ await db.schema.dropTable('metadata').execute();
+ },
+};
+
export const appDatabaseMigrations: Record<string, Migration> = {
'00001_create_servers_table': createServersTable,
'00002_create_accounts_table': createAccountsTable,
'00003_create_workspaces_table': createWorkspacesTable,
- '00005_create_deleted_tokens_table': createDeletedTokensTable,
+ '00004_create_deleted_tokens_table': createDeletedTokensTable,
+ '00005_create_metadata_table': createMetadataTable,
};
diff --git a/apps/desktop/src/main/data/app/schema.ts b/apps/desktop/src/main/data/app/schema.ts
index 0ceafd3e..1113b513 100644
--- a/apps/desktop/src/main/data/app/schema.ts
+++ b/apps/desktop/src/main/data/app/schema.ts
@@ -52,9 +52,21 @@ interface DeletedTokenTable {
created_at: ColumnType<string, string, string>;
}
+interface MetadataTable {
+ key: ColumnType<string, string, never>;
+ value: ColumnType<string, string, string>;
+ created_at: ColumnType<string, string, never>;
+ updated_at: ColumnType<string | null, string | null, string | null>;
+}
+
+export type SelectMetadata = Selectable<MetadataTable>;
+export type CreateMetadata = Insertable<MetadataTable>;
+export type UpdateMetadata = Updateable<MetadataTable>;
+
export interface AppDatabaseSchema {
servers: ServerTable;
accounts: AccountTable;
workspaces: WorkspaceTable;
deleted_tokens: DeletedTokenTable;
+ metadata: MetadataTable;
}
diff --git a/apps/desktop/src/main/services/metadata-service.ts b/apps/desktop/src/main/services/metadata-service.ts
new file mode 100644
index 00000000..a52f9d1b
--- /dev/null
+++ b/apps/desktop/src/main/services/metadata-service.ts
@@ -0,0 +1,43 @@
+import { databaseService } from '../data/database-service';
+
+class MetadataService {
+ public async get<T>(key: string): Promise<T | null> {
+ const metadata = await databaseService.appDatabase
+ .selectFrom('metadata')
+ .selectAll()
+ .where('key', '=', key)
+ .executeTakeFirst();
+
+ if (!metadata) {
+ return null;
+ }
+
+ return JSON.parse(metadata.value) as T;
+ }
+
+ public async set<T>(key: string, value: T) {
+ await databaseService.appDatabase
+ .insertInto('metadata')
+ .values({
+ key,
+ value: JSON.stringify(value),
+ created_at: new Date().toISOString(),
+ })
+ .onConflict((b) =>
+ b.column('key').doUpdateSet({
+ value: JSON.stringify(value),
+ updated_at: new Date().toISOString(),
+ })
+ )
+ .execute();
+ }
+
+ public async delete(key: string) {
+ await databaseService.appDatabase
+ .deleteFrom('metadata')
+ .where('key', '=', key)
+ .execute();
+ }
+}
+
+export const metadataService = new MetadataService();
diff --git a/apps/desktop/src/shared/types/metadata.ts b/apps/desktop/src/shared/types/metadata.ts
new file mode 100644
index 00000000..e9bd2dca
--- /dev/null
+++ b/apps/desktop/src/shared/types/metadata.ts
@@ -0,0 +1,5 @@
+export type WindowSize = {
+ width: number;
+ height: number;
+ fullscreen: boolean;
+};