on master help sign in
Start using new Input type everywhere!

Whoo! :D

commit0018d32627f159d5703d49c9c7f0738da07b9f6d authorEvan Czaplicki <info@elm-lang.org> dateTue 18 Feb 2014, 12:14 (13 years ago) parent0c805d8
2 files changed+68−59
libraries/Graphics/Input.elm+67−58 libraries/Native/Graphics/Input.js+1−1
libraries/Graphics/Input.elm+67−58
@@ -1,22 +1,22 @@
11module Graphics.Input where
22{-| This module is for creating standard input widgets such as buttons and
3−text fields. All functions in this library follow this general pattern:
3+text fields. All functions in this library follow a general pattern in which
4+you create an `Input` that many elements can report to:
45
56```haskell
6−-- (Signal (), SignalID ())
7−(clicks, clickID) = input ()
7+clicks : Input ()
8+clicks = input ()
89
910clickableYogi : Element
10−clickableYogi = clickable clickID () (image 40 40 "/yogi.jpg")
11+clickableYogi = clickable clicks.handle () (image 40 40 "/yogi.jpg")
1112```
1213
13−Whenever the user clicks on the resulting `clickableYogi` element, it sends a
14−`()` to the `clicks` signal. Typically, you would create all your input signals
15−when setting up the signal graph, so then your graphics code can refer to them
16−easily.
14+Whenever the user clicks on the resulting `clickableYogi` element, it sends an
15+update to the `clicks` input. You will see this pattern again and again in
16+examples in this library, so just read on to get a better idea of how it works!
1717
1818# Creating Inputs
19−@docs input
19+@docs Input, input
2020
2121# Basic Input Elements
2222Text fields come later.
@@ -33,46 +33,55 @@ import Signal (Signal)
3333import Graphics.Element (Element)
3434import Native.Graphics.Input
3535
36−data SignalID a = SignalID
36+data Handle a = Handle
3737
38−{-| This function is the key to using this library. It creates a signal that
39−input elements (buttens, checkboxes, etc.) can report to. It also create a
40−unique `SignalID` that allows input elements to refer to this particular
41−signal. It may be best to look at examples in the rest of the docs and return
42−to this function.
38+{-| This is the key abstraction of this library. An `Input` is a record
39+of two fields:
4340
44−Note: creating an input signal is an inherently imperative action, so this is
45−one of very few impure functions in Elm. It is designed such that it can only
46−be used as you build your signal graph at startup, keeping Elm pure at runtime.
41+ 1. `signal` &mdash; all values coming to this input from &ldquo;the world&rdquo;
42+ 2. `handle` &mdash; a way to refer to this particular input and send it values
43+
44+This will make more sense as you see more examples.
45+-}
46+type Input a = { signal : Signal a, handle : Handle a }
47+
48+{-| Create a new `Input`. You just give the initial value for the
49+input&rsquo;s `signal`.
50+
51+Note for Advanced Users: creating an `Input` is an inherently imperative
52+action, so this is one of very few impure functions in Elm. That means
53+`(input ())` and `(input ())` are actually two different inputs with different
54+signals and handles. By design, Elm&rsquo;s impure functions can only be useful
55+as you build your signal graph at startup, so Elm is still pure at runtime.
4756-}
48−input : a -> (Signal a, SignalID a)
57+input : a -> Input a
4958input = Native.Graphics.Input.input
5059
5160{-| Create a standard button. The following example begins making a basic
5261calculator:
5362
54− data Input = Number Int | Plus | Minus | Clear
63+ data Keys = Number Int | Plus | Minus | Clear
5564
56− -- (Signal Input, SignalID Input)
57− (calcInput, pressID) = input Clear
65+ keys : Input Keys
66+ keys = input Clear
5867
59− buttons : Element
60− buttons =
61− flow right [ button pressID (Number 1) "1"
62− , button pressID (Number 2) "2"
63− , button pressID Plus "+"
68+ calculator : Element
69+ calculator =
70+ flow right [ button keys.handle (Number 1) "1"
71+ , button keys.handle (Number 2) "2"
72+ , button keys.handle Plus "+"
6473 ]
6574
66−If the user presses the "plus" button, the `calcInput` signal will update to
67−`Plus`. If the users presses "2", `calcInput` will update to `(Number 2)`.
75+If the user presses the "+" button, `keys.signal` will update to `Plus`. If the
76+users presses "2", `keys.signal` will update to `(Number 2)`.
6877-}
69−button : SignalID a -> a -> String -> Element
78+button : Handle a -> a -> String -> Element
7079button = Native.Graphics.Input.button
7180
72−customButton : SignalID a -> a -> Element -> Element -> Element -> Element
81+customButton : Handle a -> a -> Element -> Element -> Element -> Element
7382customButton = Native.Graphics.Input.customButton
7483
75−checkbox : SignalID a -> (Bool -> a) -> Bool -> Element
84+checkbox : Handle a -> (Bool -> a) -> Bool -> Element
7685checkbox = Native.Graphics.Input.checkbox
7786
7887{-| Create a drop-down menu. The following drop-down lets you choose your
@@ -80,38 +89,38 @@ favorite British sport:
8089
8190 data Sport = Football | Cricket | Snooker
8291
83− -- (Signal (Maybe Sport), SignalID (Maybe Sport))
84− (dropStatus, dropID) = input Nothing
92+ sport : Input Sport
93+ sport = input Nothing
8594
8695 sportDropDown : Element
8796 sportDropDown =
88− dropDown dropID
97+ dropDown sport.handle
8998 [ ("" , Nothing)
9099 , ("Football", Just Football)
91100 , ("Cricket" , Just Cricket)
92101 , ("Snooker" , Just Snooker)
93102 ]
94103
95−If the user selects "Football" from the drop down menue, the `dropStatus`
96−signal will update to `Just Football`.
104+If the user selects "Football" from the drop down menue, `sport.signal`
105+will update to `Just Football`.
97106-}
98−dropDown : SignalID a -> [(String,a)] -> Element
107+dropDown : Handle a -> [(String,a)] -> Element
99108dropDown = Native.Graphics.Input.dropDown
100109
101110{-| Detect mouse hovers over a specific `Element`. In the following example,
102111we will create a hoverable picture called `cat`.
103112
104− -- (Signal Bool, SignalID Bool)
105− (hover, hoverID) = input False
113+ hover : Input Bool
114+ hover = input False
106115
107116 cat : Element
108117 cat = image 30 30 "/cat.jpg"
109− |> hoverable hoverID (\hover -> hover)
118+ |> hoverable hover.handle id
110119
111−When the mouse hovers above the `cat` element, the `hover` signal will become
112−`True`. When the mouse leaves it, `hover` will become `False`.
120+When the mouse hovers above the `cat` element, `hover.signal` will become
121+`True`. When the mouse leaves it, `hover.signal` will become `False`.
113122-}
114−hoverable : SignalID a -> (Bool -> a) -> Element -> Element
123+hoverable : Handle a -> (Bool -> a) -> Element -> Element
115124hoverable = Native.Graphics.Input.hoverable
116125
117126{-| Detect mouse clicks on a specific `Element`. In the following example,
@@ -119,24 +128,24 @@ we will create a clickable picture called `cat`.
119128
120129 data Picture = Cat | Hat
121130
122− -- (Signal Picture, SignalID Picture)
123− (click, clickID) = input Cat
131+ picture : Input Picture
132+ picture = input Cat
124133
125134 cat : Element
126135 cat = image 30 30 "/cat.jpg"
127− |> clickable clickID Cat
136+ |> clickable picture.handle Cat
128137
129138 hat : Element
130139 hat = image 30 30 "/hat.jpg"
131− |> clickable clickID Hat
140+ |> clickable picture.handle Hat
132141
133−When the user clicks on the `cat` element, the `click` signal receives
142+When the user clicks on the `cat` element, `picture.signal` receives
134143an update containing the value `Cat`. When the user clicks on the `hat` element,
135−the `click` signal receives an update containing the value `Hat`. This lets you
144+`picture.signal` receives an update containing the value `Hat`. This lets you
136145distinguish which element was clicked. In a more complex example, they could be
137146distinguished with IDs or more complex data structures.
138147-}
139−clickable : SignalID a -> a -> Element -> Element
148+clickable : Handle a -> a -> Element -> Element
140149clickable = Native.Graphics.Input.clickable
141150
142151{-| Represents the current content of a text field. For example:
@@ -170,25 +179,25 @@ noContent = FieldContent "" (Selection 0 0 Forward)
170179called `nameField`. As the user types their name, the field will be updated
171180to match what they have entered.
172181
173− -- (Signal FieldContent, SignalID FieldContent)
174− (nameContent,nameID) = input noContent
182+ name : Input FieldContent
183+ name = input noContent
175184
176185 nameField : Signal Element
177− nameField = field nameID (\content -> content) "Name" <~ nameContent
186+ nameField = field name.handle (\content -> content) "Name" <~ nameContent
178187-}
179−field : SignalID a -> (FieldContent -> a) -> String -> FieldContent -> Element
188+field : Handle a -> (FieldContent -> a) -> String -> FieldContent -> Element
180189field = Native.Graphics.Input.field
181190
182191{-| Same as `field` but the UI element blocks out each characters. -}
183−password : SignalID a -> (FieldContent -> a) -> String -> FieldContent -> Element
192+password : Handle a -> (FieldContent -> a) -> String -> FieldContent -> Element
184193password = Native.Graphics.Input.password
185194
186195{-| Same as `field` but it adds an annotation that this field is for email
187196addresses. This is helpful for auto-complete and for mobile users who may
188197get a custom keyboard with an `@` and `.com` button.
189198-}
190−email : SignalID a -> (FieldContent -> a) -> String -> FieldContent -> Element
199+email : Handle a -> (FieldContent -> a) -> String -> FieldContent -> Element
191200email = Native.Graphics.Input.email
192201
193−-- area : SignalID a -> (FieldContent -> a) -> SignalID b -> ((Int,Int) -> b) -> (Int,Int) -> String -> FieldContent -> Element
202+-- area : Handle a -> (FieldContent -> a) -> Handle b -> ((Int,Int) -> b) -> (Int,Int) -> String -> FieldContent -> Element
194203-- area = Native.Graphics.Input.area
libraries/Native/Graphics/Input.js+1−1
@@ -17,7 +17,7 @@ Elm.Native.Graphics.Input.make = function(elm) {
1717
1818 function input(initialValue) {
1919 var sig = Signal.constant(initialValue);
20− return Tuple2(sig, sig);
20+ return { _:{}, signal:sig, handle:sig };
2121 }
2222
2323 function renderDropDown(signal, values) {