151 lines
4.8 KiB
TypeScript
151 lines
4.8 KiB
TypeScript
import { app, BrowserWindow, dialog, shell } from 'electron'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
export class WindowManager {
|
|
private readonly mainWindow: BrowserWindow
|
|
private readonly pathToPagesDir: string
|
|
private announcementWindow: BrowserWindow | null = null
|
|
|
|
constructor(mainWindow: BrowserWindow, pathToPagesDir: string) {
|
|
this.pathToPagesDir = pathToPagesDir
|
|
this.mainWindow = mainWindow
|
|
this.log('WindowManager initialized.')
|
|
}
|
|
|
|
// Logging helper function
|
|
private log(message: string, level: 'log' | 'error' = 'log'): void {
|
|
const prefix = '[WindowManager]'
|
|
if (level === 'error') {
|
|
console.error(`${prefix} ${message}`)
|
|
} else {
|
|
console.log(`${prefix} ${message}`)
|
|
}
|
|
}
|
|
|
|
// 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'],
|
|
})
|
|
this.log(`Alert displayed with message: "${message}"`)
|
|
} else {
|
|
this.log('Main window is not available.', 'error')
|
|
}
|
|
}
|
|
|
|
// 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`)
|
|
this.log(`Navigating to: ${destinationPath}`)
|
|
|
|
// Load the destination HTML file into the main window
|
|
await this.mainWindow.loadFile(destinationPath)
|
|
this.log(`Navigated to ${destination}`)
|
|
} catch (error) {
|
|
this.log(`Error changing content: ${error}`, 'error')
|
|
throw error // Pass the error back to the render process
|
|
}
|
|
} else {
|
|
this.log('Main window is not available.', 'error')
|
|
}
|
|
}
|
|
|
|
// New method to select a directory
|
|
async selectDirectory(): Promise<string | undefined> {
|
|
const result = await dialog.showOpenDialog(this.mainWindow, {
|
|
properties: ['openDirectory'], // Only allow selecting directories
|
|
defaultPath: app.getPath('home'),
|
|
})
|
|
|
|
if (result.filePaths && result.filePaths.length > 0) {
|
|
this.log(`Directory selected: ${result.filePaths[0]}`)
|
|
return result.filePaths[0] // Return the selected directory path
|
|
} else {
|
|
this.log('No directory selected.')
|
|
return undefined // Return undefined if no directory was selected
|
|
}
|
|
}
|
|
|
|
// Show a file in the explorer
|
|
async showFileInExplorer(filePath: string): Promise<void> {
|
|
if (filePath && fs.existsSync(filePath)) {
|
|
try {
|
|
shell.showItemInFolder(filePath)
|
|
this.log(`Opened file explorer for: ${filePath}`)
|
|
} catch (error: any) {
|
|
this.log(`Error showing file in explorer: ${error.message}`, 'error')
|
|
}
|
|
} else {
|
|
this.log('File path is undefined or does not exist.', 'error')
|
|
}
|
|
}
|
|
|
|
// 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
|
|
defaultPath: app.getPath('desktop'),
|
|
filters: [{ name: 'All Files', extensions: ['*'] }],
|
|
})
|
|
|
|
if (result.filePaths && result.filePaths.length > 0) {
|
|
this.log(`File selected: ${result.filePaths[0]}`)
|
|
return result.filePaths[0] // Return the selected file path
|
|
} else {
|
|
this.log('No file selected.')
|
|
return undefined // Return undefined if no file was selected
|
|
}
|
|
}
|
|
|
|
// Method to display an announcement in a new window
|
|
async displayAnnouncement(): Promise<void> {
|
|
if (this.announcementWindow) {
|
|
this.announcementWindow.focus()
|
|
this.log('Announcement window focused.')
|
|
return
|
|
}
|
|
|
|
const mainScreen = require('electron').screen.getPrimaryDisplay()
|
|
const { width, height } = mainScreen.size
|
|
|
|
this.announcementWindow = new BrowserWindow({
|
|
width: width / 3,
|
|
height: height / 2,
|
|
resizable: false,
|
|
title: 'Announcement',
|
|
webPreferences: {
|
|
preload: path.join(__dirname, '..', 'main', 'preload.js'),
|
|
nodeIntegration: false,
|
|
contextIsolation: true,
|
|
sandbox: false,
|
|
},
|
|
})
|
|
|
|
this.announcementWindow.removeMenu()
|
|
const announcementPath = path.join(this.pathToPagesDir, 'announcement.html')
|
|
await this.announcementWindow.loadFile(announcementPath)
|
|
|
|
this.log(`Announcement window opened at: ${announcementPath}`)
|
|
|
|
// Handle window close
|
|
this.announcementWindow.on('closed', () => {
|
|
this.announcementWindow = null
|
|
this.log('Announcement window closed.')
|
|
})
|
|
}
|
|
|
|
async closeAnnouncementWindow(): Promise<void> {
|
|
if (this.announcementWindow) {
|
|
this.announcementWindow.close()
|
|
this.log('Announcement window closed by user.')
|
|
}
|
|
}
|
|
}
|