BACKEND DONE FOR ALL APPS

This commit is contained in:
andrei-mihnea-cerbu
2024-10-21 18:34:45 +03:00
parent 4157ca9e7d
commit ab1eaec413
349 changed files with 17199 additions and 14015 deletions
+92
View File
@@ -0,0 +1,92 @@
import { BrowserWindow, dialog, shell } from 'electron';
import fs from 'fs';
import path from 'path';
export class WindowManager {
private readonly mainWindow: BrowserWindow;
private readonly pathToPagesDir: string;
constructor(mainWindow: BrowserWindow, pathToPagesDir: string) {
this.pathToPagesDir = pathToPagesDir;
this.mainWindow = mainWindow;
}
// Show an alert dialog
async showAlert(message: string): Promise<void> {
if (this.mainWindow) {
await dialog.showMessageBox(this.mainWindow, {
type: 'info',
title: 'Alert',
message: message,
buttons: ['OK'],
});
} else {
console.error('Main window is not available.');
}
}
// Change the content of the current window to load a new HTML file
async changeContent(destination: string): Promise<void> {
if (this.mainWindow) {
try {
const destinationPath = path.join(this.pathToPagesDir, `${destination}.html`);
console.log(`Navigating to: ${destinationPath}`);
// Load the destination HTML file into the main window
await this.mainWindow.loadFile(destinationPath);
console.log(`Navigated to ${destination}`);
} catch (error) {
console.error('Error changing content:', error);
throw error; // Pass the error back to the render process
}
} else {
console.error('Main window is not available.');
}
}
// New method to select a directory
async selectDirectory(): Promise<string | undefined> {
const result = await dialog.showOpenDialog(this.mainWindow, {
properties: ['openDirectory'], // Only allow selecting directories
});
// If the user cancels, result.filePaths will be an empty array
if (result.filePaths && result.filePaths.length > 0) {
return result.filePaths[0]; // Return the selected directory path
} else {
console.log('No directory selected.');
return undefined; // Return undefined if no directory was selected
}
}
async showFileInExplorer(filePath: string): Promise<void> {
if (filePath && fs.existsSync(filePath)) {
try {
// Use Electron's shell module to show the file in the explorer
shell.showItemInFolder(filePath);
console.log(`Opened file explorer for: ${filePath}`);
} catch (error: any) {
console.error(`Error showing file in explorer: ${error.message}`);
}
} else {
console.error('File path is undefined or does not exist.');
}
}
// New method to open the file explorer and choose a file
async selectFile(): Promise<string | undefined> {
const result = await dialog.showOpenDialog(this.mainWindow, {
properties: ['openFile'], // Allow selecting a file
filters: [
{ name: 'All Files', extensions: ['*'] } // Optionally filter for specific file types
]
});
if (result.filePaths && result.filePaths.length > 0) {
return result.filePaths[0]; // Return the selected file path
} else {
console.log('No file selected.');
return undefined; // Return undefined if no file was selected
}
}
}