From 80ef34d43bf5ddf91c82a28a3332cd266ed9626f Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Mon, 14 Jun 2021 16:33:30 +0200 Subject: [PATCH] Adds support for captions, more shortcuts --- index.html | 110 +++---------------------------------------------- src/Events.elm | 79 +++++++++++++++++++++++++++++++++++ src/Video.elm | 65 ++++++++++++++++++++++++++++- src/Views.elm | 47 ++++++++++++++++++++- 4 files changed, 194 insertions(+), 107 deletions(-) diff --git a/index.html b/index.html index f161478..75f2efb 100644 --- a/index.html +++ b/index.html @@ -14,112 +14,14 @@
+ + diff --git a/src/Events.elm b/src/Events.elm index b3fece3..fcb27f7 100644 --- a/src/Events.elm +++ b/src/Events.elm @@ -21,6 +21,24 @@ subs model = Ok s -> Video.NowHasQuality s + _ -> + Video.Noop + ) + , Video.nowHasSubtitles + (\x -> + case Decode.decodeValue decodeSubtitles x of + Ok s -> + Video.NowHasSubtitles s + + _ -> + Video.Noop + ) + , Video.nowHasSubtitleTrack + (\x -> + case Decode.decodeValue decodeMaybeSubtitleTrack x of + Ok t -> + Video.NowHasSubtitleTrack t + _ -> Video.Noop ) @@ -185,6 +203,67 @@ decodeKeyDown model = Video.RequestFullscreen ) + -- 0 key + 48 -> + Decode.succeed (Video.Seek 0) + + -- 1 key + 49 -> + Decode.succeed (Video.Seek (0.1 * model.duration)) + + -- 2 key + 50 -> + Decode.succeed (Video.Seek (0.2 * model.duration)) + + -- 3 key + 51 -> + Decode.succeed (Video.Seek (0.3 * model.duration)) + + -- 4 key + 52 -> + Decode.succeed (Video.Seek (0.4 * model.duration)) + + -- 5 key + 53 -> + Decode.succeed (Video.Seek (0.5 * model.duration)) + + -- 6 key + 54 -> + Decode.succeed (Video.Seek (0.6 * model.duration)) + + -- 7 key + 55 -> + Decode.succeed (Video.Seek (0.7 * model.duration)) + + -- 8 key + 56 -> + Decode.succeed (Video.Seek (0.8 * model.duration)) + + -- 9 key + 57 -> + Decode.succeed (Video.Seek (0.9 * model.duration)) + _ -> Decode.fail ("no shortcut for code " ++ String.fromInt x) ) + + +decodeSubtitleTrack : Decode.Decoder Video.SubtitleTrack +decodeSubtitleTrack = + Decode.map6 Video.SubtitleTrack + (Decode.field "name" Decode.string) + (Decode.field "groupId" Decode.string) + (Decode.field "type" Decode.string) + (Decode.field "autoselect" Decode.bool) + (Decode.field "default" Decode.bool) + (Decode.field "forced" Decode.bool) + + +decodeMaybeSubtitleTrack : Decode.Decoder (Maybe Video.SubtitleTrack) +decodeMaybeSubtitleTrack = + Decode.nullable decodeSubtitleTrack + + +decodeSubtitles : Decode.Decoder (List Video.SubtitleTrack) +decodeSubtitles = + Decode.list decodeSubtitleTrack diff --git a/src/Video.elm b/src/Video.elm index d598a01..7c68766 100644 --- a/src/Video.elm +++ b/src/Video.elm @@ -1,4 +1,16 @@ -port module Video exposing (Msg(..), Settings(..), Video, fromUrl, init, nowHasQualities, nowHasQuality, update) +port module Video exposing + ( Msg(..) + , Settings(..) + , SubtitleTrack + , Video + , fromUrl + , init + , nowHasQualities + , nowHasQuality + , nowHasSubtitleTrack + , nowHasSubtitles + , update + ) import Json.Decode as Decode import Quality exposing (Quality) @@ -21,6 +33,8 @@ type alias Video = , playbackRate : Float , settings : Settings , showSettings : Bool + , subtitles : List SubtitleTrack + , subtitleTrack : Maybe SubtitleTrack } @@ -42,6 +56,8 @@ fromUrl url = , playbackRate = 1 , settings = All , showSettings = False + , subtitles = [] + , subtitleTrack = Nothing } @@ -49,6 +65,17 @@ type Settings = All | Speed | Quality + | Subtitles + + +type alias SubtitleTrack = + { name : String + , groupdId : String + , ty : String + , autoselect : Bool + , default : Bool + , forced : Bool + } type Msg @@ -59,6 +86,7 @@ type Msg | SetSettings Settings | SetPlaybackRate Float | SetQuality Quality.Quality + | SetSubtitleTrack Int | SetVolume Float Bool | RequestFullscreen | ExitFullscreen @@ -75,6 +103,8 @@ type Msg | NowHasQuality Quality.Quality | NowHasSize ( Int, Int ) | NowHasPlaybackRate Float + | NowHasSubtitles (List SubtitleTrack) + | NowHasSubtitleTrack (Maybe SubtitleTrack) update : Msg -> Video -> ( Video, Cmd Msg ) @@ -107,6 +137,9 @@ update msg model = SetQuality q -> ( { model | showSettings = False, settings = All }, setQuality q ) + SetSubtitleTrack t -> + ( { model | showSettings = False, settings = All }, setSubtitleTrack t ) + SetVolume v m -> ( model, setVolume { volume = v, muted = m } ) @@ -153,6 +186,12 @@ update msg model = NowHasPlaybackRate rate -> ( { model | playbackRate = rate }, Cmd.none ) + NowHasSubtitles tracks -> + ( { model | subtitles = tracks }, Cmd.none ) + + NowHasSubtitleTrack track -> + ( { model | subtitleTrack = track }, Cmd.none ) + port polymnyVideoInit : String -> Cmd msg @@ -210,6 +249,14 @@ setQuality = polymnyVideoSetQuality +port polymnyVideoSetSubtitleTrack : Int -> Cmd msg + + +setSubtitleTrack : Int -> Cmd msg +setSubtitleTrack = + polymnyVideoSetSubtitleTrack + + port polymnyVideoSetVolume : { volume : Float, muted : Bool } -> Cmd msg @@ -232,3 +279,19 @@ port polymnyVideoNowHasQuality : (Decode.Value -> msg) -> Sub msg nowHasQuality : (Decode.Value -> msg) -> Sub msg nowHasQuality = polymnyVideoNowHasQuality + + +port polymnyVideoNowHasSubtitles : (Decode.Value -> msg) -> Sub msg + + +nowHasSubtitles : (Decode.Value -> msg) -> Sub msg +nowHasSubtitles = + polymnyVideoNowHasSubtitles + + +port polymnyVideoNowHasSubtitleTrack : (Decode.Value -> msg) -> Sub msg + + +nowHasSubtitleTrack : (Decode.Value -> msg) -> Sub msg +nowHasSubtitleTrack = + polymnyVideoNowHasSubtitleTrack diff --git a/src/Views.elm b/src/Views.elm index 33415a1..36d6b83 100644 --- a/src/Views.elm +++ b/src/Views.elm @@ -65,7 +65,7 @@ embed screenSize model = else fadeOut ) - [ Element.width Element.fill, Element.height Element.fill ] + [ Element.width Element.fill, Element.alignBottom ] (Element.column [ Element.width Element.fill , Element.alignBottom @@ -197,6 +197,17 @@ settings model = _ -> Element.none + subtitlesButton = + case ( model.subtitleTrack, model.subtitles ) of + ( Just t, _ :: _ ) -> + makeMenuButton Video.Subtitles (Element.text "Subtitles") (Element.text t.name) + + ( _, _ :: _ ) -> + makeMenuButton Video.Subtitles (Element.text "Subtitles") (Element.text "Disabled") + + _ -> + Element.none + returnButton = Input.button [ Element.width Element.fill @@ -267,16 +278,48 @@ settings model = ) |> (\x -> returnButton :: x) + subtitleOptions = + model.subtitles + |> List.indexedMap (\i x -> ( i, Just x )) + |> (\x -> ( -1, Nothing ) :: x) + |> List.map + (\( i, x ) -> + Input.button [ Element.width Element.fill, Element.paddingXY 0 10 ] + { label = + Element.row [ Element.width Element.fill ] + [ if Maybe.map .name model.subtitleTrack == Maybe.map .name x then + Icons.check False + + else + Element.el [ Font.color (Element.rgba 0 0 0 0) ] (Icons.check False) + , Element.el + [ Element.paddingEach + { left = 10 + , right = 0 + , top = 0 + , bottom = 0 + } + ] + (Element.text (Maybe.withDefault "Disabled" (Maybe.map .name x))) + ] + , onPress = Just (Video.SetSubtitleTrack i) + } + ) + |> (\x -> returnButton :: x) + buttons = case model.settings of Video.All -> - [ speedButton, qualityButton ] + [ speedButton, qualityButton, subtitlesButton ] Video.Speed -> speedOptions Video.Quality -> qualityOptions + + Video.Subtitles -> + subtitleOptions in animatedEl (if model.showSettings then