Skip to content

Check dirty flag before opening new or existing project #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions electron/app/js/ipcRendererPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ contextBridge.exposeInMainWorld(
send: (channel, ...args) => {
const validChannels = [
'close-window',
'new-project',
'open-project',
'window-app-quit',
'window-is-ready',
Expand Down Expand Up @@ -65,6 +66,8 @@ contextBridge.exposeInMainWorld(
'show-startup-dialogs',
'app-update-available',
'blur-focused-item',
'start-new-project',
'start-open-project',
'start-prepare-model',
'start-validate-model',
'start-create-image',
Expand Down
38 changes: 23 additions & 15 deletions electron/app/js/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@ async function createNewProject(targetWindow) {
return;
}

let projectWindow = await _createOrReplace(targetWindow);
sendToWindow(targetWindow, 'start-new-project', saveResponse.filePath);
// window will reply with new-project -> initializeNewProject() including isDirty flag
}

async function initializeNewProject(targetWindow, projectFile, isDirty) {
// finish creating new project with project dirty flag
let projectWindow = await _createOrReplace(targetWindow, isDirty);
if (!projectWindow) {
return;
}

let projectFileName = getProjectFileName(saveResponse.filePath);
let projectFileName = getProjectFileName(projectFile);
if (path.extname(projectFileName) !== `.${projectFileExtension}`) {
projectFileName = `${projectFileName}.${projectFileExtension}`;
}
Expand All @@ -92,24 +98,18 @@ async function openProject(targetWindow) {
}

const projectFileName = openResponse.filePaths[0];
await openProjectFile(targetWindow, projectFileName)
.catch(err => {
dialog.showErrorBox(
i18n.t('dialog-openProjectFileErrorTitle'),
i18n.t('dialog-openProjectFileErrorMessage', { projectFileName: projectFileName, err: err }),
);
getLogger().error('Failed to open project file %s: %s', projectFileName, err);
});
sendToWindow(targetWindow, 'start-open-project', projectFileName);
// window will reply with open-project -> openProjectFile() including isDirty flag
}

async function openProjectFile(targetWindow, projectFile) {
async function openProjectFile(targetWindow, projectFile, isDirty) {
return new Promise((resolve, reject) => {
const existingProjectWindow = _getOpenWindowForProject(projectFile);
if (existingProjectWindow) {
showExistingProjectWindow(existingProjectWindow);
resolve();
} else {
_createOrReplace(targetWindow)
_createOrReplace(targetWindow, isDirty)
.then(projectWindow => {
if (!projectWindow) {
return resolve();
Expand All @@ -122,7 +122,14 @@ async function openProjectFile(targetWindow, projectFile) {
})
.catch(err => reject(err));
}
});
})
.catch(err => {
dialog.showErrorBox(
i18n.t('dialog-openProjectFileErrorTitle'),
i18n.t('dialog-openProjectFileErrorMessage', { projectFileName: projectFile, err: err }),
);
getLogger().error('Failed to open project file %s: %s', projectFile, err);
});
}

// request the existing project file, prompting the user if needed.
Expand Down Expand Up @@ -441,9 +448,9 @@ async function _saveExternalFileContents(projectDirectory, externalFileContents)
return saveResult;
}

async function _createOrReplace(targetWindow) {
async function _createOrReplace(targetWindow, isDirty) {
let projectWindow = targetWindow;
if (openProjects.has(targetWindow)) {
if (openProjects.has(targetWindow) || isDirty) {
const buttonResponse = await dialog.showMessageBox(targetWindow, {
type: 'question',
message: i18n.t('dialog-openProjectWindowPrompt'),
Expand Down Expand Up @@ -867,6 +874,7 @@ module.exports = {
getWindowForProject,
exportArchiveFile,
isWktProjectFile,
initializeNewProject,
openProject,
openProjectFile,
_openProjectFile,
Expand Down
13 changes: 11 additions & 2 deletions electron/app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,19 @@ class Main {
});
});

ipcMain.on('open-project', async (event, projectFile) => {
ipcMain.on('open-project', async (event, projectFile, isDirty) => {
try {
const currentWindow = event.sender.getOwnerBrowserWindow();
await project.openProjectFile(currentWindow, projectFile);
await project.openProjectFile(currentWindow, projectFile, isDirty);
} catch (e) {
this._logger.error(e);
}
});

ipcMain.on('new-project', async (event, projectFile, isDirty) => {
try {
const currentWindow = event.sender.getOwnerBrowserWindow();
await project.initializeNewProject(currentWindow, projectFile, isDirty);
} catch (e) {
this._logger.error(e);
}
Expand Down
2 changes: 1 addition & 1 deletion webui/src/js/viewModels/app-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function(accUtils, ko, i18n, project, wktConsole, dialogHelper, viewHelper, scre
const file = getDroppedFile(event);
wktLogger.debug(`dropped file path = ${file.path}`);
if (window.api.path.extname(file.name).toLowerCase() === '.wktproj') {
window.api.ipc.send('open-project', file.path);
window.api.ipc.send('open-project', file.path, project.isDirty());
} else {
wktLogger.warn(`Dropped file ${file.path} extension was not recognized as a wktproj file`);
}
Expand Down
12 changes: 12 additions & 0 deletions webui/src/js/windowStateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ function(wktProject, wktConsole, wdtDiscoverer, dialogHelper, projectIO,
});
});

// continue opening project with isDirty value
window.api.ipc.receive('start-open-project', projectFileName => {
blurSelection();
window.api.ipc.send('open-project', projectFileName, wktProject.isDirty());
});

// continue new project with isDirty value
window.api.ipc.receive('start-new-project', projectFileName => {
blurSelection();
window.api.ipc.send('new-project', projectFileName, wktProject.isDirty());
});

window.api.ipc.receive('start-close-project', () => {
blurSelection();
projectIO.closeProject(false).catch(err => {
Expand Down