forked from open-dynaMIX/simple-mpv-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlider.elm
95 lines (77 loc) · 2.66 KB
/
Slider.elm
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
module Slider exposing (..)
import Browser.Dom
import Element exposing (..)
import Element.Background as Background
import Element.Border as Border
import Html.Attributes as HtmlA
import Html.Events
import Json.Decode as D exposing (Decoder)
import Types exposing (..)
type Msg
= PointerDownMsg Coords
| PointerMoveMsg Coords
| PointerUpMsg Coords
| PointerCancelMsg
view : String -> Bool -> Theme -> Maybe Browser.Dom.Element -> Int -> Element Msg
view id pointerDown theme maybePositionElement position =
let
value : Int
value =
case maybePositionElement of
Just element ->
round <| toFloat position / 100 * element.element.width
Nothing ->
0
pointerAttrs =
List.concat
[ [ onPointerCancel PointerCancelMsg ]
, if pointerDown then
[ onPointerMoveCoords PointerMoveMsg
, onPointerUpCoords PointerUpMsg
]
else
[ onPointerDownCoords PointerDownMsg ]
]
in
el
[ width fill
, height theme.buttonHeight
, Border.color theme.borderColor
, Border.width theme.sliderBorderWidth
, Border.rounded theme.borderRounded
, HtmlA.style "-webkit-user-select" "none" |> htmlAttribute
, HtmlA.style "user-select" "none" |> htmlAttribute
]
(el [ width fill, height fill, HtmlA.id id |> Element.htmlAttribute ]
(el
([ width fill
, height fill
]
++ pointerAttrs
)
(el
[ width (px value)
, height fill
, Background.color theme.borderColor
]
Element.none
)
)
)
onPointerDownCoords : (Coords -> msg) -> Attribute msg
onPointerDownCoords msg =
Html.Events.on "pointerdown" (D.map msg localCoords) |> Element.htmlAttribute
onPointerMoveCoords : (Coords -> msg) -> Attribute msg
onPointerMoveCoords msg =
Html.Events.on "pointermove" (D.map msg localCoords) |> Element.htmlAttribute
onPointerUpCoords : (Coords -> msg) -> Attribute msg
onPointerUpCoords msg =
Html.Events.on "pointerup" (D.map msg localCoords) |> Element.htmlAttribute
onPointerCancel : msg -> Attribute msg
onPointerCancel msg =
Html.Events.on "pointercancel" (D.succeed msg) |> Element.htmlAttribute
localCoords : Decoder Coords
localCoords =
D.map2 Coords
(D.field "offsetX" D.int)
(D.field "offsetY" D.int)