Skip to content

Commit

Permalink
Embed asset files in Nix build
Browse files Browse the repository at this point in the history
It turns out files under `extra-source-files` are not found at compile time (`file-embed`) unless they're in `rsc` (and not as symlinks), and known to Git (staging makes a difference, which confused me for a while, e.g. in georgefst/hello-hs@fa41589).
  • Loading branch information
georgefst committed Nov 22, 2022
1 parent 2725c68 commit 814b4cc
Show file tree
Hide file tree
Showing 8 changed files with 32,630 additions and 1 deletion.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ dist*/
elm-stuff*/
.ghc.environment.*
cabal.project.local*
haskell/rsc
result
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
compiler-nix-name = "ghc924";
shell.tools = { cabal = { }; };
shell = { inherit crossPlatforms; };
configureArgs = "-frelease";
});
})

Expand Down
4 changes: 4 additions & 0 deletions haskell/rsc/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
/*TODO this may be redundant (except on iOS?) since we also disable scaling via the `viewport` meta tag */
touch-action: none
}
6 changes: 6 additions & 0 deletions haskell/rsc/common.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body {
background-color: rgb(207, 231, 247);
overflow: hidden;
padding: 0;
margin: 0;
}
32,376 changes: 32,376 additions & 0 deletions haskell/rsc/default.dhall

Large diffs are not rendered by default.

143 changes: 143 additions & 0 deletions haskell/rsc/elm.js

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions haskell/rsc/login.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
body {
height: 100vh;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
font-family: Helvetica, Arial, sans-serif;
}

form {
padding: 0.5em;
text-align: center;
font-size: 5vw;
}

input {
font-size: inherit;
}

.colours {
display: flex;
flex-direction: row;
}

.colour {
flex-grow: 1;
margin: 3vh;
height: 10vh;
}
72 changes: 72 additions & 0 deletions haskell/rsc/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const attr = s => document.currentScript.attributes.getNamedItem(s).value

const params = new URLSearchParams(window.location.search)
const username = params.get("username")

const wsAddress = "ws://" + location.hostname + ":" + attr("wsPort") + "?" + params
const ws = new WebSocket(wsAddress)
ws.onclose = event => {
console.log(event)
alert("Connection lost. See console for details.")
}

const layouts = JSON.parse(attr("layouts"))
const encoding = JSON.parse(attr("encoding"))

ws.onopen = _event => {
// Elm
elmInitialised = false
const app = Elm.Main.init({
flags: {
username, layouts, encoding,
supportsFullscreen: document.documentElement.requestFullscreen != null
}
})
//TODO this (waiting for an initialisation message)
// is all just a workaround for the fact that Elm has no built-in way of signalling that 'init' has finished
app.ports.initPort.subscribe(() => {
elmInitialised = true
dispatchEvent(new Event("elmInit"))
})

// fullscreen
app.ports.fullscreenPort.subscribe(() => {
document.documentElement.requestFullscreen()
})
document.onfullscreenchange = _event => {
app.ports.fullscreenChangePort.send(
!(document.fullscreenElement == null)
)
}

// update messages
app.ports.sendUpdatePortJSON.subscribe(message => ws.send(JSON.stringify(message)))
app.ports.sendUpdatePortBinary.subscribe(message => ws.send(Uint8Array.from(message)))
ws.addEventListener("message", event => {
const send = () => app.ports.receiveUpdatePort.send(JSON.parse(event.data))
if (elmInitialised) {
send()
} else {
addEventListener("elmInit", () => {
setTimeout(send, 0)
})
}
})

// reset form
app.ports.resetFormPort.subscribe(id => document.getElementById(id).reset())

// audio
app.ports.audioPort.subscribe(url => {
audio = new Audio(url)
audio.play()
})

// vibration
app.ports.vibratePort.subscribe(intervals => {
window.navigator.vibrate(intervals)
})

// logging
app.ports.logPort.subscribe(console.log)
}

0 comments on commit 814b4cc

Please sign in to comment.