forked from pharo-project/pharo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slider.class.st
276 lines (229 loc) · 6.42 KB
/
Slider.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
Class {
#name : #Slider,
#superclass : #MorphicModel,
#instVars : [
'slider',
'value',
'setValueSelector',
'sliderShadow',
'sliderColor',
'descending',
'dragging'
],
#category : #'Morphic-Widgets-Scrolling'
}
{ #category : #geometry }
Slider >> computeSlider [
| r |
r := self roomToMove.
self descending
ifFalse:
[slider position: (bounds isWide
ifTrue: [r topLeft + ((r width * value) asInteger @ 0)]
ifFalse: [r topLeft + (0 @ (r height * value) asInteger)])]
ifTrue:
[slider position: (bounds isWide
ifTrue: [r bottomRight - ((r width * value) asInteger @ 0)]
ifFalse: [r bottomRight - ((0 @ (r height * value) asInteger))])].
slider extent: self sliderExtent
]
{ #category : #initialization }
Slider >> defaultBorderColor [
"answer the default border color/fill style for the receiver"
^ #inset
]
{ #category : #initialization }
Slider >> defaultBorderWidth [
"answer the default border width for the receiver"
^ 1
]
{ #category : #initialization }
Slider >> defaultBounds [
"answer the default bounds for the receiver"
^ 0 @ 0 corner: 16 @ 100
]
{ #category : #initialization }
Slider >> defaultColor [
"answer the default color/fill style for the receiver"
^ Color lightGray
]
{ #category : #access }
Slider >> descending [
"It may happen that an instance variable is nil, for example, if an instance is in the image when the instance is introduced. This test should probably be removed."
^ descending == true
]
{ #category : #access }
Slider >> descending: aBoolean [
descending := aBoolean.
self value: value
]
{ #category : #access }
Slider >> dragging [
^ dragging.
]
{ #category : #access }
Slider >> dragging: aBoolean [
dragging := aBoolean.
]
{ #category : #geometry }
Slider >> extent: newExtent [
newExtent = bounds extent ifTrue: [^ self].
bounds isWide
ifTrue: [super extent: (newExtent x max: self sliderThickness * 2) @ newExtent y]
ifFalse: [super extent: newExtent x @ (newExtent y max: self sliderThickness * 2)].
self removeAllMorphs; initializeSlider
]
{ #category : #initialization }
Slider >> initialize [
"initialize the state of the receiver"
super initialize.
value := 0.0.
descending := false.
dragging := false.
self initializeSlider
]
{ #category : #initialization }
Slider >> initializeSlider [
slider := BorderedMorph newBounds: self totalSliderArea color: self theme baseColor.
sliderShadow := BorderedMorph newBounds: self totalSliderArea
color: self pagingArea color.
slider on: #mouseMove send: #scrollAbsolute: to: self.
slider on: #mouseDown send: #mouseDownInSlider: to: self.
slider on: #mouseUp send: #mouseUpInSlider: to: self.
slider
borderWidth: 1;
borderColor: self theme baseColor.
sliderShadow
borderWidth: 1;
borderColor: #inset.
"(the shadow must have the pagingArea as its owner to highlight properly)"
self pagingArea addMorph: sliderShadow.
sliderShadow hide.
self addMorph: slider.
self computeSlider.
]
{ #category : #'other events' }
Slider >> mouseDownInSlider: event [
slider borderStyle style == #raised
ifTrue: [slider borderColor: #inset. self dragging: true].
sliderShadow color: self sliderShadowColor.
sliderShadow cornerStyle: slider cornerStyle.
sliderShadow bounds: slider bounds.
sliderShadow show.
]
{ #category : #'other events' }
Slider >> mouseUpInSlider: event [
slider borderStyle style == #inset
ifTrue: [slider borderColor: #raised. self dragging: false].
sliderShadow hide
]
{ #category : #access }
Slider >> pagingArea [
^self
]
{ #category : #geometry }
Slider >> roomToMove [
^ self totalSliderArea insetBy: (0@0 extent: self sliderExtent)
]
{ #category : #scrolling }
Slider >> scrollAbsolute: event [
| r p |
"If I'm not dragging I will do nothing."
self dragging ifFalse: [ ^ self ].
r := self roomToMove.
bounds isWide
ifTrue: [ r width = 0
ifTrue: [ ^ self ] ]
ifFalse: [ r height = 0
ifTrue: [ ^ self ] ].
p := event targetPoint adhereTo: r.
self descending
ifFalse: [ self
setValue:
(bounds isWide
ifTrue: [ (p x - r left) asFloat / r width ]
ifFalse: [ (p y - r top) asFloat / r height ]) ]
ifTrue: [ self
setValue:
(bounds isWide
ifTrue: [ (r right - p x) asFloat / r width ]
ifFalse: [ (r bottom - p y) asFloat / r height ]) ]
]
{ #category : #initialization }
Slider >> setSlider: aMorph withShadow: anotherMorph [
slider ifNotNil: [ self removeMorph: slider ].
sliderShadow ifNotNil: [ self pagingArea removeMorph: sliderShadow ].
slider := aMorph.
sliderShadow := anotherMorph.
slider on: #mouseMove send: #scrollAbsolute: to: self.
slider on: #mouseDown send: #mouseDownInSlider: to: self.
slider on: #mouseUp send: #mouseUpInSlider: to: self.
"(the shadow must have the pagingArea as its owner to highlight properly)"
self pagingArea addMorph: sliderShadow.
sliderShadow hide.
self addMorph: slider.
self computeSlider.
]
{ #category : #'model access' }
Slider >> setValue: newValue [
"Called internally for propagation to model"
self value: newValue.
setValueSelector ifNotNil: [
^model perform: setValueSelector with: value
].
]
{ #category : #access }
Slider >> setValueSelector: aSymbol [
"Directly set the selector to make more flexible."
setValueSelector := aSymbol
]
{ #category : #access }
Slider >> sliderColor [
"color scheme for the whole slider widget"
sliderColor ifNil: [^ (color alphaMixed: 0.7 with: Color white) slightlyLighter].
^ sliderColor
]
{ #category : #access }
Slider >> sliderColor: newColor [
sliderColor := newColor.
slider ifNotNil: [slider color: sliderColor]
]
{ #category : #geometry }
Slider >> sliderExtent [
^ bounds isWide
ifTrue: [self sliderThickness @ self innerBounds height]
ifFalse: [self innerBounds width @ self sliderThickness]
]
{ #category : #access }
Slider >> sliderShadowColor [
^ self sliderColor alphaMixed: 0.2 with: self pagingArea color
]
{ #category : #geometry }
Slider >> sliderThickness [
^ 7
]
{ #category : #access }
Slider >> thumbColor [
"Color of the draggable 'thumb'"
^ self sliderColor
]
{ #category : #geometry }
Slider >> totalSliderArea [
^ self innerBounds
]
{ #category : #update }
Slider >> updateSliderBounds [
slider bounds: self totalSliderArea.
sliderShadow bounds: self totalSliderArea.
self computeSlider.
]
{ #category : #access }
Slider >> value [
^ value
]
{ #category : #'model access' }
Slider >> value: newValue [
"Drive the slider position externally..."
value := newValue min: 1.0 max: 0.0.
self computeSlider
]