-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTutorial5CompCreation.t
More file actions
167 lines (140 loc) · 5.88 KB
/
Tutorial5CompCreation.t
File metadata and controls
167 lines (140 loc) · 5.88 KB
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
module Tutorial5CompCreation where
import COCOA
import CTButton
import CTLabel
import CTTextArea
import Tutorial4ColorPicker
root w = class
osx = new cocoa w
w1 = new mkCocoaWindow w
start app = action
w1.setPosition ({x=100,y=100})
w1.setSize ({width=400,height=400})
w1.setBackgroundColor web_gray
w1.setTitle "Tutorial"
createComponentHierarchy -- Tutorial 1
addTextArea -- Tutorial 2
replaceTabResponder -- Tutorial 3
addColorPicker app -- Tutorial 4
addCallbackLabel -- Tutorial 5
app.addWindow w1
-- Tutorial 1: Building a component hierarchy
button = new mkCocoaButton w
label = new mkCocoaCallbackLabel (mkCocoaLabel w) textChangeCallback
leftContainer = new mkCocoaContainer w
rightContainer = new mkCocoaContainer w
createComponentHierarchy = do
leftContainer.setSize ({width=200, height=200})
leftContainer.setBackgroundColor ({r=100,g=100,b=200})
leftContainer.setPosition ({x=0,y=0})
rightContainer.setSize ({width=200, height=200})
rightContainer.setBackgroundColor ({r=100,g=200,b=100})
rightContainer.setPosition ({x=200, y=0})
button.setTitle "Click me!"
button.setSize ({width=110,height=21})
button.setPosition ({x=40, y=100})
button.setClickResponder (new buttonHandler label)
leftContainer.addComponent button
label.setText "Click counter"
label.setSize ({width=150, height=36})
label.setPosition ({x=40, y=100})
rightContainer.addComponent label
w1.addComponent leftContainer
w1.addComponent rightContainer
-- Tutorial 2: Adding a text area that resizes when the window is resized
ta = new mkCocoaTextArea w
addTextArea = do
ta.setSize ({width=300, height=80})
ta.setPosition ({x=50, y=250})
ta.setDocumentSize ({width=400,height=800})
w1.setWindowResponder (new windowResponder ta) False
w1.addComponent ta
-- Tutorial 3: Blocking TAB keys from changing focus away from the TextArea
tabCountLabel = new mkCocoaCallbackLabel (mkCocoaLabel w) textChangeCallback
replaceTabResponder = do
tabCountLabel.setText "Tab counter"
tabCountLabel.setSize ({width=150, height=36})
tabCountLabel.setPosition ({x=40, y=70})
ta.addResponder (new myTabResponder tabCountLabel)
rightContainer.addComponent tabCountLabel
-- Tutorial 4: Color grid
rgbLabel = new mkCocoaLabel w
colorButton = new mkCocoaButton w
colorWindow = new mkColorPicker w setColor
setColor color = action
rgbLabel.setText (show color ++ "\n")
leftContainer.setBackgroundColor color
addColorPicker app = do
rgbLabel.setText "R=100, G=100, B=200"
rgbLabel.setSize ({width=150, height=36})
rgbLabel.setPosition ({x=40, y=40})
rightContainer.addComponent rgbLabel
colorButton.setTitle "Open ColorPicker"
colorButton.setSize ({width=150,height=21})
colorButton.setPosition ({x=40, y=75})
colorButton.setClickResponder
(new mkColorToggle colorWindow colorButton)
leftContainer.addComponent colorButton
colorWindow.setPosition ({x=500,y=100})
app.addWindow colorWindow
-- Tutorial 5: Setting up a callback label
callbackLabel = new mkCocoaLabel w
addCallbackLabel = do
callbackLabel.setSize ({width=150, height=36})
callbackLabel.setPosition ({x=40, y=10})
callbackLabel.setText "Callback label"
rightContainer.addComponent callbackLabel
textChangeCallback newText = action
callbackLabel.setText ("CB: " ++ newText)
result action
osx.startApplication start
-- Tutorial 1: Updating a label with a button click count
buttonHandler :: Label -> Class Action
buttonHandler label = class
clickCount := 0
result action
clickCount := clickCount + 1
label.setText ("Click #" ++ show clickCount)
-- Tutorial 2: Resizing the TextArea when the window is resized
windowResponder :: TextArea -> Class RespondsToWindowEvents
windowResponder textarea = class
onWindowResize size = request
newWidth = floor ((fromInt size.width) * 0.8)
newTaSize = {width=newWidth, height=80}
newX = floor ((fromInt size.width) * 0.1)
newTaPosition = {x=newX, y=250}
textarea.setSize newTaSize
textarea.setPosition newTaPosition
onWindowCloseRequest = request
result RespondsToWindowEvents {..}
-- Tutorial 3: Blocking TAB keys from changing focus away from the TextArea
myTabResponder :: Label -> Class RespondsToInputEvents
myTabResponder label = class
tabCount := 0
respondToInputEvent (KeyEvent (KeyPressed Tab)) _ = request
tabCount := tabCount + 1
label.setText ("Tabs blocked #" ++ (show tabCount))
result Consumed
respondToInputEvent _ _ = request
result NotConsumed
result RespondsToInputEvents {..}
-- Tutorial 4: Opening/closing a window when a button is clicked
mkColorToggle :: CocoaWindow -> Button -> Class Action
mkColorToggle window button = class
toggle := True
result action
window.setVisible toggle
button.setTitle
(if toggle then "Close ColorPicker" else "Open ColorPicker")
toggle := not toggle
-- Tutorial 5: Customized label, each text change triggers an action
mkCocoaCallbackLabel :: (Class Label) -> (String->Action) -> Class Label
mkCocoaCallbackLabel mkLabel cb = class
Label {setText=setTextImpl,appendText=appendTextImpl,..} = new mkLabel
setText s = request
setTextImpl s
send cb s
appendText s = request
appendTextImpl s
send cb (<- getText)
result Label{..}