Skip to content

Commit

Permalink
Implement Help window
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseDreki committed May 23, 2024
1 parent 3a913b7 commit 607cb75
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 16 deletions.
12 changes: 10 additions & 2 deletions src/nativeMain/kotlin/com/norsedreki/dogcat/app/AppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ data class AppStateHolder(
val autoscroll: Boolean,
val packageFilter: Pair<ByPackage?, Boolean>,
val userInputLocation: Pair<Int, Int>,
val isCursorHeld: Boolean
val isCursorHeld: Boolean,
val isUiHeld: Boolean
)

interface AppState {
Expand All @@ -27,6 +28,8 @@ interface AppState {
fun setUserInputLocation(x: Int, y: Int)

fun holdCursor(hold: Boolean)

fun holdUi(hold: Boolean)
}

class InternalAppState : AppState {
Expand All @@ -38,7 +41,8 @@ class InternalAppState : AppState {
null to false,
0 to 0,
false,
)
false,
),
)

override fun autoscroll(on: Boolean) {
Expand All @@ -56,4 +60,8 @@ class InternalAppState : AppState {
override fun holdCursor(hold: Boolean) {
state.value = state.value.copy(isCursorHeld = hold)
}

override fun holdUi(hold: Boolean) {
state.value = state.value.copy(isUiHeld = hold)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ class AppModule {
)
}
bindSingleton<HelpPresenter> {
HelpPresenter()
HelpPresenter(
instance(),
instance(),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,61 @@

package com.norsedreki.dogcat.app.ui.help

import com.norsedreki.dogcat.app.AppState
import com.norsedreki.dogcat.app.Keymap
import com.norsedreki.dogcat.app.Keymap.Actions.HELP
import com.norsedreki.dogcat.app.ui.HasLifecycle
import com.norsedreki.dogcat.app.ui.Input
import kotlin.coroutines.coroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class HelpPresenter : HasLifecycle {
class HelpPresenter(
private val input: Input,
private val appState: AppState,
) : HasLifecycle {

private lateinit var view: HelpView

override suspend fun start() {
view = HelpView()
view.start()

view.state = HelpView.State("")
private var showing = false

override suspend fun start() {
val scope = CoroutineScope(coroutineContext)

// scope.launch { collectAutoscroll() }
scope.launch { collectKeypresses() }
}

override suspend fun stop() {
if (this::view.isInitialized) {
view.stop()
// No op since views come and go along with hotkey for help.
}

private suspend fun collectKeypresses() {
input.keypresses.collect { key ->
when (Keymap.bindings[key]) {
HELP -> {
val h = Keymap.bindings.entries.map { "${it.value.name} -- '${Char(it.key)}'" }

if (!showing) {
appState.holdUi(true)

view = HelpView()
view.start()

view.state = HelpView.State(h)

showing = true
} else {
showing = false
view.stop()

appState.holdUi(false)
}
}

else -> {
// Other keys are handled elsewhere
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,73 @@

package com.norsedreki.dogcat.app.ui.help

import com.norsedreki.dogcat.app.AppConfig.STATUS_VIEW_BOTTOM_MARGIN
import com.norsedreki.dogcat.app.ui.HasLifecycle
import com.norsedreki.logger.Logger
import kotlin.properties.Delegates
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import ncurses.WINDOW
import ncurses.box
import ncurses.delwin
import ncurses.getmaxx
import ncurses.getmaxy
import ncurses.mvwaddstr
import ncurses.mvwin
import ncurses.newwin
import ncurses.stdscr
import ncurses.werase
import ncurses.wrefresh
import ncurses.wresize

@OptIn(ExperimentalForeignApi::class)
class HelpView : HasLifecycle {

data class State(
val packageName: String = "",
val text: List<String> = listOf()
)

var state: State by Delegates.observable(State()) { _, _, newValue -> updateView(newValue) }

private lateinit var window: CPointer<WINDOW>

override suspend fun start() {
val sy = getmaxy(stdscr)
window = newwin(0, 0, sy - STATUS_VIEW_BOTTOM_MARGIN, 0)!!
val sy = getmaxy(stdscr) / 2
val sx = getmaxx(stdscr) / 2


window = newwin(1, 1, sy, sx)!!
werase(window) // clear the window
wrefresh(window) // refresh the window to apply the clearing

}

override suspend fun stop() {
//werase(window)
delwin(window)
}

private fun updateView(state: State) {
val padding = 2 // adjust this value as needed
val maxWidth = state.text.maxOf { it.length } + padding * 2
val height = state.text.size + padding * 2

val sy = getmaxy(stdscr)
val sx = getmaxx(stdscr)

val startY = (sy - height) / 2
val startX = (sx - maxWidth) / 2

wresize(window, height, maxWidth)
mvwin(window, startY, startX)
Logger.d("UPDATE HELP VIEW: $startX, $startY, $maxWidth, $height")
box(window, 0U, 0U) // draw a box around the window

state.text.forEachIndexed { index, line ->
mvwaddstr(window, index + padding, padding, line)
}

wrefresh(window)

Logger.d("HELP view refreshed")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class LogLinesPresenter(
tagWidth = appArguments.tagWidth ?: DEFAULT_TAG_WIDTH,
isCursorHeld = it.isCursorHeld,
cursorReturnLocation = it.userInputLocation,
isUiHeld = it.isUiHeld,
)
}
}
Expand Down

0 comments on commit 607cb75

Please sign in to comment.