From 3c3b81c585489f857c29bffceced8dbf75de8be7 Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Thu, 1 Feb 2024 16:45:03 +0100 Subject: [PATCH] added "recent files" functionality --- src/Ation.js | 23 ++++++++++++++++++++++- src/MainMenu.js | 16 +++++++++++++++- src/SettingsManager.js | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/Ation.js b/src/Ation.js index b97cc6c..738629b 100644 --- a/src/Ation.js +++ b/src/Ation.js @@ -13,6 +13,7 @@ const {parser} = require("./Parser"); app.commandLine.appendSwitch("disable-http-cache"); +const RECENT_FILES = 5; class Ation { windowManager; fontManager; @@ -21,6 +22,7 @@ class Ation { watcher; currentFile; fileToOpen; + recentFiles; constructor() { if (Ation.Instances > 0) @@ -31,6 +33,7 @@ class Ation { this.windowManager = new WindowManager(this); this.fontManager = new FontManager(); this.settingsManager = new SettingsManager(this); + this.recentFiles = this.settingsManager.get("recentFiles", []); this.mainMenu = new MainMenu(this); ipcMain.handle("Ation::appVersion", () => AppInfo.version); @@ -64,6 +67,23 @@ class Ation { }); } + addRecentFile(filePath) { + if (this.recentFiles.includes(filePath)) { + const position = this.recentFiles.indexOf(filePath); + + this.recentFiles.splice(position, 1), + this.recentFiles.push(filePath); + } else if (this.recentFiles.length < RECENT_FILES) { + this.recentFiles.push(filePath); + } else { + this.recentFiles.shift(); + this.recentFiles.push(filePath); + } + + this.settingsManager.set("recentFiles", this.recentFiles); + this.mainMenu.refresh(); + } + async openFile(filePath = null, change = false) { if (!this.windowManager.mainWindow) return null; @@ -99,8 +119,9 @@ class Ation { else this.openFile(this.currentFile, true); }); - Menu.getApplicationMenu().getMenuItemById("close-file").enabled = this.currentFile !== ""; this.windowManager.mainWindow.send("Ation::openFile", [basePath, data]); + this.addRecentFile(filePath); + Menu.getApplicationMenu().getMenuItemById("close-file").enabled = this.currentFile !== ""; } catch (error) { console.log(error); return; diff --git a/src/MainMenu.js b/src/MainMenu.js index b51fcc0..208d803 100644 --- a/src/MainMenu.js +++ b/src/MainMenu.js @@ -1,14 +1,18 @@ "use strict"; const {Menu, app} = require("electron"); +const path = require("path"); class MainMenu { constructor(parentApp) { this.app = parentApp; this.menu = null; + this.refresh(); + } + + refresh() { this.buildItems(); Menu.setApplicationMenu(this.menu); - } buildItems() { @@ -53,9 +57,19 @@ class MainMenu { role : "windowMenu" } ]; + template[1].submenu.push({ + id : "recent-files", + label : "Recent files", + enabled : this.app.recentFiles.length > 0, + ...(this.app.recentFiles.length > 0 ? {submenu : this.app.recentFiles.map(item => ({ + label : path.basename(item), + click : () => this.app.openFile("" + item) + }))} : {}), + }); this.menu = Menu.buildFromTemplate(template); } + } module.exports = MainMenu; \ No newline at end of file diff --git a/src/SettingsManager.js b/src/SettingsManager.js index 911213f..b844811 100644 --- a/src/SettingsManager.js +++ b/src/SettingsManager.js @@ -38,7 +38,7 @@ class SettingsManager { } change() { - this.app.windowManager.mainWindow.send("SettingsManager::change", this.data); + this.app.windowManager.mainWindow?.send("SettingsManager::change", this.data); } save() {