added "recent files" functionality

master
Michael Ochmann 1 year ago
parent 22b1617afe
commit 3c3b81c585
  1. 23
      src/Ation.js
  2. 16
      src/MainMenu.js
  3. 2
      src/SettingsManager.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;

@ -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;

@ -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() {

Loading…
Cancel
Save