fixed close sketch

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
This commit is contained in:
Akos Kitta
2020-07-18 13:34:36 +02:00
parent e8c3abd2ec
commit 7873e492d4
13 changed files with 79 additions and 46 deletions

View File

@@ -37,7 +37,7 @@ import { ColorRegistry } from '@theia/core/lib/browser/color-registry';
import { ArduinoDaemon } from '../common/protocol/arduino-daemon';
import { ConfigService } from '../common/protocol/config-service';
import { BoardsConfigStore } from './boards/boards-config-store';
import { MainMenuManager } from './menu/main-menu-manager';
import { MainMenuManager } from '../common/main-menu-manager';
import { FileSystemExt } from '../common/protocol/filesystem-ext';
import { ArduinoMenus } from './menu/arduino-menus';

View File

@@ -6,7 +6,7 @@ import { BoardsServiceClientImpl } from './boards-service-client-impl';
import { Board, ConfigOption } from '../../common/protocol';
import { FrontendApplicationContribution } from '@theia/core/lib/browser';
import { BoardsConfigStore } from './boards-config-store';
import { MainMenuManager } from '../menu/main-menu-manager';
import { MainMenuManager } from '../../common/main-menu-manager';
import { ArduinoMenus } from '../menu/arduino-menus';
@injectable()

View File

@@ -1,13 +1,17 @@
import { injectable } from 'inversify';
import { inject, injectable } from 'inversify';
import { remote } from 'electron';
import { WorkspaceCommands } from '@theia/workspace/lib/browser/workspace-commands';
import { ArduinoMenus } from '../menu/arduino-menus';
import { SketchContribution, Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry } from './contribution';
import { SketchContribution, Command, CommandRegistry, MenuModelRegistry, KeybindingRegistry, URI, Sketch } from './contribution';
import { SaveAsSketch } from './save-as-sketch';
import { EditorManager } from '@theia/editor/lib/browser';
import { MonacoEditor } from '@theia/monaco/lib/browser/monaco-editor';
@injectable()
export class CloseSketch extends SketchContribution {
@inject(EditorManager)
protected readonly editorManager: EditorManager;
registerCommands(registry: CommandRegistry): void {
registry.registerCommand(CloseSketch.Commands.CLOSE_SKETCH, {
execute: async () => {
@@ -16,8 +20,7 @@ export class CloseSketch extends SketchContribution {
return;
}
const isTemp = await this.sketchService.isTemp(sketch);
if (isTemp) {
// TODO: check monaco model version. If `0` just close the app.
if (isTemp && await this.wasTouched(sketch)) {
const { response } = await remote.dialog.showMessageBox({
type: 'question',
buttons: ["Don't Save", 'Cancel', 'Save'],
@@ -34,7 +37,7 @@ export class CloseSketch extends SketchContribution {
}
}
}
await this.commandService.executeCommand(WorkspaceCommands.CLOSE.id);
window.close();
}
});
}
@@ -54,6 +57,23 @@ export class CloseSketch extends SketchContribution {
});
}
/**
* If the file was ever touched/modified. We get this based on the `version` of the monaco model.
*/
protected async wasTouched(sketch: Sketch): Promise<boolean> {
const editorWidget = await this.editorManager.getByUri(new URI(sketch.uri).resolve(`${sketch.name}.ino`));
if (editorWidget) {
const { editor } = editorWidget;
if (editor instanceof MonacoEditor) {
const versionId = editor.getControl().getModel()?.getVersionId();
if (Number.isInteger(versionId) && versionId! > 1) {
return true;
}
}
}
return false;
}
}
export namespace CloseSketch {

View File

@@ -1,6 +1,6 @@
import { injectable } from 'inversify';
import { BrowserMainMenuFactory as TheiaBrowserMainMenuFactory, MenuBarWidget } from '@theia/core/lib/browser/menu/browser-menu-plugin';
import { MainMenuManager } from '../../menu/main-menu-manager';
import { MainMenuManager } from '../../../common/main-menu-manager';
@injectable()
export class BrowserMainMenuFactory extends TheiaBrowserMainMenuFactory implements MainMenuManager {

View File

@@ -1,7 +1,7 @@
import '../../../../src/browser/style/browser-menu.css';
import { ContainerModule } from 'inversify';
import { BrowserMenuBarContribution, BrowserMainMenuFactory as TheiaBrowserMainMenuFactory } from '@theia/core/lib/browser/menu/browser-menu-plugin';
import { MainMenuManager } from '../../menu/main-menu-manager';
import { MainMenuManager } from '../../../common/main-menu-manager';
import { ArduinoMenuContribution } from './browser-menu-plugin';
import { BrowserMainMenuFactory } from './browser-main-menu-factory';

View File

@@ -18,17 +18,12 @@ export class WorkspaceFrontendContribution extends TheiaWorkspaceFrontendContrib
WorkspaceCommands.OPEN_WORKSPACE,
WorkspaceCommands.OPEN_RECENT_WORKSPACE,
WorkspaceCommands.SAVE_WORKSPACE_AS,
WorkspaceCommands.SAVE_AS
WorkspaceCommands.SAVE_AS,
WorkspaceCommands.CLOSE
].filter(commands.has.bind(commands)).forEach(registry.unregisterCommand.bind(registry));
}
registerMenus(_: MenuModelRegistry): void {
// NOOP
}
protected async closeWorkspace(): Promise<void> {
// Do not ask to close the workspace, just close it. The dirty/temp state of the sketch is handled somewhere else.
await this.workspaceService.close();
}
}

View File

@@ -2,7 +2,7 @@ import { injectable, inject } from 'inversify';
import { ApplicationShell, FrontendApplicationContribution, FrontendApplication, Widget } from '@theia/core/lib/browser';
import { EditorWidget } from '@theia/editor/lib/browser';
import { OutputWidget } from '@theia/output/lib/browser/output-widget';
import { MainMenuManager } from './menu/main-menu-manager';
import { MainMenuManager } from '../common/main-menu-manager';
import { BoardsListWidget } from './boards/boards-list-widget';
import { LibraryListWidget } from './library/library-list-widget';

View File

@@ -1,8 +0,0 @@
export const MainMenuManager = Symbol('MainMenuManager');
export interface MainMenuManager {
/**
* Call this method if you have changed the content of the main menu (updated a toggle flag, removed/added new groups or menu items)
* and you want to re-render it from scratch. Works for electron too.
*/
update(): void;
}