@@ -13,13 +13,37 @@ const { exec } = require("child_process");
1313const os = require ( "os" ) ;
1414
1515let mainWindow = null ;
16+ let settingsWindow = null ;
1617
1718// --- App Setup: Create Thumbnail Cache Directory ---
1819const cacheDir = path . join ( app . getPath ( "userData" ) , "thumbnails" ) ;
1920if ( ! fsSync . existsSync ( cacheDir ) ) {
2021 fsSync . mkdirSync ( cacheDir , { recursive : true } ) ;
2122}
2223
24+ // --- Settings File Setup ---
25+ const settingsPath = path . join ( app . getPath ( "userData" ) , "settings.json" ) ;
26+
27+ async function getSettings ( ) {
28+ try {
29+ await fs . access ( settingsPath ) ;
30+ const settingsFile = await fs . readFile ( settingsPath , "utf-8" ) ;
31+ return JSON . parse ( settingsFile ) ;
32+ } catch ( error ) {
33+ // If the file doesn't exist or there's an error, return default settings
34+ const defaultSettings = {
35+ theme : "tokyo-night-blue" ,
36+ openCommand : "CommandOrControl+Shift+P" ,
37+ } ;
38+ await saveSettings ( defaultSettings ) ;
39+ return defaultSettings ;
40+ }
41+ }
42+
43+ async function saveSettings ( settings ) {
44+ await fs . writeFile ( settingsPath , JSON . stringify ( settings , null , 2 ) ) ;
45+ }
46+
2347const run = ( cmd ) =>
2448 new Promise ( ( resolve , reject ) => {
2549 exec ( cmd , { maxBuffer : 1024 * 1024 * 10 } , ( error , stdout , stderr ) => {
@@ -64,6 +88,8 @@ function createWindow() {
6488 focusable : true ,
6589 } ) ;
6690
91+ console . log ( "Main window created." ) ;
92+
6793 if ( process . platform === "darwin" ) {
6894 app . dock . hide ( ) ;
6995 }
@@ -79,8 +105,18 @@ function createWindow() {
79105 } ) ;
80106}
81107
82- app . whenReady ( ) . then ( ( ) => {
83- globalShortcut . register ( "CommandOrControl+Shift+P" , ( ) => {
108+ ipcMain . on ( "toggle-settings" , ( ) => {
109+ if ( mainWindow ) {
110+ mainWindow . webContents . send ( "toggle-settings" ) ;
111+ }
112+ } ) ;
113+
114+ app . whenReady ( ) . then ( async ( ) => {
115+ console . log ( "App is ready." ) ;
116+ const settings = await getSettings ( ) ;
117+ console . log ( "Settings loaded:" , settings ) ;
118+ globalShortcut . register ( settings . openCommand , ( ) => {
119+ console . log ( "Global shortcut triggered." ) ;
84120 if ( mainWindow ) {
85121 mainWindow . close ( ) ;
86122 } else {
@@ -101,6 +137,50 @@ app.on("will-quit", () => {
101137
102138// --- IPC Handlers ---
103139
140+
141+
142+ ipcMain . handle ( "get-settings" , async ( ) => {
143+ return await getSettings ( ) ;
144+ } ) ;
145+
146+ ipcMain . handle ( "save-settings" , async ( event , settings ) => {
147+ await saveSettings ( settings ) ;
148+ // You might want to re-register the shortcut if it changed
149+ globalShortcut . unregisterAll ( ) ;
150+ const newSettings = await getSettings ( ) ;
151+ globalShortcut . register ( newSettings . openCommand , ( ) => {
152+ if ( mainWindow ) {
153+ mainWindow . close ( ) ;
154+ } else {
155+ createWindow ( ) ;
156+ }
157+ } ) ;
158+ if ( mainWindow ) {
159+ mainWindow . webContents . send ( "theme-updated" , newSettings . theme ) ;
160+ }
161+ } ) ;
162+
163+ ipcMain . handle ( "get-themes" , async ( ) => {
164+ const themeDir = path . join ( __dirname , "src/themes" ) ;
165+ try {
166+ const files = await fs . readdir ( themeDir ) ;
167+ const themes = await Promise . all (
168+ files . map ( async ( file ) => {
169+ if ( file . endsWith ( ".json" ) ) {
170+ const filePath = path . join ( themeDir , file ) ;
171+ const content = await fs . readFile ( filePath , "utf-8" ) ;
172+ return JSON . parse ( content ) ;
173+ }
174+ return null ;
175+ } )
176+ ) ;
177+ return themes . filter ( ( theme ) => theme !== null ) ;
178+ } catch ( error ) {
179+ console . error ( `Error reading theme directory: ${ themeDir } ` , error ) ;
180+ return [ ] ;
181+ }
182+ } ) ;
183+
104184// **FIXED**: Handler to open external links securely in the user's default browser.
105185ipcMain . handle ( "open-external-link" , async ( event , url ) => {
106186 await shell . openExternal ( url ) ;
0 commit comments