diff --git a/src/Tron/Control/Impl/Button.elm b/src/Tron/Control/Impl/Button.elm index 8c5c7ef..fc17ad7 100644 --- a/src/Tron/Control/Impl/Button.elm +++ b/src/Tron/Control/Impl/Button.elm @@ -50,19 +50,72 @@ iconAt : List String -> Icon iconAt path = Icon (always - <| Url.fromString - <| Url.relative path []) + <| Just + <| toLocalUrl + <| path + ) themedIconAt : (Theme -> List String) -> Icon themedIconAt f = themedIcon <| \theme -> - Url.fromString - <| Url.relative (f theme) [] + Just + <| toLocalUrl + <| f theme setFace : Face -> Control a -> Control a setFace face (Core.Control _ val handler) = - Core.Control face val handler \ No newline at end of file + Core.Control face val handler + + +-- https://github.com/elm/url/issues/10 +-- We would like to use `Url` for icons, but it doesn't support local files, +-- so here we have some dirty hacks; + + +localMarker : String +localMarker = "@@@@____local___@@@@" + + +toLocalUrl : List String -> Url +toLocalUrl path = + toLocalUrl_ <| Url.relative path [] + + +toLocalUrl_ : String -> Url +toLocalUrl_ path = + { protocol = Url.Https + , host = localMarker + , port_ = Nothing + , path = path + , query = Nothing + , fragment = Nothing + } + + +encodeMaybeLocalUrl : Url -> String +encodeMaybeLocalUrl url = + if url.host == localMarker then + localMarker ++ url.path + else + Url.toString url + + +decodeMaybeLocalUrl : String -> Maybe Url +decodeMaybeLocalUrl str = + if String.startsWith localMarker str then + String.dropLeft (String.length localMarker) str + |> toLocalUrl_ + |> Just + else Url.fromString str + + +maybeLocalUrlToString : Url -> String +maybeLocalUrlToString url = + if url.host == localMarker then + "./" ++ url.path + else + Url.toString url \ No newline at end of file diff --git a/src/Tron/Control/Json/Button.elm b/src/Tron/Control/Json/Button.elm index 451bb3a..fc8c0e3 100644 --- a/src/Tron/Control/Json/Button.elm +++ b/src/Tron/Control/Json/Button.elm @@ -38,11 +38,11 @@ encodeFace face = E.object [ ( "kind", E.string "icon" ) , ( "dark", case fn Theme.Dark of - Just url -> E.string <| Url.toString url + Just url -> E.string <| Button.encodeMaybeLocalUrl url Nothing -> E.null ) , ( "light", case fn Theme.Light of - Just url -> E.string <| Url.toString url + Just url -> E.string <| Button.encodeMaybeLocalUrl url Nothing -> E.null ) ] @@ -68,8 +68,8 @@ decodeFace = D.map2 (\darkSrc lightSrc theme -> case theme of - Theme.Dark -> darkSrc |> Maybe.andThen Url.fromString - Theme.Light -> lightSrc |> Maybe.andThen Url.fromString + Theme.Dark -> darkSrc |> Maybe.andThen Button.decodeMaybeLocalUrl + Theme.Light -> lightSrc |> Maybe.andThen Button.decodeMaybeLocalUrl ) (D.field "dark" <| D.maybe D.string) (D.field "light" <| D.maybe D.string) diff --git a/src/Tron/Render/Control/Button.elm b/src/Tron/Render/Control/Button.elm index dbf4165..8fdbfd4 100644 --- a/src/Tron/Render/Control/Button.elm +++ b/src/Tron/Render/Control/Button.elm @@ -82,7 +82,7 @@ viewFace theme ctx face label = Button.WithIcon (Button.Icon icon) -> let iconUrl = - icon theme |> Maybe.map Url.toString |> Maybe.withDefault "" + icon theme |> Maybe.map Button.maybeLocalUrlToString |> Maybe.withDefault "" --"./assets/" ++ icon ++ "_" ++ Theme.toString theme ++ ".svg" ( iconWidth, iconHeight ) = iconSize ctx.cellShape bounds ( iconX, iconY ) =