on master help sign in
Add `hoverable` and `hoverables`
afacd1bevancz
changelog.txt+1
@@ -5,6 +5,7 @@ Release 0.8.1
5* Hot-swapping
5* Hot-swapping
6* Type annotations in let expressions (Andrew)
6* Type annotations in let expressions (Andrew)
7* Pattern aliases with `as` (Andrew)
7* Pattern aliases with `as` (Andrew)
8+* Detect hovering over any Element
8* Opacity on arbitrary forms in collages
9* Opacity on arbitrary forms in collages
9
10
10
11
compiler/Language/Elm.hs−1
@@ -56,4 +56,3 @@ runtime = getDataFileName "elm-runtime.js"
56-- |The absolute path to Elm's core library documentation.
56-- |The absolute path to Elm's core library documentation.
57docs :: IO FilePath
57docs :: IO FilePath
58docs = getDataFileName "docs.json"
58docs = getDataFileName "docs.json"
59−
libraries/Graphics/Element.elm+3−2
@@ -25,7 +25,8 @@ type Properties = {
25 opacity : Float,
25 opacity : Float,
26 color : Maybe Color,
26 color : Maybe Color,
27 href : JSString,
27 href : JSString,
28− tag : JSString
28+ tag : JSString,
29+ hover : ()
29 }
30 }
30
31
31type Element = { props : Properties, element : ElementPrim }
32type Element = { props : Properties, element : ElementPrim }
@@ -87,7 +88,7 @@ link href e = let p = e.props in
87
88
88emptyStr = JS.fromString ""
89emptyStr = JS.fromString ""
89newElement w h e =
90newElement w h e =
90− { props = Properties (guid ()) w h 1 Nothing emptyStr emptyStr, element = e }
91+ { props = Properties (guid ()) w h 1 Nothing emptyStr emptyStr (), element = e }
91
92
92data ElementPrim
93data ElementPrim
93 = Image ImageStyle Int Int JSString
94 = Image ImageStyle Int Int JSString
libraries/Graphics/Input.elm+8
@@ -67,6 +67,14 @@ checkbox b =
67 let cbs = N.checkboxes b
67 let cbs = N.checkboxes b
68 in (lift (cbs.box id) cbs.events, cbs.events)
68 in (lift (cbs.box id) cbs.events, cbs.events)
69
69
70+hoverables : a -> { events : Signal a,
71+ hoverable : (Bool -> a) -> Element -> Element }
72+
73+hoverable : Element -> (Element, Signal Bool)
74+hoverable elem =
75+ let pool = N.hoverables False
76+ in (pool.hoverable id elem, pool.events)
77+
70-- Represents the current state of a text field. The `string` represents the
78-- Represents the current state of a text field. The `string` represents the
71-- characters filling the text field. The `selectionStart` and `selectionEnd`
79-- characters filling the text field. The `selectionStart` and `selectionEnd`
72-- values represent what the user has selected with their mouse or keyboard.
80-- values represent what the user has selected with their mouse or keyboard.
libraries/Native/Graphics/Input.js+16−1
@@ -12,7 +12,8 @@ Elm.Native.Graphics.Input = function(elm) {
12 var Signal = Elm.Signal(elm);
12 var Signal = Elm.Signal(elm);
13 var newElement = Elm.Graphics.Element(elm).newElement;
13 var newElement = Elm.Graphics.Element(elm).newElement;
14 var JS = Elm.Native.JavaScript(elm);
14 var JS = Elm.Native.JavaScript(elm);
15− var Tuple2 = Elm.Native.Utils(elm).Tuple2;
15+ var Utils = Elm.Native.Utils(elm);
16+ var Tuple2 = Utils.Tuple2;
16
17
17 function dropDown(values) {
18 function dropDown(values) {
18 var entries = JS.fromList(values);
19 var entries = JS.fromList(values);
@@ -145,6 +146,19 @@ Elm.Native.Graphics.Input = function(elm) {
145 }
146 }
146
147
147
148
149+ function hoverables(defaultValue) {
150+ var events = Signal.constant(defaultValue);
151+ function hoverable(handler, elem) {
152+ function onHover(bool) {
153+ elm.notify(events.id, handler(bool));
154+ }
155+ var props = Utils.replace([['hover',onHover]], elem.props);
156+ return { props:props, element:elem.element };
157+ }
158+ return { _:{}, hoverable:F2(hoverable), events:events };
159+ }
160+
161+
148 function checkboxes(defaultValue) {
162 function checkboxes(defaultValue) {
149 var events = Signal.constant(defaultValue);
163 var events = Signal.constant(defaultValue);
150
164
@@ -271,6 +285,7 @@ Elm.Native.Graphics.Input = function(elm) {
271 return elm.Native.Graphics.Input = {
285 return elm.Native.Graphics.Input = {
272 buttons:buttons,
286 buttons:buttons,
273 customButtons:customButtons,
287 customButtons:customButtons,
288+ hoverables:hoverables,
274 checkboxes:checkboxes,
289 checkboxes:checkboxes,
275 fields:mkTextPool('text'),
290 fields:mkTextPool('text'),
276 emails:mkTextPool('email'),
291 emails:mkTextPool('email'),
runtime/Render/Element.js+12
@@ -21,6 +21,18 @@ function setProps(props, e) {
21 a.appendChild(e);
21 a.appendChild(e);
22 return a;
22 return a;
23 }
23 }
24+ if (props.hover.ctor !== 'Tuple0') {
25+ var overCount = 0;
26+ e.addEventListener('mouseover', function() {
27+ if (overCount++ > 0) return;
28+ props.hover(true);
29+ });
30+ e.addEventListener('mouseout', function(evt) {
31+ if (e.contains(evt.toElement || evt.relatedTarget)) return;
32+ overCount = 0;
33+ props.hover(false);
34+ });
35+ }
24 return e;
36 return e;
25}
37}
26
38