-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobjectdrawBundle.grace
2064 lines (1624 loc) · 66.2 KB
/
objectdrawBundle.grace
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
dialect "standard"
import "dom" as dom
import "random" as rand
import "sys" as sys
import "intrinsic" as intrinsic
// ** Helpers ***************************************************
type Foreign = Unknown
def document: Foreign = dom.document
def frameRate:Number = 30 // The frame rate of the drawing
trait open {
// ** Types ********************************************************************
type Component = Object & interface {
// The super-type of all components in a GUI.
element // The underlying DOM element of the component.
width -> Number // The width of this component
height -> Number // The height of this component.
size -> Point // The size (width, height) of this component
onMouseClickDo (f: MouseResponse) -> Done
// Respond to a mouse click (press and release) in this component by executing f
onMousePressDo (f: MouseResponse) -> Done
// Respond to a mouse press in this component by executing f.
onMouseReleaseDo (f: MouseResponse) -> Done
// Respond to a mouse release in this component by executing f.
onMouseMoveDo (f: MouseResponse) -> Done
// Respond to a mouse move in this component by executing f.
onMouseDragDo (f: MouseResponse) -> Done
// Respond to a mouse drag (move during press) in this component by executing f.
onMouseEnterDo (f: MouseResponse) -> Done
// Respond to a mouse entering this component by executing f.
onMouseExitDo (f: MouseResponse) -> Done
// Respond to a mouse exiting this component by executing f.
onKeyTypeDo (f: KeyResponse) -> Done
// Respond to a key type (press and release) in this component by executing f.
onKeyPressDo (f: KeyResponse) -> Done
// Respond to a key press in this component by executing f.
onKeyReleaseDo (f: KeyResponse) -> Done
// Respond to a key release in this component by executing f.
isFlexible -> Boolean
// will this component fill any empty space in the
// direction of its parent container.
flexible := (value: Boolean) -> Done
// Sets whether this component will fill empty space in the
// direction of its parent container.
}
type Container = Component & interface {
// The type of components that contain other components.
size -> Number // The number of components inside this container.
at (index: Number) -> Component
// Retrieve the component at the given index.
at (index: Number) put(component: Component) -> Done
// Put the given component at the given index.
append (component: Component) -> Done
// Add a component to the end of the container.
prepend (component: Component) -> Done
// Add a component to the start of the container.
do (f: Procedure1⟦Component⟧) -> Done
// Perform the action f for every component inside this container.
arrangeHorizontal -> Done
// Arrange the contents of this container horizontally.
// Components that exceed the width of the container will wrap around.
arrangeVertical -> Done
// Arrange the contents of this container vertically.
// Components that exceed the height of the container will wrap around.
}
type Application = Container & interface {
// A standalone window that contains other components.
windowTitle -> String
// The title of this application window.
windowTitle:= (value: String) -> Done
// Sets the title of the application window.
startApplication -> Done
// Makes this window visible, with active listeners for mouse actions
stopApplication -> Done
// Close this window
}
type Graphic = Object & interface {
// an object that can be drawn on a canvas and moved around.
location -> Point
// Tte location of this object with respect to the top-left corner of the screen.
x -> Number
// the horizontal offset of this object from the left edge of the screen.
y -> Number
// Tte vertical offset of this object from the top of the screen.
addToCanvas (canvas: DrawingCanvas) -> Done
// adds this object to canvas.
removeFromCanvas -> Done
// removes this object from its canvas.
isVisible -> Boolean
// whether this object is currently visible on the canvas.
visible:= (value: Boolean) -> Done
// sets whether this object should be visible on the canvas.
moveTo(newLocation: Point) -> Done
// moves this object to newLocation on the canvas.
moveBy(dx: Number, dy: Number) -> Done
// moves this object on the canvas by dx horizontally, and dy vertically.
contains(pt: Point) -> Boolean
// is pt is inside this object?
overlaps(graphic: Graphic2D) -> Boolean
// is any point in graphic also inside this object?
color := (c: Color)->Done
// sets the color of this object to c
color -> Color
// answers the color of this object
sendForward -> Done
// sends this object up one layer on the screen
sendBackward -> Done
// sends this object down one layer on the screen
sendToFront -> Done
// sends this object to the top layer on the screen
sendToBack -> Done
// sends this object to the bottom layer on the screen
}
type DrawingCanvas = Component & interface {
// a canvas that can hold Graphic objects
startDrawing -> Done
// redraws the canvas and its contents regularly as needed
add (d: Graphic)->Done
// adds d to canvas
remove (d: Graphic)->Done
// removes d from canvas
notifyRedraw -> Done
// informs me that I need to be redrawn
clear -> Done
// clears the canvas
sendToFront (d: Graphic) -> Done
// sends d to top layer of graphics
sendToBack (d: Graphic) -> Done
// sends d to bottom layer of graphics
sendForward (d: Graphic) -> Done
// sends d up one layer in graphics
sendBackward (d: Graphic) -> Done
// sends d down one layer in graphics
}
type GraphicApplication = Application & interface {
// Type of an object that runs a graphical application that draws
// on a canvas displayed in a window, and responds to mouse actions
canvas -> DrawingCanvas
// canvas holds graphic objects on screen
onMouseClick (m: Point) -> Done
// Respond to a mouse click (press and release) in the canvas at m
onMousePress (m: Point) -> Done
// Respond to a mouse press in the canvas at m
onMouseRelease (m: Point) -> Done
// Respond to a mouse release in the canvas at m
onMouseMove (m: Point) -> Done
// Respond to a mouse move in the canvas at tm
onMouseDrag (m: Point) -> Done
// Respond to a mouse drag (move during press) in the canvas at m
onMouseEnter (m: Point) -> Done
// Respond to a mouse entering the canvas at m
onMouseExit (m: Point) -> Done
// Respond to a mouse exiting the canvas at m
startGraphics -> Done
// makes this object's window visisble, along with its contents, and
// prepares the window to handle mouse events
}
type Graphic2D = Graphic & interface {
// Two-dimensional objects that have a size, and can be resized
width -> Number // dimensions of this object
height -> Number
size -> Point
size := (dimensions: Point) -> Done // change dimensions of this object
width := (newWidth: Number) -> Done
height := (newHeight: Number) -> Done
}
type Line = Graphic & interface {
// One-dimensional objects
start -> Point // start of this line
end -> Point // end of this line
start := (start': Point) -> Done // sets start of this line to start'
end := (end': Point) -> Done // sets end of this line to end'
setEndPoints (start': Point, end': Point) -> Done // sets both start and end
}
type Text = Graphic & interface {
// Text that can be drawn on a canvas.
contents -> String // return my contents
contents := (s: String) -> Done // sets my contents to s
width -> Number // returns the width of my content (currently inaccurate)
fontSize -> Number // return size of the font used to display my contents
fontSize := (size: Number) -> Done // sets the size of the font used to display my contents
}
type TextBox = Component & interface {
// Component of window that can hold text
contents -> String // The text contents of this box.
contents:= (value: String) -> Done
}
type Labeled = Component & interface {
// Component of window that holds text
label -> String // The label name.
label:= (value: String) -> Done
}
type Button = Labeled
// type of button component in window
type Input = Component & interface {
// Component taking input and responding to an event
onFocusDo(f: Response) -> Done
// Respond to this input gaining focus with the given event.
onBlurDo(f: Response) -> Done
// Respond to this input losing focus with the given event.
onChangeDo(f: Response) -> Done
// Respond to this input having its value changed.
}
type TextField = Input & interface { // Component taking user's text input
text -> String // The contents of the field
text := (value: String) -> Done // changes text
}
type NumberField = Input & interface { // Component taking user's numeric input
number -> Number // The contents of the field.
number := (value: Number) -> Done // Changes number
}
type Choice = Input & interface { // Pop-up menus
selected -> String // The currently selected option
selected := (value: String) -> Done // Changes selected
}
// ** Colors *******************************************************************
type Color = interface {
red -> Number // The red component of the color.
green -> Number // The green component of the color.
blue -> Number // The blue component of the color.
}
type ColorFactory = interface {
r (r': Number) g (g': Number) b (b': Number) -> Color
random -> Color
white -> Color
black -> Color
green -> Color
red -> Color
gray -> Color
blue -> Color
cyan -> Color
magenta -> Color
yellow -> Color
neutral -> Color
}
once method ColorOutOfRange -> ExceptionKind {
// the Exception that will be thrown if the r, b, or g components
// are not between 0 and 255 (inclusive)
self.ProgrammingError.refine "ColorOutOfRange"
}
once class colorGen -> ColorFactory {
class r (r': Number) g (g': Number) b (b': Number) -> Color {
use equality
// Creates a color with rgb coordinates r', g', and b'
if ((r' < 0) || (r' > 255)) then {
ColorOutOfRange.raise "red index {r'} out of bounds 0..255"
}
if ((g' < 0) || (g' > 255)) then {
ColorOutOfRange.raise "green index {g'} out of bounds 0..255"
}
if ((b' < 0) || (b' > 255)) then {
ColorOutOfRange.raise "blue index {b'} out of bounds 0..255"
}
def red:Number is public = r'.truncated
def green:Number is public = g'.truncated
def blue:Number is public = b'.truncated
method == (c: Color) -> Boolean {
(red == c.red) && (green == c.green) && (blue == c.blue)
}
method hash -> Number {
hashCombine(hashCombine(red.hash, green.hash), blue.hash)
}
method asString -> String {
"color w/ rgb({red}, {green}, {blue})"
}
}
method random -> Color {
// return a random color.
r (rand.integerIn 0 to 255)
g (rand.integerIn 0 to 255)
b (rand.integerIn 0 to 255)
}
def white:Color is public = r 255 g 255 b 255
def black:Color is public = r 0 g 0 b 0
def green:Color is public = r 0 g 255 b 0
def red:Color is public = r 255 g 0 b 0
def gray:Color is public = r 60 g 60 b 60
def blue:Color is public = r 0 g 0 b 255
def cyan:Color is public = r 0 g 255 b 255
def magenta:Color is public = r 255 g 0 b 255
def yellow:Color is public = r 255 g 255 b 0
def neutral:Color is public = r 220 g 220 b 220
}
// ** Events *******************************************************************
type Event = interface {
// Generic event containing source of the event.
source -> Component
}
type MouseEvent = Event & interface {
// Mouse event containing mouse location when event generated
at -> Point
}
type KeyEvent = Event & interface {
// Type of an event associated with a key press
code -> Number
//character -> String
//modifiers -> Modifiers
}
type Response = Procedure1⟦Event⟧ // an action taking an Event as argument
type MouseResponse = Procedure1⟦MouseEvent⟧ // action taking a MouseEvent as argument
type KeyResponse = Procedure1⟦KeyEvent⟧ // an action taking a KeyEvent as argument
class eventSource (source':Component) -> Event {
// Creates an event generated by source'
def source: Component is public = source'
method asString -> String {
"Event on {source}"
}
}
class mouseEventSource (source':Component) event (event':Foreign) -> MouseEvent {
// Creates a mouseEvent with the mouse location from event'
inherit eventSource (source')
def at: Point is public = (event'.pageX - source.element.offsetLeft) @
(event'.pageY - source.element.offsetTop)
// String representation of the mouse event
method asString -> String {
"Mouse event on {source} at {at}"
}
}
class keyEventSource (source':Component) event(event':Foreign) -> KeyEvent {
// represents the keyboard event' from source'
inherit eventSource (source')
method code → Number { event'.which }
method character → String { event'.key }
method shiftKey → Boolean { event'.shiftKey }
method ctrlKey → Boolean { event'.ctrlKey }
method altKey → Boolean { event'.altKey }
method metaKey → Boolean { event'.metaKey }
method asString → String {
"an event on {source} for key-code {code}"
}
}
// ** Internal factories *******************************************************
type ComponentFactory⟦T where T <: Component⟧ = interface {
fromElement (element) -> T
// Build a component around the existing element.
ofElementType (tagName: String) -> T
// Build a component around a new element with the given tag name.
}
once method maxClickTime -> Number { 200 }
// The maximum number of milliseconds for which the mouse-button can
// be held down in order for the event to be registered as a click.
class componentFromElement (element') -> Component {
def element is public = element'
method width -> Number {
// width of component
element.width
}
method height -> Number {
// height of component
element.height
}
method size -> Point {
// dimensions of component
element.width @ element.height
}
method on(event': String) do(f: Procedure1⟦Foreign⟧) -> Done is confidential {
// associates action f with event' on this component
element.addEventListener(event', f)
done
}
method on (kind: String) withPointDo (f:MouseResponse) -> Done is confidential {
// associates response f with mouse event of kind
on (kind) do { event' ->
f.apply (mouseEventSource (self) event (event'))
}
}
method onMouseClickDo (f:MouseResponse) -> Done {
// associates action f with mouse click event
var lastDown: Foreign
on "mousedown" do {event': Foreign ->
lastDown:= event'.timeStamp
}
on "click" do {event': Foreign ->
if ((event'.timeStamp - lastDown) <= maxClickTime) then {
f.apply (mouseEventSource(self) event(event'))
}
}
}
method onMousePressDo (f: MouseResponse) -> Done {
// Associates action f with any mouse press event
on "mousedown" withPointDo (f)
}
method onMouseReleaseDo (f: MouseResponse) -> Done {
// Associates action f with any mouse release event
on "mouseup" withPointDo (f)
}
method onMouseMoveDo (f: MouseResponse) -> Done {
// Associates action f with any mouse move event
on "mousemove" do { event' ->
if (if (dom.doesObject (event') haveProperty ("buttons")) then {
event'.buttons == 0
} else {
event'.which == 0
}) then {
f.apply(mouseEventSource (self) event (event'))
}
}
}
method onMouseDragDo (f: MouseResponse) -> Done {
// Associate action f with mouse drag event
on "mousemove" do { event' ->
if (if (dom.doesObject (event') haveProperty ("buttons")) then {
event'.buttons == 1
} else {
event'.which == 1
}) then {
f.apply(mouseEventSource (self) event(event'))
}
}
}
method onMouseEnterDo (f: MouseResponse) -> Done {
// Associate action f with mouse enter (of window) event
on "mouseover" do { event' ->
def from = event'.relatedTarget
if ((dom.noObject == from) || {!element.contains(from)}) then {
f.apply (mouseEventSource (self) event (event'))
}
}
}
method onMouseExitDo (f: MouseResponse) -> Done {
// Associate action f with mouse exit event
on "mouseout" do {event' ->
def to = event'.relatedTarget
if ((dom.noObject == to) || {!element.contains (to)}) then {
f.apply (mouseEventSource (self) event (event'))
}
}
}
method on (kind: String)
// Associate action f with key event of kind
withKeyDo (f: KeyResponse) -> Done is confidential {
on (kind) do {event' ->
f.apply (keyEventSource (self) event (event'))
}
}
method onKeyPressDo(f: KeyResponse) -> Done {
// Associate action f with key press event
on "keydown" withKeyDo (f)
}
method onKeyReleaseDo (f: KeyResponse) -> Done {
// Associate action f with key release event
on "keyup" withKeyDo (f)
}
method onKeyTypeDo (f: KeyResponse) -> Done {
// Associate action f with key type (press & release) event
on "keypress" withKeyDo (f)
}
method isFlexible -> Boolean {
// Does this component have flexible size?
element.style.flexGrow.asNumber > 0
}
method flexible:= (value: Boolean) -> Done {
// Sets whether I am flexibile
element.style.flexGrow := if (value) then {
1
} else {
0
}
}
method asString -> String { "a component" }
}
class componentOfElementType (tagName:String) -> Component {
// Creates a component with type tagName
inherit componentFromElement (document.createElement (tagName))
}
class containerOfElementType (tagName: String) -> Component {
inherit containerFromElement (document.createElement (tagName))
}
class containerFromElement (element') -> Container {
// Create a and return new Container from element'
inherit componentFromElement (element')
def children = list []
method size -> Number {
// answers the number of children of this container
children.size
}
method isEmpty -> Boolean {
// is this container empty?
size == 0
}
method at (index: Number) -> Component {
// returns my subcomponent at position index
children.at (index) // children.at will check bounds
}
method at (index: Number) put (aComponent: Component) -> Done {
// replaces subcomponent at index by aComponent
if ((index < 1) || (index > (size + 1))) then {
BoundsError.raise
"Can't put component at {index} because I have only {size} elements"
}
if (index == (size + 1)) then {
element.appendChild (aComponent.element)
} else {
element.insertBefore (aComponent.element, children.at (index).element)
}
children.at (index) put (aComponent)
done
}
method append (aComponent: Component) -> Done {
// adds aComponent after all my existing components
element.appendChild (aComponent.element)
children.addLast (aComponent)
done
}
method prepend (aComponent: Component) -> Done {
// adds aComponent before all my existing components
if (isEmpty) then {
element.appendChild (aComponent.element)
} else {
element.insertBefore (aComponent.element, element.firstChild)
}
children.addFirst (aComponent)
done
}
method do (f: Procedure1⟦Component⟧) -> Done {
// apply f to all my children
children.do {aComponent: Component ->
f.apply(aComponent)
}
}
method fold⟦T⟧(f: Function2⟦T, Component, T⟧) startingWith (initial: T) -> T {
// answers the fold of the binary function f over all my children.
// answers initial if I have no children.
var value:T := initial
children.do {aComponent: Component ->
value:= f.apply (value, aComponent)
}
value
}
method flex -> Done is confidential {
// makes me more flexible
element.style.display := "inline-flex"
element.style.justifyContent := "center"
element.style.flexFlow := "row wrap"
}
method arrangeHorizontal -> Done {
// arranges my elements in rows
flex
element.style.flexDirection:= "row"
}
method arrangeVertical -> Done {
// Arranges my elements in columns
flex
element.style.flexDirection := "column"
}
method asString -> String {
// answers a description of me
"container: with {size} children"
}
}
class emptyContainer -> Container {
// creates and returns an empty container ready to add in row
inherit containerOfElementType "div"
self.arrangeHorizontal
}
class containerSize (width':Number, height':Number) -> Container {
// creates and returns an empty container with width' and height'
inherit emptyContainer
self.element.style.width:= "{width'}px"
self.element.style.height:= "{height'}px"
self.element.style.overflow:= "auto"
}
class inputFromElement (element') -> Input {
// A factory building components that take input
inherit componentFromElement(element')
method onFocusDo (f: Response) -> Done {
// Respond with action f to this input gaining focus with the given event.
element.addEventListener ("focus", { _ ->
f.apply(eventSource(self))
})
}
method onBlurDo (f: Response) -> Done {
// Respond with action f to this input losing focus with the given event.
element.addEventListener("blur", { _ ->
f.apply(eventSource(self))
})
}
method onChangeDo (f: Response) -> Done {
// Respond with action f to this input having its value changed.
element.addEventListener ("change", { _ ->
f.apply(eventSource(self))
})
}
method asString -> String {
// return description of component
"an input"
}
}
class inputOfElementType(elementType: String) -> Input {
// Create component of type elementType to handle input
inherit inputFromElement (document.createElement (elementType))
}
class inputOfType (inputType: String) -> Input {
// Create component of type inputType to handle input
inherit inputOfElementType("input")
self.element.setAttribute ("type", inputType)
}
class labeledWidgetFromElement (element') -> Labeled {
// create labeled input from element'
inherit inputFromElement (element')
var labeler:Foreign // debug -- work around for selectBox
method labelElement -> Foreign is confidential {
self.element
}
method label -> String {
// answers the Text of the label
labelElement.textContent
}
method label:= (value: String) -> Done {
// sets the label to value
labelElement.textContent:= value
done
}
method asString -> String {
// a human-readable description of this object
"an input labeled: {label}"
}
}
class labeledWidgetOfElementType (elementType:String) -> Labeled {
// creates labeled input a new document of elementType
inherit labeledWidgetFromElement (document.createElement (elementType))
}
class labeledWidgetOfElementType (elementType: String)
labeled(newLabel: String) -> Labeled {
// Create labeled element from elementType with newLabel
inherit labeledWidgetOfElementType(elementType)
// method to help get initialization code right in choice elements
method init -> Done is confidential {}
init
self.label := newLabel
}
class fieldOfType(inputType: String) labeled(label': String) -> Input {
// Create input field of type inputType showing label'
inherit inputOfType(inputType)
// label on the field
method label -> String {
self.element.placeholder
}
// reset the label on the field
method label:= (value: String) -> Done {
self.element.placeholder:= value
done
}
// String representing the labeled field (including label)
method asString -> String {
"a field labeled: {label}"
}
label:= label'
}
// ** External factories *******************************************************
class eventLogKind(kind': String) response(response': Response) is confidential {
// Log entry to keep track of response to an event
def kind: String is public = kind'
def response: Response is public = response'
}
class applicationTitle(initialTitle: String) size (dimensions': Point) -> Application {
// returns an Application with window with initialTitle and dimensions'
inherit containerFromElement(document.createDocumentFragment)
alias containerElement = element
alias containerArrangeHorizontal = arrangeHorizontal
alias containerArrangeVertical = arrangeVertical
var isOpened: Boolean:= false // whether window is visible
var theWindow: Foreign
var theTitle: String:= initialTitle
var theWidth: Number:= dimensions'.x
var theHeight: Number:= dimensions'.y
def events = list []
method element -> Foreign {
if (isOpened) then {
theWindow.document.body
} else {
containerElement
}
}
// Whether new items are added to window from left to right or top to bottom
var isHorizontal: Boolean:= true
// Arrange the contents of this container along the horizontal axis.
// Components that exceed the width of the container will wrap around.
method arrangeHorizontal -> Done {
if (isOpened) then {
containerArrangeHorizontal
} else {
isHorizontal:= true
}
}
// Arrange the contents of this container along the vertical axis.
// Components which exceed the height of the container will wrap around.
method arrangeVertical -> Done {
if (isOpened) then {
containerArrangeVertical
} else {
isHorizontal:= false
}
}
// Associate response with event kind
method on (kind: String)
do (response: Procedure1⟦Event⟧) -> Done is confidential {
if (isOpened) then {
theWindow.addEventListener (kind, response)
} else {
events.addLast (eventLogKind (kind) response (response))
}
}
// The title of the application window.
method windowTitle -> String {
if (isOpened) then {
theWindow.title
} else {
theTitle
}
}
// Set the title of the application window.
method windowTitle:= (value: String) -> Done {
if (isOpened) then {
theWindow.title:= value
} else {
theTitle:= value
}
}
// The current width of the window
method width -> Number {
if (isOpened) then {
theWindow.width
} else {
theWidth
}
}
// the current height of the window
method height -> Number {
if (isOpened) then {
theWindow.height
} else {
theHeight
}
}
// The enter/exit events need to be redefined to account for the body element
// not necessarily accounting for all of the space in the window. If we only
// consider cases where relatedTarget is null, then it represents only enter
// and exit of the whole window.
// Respond to a mouse entering this window with the response f.
method onMouseEnterDo(f: MouseResponse) -> Done {
on "mouseover" do { event' ->
def from = event'.relatedTarget
if (dom.noObject == from) then {
f.apply(mouseEventSource(self) event(event'))
}
}
}
// Respond to a mouse exiting this window with the response f.
method onMouseExitDo(f: MouseResponse) -> Done {
on "mouseout" do { event' ->
def to = event'.relatedTarget
if (dom.noObject == to) then {
f.apply(mouseEventSource(self) event(event'))
}
}
}
// Respond to mouse action of kind with response bl
method onMouse (kind: String) do (bl: MouseResponse) -> Done is confidential {
theWindow.addEventListener(kind, {evt ->
bl.apply(evt.pageX @ evt.pageY)
})
}
// Create window with listeners for mouse enter and exit actions
method startApplication -> Done {
if (!isOpened) then {
theWindow:= dom.window.open("", "", "width={theWidth},height={theHeight}")
if (intrinsic.inBrowser && (dom.noObject == theWindow)) then {
print "Failed to open the graphics window.\nIs your browser blocking pop-ups?"
sys.exit 1
}
theWindow.document.title:= theTitle
theWindow.document.body.appendChild(element)
if (dom.doesObject(dom.window) haveProperty("graceRegisterWindow")) then {
dom.window.graceRegisterWindow(theWindow)
}
isOpened:= true
element.style.width:= "100%"
element.style.margin:= "0px"