diff options
author | romangraef <romangraef@users.noreply.github.com> | 2021-08-12 23:37:33 +0000 |
---|---|---|
committer | romangraef <romangraef@users.noreply.github.com> | 2021-08-12 23:37:33 +0000 |
commit | bb618136911c338a926496dfb6971aa86f7d87c2 (patch) | |
tree | a57c587eca5bde9ea02996a002d9eaf3c269a3cb /src/jsMain/kotlin/WebOS.kt | |
parent | 09dbaa8df9aec51cf41694266574ad330963dfbe (diff) | |
download | webos-bb618136911c338a926496dfb6971aa86f7d87c2.tar.gz webos-bb618136911c338a926496dfb6971aa86f7d87c2.tar.bz2 webos-bb618136911c338a926496dfb6971aa86f7d87c2.zip |
Automated deployment: Thu Aug 12 23:37:33 UTC 2021 b6187f47b9e969ce311de73de1c6b32d970fd69f
Diffstat (limited to 'src/jsMain/kotlin/WebOS.kt')
-rw-r--r-- | src/jsMain/kotlin/WebOS.kt | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/jsMain/kotlin/WebOS.kt b/src/jsMain/kotlin/WebOS.kt new file mode 100644 index 0000000..b7c4cfb --- /dev/null +++ b/src/jsMain/kotlin/WebOS.kt @@ -0,0 +1,82 @@ +import io.IOHandler +import io.Path +import kotlinx.browser.document +import kotlinx.browser.window +import org.w3c.dom.Element +import org.w3c.dom.asList + +fun main() { + console.log("Hello from Kotlin") + val webos = WebOS() + document.body?.addEventListener("load", { + document.body?.querySelectorAll(".webosconsole")?.asList()?.forEach { + if (it !is Element) return@forEach + webos.registerConsole(it) + } + }) +} + +data class CharacterRun(val text: String, val color: String) + +abstract class Activity(val console: Console) { + abstract fun render(columns: Int, rows: Int): List<List<CharacterRun>> +} + +class Console(val os: WebOS, val renderElement: Element?) { + val isVirtual get() = renderElement == null + val activityStack = ArrayDeque<Activity>() + + var columns: Int = 80 + var rows: Int = 46 + + var shouldRerender = true + + var currentUser: User? = null + + private var _workingDirectory: Path.Absolute? = null + + var workingDirectory: Path.Absolute + get() = _workingDirectory ?: currentUser?.homeDirectory ?: Path.root + set(value) { + _workingDirectory = value + } + + fun openActivity(activity: Activity) { + activityStack.addLast(activity) + invalidateRender() + } + + fun render() { + if (renderElement == null) return + if (!shouldRerender) return + shouldRerender = false + activityStack.last() + } + + fun invalidateRender() { + shouldRerender = true + window.requestAnimationFrame { render() } + } + + fun resize(newColumns: Int, newRows: Int) { + invalidateRender() + } + + // TODO: Handle resizes of the renderElement + +} + +class WebOS { + private val _consoles = mutableListOf<Console>() + val consoles get() = _consoles.toList() + val files = IOHandler() + fun registerConsole(element: Element) { + _consoles.add(Console(this, element)) + } +} + +data class User( + val name: String, + val homeDirectory: Path.Absolute +) + |