Skip to content

Commit

Permalink
to still use URL's for icons, introduce a hack for elm/url#10
Browse files Browse the repository at this point in the history
  • Loading branch information
shamansir committed Nov 13, 2021
1 parent 1106227 commit e3323f2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
63 changes: 58 additions & 5 deletions src/Tron/Control/Impl/Button.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
8 changes: 4 additions & 4 deletions src/Tron/Control/Json/Button.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
]
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Tron/Render/Control/Button.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) =
Expand Down

0 comments on commit e3323f2

Please sign in to comment.