Skip to content

Commit

Permalink
Recover a refactoring around button
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchodias committed Aug 4, 2023
1 parent 811c89b commit 1d63109
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 27 deletions.
90 changes: 90 additions & 0 deletions src/Spec-Toplo/SpToploButton.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,93 @@ Class {
],
#category : #'Spec-Toplo-Widgets'
}

{ #category : #'class initialization' }
SpToploButton class >> initialize [

NullIcon := Form extent: 0 asPoint
]

{ #category : #'t - initialization dresser' }
SpToploButton >> defaultDresser [

^ SpToploButtonDresser new
]

{ #category : #accessing }
SpToploButton >> hasIcon [

^ imageElement innerImage == NullIcon
]

{ #category : #accessing }
SpToploButton >> iconForm [

^ self hasIcon
ifTrue: [ nil ]
ifFalse: [ imageElement innerImage ]
]

{ #category : #accessing }
SpToploButton >> iconForm: aForm [

imageElement innerImage: aForm.
imageElement visibility: BlVisibility visible
]

{ #category : #accessing }
SpToploButton >> iconFormOrNil: aFormOrNil [

aFormOrNil
ifNil: [ self setNoIcon ]
ifNotNil: [ self iconForm: aFormOrNil ]
]

{ #category : #initialization }
SpToploButton >> initialize [

| pane |
super initialize.

imageElement := ToImage new.
labelElement := ToLabel new.

pane := ToPane new.
pane
fitContent;
addChild: imageElement as: #image;
addChild: labelElement as: #label.
pane layout alignCenter.

self
matchParent;
addChild: pane.
self layout alignCenter.

self setNoIcon
]

{ #category : #accessing }
SpToploButton >> labelElement [

^ labelElement
]

{ #category : #accessing }
SpToploButton >> labelString [

^ labelElement text asString
]

{ #category : #accessing }
SpToploButton >> labelString: aString [

labelElement text: aString asRopedText
]

{ #category : #accessing }
SpToploButton >> setNoIcon [

self iconForm: NullIcon.
imageElement visibility: BlVisibility hidden
]
32 changes: 5 additions & 27 deletions src/Spec-Toplo/SpToploButtonAdapter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ Class {
{ #category : #factory }
SpToploButtonAdapter >> buildWidget [

| pane |
pane := ToPane new
fitContent;
addChild: ToImage new as: #image;
addChild: ToLabel new as: #label.
pane layout alignCenter.

^ ToGeneralButton new
dresser: ToGeneralButtonDresser new;
matchParent;
addChild: pane;
in: [ :b | b layout alignCenter ];
^ SpToploButton new
addEventHandler: (BlEventHandler
on: BlClickEvent
do: [ model performAction ]);
Expand All @@ -38,13 +27,13 @@ SpToploButtonAdapter >> buildWidget [
{ #category : #accessing }
SpToploButtonAdapter >> icon [

^ ((widget childAt: 1) childWithId: #image) innerImage
^ widget iconForm
]

{ #category : #accessing }
SpToploButtonAdapter >> label [

^ ((widget childAt: 1) childWithId: #label) text asString
^ widget labelString
]

{ #category : #factory }
Expand All @@ -53,12 +42,6 @@ SpToploButtonAdapter >> newContextMenu [
^ model contextMenu ifNotNil: #build
]

{ #category : #factory }
SpToploButtonAdapter >> nullIcon [

^ nullIcon ifNil: [ nullIcon := Form extent: 0 asPoint ]
]

{ #category : #factory }
SpToploButtonAdapter >> subscribeToPresenter [

Expand Down Expand Up @@ -88,18 +71,13 @@ SpToploButtonAdapter >> updateAll [
{ #category : #factory }
SpToploButtonAdapter >> updateIcon [

| iconForm image |
iconForm := self presenter icon ifNil: [ self nullIcon ].
image := (widget childAt: 1) childWithId: #image.
image innerImage: iconForm
widget iconFormOrNil: model icon
]

{ #category : #factory }
SpToploButtonAdapter >> updateLabel [

| label |
label := (widget childAt: 1) childWithId: #label.
label text: model label
widget labelString: model label
]

{ #category : #updating }
Expand Down
46 changes: 46 additions & 0 deletions src/Spec-Toplo/SpToploButtonDresser.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"
I'm a dresser for `SpToploButton`.
"
Class {
#name : #SpToploButtonDresser,
#superclass : #ToGeneralButtonDresser,
#instVars : [
'enabledHandler',
'disabledHandler'
],
#category : #'Spec-Toplo-Widgets'
}

{ #category : #private }
SpToploButtonDresser >> applyEnabledTo: aToGeneralButton [

aToGeneralButton labelElement text foreground: (aToGeneralButton isEnabled
ifTrue: [ theme button defaultTextColor ]
ifFalse: [ theme button disabledTextColor ])
]

{ #category : #'api - install/uninstall hook' }
SpToploButtonDresser >> installEventListenersIn: anElement [

super installEventListenersIn: anElement.

anElement labelElement whenTextReplacedDo: [
self applyEnabledTo: anElement ].

enabledHandler := BlEventHandler
on: ToWidgetEnabledEvent
do: [ :event | self applyEnabledTo: anElement ].
disabledHandler := BlEventHandler
on: ToWidgetDisabledEvent
do: [ :event | self applyEnabledTo: anElement ].
anElement addEventHandler: enabledHandler.
anElement addEventHandler: disabledHandler
]

{ #category : #'api - install/uninstall hook' }
SpToploButtonDresser >> uninstallEventListenersIn: anElement [

super uninstallEventListenersIn: anElement.
anElement removeEventHandler: enabledHandler.
anElement removeEventHandler: disabledHandler
]

0 comments on commit 1d63109

Please sign in to comment.