Selaa lähdekoodia

Update view.js (#1496)

Lutz Roeder 5 kuukautta sitten
vanhempi
sitoutus
3529217bcf
4 muutettua tiedostoa jossa 43 lisäystä ja 22 poistoa
  1. 8 8
      source/app.js
  2. 1 1
      source/desktop.mjs
  3. 31 13
      source/view.js
  4. 3 0
      test/worker.js

+ 8 - 8
source/app.js

@@ -276,7 +276,7 @@ app.Application = class {
             case 'about': this._about(); break;
             default: {
                 const view = this._views.get(window) || this._views.activeView;
-                if (view) {
+                if (view && view.get(`${command}.enabled`) !== false) {
                     view.execute(command, value || {});
                 }
                 this._menu.update();
@@ -507,10 +507,10 @@ app.Application = class {
                     },
                     { type: 'separator' },
                     {
-                        id: 'view.reset-zoom',
+                        id: 'view.zoom-reset',
                         label: 'Actual &Size',
                         accelerator: 'Shift+Backspace',
-                        click: async () => await this.execute('reset-zoom', null),
+                        click: async () => await this.execute('zoom-reset', null),
                     },
                     {
                         id: 'view.zoom-in',
@@ -579,7 +579,7 @@ app.Application = class {
                 enabled: (view) => view && view.path ? true : false
             });
             commandTable.set('edit.copy', {
-                enabled: (view) => view && (view.path || view.get('can-copy')) ? true : false
+                enabled: (view) => view && (view.path || view.get('copy.enabled')) ? true : false
             });
             commandTable.set('edit.paste', {
                 enabled: (view) => view && view.path ? true : false
@@ -613,14 +613,14 @@ app.Application = class {
             commandTable.set('view.reload', {
                 enabled: (view) => view && view.path ? true : false
             });
-            commandTable.set('view.reset-zoom', {
-                enabled: (view) => view && view.path ? true : false
+            commandTable.set('view.zoom-reset', {
+                enabled: (view) => view && view.path && view.get('zoom-reset.enabled') ? true : false
             });
             commandTable.set('view.zoom-in', {
-                enabled: (view) => view && view.path ? true : false
+                enabled: (view) => view && view.path && view.get('zoom-in.enabled') ? true : false
             });
             commandTable.set('view.zoom-out', {
-                enabled: (view) => view && view.path ? true : false
+                enabled: (view) => view && view.path && view.get('zoom-out.enabled') ? true : false
             });
             commandTable.set('view.show-properties', {
                 enabled: (view) => view && view.path ? true : false

+ 1 - 1
source/desktop.mjs

@@ -220,7 +220,7 @@ desktop.Host = class {
         electron.ipcRenderer.on('zoom-out', () => {
             this._element('zoom-out-button').click();
         });
-        electron.ipcRenderer.on('reset-zoom', () => {
+        electron.ipcRenderer.on('zoom-reset', () => {
             this._view.resetZoom();
         });
         electron.ipcRenderer.on('show-properties', () => {

+ 31 - 13
source/view.js

@@ -64,11 +64,11 @@ view.View = class {
                 }
             });
             if (this._host.type === 'Electron') {
-                this._host.update({ 'can-copy': false });
+                this._host.update({ 'copy.enabled': false });
                 this._host.document.addEventListener('selectionchange', () => {
                     const selection = this._host.document.getSelection();
                     const selected = selection.rangeCount === 0 || selection.toString().trim() !== '';
-                    this._host.update({ 'can-copy': selected });
+                    this._host.update({ 'copy.enabled': selected });
                 });
             }
             const platform = this._host.environment('platform');
@@ -175,19 +175,19 @@ view.View = class {
                     label: 'Zoom &In',
                     accelerator: 'Shift+Up',
                     execute: () => this.zoomIn(),
-                    enabled: () => this.activeTarget
+                    enabled: () => this.activeTarget && this.target
                 });
                 view.add({
                     label: 'Zoom &Out',
                     accelerator: 'Shift+Down',
                     execute: () => this.zoomOut(),
-                    enabled: () => this.activeTarget
+                    enabled: () => this.activeTarget && this.target
                 });
                 view.add({
                     label: 'Actual &Size',
                     accelerator: 'Shift+Backspace',
                     execute: () => this.resetZoom(),
-                    enabled: () => this.activeTarget
+                    enabled: () => this.activeTarget && this.target
                 });
                 view.add({});
                 view.add({
@@ -299,6 +299,28 @@ view.View = class {
         return this._options;
     }
 
+    get target() {
+        return this._target;
+    }
+
+    set target(value) {
+        if (this._target !== value) {
+            if (this._target) {
+                this._target.unregister();
+            }
+            const enabled = value ? true : false;
+            this._host.update({
+                'zoom-reset.enabled': enabled,
+                'zoom-in.enabled': enabled,
+                'zoom-out.enabled': enabled
+            });
+            this._target = value;
+            if (this._target) {
+                this._target.register();
+            }
+        }
+    }
+
     toggle(name) {
         switch (name) {
             case 'names':
@@ -596,10 +618,7 @@ view.View = class {
     }
 
     async render(target, signature) {
-        if (this._target) {
-            this._target.unregister();
-            this._target = null;
-        }
+        this.target = null;
         const element = this._element('target');
         while (element.lastChild) {
             element.removeChild(element.lastChild);
@@ -623,8 +642,7 @@ view.View = class {
                 viewGraph.update();
                 const state = this._path && this._path.length > 0 && this._path[0] && this._path[0].state ? this._path[0].state : null;
                 viewGraph.restore(state);
-                this._target = viewGraph;
-                this._target.register();
+                this.target = viewGraph;
             }
         }
         return status;
@@ -2307,8 +2325,8 @@ view.Node = class extends grapher.Node {
 
     toggle() {
         this._expand.content = '-';
-        this._target = new view.Graph(this.context.view, false);
-        this._target.add(this.value);
+        this.context.view.target = new view.Graph(this.context.view, false);
+        this.context.view.target.add(this.value);
         // const document = this.element.ownerDocument;
         // const parent = this.element.parentElement;
         // this._target.build(document, parent);

+ 3 - 0
test/worker.js

@@ -79,6 +79,9 @@ host.TestHost = class {
         return this._environment[name];
     }
 
+    update() {
+    }
+
     screen(/* name */) {
     }