From 09c141fa30341ca270b7664a6662e581f74d5ed4 Mon Sep 17 00:00:00 2001 From: Pierre Laborde <49183340+labordep@users.noreply.github.com> Date: Sun, 3 Sep 2023 17:46:52 +0200 Subject: [PATCH] Fix #61, refactoring, naming and minor fixes --- ...midRemoveSelectedElementsCommand.class.st} | 30 +++++++++------- .../PyramidSelectionMakerExtension.class.st | 2 +- .../PyramidSelectionWidgetExtension.class.st | 24 +++++++------ src/Pyramid-Bloc/PyramidTreePlugin.class.st | 23 ++++++------ .../PyramidTreePresenter.class.st | 7 ++-- ...PyramidRemoveSelectionCommandTest.class.st | 2 +- src/Pyramid/PyramidProjectModel.class.st | 35 ++++++++++++------- src/Pyramid/PyramidSelectionView.class.st | 13 ++++--- 8 files changed, 78 insertions(+), 58 deletions(-) rename src/Pyramid-Bloc/{PyramidRemoveSelectionCommand.class.st => PyramidRemoveSelectedElementsCommand.class.st} (60%) diff --git a/src/Pyramid-Bloc/PyramidRemoveSelectionCommand.class.st b/src/Pyramid-Bloc/PyramidRemoveSelectedElementsCommand.class.st similarity index 60% rename from src/Pyramid-Bloc/PyramidRemoveSelectionCommand.class.st rename to src/Pyramid-Bloc/PyramidRemoveSelectedElementsCommand.class.st index fe7af48e..2beae596 100644 --- a/src/Pyramid-Bloc/PyramidRemoveSelectionCommand.class.st +++ b/src/Pyramid-Bloc/PyramidRemoveSelectedElementsCommand.class.st @@ -1,5 +1,5 @@ Class { - #name : #PyramidRemoveSelectionCommand, + #name : #PyramidRemoveSelectedElementsCommand, #superclass : #PyramidAbstractBlocCommand, #instVars : [ 'mementoInverse' @@ -8,13 +8,13 @@ Class { } { #category : #'as yet unclassified' } -PyramidRemoveSelectionCommand >> getValueFor: anElementOfSelection [ +PyramidRemoveSelectedElementsCommand >> getValueFor: anElementOfSelection [ ^ nil ] { #category : #'as yet unclassified' } -PyramidRemoveSelectionCommand >> mementoFor: anElement withArguments: anArgument [ +PyramidRemoveSelectedElementsCommand >> mementoFor: anElement withArguments: anArgument [ (anArgument includes: anElement) ifTrue: [ ^ PyramidCommandMemento new @@ -30,26 +30,26 @@ PyramidRemoveSelectionCommand >> mementoFor: anElement withArguments: anArgument ] { #category : #accessing } -PyramidRemoveSelectionCommand >> mementoInverse [ +PyramidRemoveSelectedElementsCommand >> mementoInverse [ ^ mementoInverse ] { #category : #accessing } -PyramidRemoveSelectionCommand >> mementoInverse: anObject [ +PyramidRemoveSelectedElementsCommand >> mementoInverse: anObject [ mementoInverse := anObject ] { #category : #'as yet unclassified' } -PyramidRemoveSelectionCommand >> saveStatesOf: aCollection with: aRoots [ +PyramidRemoveSelectedElementsCommand >> saveStatesOf: aCollection with: aRoots [ "We ignore the state recovery for the redo, we will force the inverse of the undo state." ^ self mementoInverse accept: PyramidMementoInverser new. ] { #category : #'as yet unclassified' } -PyramidRemoveSelectionCommand >> saveStatesWithCommandInverseOf: aCollection with: aRoots [ +PyramidRemoveSelectedElementsCommand >> saveStatesWithCommandInverseOf: aCollection with: aRoots [ | mementos finalMemento | mementos := aCollection asArray collect: [ :anElement | @@ -65,10 +65,16 @@ PyramidRemoveSelectionCommand >> saveStatesWithCommandInverseOf: aCollection wit ] { #category : #'as yet unclassified' } -PyramidRemoveSelectionCommand >> setValueFor: anElementOfSelection with: aRoot [ +PyramidRemoveSelectedElementsCommand >> setValueFor: aBlElementToRemove with: aRoot [ + "Remove the element from its parent" - (aRoot includes: anElementOfSelection) ifTrue: [ - aRoot remove: anElementOfSelection. - ^ self ]. - anElementOfSelection parent removeChild: anElementOfSelection + "When the parent is the root" + (aRoot includes: aBlElementToRemove) ifTrue: [ + aRoot remove: aBlElementToRemove. + ^ self + ]. + + "When the parent is not the root" + aBlElementToRemove hasParent ifFalse:[ ^ self ]. + aBlElementToRemove parent removeChild: aBlElementToRemove ] diff --git a/src/Pyramid-Bloc/PyramidSelectionMakerExtension.class.st b/src/Pyramid-Bloc/PyramidSelectionMakerExtension.class.st index 6eb4e111..79289cfa 100644 --- a/src/Pyramid-Bloc/PyramidSelectionMakerExtension.class.st +++ b/src/Pyramid-Bloc/PyramidSelectionMakerExtension.class.st @@ -35,7 +35,7 @@ PyramidSelectionMakerExtension >> computeExclusifSelection: aCollection [ PyramidSelectionMakerExtension >> computeNormalSelection: aCollection [ self projectModel ifNil: [ ^ self ]. - self projectModel selection replaceAll: aCollection + self projectModel setSelection: aCollection ] { #category : #'as yet unclassified' } diff --git a/src/Pyramid-Bloc/PyramidSelectionWidgetExtension.class.st b/src/Pyramid-Bloc/PyramidSelectionWidgetExtension.class.st index 72ca5d62..a1f4faff 100644 --- a/src/Pyramid-Bloc/PyramidSelectionWidgetExtension.class.st +++ b/src/Pyramid-Bloc/PyramidSelectionWidgetExtension.class.st @@ -28,7 +28,7 @@ PyramidSelectionWidgetExtension >> centerDragEnd: anEvent [ self widgetElement transformDo: [ :t | t translateBy: 0 @ 0 ]. self widgetElement childrenDo: [ :child | child - childNamed: #drag_ghost + childWithId: #drag_ghost ifFound: [ :ghost | ghost visibility: BlVisibility hidden ] ] ] @@ -50,7 +50,10 @@ PyramidSelectionWidgetExtension >> centerDragStart: anEvent [ self isDragging: true. self dragOrigin: anEvent position. anEvent consumed: true. - self widgetElement childrenDo: [ :child | child childNamed: #drag_ghost ifFound: [ :ghost | ghost visibility: BlVisibility visible ] ] + self widgetElement childrenDo: [ :child | + child + childWithId: #drag_ghost + ifFound: [ :ghost | ghost visibility: BlVisibility visible ] ] ] { #category : #accessing } @@ -189,18 +192,10 @@ PyramidSelectionWidgetExtension >> projectModel: aProjectModel [ self makeNewSelection. ] -{ #category : #events } -PyramidSelectionWidgetExtension >> pyramidElementsChanged [ - - self removeSelection. - self makeNewSelection -] - { #category : #events } PyramidSelectionWidgetExtension >> pyramidSelectionChanged [ - self removeSelection. - self makeNewSelection + self updateSelection ] { #category : #'selection element' } @@ -225,6 +220,13 @@ PyramidSelectionWidgetExtension >> removeSelection [ self widgetElement removeChildren. ] +{ #category : #selection } +PyramidSelectionWidgetExtension >> updateSelection [ + + self removeSelection. + self makeNewSelection +] + { #category : #'as yet unclassified' } PyramidSelectionWidgetExtension >> usePositionOffsetCommand: aPosition [ diff --git a/src/Pyramid-Bloc/PyramidTreePlugin.class.st b/src/Pyramid-Bloc/PyramidTreePlugin.class.st index a74e61b1..0da26612 100644 --- a/src/Pyramid-Bloc/PyramidTreePlugin.class.st +++ b/src/Pyramid-Bloc/PyramidTreePlugin.class.st @@ -201,7 +201,7 @@ PyramidTreePlugin >> menuAddAndDeleteOn: aBuilder [ addGroupEmptySelection: [ :group :empty | group addItem: [ :anItem | anItem - name: 'Add new element'; + name: 'Add element...'; help: 'Add a new element on the roots of the design.'; icon: (self iconNamed: #add); action: [ @@ -215,7 +215,7 @@ PyramidTreePlugin >> menuAddAndDeleteOn: aBuilder [ addGroupSingleSelection: [ :group :single | group addItem: [ :anItem | anItem - name: 'Add new element'; + name: 'Add child element...'; help: 'Add a new element as a child of the selected element.'; icon: (self iconNamed: #add); action: [ @@ -225,11 +225,11 @@ PyramidTreePlugin >> menuAddAndDeleteOn: aBuilder [ position: SpPopoverPosition right) popup ] ]. group addItem: [ :anItem | anItem - name: 'Remove selection'; + name: 'Remove element'; help: - 'Delete the selected element and its children from the design.'; + 'Delete the selected element and its content'; icon: (self iconNamed: #delete); - action: [ self removeSelection ] ] ] + action: [ self removeSelectedElements ] ] ] order: 10. aBuilder @@ -238,19 +238,22 @@ PyramidTreePlugin >> menuAddAndDeleteOn: aBuilder [ anItem name: 'Remove selection'; help: - 'Delete all selected elements and their children from the design.'; + 'Delete all selected elements and their contents'; icon: (self iconNamed: #delete); - action: [ self removeSelection ] ] ] + action: [ self removeSelectedElements ] ] ] order: 10 ] { #category : #removing } -PyramidTreePlugin >> removeSelection [ +PyramidTreePlugin >> removeSelectedElements [ self editor propertiesManager commandExecutor - use: PyramidRemoveSelectionCommand new + use: PyramidRemoveSelectedElementsCommand new on: self editor projectModel selection - with: self editor projectModel roots + with: self editor projectModel roots. + + "Update the selection after remove" + self editor projectModel updateSelection. ] { #category : #accessing } diff --git a/src/Pyramid-Bloc/PyramidTreePresenter.class.st b/src/Pyramid-Bloc/PyramidTreePresenter.class.st index 7a80e832..0dc85a12 100644 --- a/src/Pyramid-Bloc/PyramidTreePresenter.class.st +++ b/src/Pyramid-Bloc/PyramidTreePresenter.class.st @@ -35,10 +35,7 @@ PyramidTreePresenter >> actionSelectionChanged: aCollection [ self projectModel ifNil: [ ^ self ]. self shouldUpdateSelection ifFalse: [ ^ self ]. self shouldUpdateSelection: false. - [ - self projectModel selection replaceAll: - aCollection selectedItems ] ensure: [ - self shouldUpdateSelection: true ] + [self projectModel setSelection: aCollection selectedItems ] ensure: [ self shouldUpdateSelection: true ] ] { #category : #accessing } @@ -194,7 +191,7 @@ PyramidTreePresenter >> updateSelection [ [ self tree unselectAll. self projectModel selection - ifNotEmpty: [ self tree selectItems: self projectModel selection ] ] + ifNotEmpty: [ :e | self tree selectItems: e ] ] ensure: [ self shouldUpdateSelection: true ] ] diff --git a/src/Pyramid-Tests/PyramidRemoveSelectionCommandTest.class.st b/src/Pyramid-Tests/PyramidRemoveSelectionCommandTest.class.st index 2c3557ce..18aa0bb1 100644 --- a/src/Pyramid-Tests/PyramidRemoveSelectionCommandTest.class.st +++ b/src/Pyramid-Tests/PyramidRemoveSelectionCommandTest.class.st @@ -76,7 +76,7 @@ PyramidRemoveSelectionCommandTest >> arraysToTest [ { #category : #accessing } PyramidRemoveSelectionCommandTest >> command [ - ^ PyramidRemoveSelectionCommand new + ^ PyramidRemoveSelectedElementsCommand new ] { #category : #'as yet unclassified' } diff --git a/src/Pyramid/PyramidProjectModel.class.st b/src/Pyramid/PyramidProjectModel.class.st index fdee5061..3ce7ec45 100644 --- a/src/Pyramid/PyramidProjectModel.class.st +++ b/src/Pyramid/PyramidProjectModel.class.st @@ -23,28 +23,37 @@ PyramidProjectModel >> initialize [ selection := PyramidCollectionWithCallbacks new. roots := PyramidSortedCollectionWithCallbacks new sortFunction: PyramidElevationSortFunction new; yourself. - selection - subscribe: self - withCallback: [ self informSelectionChanged ]. + selection subscribe: self withCallback: [ self informSelectionChanged ]. roots subscribe: self withCallback: [ self informRootsChanged ] ] -{ #category : #removing } -PyramidProjectModel >> removeSelection [ - - (self selection asArray difference: self roots asArray) do: [ :each | - each parent ifNotNil: [ :p | p removeChild: each ] ]. - self roots removeAllFoundIn: self selection asArray. - self selection removeAll -] - { #category : #accessing } PyramidProjectModel >> roots [ ^ roots ] -{ #category : #accessing } +{ #category : #selection } PyramidProjectModel >> selection [ ^ selection ] + +{ #category : #selection } +PyramidProjectModel >> setSelection: aBlElementCollection [ + "Replace current selection by another one" + + self selection replaceAll: aBlElementCollection +] + +{ #category : #selection } +PyramidProjectModel >> updateSelection [ + "Update the selection list regarding existing elements" + + | toBeRemovedFromSelectionElements | + "Select not attached to scene or without parents elements (this is the status after a deletion)" + toBeRemovedFromSelectionElements := self selection collection select:[ :e | e hasParent not or:[ e isAttachedToSceneGraph ] ]. + + self selection removeAll: toBeRemovedFromSelectionElements. + + +] diff --git a/src/Pyramid/PyramidSelectionView.class.st b/src/Pyramid/PyramidSelectionView.class.st index 8ffaae09..12b3c74b 100644 --- a/src/Pyramid/PyramidSelectionView.class.st +++ b/src/Pyramid/PyramidSelectionView.class.st @@ -74,12 +74,15 @@ PyramidSelectionView >> pyramidSelectionChanged [ { #category : #'as yet unclassified' } PyramidSelectionView >> updateSelection [ - self list items: self currentSelection. - self currentSelection ifEmpty: [ + | currentSelection | + currentSelection := self currentSelection. + + self list items: currentSelection. + currentSelection ifEmpty: [ self connectPlaygroundTo: nil. ^ self ]. - self currentSelection size = 1 ifTrue: [ - self connectPlaygroundTo: self currentSelection first. + currentSelection size = 1 ifTrue: [ + self connectPlaygroundTo: currentSelection first. ^ self ]. - self connectPlaygroundTo: self currentSelection + self connectPlaygroundTo: currentSelection ]