Skip to content

Commit

Permalink
feat: store recent project folders and offer them in the File menu (#755
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Dotneteer committed Apr 13, 2024
1 parent efe1648 commit 1f54b64
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -4,7 +4,7 @@

### Fixes

Fix the segment loading issue in the ZX Spectrum 128 exported file loader
- Fix the segment loading issue in the ZX Spectrum 128 exported file loader

## 0.31.0

Expand Down
30 changes: 29 additions & 1 deletion src/main/app-menu.ts
Expand Up @@ -40,7 +40,7 @@ import { sendFromMainToEmu } from "../common/messaging/MainToEmuMessenger";
import { createMachineCommand } from "../common/messaging/main-to-emu";
import { sendFromMainToIde } from "../common/messaging/MainToIdeMessenger";
import { appSettings, saveAppSettings } from "./settings";
import { openFolder, saveKliveProject } from "./projects";
import { addRecentProject, openFolder, saveKliveProject } from "./projects";
import {
EXPORT_CODE_DIALOG,
NEW_PROJECT_DIALOG,
Expand All @@ -66,6 +66,7 @@ export const KLIVE_GITHUB_PAGES = "https://dotneteer.github.io/kliveide";
const SYSTEM_MENU_ID = "system_menu";
const NEW_PROJECT = "new_project";
const OPEN_FOLDER = "open_folder";
const RECENT_PROJECTS = "recent_projects";
const CLOSE_FOLDER = "close_folder";
const TOGGLE_KEYBOARD = "toggle_keyboard";
const TOGGLE_DEVTOOLS = "toggle_devtools";
Expand Down Expand Up @@ -182,6 +183,32 @@ export function setupMenu (

// ==========================================================================
// File menu
const recentProjectNames = appSettings.recentProjects ?? [];

const recentProjects: MenuItemConstructorOptions[] = recentProjectNames.map(
rp => {
return {
label: rp,
click: async () => {
await executeIdeCommand(ideWindow, `open "${rp}"`, undefined, true);
}
};
}
);

let recentProjectHolder: MenuItemConstructorOptions[] = [];

if (recentProjects.length > 0) {
recentProjectHolder.push(
{ type: "separator" },
{
id: RECENT_PROJECTS,
label: "Recent Folders",
submenu: recentProjects
}
);
}

template.push({
label: "File",
submenu: filterVisibleItems([
Expand All @@ -201,6 +228,7 @@ export function setupMenu (
await openFolder(ideWindow);
}
},
...recentProjectHolder,
{ type: "separator" },
{
id: CLOSE_FOLDER,
Expand Down
25 changes: 25 additions & 0 deletions src/main/projects.ts
Expand Up @@ -227,6 +227,7 @@ export async function openFolderByPath (
}
}

addRecentProject(projectFolder);
disp(openFolderAction(projectFolder, isValidProject));

// --- Chck for a build file
Expand Down Expand Up @@ -416,3 +417,27 @@ export async function saveKliveProject (): Promise<void> {
// --- Intentionally ignored
}
}

let recentProjects: string[] = [];
const MAX_RECENT_PROJECTS = 10;

// --- Retrieve the recent projects
export function getRecentProjects (): string[] {
return recentProjects;
}

// --- Set the recent projects (after loading the settings)
export function setRecentProjects (projects: string[]): void {
recentProjects = projects;
}

// --- Add a recent project
export function addRecentProject (projectFolder: string): void {
if (recentProjects.includes(projectFolder)) {
recentProjects = recentProjects.filter((p) => p !== projectFolder);
}
recentProjects.unshift(projectFolder);
recentProjects = recentProjects.slice(0, MAX_RECENT_PROJECTS);
appSettings.recentProjects = recentProjects;
saveAppSettings();
}
6 changes: 6 additions & 0 deletions src/main/settings.ts
Expand Up @@ -3,6 +3,7 @@ import * as fs from "fs";
import { app } from "electron";
import { WindowState } from "./WindowStateManager";
import { mainStore } from "./main-store";
import { getRecentProjects, setRecentProjects } from "./projects";

export const KLIVE_HOME_FOLDER = "Klive";
export const SETTINGS_FILE_NAME = "klive.settings";
Expand Down Expand Up @@ -39,6 +40,7 @@ export type AppSettings = {
excludedProjectItems?: string[];
keyMappingFile?: string;
userSettings?: Record<string, any>;
recentProjects?: string[];
};

let finalSaveDone = false;
Expand Down Expand Up @@ -78,6 +80,7 @@ export function saveAppSettings (): void {
appSettings.soundLevel = state.emulatorState?.soundLevel ?? 0.5;
appSettings.media = state.media ?? {};
appSettings.keyMappingFile = state.keyMappingFile;
appSettings.recentProjects = getRecentProjects();
}

// --- Save to the settings file
Expand All @@ -95,6 +98,9 @@ export function loadAppSettings (): void {
try {
const contents = fs.readFileSync(getSettingsFilePath(), "utf8");
appSettings = JSON.parse(contents) as AppSettings;

// --- Apply settings to the current main-only state
setRecentProjects(appSettings.recentProjects ?? []);
} catch {
appSettings = {};
}
Expand Down

0 comments on commit 1f54b64

Please sign in to comment.