Selaa lähdekoodia

mol-plugin: refactored CameraSnapshotManager

David Sehnal 6 vuotta sitten
vanhempi
commit
0b12a4fa52
3 muutettua tiedostoa jossa 18 lisäystä ja 20 poistoa
  1. 2 1
      src/mol-plugin/state.ts
  2. 15 18
      src/mol-plugin/state/camera.ts
  3. 1 1
      src/mol-plugin/ui/camera.tsx

+ 2 - 1
src/mol-plugin/state.ts

@@ -22,7 +22,7 @@ class PluginState {
     readonly dataState: State;
     readonly behaviorState: State;
     readonly animation: PluginAnimationManager;
-    readonly cameraSnapshots = new CameraSnapshotManager();
+    readonly cameraSnapshots: CameraSnapshotManager;
 
     readonly snapshots = new PluginStateSnapshotManager();
 
@@ -89,6 +89,7 @@ class PluginState {
 
         this.behavior.currentObject.next(this.dataState.behaviors.currentObject.value);
 
+        this.cameraSnapshots = new CameraSnapshotManager(plugin);
         this.animation = new PluginAnimationManager(plugin);
     }
 }

+ 15 - 18
src/mol-plugin/state/camera.ts

@@ -7,57 +7,54 @@
 import { Camera } from 'mol-canvas3d/camera';
 import { OrderedMap } from 'immutable';
 import { UUID } from 'mol-util';
-import { RxEventHelper } from 'mol-util/rx-event-helper';
+import { PluginComponent } from 'mol-plugin/component';
+import { PluginContext } from 'mol-plugin/context';
 
 export { CameraSnapshotManager }
 
-class CameraSnapshotManager {
-    private ev = RxEventHelper.create();
-    private _entries = OrderedMap<string, CameraSnapshotManager.Entry>().asMutable();
-
+class CameraSnapshotManager extends PluginComponent<{ entries: OrderedMap<string, CameraSnapshotManager.Entry> }> {
     readonly events = {
         changed: this.ev()
     };
 
-    get entries() { return this._entries; }
-
     getEntry(id: string) {
-        return this._entries.get(id);
+        return this.state.entries.get(id);
     }
 
     remove(id: string) {
-        if (!this._entries.has(id)) return;
-        this._entries.delete(id);
+        if (!this.state.entries.has(id)) return;
+        this.updateState({ entries: this.state.entries.delete(id) });
         this.events.changed.next();
     }
 
     add(e: CameraSnapshotManager.Entry) {
-        this._entries.set(e.id, e);
+        this.updateState({ entries: this.state.entries.set(e.id, e) });
         this.events.changed.next();
     }
 
     clear() {
-        if (this._entries.size === 0) return;
-        this._entries = OrderedMap<string, CameraSnapshotManager.Entry>().asMutable();
+        if (this.state.entries.size === 0) return;
+        this.updateState({ entries: OrderedMap<string, CameraSnapshotManager.Entry>() });
         this.events.changed.next();
     }
 
     getStateSnapshot(): CameraSnapshotManager.StateSnapshot {
         const entries: CameraSnapshotManager.Entry[] = [];
-        this._entries.forEach(e => entries.push(e!));
+        this.state.entries.forEach(e => entries.push(e!));
         return { entries };
     }
 
     setStateSnapshot(state: CameraSnapshotManager.StateSnapshot ) {
-        this._entries = OrderedMap<string, CameraSnapshotManager.Entry>().asMutable();
+        const entries = OrderedMap<string, CameraSnapshotManager.Entry>().asMutable();
         for (const e of state.entries) {
-            this._entries.set(e.id, e);
+            entries.set(e.id, e);
         }
+        this.updateState({ entries: entries.asImmutable() });
         this.events.changed.next();
     }
 
-    dispose() {
-        this.ev.dispose();
+    constructor(ctx: PluginContext) {
+        super(ctx, { entries: OrderedMap<string, CameraSnapshotManager.Entry>() });
     }
 }
 

+ 1 - 1
src/mol-plugin/ui/camera.tsx

@@ -65,7 +65,7 @@ class CameraSnapshotList extends PluginUIComponent<{ }, { }> {
 
     render() {
         return <ul style={{ listStyle: 'none' }} className='msp-state-list'>
-            {this.plugin.state.cameraSnapshots.entries.valueSeq().map(e =><li key={e!.id}>
+            {this.plugin.state.cameraSnapshots.state.entries.valueSeq().map(e =><li key={e!.id}>
                 <button className='msp-btn msp-btn-block msp-form-control' onClick={this.apply(e!.id)}>{e!.name || e!.timestamp} <small>{e!.description}</small></button>
                 <button onClick={this.remove(e!.id)} className='msp-btn msp-btn-link msp-state-list-remove-button'>
                     <span className='msp-icon msp-icon-remove' />