From a677998f4794d90cc6328614c57bf395f78ca4f4 Mon Sep 17 00:00:00 2001
From: Jonathan Haas <jonathan.haas@live.de>
Date: Mon, 22 Mar 2021 18:49:39 +0100
Subject: [PATCH] Don't select new items after cancelling move following item
 duplication

Fixes https://gitlab.com/kicad/code/kicad/issues/7956
---
 pcbnew/tools/edit_tool.cpp | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp
index 31deaab2cf..cc84418eac 100644
--- a/pcbnew/tools/edit_tool.cpp
+++ b/pcbnew/tools/edit_tool.cpp
@@ -932,7 +932,8 @@ int EDIT_TOOL::doMoveSelection( TOOL_EVENT aEvent, bool aPickReference )
         m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
 
     editFrame->PopTool( tool );
-    return 0;
+
+    return restore_state ? -1 : 0;
 }
 
 
@@ -2086,13 +2087,16 @@ int EDIT_TOOL::Duplicate( const TOOL_EVENT& aEvent )
         // If items were duplicated, pick them up
         // this works well for "dropping" copies around and pushes the commit
         TOOL_EVENT evt = PCB_ACTIONS::move.MakeEvent();
-        Move( evt );
+        bool       move_cancelled = Move( evt ) == -1;
 
         // After moving the new items, we need to refresh the group and view flags
         m_toolMgr->RunAction( PCB_ACTIONS::selectionClear, true );
 
-        if( !is_hover )
+        if( !is_hover && !move_cancelled )
+        {
+            // Do not select new items if they've been deleted again by cancelling Move()
             m_toolMgr->RunAction( PCB_ACTIONS::selectItems, true, &new_items );
+        }
     }
 
     return 0;