-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from LABSARI/main
Adding a builder for menu items so one can define its own menu items.
- Loading branch information
Showing
5 changed files
with
167 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Hierarchical-Roassal3-Menu/HDefaultMenuItemStrategy.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
" | ||
I am a strategy for HMenuBuilder. | ||
I define menu items that can be executed (or needed) for a given object. | ||
" | ||
Class { | ||
#name : #HDefaultMenuItemStrategy, | ||
#superclass : #Object, | ||
#category : #'Hierarchical-Roassal3-Menu' | ||
} | ||
|
||
{ #category : #public } | ||
HDefaultMenuItemStrategy >> classFor: aRoassalShape [ | ||
aRoassalShape isShape | ||
ifFalse: [ ^ HCanvasMenuItem ]. | ||
^ aRoassalShape isNode | ||
ifTrue: [ | ||
(aRoassalShape canvas selectedShapes notEmpty | ||
and: [ (aRoassalShape canvas selectedShapes includes: aRoassalShape)]) | ||
ifTrue: [ HGroupMenuItem ] | ||
ifFalse: [ HNodeMenuItem ] ] | ||
ifFalse: [ HLineMenuItem ] | ||
|
||
] | ||
|
||
{ #category : #public } | ||
HDefaultMenuItemStrategy >> menuItemClassesFor: aShape [ | ||
|
||
^ (self classFor: aShape) subclasses , { HInspectorMenuItem } | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
" | ||
I build a roassal menu given a shape in a visualization. | ||
Entry point is `buildIn: aMorph shape: aShape visualization: aCanvas` | ||
Items in the menu are provided by the method `menuItemsFor:` | ||
" | ||
Class { | ||
#name : #HMenuBuilder, | ||
#superclass : #Object, | ||
#instVars : [ | ||
'menuItemStrategy' | ||
], | ||
#category : #'Hierarchical-Roassal3-Menu' | ||
} | ||
|
||
{ #category : #public } | ||
HMenuBuilder >> buildIn: aMenuMorph shape: aRoassalShape visualization: anHSimpleVisualizationBuilder [ | ||
|
||
| items | | ||
items := self groupItems: (self menuItemsFor: aRoassalShape). | ||
|
||
self | ||
buildIn: aMenuMorph | ||
shape: aRoassalShape | ||
visualization: anHSimpleVisualizationBuilder | ||
withItems: items | ||
] | ||
|
||
{ #category : #public } | ||
HMenuBuilder >> buildIn: aMenuMorph shape: aRoassalShape visualization: anHSimpleVisualizationBuilder withItems: items [ | ||
|
||
items keys sorted | ||
do: [ :key | | ||
| group | | ||
group := (items at: key) sorted: [ :a :b | a order < b order ]. | ||
group do: [ :item | | ||
item | ||
shape: aRoassalShape; | ||
visualization: anHSimpleVisualizationBuilder; | ||
buildOn: aMenuMorph ] ] | ||
separatedBy: [ aMenuMorph addSeparator ] | ||
] | ||
|
||
{ #category : #public } | ||
HMenuBuilder >> groupItems: items [ | ||
|
||
^ items groupedBy: [ :item | item group ] | ||
] | ||
|
||
{ #category : #initialization } | ||
HMenuBuilder >> initialize [ | ||
|
||
super initialize. | ||
menuItemStrategy := HDefaultMenuItemStrategy new | ||
] | ||
|
||
{ #category : #accessing } | ||
HMenuBuilder >> menuItemStrategy [ | ||
|
||
^ menuItemStrategy | ||
] | ||
|
||
{ #category : #accessing } | ||
HMenuBuilder >> menuItemStrategy: anObject [ | ||
|
||
menuItemStrategy := anObject | ||
] | ||
|
||
{ #category : #public } | ||
HMenuBuilder >> menuItemsFor: aShape [ | ||
|
||
^ (menuItemStrategy menuItemClassesFor: aShape) collect: #new | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
Class { | ||
#name : #HMenuBuilderTest, | ||
#superclass : #TestCase, | ||
#instVars : [ | ||
'menuBuilder' | ||
], | ||
#category : #'Hierarchical-Roassal3-Tests' | ||
} | ||
|
||
{ #category : #running } | ||
HMenuBuilderTest >> setUp [ | ||
super setUp. | ||
|
||
menuBuilder := HMenuBuilder new. | ||
] | ||
|
||
{ #category : #tests } | ||
HMenuBuilderTest >> testMenuForCanvas [ | ||
|
||
| items | | ||
items := menuBuilder menuItemsFor: RSCanvas new. | ||
self assert: items size equals: HCanvasMenuItem subclasses size + 1. | ||
self assert: | ||
(items anySatisfy: [ :i | i class = HInspectorMenuItem ]) | ||
] | ||
|
||
{ #category : #tests } | ||
HMenuBuilderTest >> testMenuForGroup [ | ||
|
||
| items shapes canvas | | ||
canvas := RSCanvas new. | ||
shapes := { | ||
RSComposite new. | ||
RSComposite new }. | ||
canvas addAll: shapes. | ||
canvas selectedShapes: shapes. | ||
items := menuBuilder menuItemsFor: shapes first. | ||
self assert: items size equals: HGroupMenuItem subclasses size + 1. | ||
] | ||
|
||
{ #category : #tests } | ||
HMenuBuilderTest >> testMenuForLine [ | ||
|
||
| items | | ||
items := menuBuilder menuItemsFor: RSLine new. | ||
self assert: items size equals: HLineMenuItem subclasses size + 1. | ||
self assert: | ||
(items anySatisfy: [ :i | i class = HInspectorMenuItem ]) | ||
] | ||
|
||
{ #category : #tests } | ||
HMenuBuilderTest >> testMenuForNode [ | ||
|
||
| items canvas shape | | ||
canvas := RSCanvas new. | ||
shape := RSComposite new. | ||
canvas add: shape. | ||
items := menuBuilder menuItemsFor: shape. | ||
|
||
self assert: items size equals: HNodeMenuItem subclasses size + 1. | ||
self assert: | ||
(items anySatisfy: [ :i | i class = HInspectorMenuItem ]) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters