Skip to content

Commit 4dfcecb

Browse files
authored
Merge pull request #32 from Enzo-Demeulenaere/main
correcting "Building/skinning a widget" chapters according to feedback
2 parents f848b03 + 6ac19d9 commit 4dfcecb

File tree

2 files changed

+53
-89
lines changed

2 files changed

+53
-89
lines changed

Chapters/gettingStarted/buildingAWidget.md

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ space root addChild: anInput.
2020
space show.
2121
```
2222

23-
The main widget is composed of four elements: two buttons, a label, and a value.
24-
Let us detail these elements:
23+
The main widget is composed of four elements: two buttons (the circle with a "+" and the circle with the "-"), a label (that displays "input"), and a value (that displays "20"). Let us detail these elements:
2524
- the label and the value are text elements,
26-
- the two buttons are, in fact, a composite element composed of a circle element with a text element inside.
25+
- the two buttons are, in fact, a composite element composed of an element with a circle geometry, holding a text element inside.
2726

2827

2928
### Getting started
@@ -37,28 +36,15 @@ BlElement << #BlIntegerInputElement
3736
package: 'ABlocPackage'
3837
```
3938

40-
We start by defining the shape of the main element. Notice that visual properties such the background, the border could be stored differently to be customized afterwards.This is what we will show in a following chapter using stylesheet and skins as done in Toplo.
41-
42-
43-
```
44-
BlIntegerInputElement >> widgetExtent
45-
46-
^ 300@120
47-
```
48-
49-
```
50-
BlIntegerInputElement >> backgroundPaint
51-
52-
^ Color black
53-
```
39+
We start by defining the shape of the main element. Notice that visual properties such as the background or the border could be stored differently to be customized afterwards. This is what we will show in a following chapter using stylesheet and skins as done in Toplo.
5440

5541

5642
```
5743
BlIntegerInputElement >> initialize
5844
5945
super initialize.
60-
self size: self widgetExtent.
61-
self background: self backgroundPaint.
46+
self size: 300@120.
47+
self background: Color black.
6248
self geometry: (BlRoundedRectangleGeometry cornerRadius: 20).
6349
self layout: BlFrameLayout new.
6450
self border: (BlBorder paint: Color pink).
@@ -80,13 +66,13 @@ BlIntegerInputElement >> configuredString: aString
8066
^ aString asRopedText attributes: { (BlTextForegroundAttribute paint: Color white) }
8167
```
8268

83-
The `label:` method creates a `BlTextElement`, sets its text using properties and translates it to place it above the center.
69+
The `initializeInputLabel` method creates a `BlTextElement`, sets its text using properties and translates it to place it above the center.
8470

8571
```
86-
BlIntegerInputElement >> label: aString
72+
BlIntegerInputElement >> initializeInputLabel
8773
8874
inputLabel := BlTextElement new.
89-
inputLabel text: (self configuredString: aString).
75+
inputLabel text: (self configuredString: 'Input').
9076
inputLabel text fontSize: 25.
9177
inputLabel constraintsDo: [ :c |
9278
c frame horizontal alignCenter.
@@ -98,17 +84,17 @@ Note that we use `addChild:` method to add the text element in the composite (th
9884

9985
![With a label. % anchor=input1&width=50](figures/Input1.png)
10086

101-
We modify the initialize method to invoke the `label` method.
87+
We modify the initialize method to invoke the `initializeInputLabel` method.
10288
```
10389
BlIntegerInputElement >> initialize
10490
10591
super initialize.
106-
self size: self widgetExtent.
107-
self background: self backgroundPaint.
92+
self size: 300@120.
93+
self background: Color black.
10894
self geometry: (BlRoundedRectangleGeometry cornerRadius: 20).
10995
self layout: BlFrameLayout new.
11096
self border: (BlBorder paint: Color pink).
111-
self label: 'Input'
97+
self initializeInputlabel
11298
```
11399

114100
We should get now a widget similar to the one shown in Fig *@input1@*.
@@ -128,8 +114,7 @@ BlIntegerInputElement >> initializeInputValue: aValue
128114
self addChild: inputValue
129115
```
130116

131-
We define a little helper method `changeValueTo:` that we will expose as pubic API for example
132-
to start the input with a specific value.
117+
We define a little helper method `changeValueTo:` that we will expose as public API but we will also use this method internally later.
133118
Note again that we add the input value element in the composite one.
134119

135120
```
@@ -147,13 +132,14 @@ Then we change the `initialize` method to invoke `initializeInputValue:`.
147132
BlIntegerInputElement >> initialize
148133
149134
super initialize.
150-
self size: self widgetExtent.
151-
self background: self backgroundPaint.
135+
self size: 300@120.
136+
self background: Color black.
152137
self geometry: (BlRoundedRectangleGeometry cornerRadius: 20).
153138
self layout: BlFrameLayout new.
154139
self border: (BlBorder paint: Color pink).
155-
self initializeInputValue: 20.
156-
self label: 'Input'
140+
self initializeInputLabel.
141+
self initializeInputValue: 20
142+
157143
```
158144

159145
We should obtain now a widget similar to the one show in Fig *@input2@*.
@@ -221,14 +207,15 @@ the circle element to react to mouse-down events.
221207
BlIntegerInputElement >> initialize
222208
223209
super initialize.
224-
self size: self widgetExtent.
225-
self background: self backgroundPaint.
210+
self size: 300@120.
211+
self background: Color black.
226212
self geometry: (BlRoundedRectangleGeometry cornerRadius: 20).
227213
self layout: BlFrameLayout new.
228214
self border: (BlBorder paint: Color pink).
229-
self initializePlusButton.
215+
self initializeInputLabel.
230216
self initializeInputValue: 20.
231-
self label: 'Input'
217+
self initializePlusButton.
218+
232219
```
233220

234221
We should now get a widget similar to the one shown in Fig *@input3@*.
@@ -278,15 +265,15 @@ FInally we update the initialize method to call the minus creation.
278265
BlIntegerInputElement >> initialize
279266
280267
super initialize.
281-
self size: self widgetExtent.
282-
self background: self backgroundPaint.
268+
self size: 300@120.
269+
self background: Color black.
283270
self geometry: (BlRoundedRectangleGeometry cornerRadius: 20).
284271
self layout: BlFrameLayout new.
285272
self border: (BlBorder paint: Color pink).
286-
self initializePlusButton.
287-
self initializeMinusButton.
273+
self initializeInputLabel.
288274
self initializeInputValue: 20.
289-
self label: 'Input'
275+
self initializePlusButton.
276+
self initializeMinusButton
290277
```
291278

292279
Now we should have the full widget.
@@ -299,15 +286,19 @@ Any time the `inputValue` is changed we will value a block with the new value of
299286
First we initialize the callback block.
300287

301288
```
302-
BlIntegerInputElement >> initializeInputValue: aValue
289+
BlIntegerInputElement >> initialize.
303290
291+
super initialize.
292+
self size: 300@120.
293+
self background: Color black.
294+
self geometry: (BlRoundedRectangleGeometry cornerRadius: 20).
295+
self layout: BlFrameLayout new.
296+
self border: (BlBorder paint: Color pink).
297+
self initializeInputLabel.
298+
self initializeInputValue: 20.
299+
self initializePlusButton.
300+
self initializeMinusButton
304301
callbackBlock := [ :newInputValue | ].
305-
inputValue := BlTextElement new.
306-
inputValue constraintsDo: [ :c |
307-
c frame horizontal alignCenter.
308-
c frame vertical alignCenter ].
309-
self changeValueTo: aValue.
310-
self addChild: inputValue
311302
```
312303

313304
Then anytime we changed the state, we update the value.
@@ -321,10 +312,10 @@ BlIntegerInputElement >> changeValueTo: aValue
321312
callbackBlock value: aValue
322313
```
323314

324-
Finnaly we create a mutator for the `callbackBlock` variable.
315+
Finnaly we create a mutator for the `callbackBlock` variable called `whenInputValueChangedDo:`.
325316

326317
```
327-
BlIntegerInputElement >> callbackBlock: aBlock
318+
BlIntegerInputElement >> whenInputValueChangedDo: aBlock
328319
329320
callbackBlock := aBlock
330321
```
@@ -345,19 +336,6 @@ BlNumberInputElementTest >> testChildrenAreSet
345336
self assert: inputElem children size equals: 4
346337
```
347338

348-
Let us see a more interesting test.
349-
The following test shows that we can effectively changes the label.
350-
351-
```
352-
BlNumberInputElementTest >> testCanChangeLabel
353-
354-
| inputElem |
355-
inputElem := BlNumberInputElement new.
356-
self assert: inputElem label text asString equals: 'Input'.
357-
inputElem label: 'Volume'.
358-
self assert: inputElem label text asString equals: 'Volume'.
359-
```
360-
361339
The following tests are checking that the interaction on the buttons is working correctly.
362340
```
363341
BlNumberInputElementTest >> testValueUpdatedOnClick
@@ -391,7 +369,7 @@ BlNumberInputElementTest >> testCallbackCallOnClick
391369
testNumberOfCall := 0.
392370
testValue := -1.
393371
inputElem := BlNumberInputElement new.
394-
inputElem callbackBlock: [ :val |
372+
inputElem whenInputValueChangedDo: [ :val |
395373
testNumberOfCall := testNumberOfCall + 1.
396374
testValue := val.
397375
]

Chapters/toplo/skinningAWidget.md

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
In this chapter we show how we can take the simple input widget developed in a previous chapter and make it skinnable.
44

5-
In a first period, we will focus on what is called raw skins. The idea behind raw skins are that we define classes with states and methods to define the behavior of a skin in a given theme. At the end of this chapter we will see that we can also skin widgets
6-
using stylesheets following the spirit of CSS.
5+
In a first period, we will focus on what is called raw skins. The idea behind raw skins are that we define classes with states and methods to define the behavior of a skin in a given theme. At the end of this chapter we will see that we can also skin widgets using stylesheets following the spirit of CSS.
76

87
The outline of this chapter is then: first we show that we can extend a theme and define a skin.
98
Then in a second time we show that we can define an autonomous theme.
10-
Finally we will show that we can use stylesheet
9+
Finally we will show that we can use stylesheet.
1110

1211
Remember that we want to create a widget as shown in Figure *@inputFinalSkin@*.
1312

@@ -16,13 +15,12 @@ Remember that we want to create a widget as shown in Figure *@inputFinalSkin@*.
1615
### Getting started
1716

1817
If you implemented the widget as presented earlier, just copy the class giving it a new name for example `ToIntegerInputElement`.
19-
The definition of the `BlIntegerInputElement` is available SD:DefineAPlace.
2018

2119
The first thing that we should do is to make `ToIntegerInputElement` inherit from `ToElement` as follows:
2220

2321
```
2422
ToElement << #ToIntegerInputElement
25-
slots: { #plus . #minus . #inputValue . #value . #inputLabel };
23+
slots: { #plus . #minus . #inputValue . #value . #inputLabel. #callbackBlock };
2624
tag: 'Input';
2725
package: 'Bloc-Book'
2826
```
@@ -32,7 +30,7 @@ Our widget will then inherit the behavior to install a skin when instantiated, w
3230
### Define a skin
3331

3432
We define a skin by inheriting from `ToRawSkin`, this class defines methods reacting to some events.
35-
The class `ToRawSkin` is th default skin class when using the `ToRawTheme` which is the default theme but themes are detailed later.
33+
The class `ToRawSkin` is the default skin class when using the `ToRawTheme` which is the default theme but themes are detailed later.
3634
In fact, skins in Toplo are EventHandlers we simply add to our elements, changing their visual properties according to incoming events
3735

3836
```
@@ -50,7 +48,7 @@ can simply use values specific to this skin.
5048

5149
```
5250
ToInputElementSkin >> installSkinEvent: anEvent
53-
"when installing the skin, changes the properties of widget mentionned down here"
51+
"when installing the skin, changes the properties of widget mentioned down here"
5452
5553
super installSkinEvent: anEvent.
5654
anEvent elementDo: [ :e |
@@ -84,11 +82,6 @@ ToIntegerInputElement >> minus
8482
Here we redefine the background of the element and its 'plus' and 'minus' sub-elements, but we also define a border to our element using tokens from our theme.
8583
We accessed our element through the event received, this can be done in both following ways
8684

87-
88-
##### Remark
89-
Notice that the two following forms are equivalent.
90-
This is important if you want to maximize
91-
9285
```
9386
anEvent elementDo: [ :e |
9487
e border: (e valueOfTokenNamed: #'color-border-checkable’).
@@ -98,8 +91,11 @@ target := anEvent currentTarget.
9891
target border: target valueOfTokenNamed: #'color-border-checkable’)
9992
```
10093

94+
##### Remark
95+
Notice that the two following forms are equivalent.
96+
10197

102-
Now that we defined our skin, we only need to tell our element to install this skins during initialization
98+
Now that we defined our skin, we only need to tell our element to install this skin during initialization
10399
In the `ToNumberInputElement` we define the method
104100

105101
### Declaring the skin
@@ -133,7 +129,7 @@ ToIntegerInputElement >> initialize
133129

134130
We can now open our element and see our new skin installed.
135131

136-
![An integer input widget with a new skin installed. % anchor=inputFinalSkin&width=50](figures/inputWithSkin.png )
132+
![An integer input widget with a new skin installed. %width=50](figures/inputWithSkin.png )
137133

138134

139135
### Define a theme that extends an existing one
@@ -190,7 +186,7 @@ space root addChild: anInput.
190186
space show.
191187
```
192188

193-
![An integer input widget with a new skin and new theme installed. % anchor=inputFinalSkin&width=50](figures/inputWithTheme.png )
189+
![An integer input widget with a new skin and new theme installed. %width=50](figures/inputWithTheme.png )
194190

195191

196192
### Decorating a BlElement to get a ToElement
@@ -216,16 +212,6 @@ ToNumberInputElement >> initialize
216212
self initializeForToplo
217213
```
218214

219-
220-
SD: we should check if the following is necessary
221-
```
222-
ToNumberInputElement >> onAddedToSceneGraph
223-
224-
super onAddedToSceneGraph.
225-
self ensuredSkinManager requestInstallSkinIn: self.
226-
self addEventHandler: ToSkinStateGenerator new
227-
```
228-
229215
### Autonome theme
230216

231217
We should now how we can define a full new theme.

0 commit comments

Comments
 (0)