Skip to content

Commit

Permalink
Save last filename/folder for each dialog input.
Browse files Browse the repository at this point in the history
Also serves as test for better auto update.
  • Loading branch information
luckytyphlosion committed Jul 23, 2023
1 parent 68f1553 commit 3fe6d99
Show file tree
Hide file tree
Showing 15 changed files with 117 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"productName": "Auto-TT-Recorder GUI",
"description": "GUI wrapper for Auto-TT-Recorder",
"author": "luckytyphlosion",
"version": "1.1.1",
"version": "1.1.2",
"autoTTRecorderVersion": "1.3.8",
"dolphinVersion": "1.0.1",
"private": true,
Expand Down
48 changes: 40 additions & 8 deletions src/main/confighandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,30 @@ import * as versions from "../versions";

export let globalConfig: Config;

export interface SavedDialogPathnames {
"iso-wbfs": string,
"music": string,
"rkgs": string,
"extra-gecko-codes": string,
"extra-hq-textures": string,
"output-video": string,
"szs": string,
"top-10-gecko-code": string,
}

export type DialogId = keyof SavedDialogPathnames;

interface ConfigOptions {
autoTTRecorderVersion: string,
guiVersion: string,
dolphinVersion: string
dolphinVersion: string,
savedDialogPathnames: SavedDialogPathnames,
}

export class Config {
app: App;
fileIOMutex: Mutex;
options: ConfigOptions;
_options: ConfigOptions;
configFilepath: string;

// paths
Expand All @@ -37,7 +51,7 @@ export class Config {
this.userDataPath = path.resolve(appDataPath, name, "auto-tt-recorder-gui-working");
console.log("this.userDataPath:", this.userDataPath);
this.configFilepath = path.resolve(this.userDataPath, "config.json");
this.options = this.readConfig();
this._options = this.readConfig();
this.dolphinPath = path.resolve(this.userDataPath, "dolphin");
console.log("this.workingDolphinPath:", this.dolphinPath);
this.storagePath = path.resolve(this.userDataPath, "storage");
Expand All @@ -55,6 +69,16 @@ export class Config {
autoTTRecorderVersion: versions.AUTO_TT_RECORDER_VERSION,
guiVersion: versions.AUTO_TT_RECORDER_GUI_VERSION,
dolphinVersion: versions.DOLPHIN_VERSION,
savedDialogPathnames: {
"iso-wbfs": "",
"music": "",
"rkgs": "",
"extra-gecko-codes": "",
"extra-hq-textures": "",
"output-video": "",
"szs": "",
"top-10-gecko-code": ""
}
};
}

Expand Down Expand Up @@ -95,9 +119,9 @@ export class Config {
partialOptions = {};
}

this.options = this.fillOptionsWithDefaults(partialOptions);
this._options = this.fillOptionsWithDefaults(partialOptions);
this.writeConfig().then();
return this.options;
return this._options;
}

async writeConfig() {
Expand All @@ -108,12 +132,20 @@ export class Config {
}

async updateVersions() {
this.options.autoTTRecorderVersion = versions.AUTO_TT_RECORDER_VERSION;
this.options.guiVersion = versions.AUTO_TT_RECORDER_GUI_VERSION;
this.options.dolphinVersion = versions.DOLPHIN_VERSION;
this._options.autoTTRecorderVersion = versions.AUTO_TT_RECORDER_VERSION;
this._options.guiVersion = versions.AUTO_TT_RECORDER_GUI_VERSION;
this._options.dolphinVersion = versions.DOLPHIN_VERSION;
await this.writeConfig();
}

get options() {
return this._options;
}

async setNewDialogPathnameAndSave(dialogId: DialogId, newDialogPathname: string) {
this._options.savedDialogPathnames[dialogId] = newDialogPathname;
await this.writeConfig();
}
}

export function loadGlobalConfig(app: App, name: string) {
Expand Down
64 changes: 47 additions & 17 deletions src/main/gui2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,70 @@ import { dialog, IpcMainInvokeEvent, FileFilter, OpenDialogOptions, SaveDialogOp
import { mainWindow } from "./electron";
import { FilenameAndContents } from "../shared-types";
import fsPromises from "fs/promises";
import { DialogId, globalConfig } from "./confighandler";

export async function openFileDialog(event: IpcMainInvokeEvent, fileFilters: FileFilter[]) {
function retrieveLastPathname(lastPathname: string | undefined, dialogId: DialogId) {
if (lastPathname === "") {
lastPathname = globalConfig.options.savedDialogPathnames[dialogId];
if (lastPathname === "") {
lastPathname = undefined;
}
}
return lastPathname;
}
export async function openFileDialog(event: IpcMainInvokeEvent, fileFilters: FileFilter[],
lastFilename: string | undefined, dialogId: DialogId): Promise<string> {
console.log("open-file-dialog fileFilters:", fileFilters);
let dialogProperties: OpenDialogOptions["properties"] = ["openFile"];
lastFilename = retrieveLastPathname(lastFilename, dialogId);

let response = await dialog.showOpenDialog(mainWindow, {
properties: dialogProperties,
filters: fileFilters
filters: fileFilters,
defaultPath: lastFilename
});
if (!response.canceled) {
console.log(response.filePaths[0]);
return response.filePaths[0];
let responseFilepath = response.filePaths[0];
console.log(responseFilepath);
globalConfig.setNewDialogPathnameAndSave(dialogId, responseFilepath);
return responseFilepath;
} else {
return "";
}
}

export async function openFolderDialog(event: IpcMainInvokeEvent) {
export async function openFolderDialog(event: IpcMainInvokeEvent,
lastFolderName: string | undefined, dialogId: DialogId): Promise<string> {
let dialogProperties: OpenDialogOptions["properties"] = ["openDirectory"];
lastFolderName = retrieveLastPathname(lastFolderName, dialogId);

let response = await dialog.showOpenDialog(mainWindow, {
properties: dialogProperties,
defaultPath: lastFolderName
});
if (!response.canceled) {
console.log(response.filePaths[0]);
return response.filePaths[0];
let responseFilepath = response.filePaths[0];
console.log(responseFilepath);
globalConfig.setNewDialogPathnameAndSave(dialogId, responseFilepath);
return responseFilepath;
} else {
return "";
}
}

export async function openFileDialogAndRead(event: IpcMainInvokeEvent, fileFilters: FileFilter[]) : Promise<FilenameAndContents> {
export async function openFileDialogAndRead(event: IpcMainInvokeEvent, fileFilters: FileFilter[],
lastFilename: string | undefined, dialogId: DialogId) : Promise<FilenameAndContents> {
let dialogProperties: OpenDialogOptions["properties"] = ["openFile"];
lastFilename = retrieveLastPathname(lastFilename, dialogId);
let response = await dialog.showOpenDialog(mainWindow, {
properties: dialogProperties,
filters: fileFilters
filters: fileFilters,
defaultPath: lastFilename
});
if (!response.canceled) {
let filename = response.filePaths[0];
const contents = await fsPromises.readFile(filename, "utf8");
globalConfig.setNewDialogPathnameAndSave(dialogId, filename);
return {
filename: filename,
contents: contents
Expand All @@ -50,32 +76,36 @@ export async function openFileDialogAndRead(event: IpcMainInvokeEvent, fileFilte
}
}

export async function saveFileDialog(event: IpcMainInvokeEvent, fileFilters: FileFilter[]) {
export async function saveFileDialog(event: IpcMainInvokeEvent, fileFilters: FileFilter[],
lastFilename: string | undefined, dialogId: DialogId): Promise<string> {
let dialogProperties: SaveDialogOptions["properties"] = [];
lastFilename = retrieveLastPathname(lastFilename, dialogId);
let response = await dialog.showSaveDialog(mainWindow, {
properties: dialogProperties,
filters: fileFilters
filters: fileFilters,
defaultPath: lastFilename
});
if (!response.canceled) {
if (!response.canceled && response.filePath !== undefined) {
globalConfig.setNewDialogPathnameAndSave(dialogId, response.filePath);
return response.filePath;
} else {
return "";
}
}

export async function saveFileDialogAndWriteText(event: IpcMainInvokeEvent, fileFilters: FileFilter[], output: string, defaultPath: string | undefined) {
export async function saveFileDialogAndWriteText(event: IpcMainInvokeEvent, fileFilters: FileFilter[],
output: string, lastFilename: string | undefined, dialogId: DialogId): Promise<string> {
let dialogProperties: SaveDialogOptions["properties"] = [];
if (defaultPath === "") {
defaultPath = undefined;
}
lastFilename = retrieveLastPathname(lastFilename, dialogId);
console.log("saveFileDialogAndWriteText output:", output);
let response = await dialog.showSaveDialog(mainWindow, {
properties: dialogProperties,
filters: fileFilters,
defaultPath: defaultPath
defaultPath: lastFilename
});
if (!response.canceled && response.filePath !== undefined) {
await fsPromises.writeFile(response.filePath, output, "utf8");
globalConfig.setNewDialogPathnameAndSave(dialogId, response.filePath);
return response.filePath;
} else {
return "";
Expand Down
10 changes: 5 additions & 5 deletions src/main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
const { contextBridge, ipcRenderer } = require("electron");

contextBridge.exposeInMainWorld("api", {
openFileDialog: (fileFilters) => ipcRenderer.invoke("open-file-dialog", fileFilters),
openFolderDialog: () => ipcRenderer.invoke("open-folder-dialog"),
saveFileDialog: (fileFilters) => ipcRenderer.invoke("save-file-dialog", fileFilters),
openFileDialogAndRead: (fileFilters) => ipcRenderer.invoke("open-file-dialog-and-read", fileFilters),
saveFileDialogAndWriteText: (fileFilters, output, defaultPath) => ipcRenderer.invoke("save-file-dialog-and-write-text", fileFilters, output, defaultPath),
openFileDialog: (fileFilters, lastFilename, dialogId) => ipcRenderer.invoke("open-file-dialog", fileFilters, lastFilename, dialogId),
openFolderDialog: (lastFolderName, dialogId) => ipcRenderer.invoke("open-folder-dialog", lastFolderName, dialogId),
openFileDialogAndRead: (fileFilters, lastFilename, dialogId) => ipcRenderer.invoke("open-file-dialog-and-read", fileFilters, lastFilename, dialogId),
saveFileDialog: (fileFilters, lastFilename, dialogId) => ipcRenderer.invoke("save-file-dialog", fileFilters, lastFilename, dialogId),
saveFileDialogAndWriteText: (fileFilters, output, lastFilename, dialogId) => ipcRenderer.invoke("save-file-dialog-and-write-text", fileFilters, output, lastFilename, dialogId),
overwriteTextFile: (outputFilename, output) => ipcRenderer.invoke("overwrite-text-file", outputFilename, output),
spawnAutoTTRec: (templateFilename, autoTTRecArgs) => ipcRenderer.invoke("spawn-auto-tt-rec", templateFilename, autoTTRecArgs),
waitAutoTTRec: () => ipcRenderer.invoke("wait-auto-tt-rec"),
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/GUIHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function GUIHeader() {
<h1 className="title-header">Auto-TT-Recorder GUI ({versions.AUTO_TT_RECORDER_GUI_VERSION})</h1>
<ul className="auto-tt-rec-notes-first-half">
<li>All regions are supported, including NTSC-K.</li>
<li>Supports all relevant features for mkchannel ghost recordings.</li>
<li>Supports almost all features from the non-GUI program</li>
<li>I will fix the visual layout later.</li>
<li>Links below aren't hyperlinks yet because of time issues, this will be fixed in the next version</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { FileFilter } from "electron";
import { SimpleErrorMessage } from "../SimpleErrorMessage";

export function ComparisonGhostFilenameInput() {
const {register, setValue} = useFormContextAutoTT();
const {register, setValue, getValues} = useFormContextAutoTT();

async function queueOpenDialog(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let response = await window.api.openFileDialog(fileFilters);
let response = await window.api.openFileDialog(fileFilters, getValues("comparison-ghost-filename"), "rkgs");
if (response !== "") {
setValue("comparison-ghost-filename", response, {shouldTouch: true});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function ExtraGeckoCodesInput(props: {isAutoTTRecRunning: boolean}) {
}

async function queueOpenDialogAndRead(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let filenameAndContents: FilenameAndContents = await window.api.openFileDialogAndRead(fileFilters);
let filenameAndContents: FilenameAndContents = await window.api.openFileDialogAndRead(fileFilters, getValues("extra-gecko-codes-filename"), "extra-gecko-codes");
if (filenameAndContents.filename !== "") {
updateExtraGeckoCodesFilename(filenameAndContents.filename);
setValue("extra-gecko-codes-contents", filenameAndContents.contents, {shouldTouch: true});
Expand All @@ -91,7 +91,7 @@ export function ExtraGeckoCodesInput(props: {isAutoTTRecRunning: boolean}) {
let success: boolean;

try {
let response = await window.api.saveFileDialogAndWriteText(fileFilters, output, defaultPath);
let response = await window.api.saveFileDialogAndWriteText(fileFilters, output, defaultPath, "extra-gecko-codes");
if (response !== "") {
updateExtraGeckoCodesFilename(response);
success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { AutoTTRecConfigFormFieldTypes } from "../../AutoTTRecFormFieldsAndArgs"
import { SimpleErrorMessage } from "../SimpleErrorMessage";

export function ExtraHQTexturesFolderInput() {
const {register, setValue} = useFormContextAutoTT();
const {register, setValue, getValues} = useFormContextAutoTT();
const renderCounter = useRenderCounter(false, "ExtraHQTexturesFolderInput");
const extraHQTexturesFolderEnable = useWatchAutoTT({name: "extra-hq-textures-folder-enable"});

async function queueOpenFolderDialog(event: React.MouseEvent<HTMLButtonElement>) {
let response = await window.api.openFolderDialog();
let response = await window.api.openFolderDialog(getValues("extra-hq-textures-folder"), "extra-hq-textures");
if (response !== "") {
setValue("extra-hq-textures-folder", response, {shouldTouch: true});
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/form_components/ISOWBFSFileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { AutoTTRecConfigFormFieldTypes } from "../../AutoTTRecFormFieldsAndArgs"
import { SimpleErrorMessage } from "../SimpleErrorMessage";

export function ISOWBFSFileInput() {
const {register, setValue} = useFormContextAutoTT();
const {register, setValue, getValues} = useFormContextAutoTT();
const renderCounter = useRenderCounter(false, "ISOWBFSFileInput");

async function queueOpenDialog(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let response = await window.api.openFileDialog(fileFilters);
let response = await window.api.openFileDialog(fileFilters, getValues("iso-filename"), "iso-wbfs");
if (response !== "") {
setValue("iso-filename", response, {shouldTouch: true});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { Set200ccInput } from "./Set200ccInput";
import { SimpleErrorMessage } from "../SimpleErrorMessage";

export function MainGhostFilenameInput() {
const {register, setValue} = useFormContextAutoTT();
const {register, setValue, getValues} = useFormContextAutoTT();

async function queueOpenDialog(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let response = await window.api.openFileDialog(fileFilters);
let response = await window.api.openFileDialog(fileFilters, getValues("main-ghost-filename"), "rkgs");
if (response !== "") {
setValue("main-ghost-filename", response, {shouldTouch: true});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { FileFilter } from "electron";
import { SimpleErrorMessage } from "../SimpleErrorMessage";

export function MusicFilenameInput() {
const {register, setValue} = useFormContextAutoTT();
const {register, setValue, getValues} = useFormContextAutoTT();

async function queueOpenDialog(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let response = await window.api.openFileDialog(fileFilters);
let response = await window.api.openFileDialog(fileFilters, getValues("music-filename"), "music");
if (response !== "") {
setValue("music-filename", response, {shouldTouch: true});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function OutputVideoFilenameInput(props: {noTop10CategoryIsNoEncode: bool
const renderCounter = useRenderCounter(false, "OutputVideoFilenameInput");

async function queueSaveDialog(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let response = await window.api.saveFileDialog(fileFilters);
let response = await window.api.saveFileDialog(fileFilters, getValues("output-video-filename"), "output-video");
if (response !== "") {
setValue("output-video-filename", response, {shouldTouch: true});
}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/components/form_components/SZSFilenameInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { FileFilter } from "electron";
import { SimpleErrorMessage } from "../SimpleErrorMessage";

export function SZSFilenameInput() {
const {register, setValue} = useFormContextAutoTT();
const {register, setValue, getValues} = useFormContextAutoTT();

async function queueOpenDialog(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let response = await window.api.openFileDialog(fileFilters);
let response = await window.api.openFileDialog(fileFilters, getValues("szs-filename"), "szs");
if (response !== "") {
setValue("szs-filename", response, {shouldTouch: true});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function Top10GeckoCodeInput(props: {isAutoTTRecRunning: boolean}) {
}

async function queueOpenDialogAndRead(event: React.MouseEvent<HTMLButtonElement>, fileFilters: FileFilter[]) {
let filenameAndContents: FilenameAndContents = await window.api.openFileDialogAndRead(fileFilters);
let filenameAndContents: FilenameAndContents = await window.api.openFileDialogAndRead(fileFilters, getValues("top-10-gecko-code-filename"), "top-10-gecko-code");
if (filenameAndContents.filename !== "") {
updateTop10GeckoCodeFilename(filenameAndContents.filename);
setValue("top-10-gecko-code-contents", filenameAndContents.contents, {shouldTouch: true});
Expand All @@ -93,7 +93,7 @@ export function Top10GeckoCodeInput(props: {isAutoTTRecRunning: boolean}) {
let success: boolean;

try {
let response = await window.api.saveFileDialogAndWriteText(fileFilters, output, defaultPath);
let response = await window.api.saveFileDialogAndWriteText(fileFilters, output, defaultPath, "top-10-gecko-code");
if (response !== "") {
updateTop10GeckoCodeFilename(response);
success = true;
Expand Down
Loading

0 comments on commit 3fe6d99

Please sign in to comment.