From c16272924c511e93a1ff87102d142a434b9a2556 Mon Sep 17 00:00:00 2001
From: David Sehnal <david.sehnal@gmail.com>
Date: Sat, 2 Mar 2019 16:37:55 +0100
Subject: [PATCH] mol-plugin: clear selection on Ctrl + right on nothing

---
 src/mol-plugin/behavior/dynamic/representation.ts  |  8 +++++++-
 src/mol-plugin/util/structure-element-selection.ts | 13 +++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/src/mol-plugin/behavior/dynamic/representation.ts b/src/mol-plugin/behavior/dynamic/representation.ts
index c3df52b80..c9126e2d0 100644
--- a/src/mol-plugin/behavior/dynamic/representation.ts
+++ b/src/mol-plugin/behavior/dynamic/representation.ts
@@ -73,7 +73,13 @@ export const SelectLoci = PluginBehavior.create({
             this.subscribeObservable(this.ctx.events.canvas3d.click, ({ current, buttons, modifiers }) => {
                 if (!this.ctx.canvas3d) return;
 
-                if (StructureElement.isLoci(current.loci)) {
+                if (current.loci.kind === 'empty-loci') {
+                    if (modifiers.control && buttons === ButtonsType.Flag.Secondary) {
+                        // clear the selection on Ctrl + Right-Click on empty
+                        const sels = sel.clear();
+                        for (const s of sels) this.ctx.canvas3d.mark({ loci: s }, MarkerAction.Deselect);
+                    }
+                } else if (StructureElement.isLoci(current.loci)) {
                     if (modifiers.control && buttons === ButtonsType.Flag.Secondary) {
                         // select only the current element on Ctrl + Right-Click
                         const old = sel.get(current.loci.structure);
diff --git a/src/mol-plugin/util/structure-element-selection.ts b/src/mol-plugin/util/structure-element-selection.ts
index a070ec298..f990cfa79 100644
--- a/src/mol-plugin/util/structure-element-selection.ts
+++ b/src/mol-plugin/util/structure-element-selection.ts
@@ -50,6 +50,19 @@ class StructureElementSelectionManager {
         return entry.selection.elements.length === 0 ? EmptyLoci : entry.selection;
     }
 
+    clear() {
+        const keys = this.entries.keys();
+        const selections: StructureElement.Loci[] = [];
+        while (true) {
+            const k = keys.next();
+            if (k.done) break;
+            const s = this.entries.get(k.value)!;
+            if (s.selection.elements.length > 0) selections.push(s.selection);
+            s.selection = StructureElement.Loci(s.selection.structure, []);
+        }
+        return selections;
+    }
+
     get(structure: Structure) {
         const entry = this.getEntry(structure);
         if (!entry) return EmptyLoci;
-- 
GitLab