Skip to content

Commit

Permalink
Merge pull request #1611 from koendehondt/gh1601-pharo12-fillWith
Browse files Browse the repository at this point in the history
Add SpMenuPresenter>>#fillWith: that applies for SpMenuBarPresenter as well, and add tests and example
  • Loading branch information
estebanlm authored Oct 4, 2024
2 parents 1e9b523 + d9c0d63 commit 5a2fe20
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/Spec2-Commander2/SpMenuBarPresenter.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Extension { #name : 'SpMenuBarPresenter' }

{ #category : '*Spec2-Commander2' }
SpMenuBarPresenter >> presenterBuilderClass [

^ SpMenuBarPresenterBuilder
]
16 changes: 16 additions & 0 deletions src/Spec2-Commander2/SpMenuPresenter.extension.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Extension { #name : 'SpMenuPresenter' }

{ #category : '*Spec2-Commander2' }
SpMenuPresenter >> fillWith: aCommandGroup [

self removeAllItems.
self presenterBuilderClass new
menuPresenter: self;
visit: aCommandGroup
]

{ #category : '*Spec2-Commander2' }
SpMenuPresenter >> presenterBuilderClass [

^ SpMenuPresenterBuilder
]
11 changes: 6 additions & 5 deletions src/Spec2-Commander2/SpMenuPresenterBuilder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ SpMenuPresenterBuilder >> fillItem: aMenuItem with: aCommand [
SpMenuPresenterBuilder >> initialize [

super initialize.
self menuPresenter: self class menuPresenterClass new.
stack := Stack new
push: self menuPresenter;
yourself
self menuPresenter: self class menuPresenterClass new
]

{ #category : 'accessing' }
Expand All @@ -50,7 +47,11 @@ SpMenuPresenterBuilder >> menuPresenter [

{ #category : 'accessing' }
SpMenuPresenterBuilder >> menuPresenter: anObject [
menuPresenter := anObject

menuPresenter := anObject.
stack := Stack new
push: self menuPresenter;
yourself
]

{ #category : 'visiting' }
Expand Down
6 changes: 6 additions & 0 deletions src/Spec2-Core/SpMenuPresenter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ SpMenuPresenter >> printOn: aStream [
nextPutAll: '''' ]
]

{ #category : 'private' }
SpMenuPresenter >> removeAllItems [

groups := OrderedCollection new
]

{ #category : 'accessing' }
SpMenuPresenter >> rootMenu [

Expand Down
118 changes: 118 additions & 0 deletions src/Spec2-Examples/SpCommandGroupExample.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"
I am an example presenter to show how commands can be used as the basis for adding a menubar and a toolbar.
"
Class {
#name : 'SpCommandGroupExample',
#superclass : 'SpPresenter',
#instVars : [
'menuBar',
'toolBar',
'list'
],
#category : 'Spec2-Examples-Demo-CommandGroup',
#package : 'Spec2-Examples',
#tag : 'Demo-CommandGroup'
}

{ #category : 'commands' }
SpCommandGroupExample class >> buildCommandsGroupWith: presenter forRoot: rootCommandGroup [

rootCommandGroup
register: (self buildMenuBarGroupWith: presenter);
register: (self buildToolBarGroupWith: presenter);
register: (self buildListMenuWith: presenter)
]

{ #category : 'commands' }
SpCommandGroupExample class >> buildListMenuWith: presenter [

^ (CmCommandGroup named: 'List') asSpecGroup
beRoot;
register: (SpExampleNewCommand forSpec context: presenter);
yourself
]

{ #category : 'commands' }
SpCommandGroupExample class >> buildMenuBarGroupWith: presenter [

^ (CmCommandGroup named: 'MenuBar') asSpecGroup
beRoot;
register: (self buildMenuWith: presenter);
yourself
]

{ #category : 'commands' }
SpCommandGroupExample class >> buildMenuWith: presenter [

^ (CmCommandGroup named: 'Menu') asSpecGroup
register: (SpExampleNewCommand forSpec context: presenter);
yourself
]

{ #category : 'commands' }
SpCommandGroupExample class >> buildToolBarGroupWith: presenter [

^ (CmCommandGroup named: 'ToolBar') asSpecGroup
beRoot;
register: (SpExampleNewCommand forSpec context: presenter);
yourself
]

{ #category : 'examples' }
SpCommandGroupExample class >> example [
"This example opens a presenter with a menubar and a toolbar created from commands."

^ self new open
]

{ #category : 'layout' }
SpCommandGroupExample >> defaultLayout [

^ SpBoxLayout newTopToBottom
add: list;
yourself
]

{ #category : 'initialization' }
SpCommandGroupExample >> initializeList [

| contextMenu |
list := self newList.
contextMenu := self newMenu.
contextMenu fillWith: self rootCommandsGroup / 'List'.
list contextMenu: contextMenu
]

{ #category : 'initialization' }
SpCommandGroupExample >> initializeMenuBar [

menuBar := self newMenuBar.
menuBar fillWith: self rootCommandsGroup / 'MenuBar'
]

{ #category : 'initialization' }
SpCommandGroupExample >> initializePresenters [

super initializePresenters.
self initializeList.
self initializeMenuBar.
self initializeToolBar
]

{ #category : 'initialization' }
SpCommandGroupExample >> initializeToolBar [

toolBar := self newToolbar.
toolBar fillWith: self rootCommandsGroup / 'ToolBar'
]

{ #category : 'initialization' }
SpCommandGroupExample >> initializeWindow: aWindowPresenter [

super initializeWindow: aWindowPresenter.
aWindowPresenter
title: 'Example with context menu, menubar and toolbar based on commands';
initialExtent: 600@300;
menu: menuBar;
toolbar: toolBar
]
31 changes: 31 additions & 0 deletions src/Spec2-Examples/SpExampleNewCommand.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Class {
#name : 'SpExampleNewCommand',
#superclass : 'CmCommand',
#category : 'Spec2-Examples-Demo-CommandGroup',
#package : 'Spec2-Examples',
#tag : 'Demo-CommandGroup'
}

{ #category : 'converting' }
SpExampleNewCommand >> asSpecCommand [

^ super asSpecCommand
iconName: #smallNew;
shortcutKey: $n meta;
yourself
]

{ #category : 'executing' }
SpExampleNewCommand >> execute [

self inform: 'This command is not implemented yet.'
]

{ #category : 'initialization' }
SpExampleNewCommand >> initialize [

super initialize.
self
name: 'New';
description: 'Create a new thing'
]
34 changes: 34 additions & 0 deletions src/Spec2-Tests/SpMenuBarPresenterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,37 @@ Class {
#package : 'Spec2-Tests',
#tag : 'Core-Widgets'
}

{ #category : 'accessing' }
SpMenuBarPresenterTest >> classToTest [
^ SpMenuBarPresenter
]

{ #category : 'tests' }
SpMenuBarPresenterTest >> testFillWith [

| menuCommandGroup menuBarCommandGroup menuBarGroup command menuItem menu topMenu menuGroup |
command := CmCommand new
name: 'Test';
context: presenter;
asSpecCommand.
menuCommandGroup := (CmCommandGroup named: 'Menu') asSpecGroup
register: command;
yourself.
menuBarCommandGroup := (CmCommandGroup named: 'MenuBar') asSpecGroup
beRoot;
register: menuCommandGroup;
yourself.
presenter fillWith: menuBarCommandGroup.
self assert: presenter menuGroups size equals: 1.
menuBarGroup := presenter menuGroups first.
self assert: menuBarGroup menuItems size equals: 1.
topMenu := menuBarGroup menuItems first.
self assert: topMenu name equals: 'Menu'.
menu := topMenu subMenu.
self assert: menu menuGroups size equals: 1.
menuGroup := menu menuGroups first.
self assert: menuGroup menuItems size equals: 1.
menuItem := menuGroup menuItems first.
self assert: menuItem name equals: 'Test'
]
20 changes: 20 additions & 0 deletions src/Spec2-Tests/SpMenuPresenterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ SpMenuPresenterTest >> classToTest [
^ SpMenuPresenter
]

{ #category : 'tests' }
SpMenuPresenterTest >> testFillWith [

| menuCommandGroup command menuItem menuGroup |
command := CmCommand new
name: 'Test';
context: presenter;
asSpecCommand.
menuCommandGroup := (CmCommandGroup named: 'Menu') asSpecGroup
beRoot;
register: command;
yourself.
presenter fillWith: menuCommandGroup.
self assert: presenter menuGroups size equals: 1.
menuGroup := presenter menuGroups first.
self assert: menuGroup menuItems size equals: 1.
menuItem := menuGroup menuItems first.
self assert: menuItem name equals: 'Test'
]

{ #category : 'tests' }
SpMenuPresenterTest >> testValue [

Expand Down

0 comments on commit 5a2fe20

Please sign in to comment.