From dc5c384b9e4b3ad233e5f8f652b62d7301b39023 Mon Sep 17 00:00:00 2001 From: Alex Dmitriev Date: Thu, 23 May 2024 12:41:14 +0300 Subject: [PATCH] Add Help menu item to status bar --- .../com/norsedreki/dogcat/app/Keymap.kt | 2 +- .../norsedreki/dogcat/app/ui/CommonColors.kt | 1 + .../dogcat/app/ui/help/HelpPresenter.kt | 11 +++--- .../norsedreki/dogcat/app/ui/help/HelpView.kt | 37 ++++++++++++------- .../dogcat/app/ui/status/StatusView.kt | 15 ++++++++ 5 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/Keymap.kt b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/Keymap.kt index 9b06806..97ae6e1 100644 --- a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/Keymap.kt +++ b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/Keymap.kt @@ -35,7 +35,7 @@ object Keymap { @OptIn(ExperimentalForeignApi::class) val bindings = mapOf( - 'h'.code to HELP, + '?'.code to HELP, 'r'.code to AUTOSCROLL, 'q'.code to QUIT, 'f'.code to INPUT_FILTER_BY_SUBSTRING, diff --git a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/CommonColors.kt b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/CommonColors.kt index bac6f10..7a4c0c9 100644 --- a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/CommonColors.kt +++ b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/CommonColors.kt @@ -12,6 +12,7 @@ import ncurses.COLOR_RED import ncurses.COLOR_WHITE import ncurses.COLOR_YELLOW +@Suppress("detekt.MagicNumber") @ExperimentalForeignApi enum class CommonColors( val colorPairCode: Int, diff --git a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpPresenter.kt b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpPresenter.kt index 0b2a5a7..cac481e 100644 --- a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpPresenter.kt +++ b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpPresenter.kt @@ -13,16 +13,13 @@ import com.norsedreki.dogcat.app.ui.Input import kotlin.coroutines.coroutineContext import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch +import kotlinx.coroutines.yield class HelpPresenter( private val input: Input, private val appState: AppState, ) : HasLifecycle { - private lateinit var view: HelpView - - private var showing = false - override suspend fun start() { val scope = CoroutineScope(coroutineContext) @@ -34,6 +31,10 @@ class HelpPresenter( } private suspend fun collectKeypresses() { + lateinit var view: HelpView + + var showing = false + input.keypresses.collect { key -> when (Keymap.bindings[key]) { HELP -> { @@ -45,6 +46,7 @@ class HelpPresenter( view = HelpView() view.start() + yield() view.state = HelpView.State(h) showing = true @@ -55,7 +57,6 @@ class HelpPresenter( appState.holdUi(false) } } - else -> { // Other keys are handled elsewhere } diff --git a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpView.kt b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpView.kt index 73495ec..5d36ac8 100644 --- a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpView.kt +++ b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/help/HelpView.kt @@ -6,7 +6,6 @@ package com.norsedreki.dogcat.app.ui.help import com.norsedreki.dogcat.app.ui.HasLifecycle -import com.norsedreki.logger.Logger import kotlin.properties.Delegates import kotlinx.cinterop.CPointer import kotlinx.cinterop.ExperimentalForeignApi @@ -26,9 +25,7 @@ import ncurses.wresize @OptIn(ExperimentalForeignApi::class) class HelpView : HasLifecycle { - data class State( - val text: List = listOf() - ) + data class State(val text: List = listOf()) var state: State by Delegates.observable(State()) { _, _, newValue -> updateView(newValue) } @@ -38,22 +35,26 @@ class HelpView : HasLifecycle { 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 horizontalPadding = 4 // adjust this value as needed + val verticalPadding = 2 // adjust this value as needed + + val maxWidth = + maxOf(state.text.maxOf { it.length }, "Help: hotkeys".length) + horizontalPadding * 2 + + val height = + state.text.size + + verticalPadding * 2 + + 2 // add extra lines for the title and blank line val sy = getmaxy(stdscr) val sx = getmaxx(stdscr) @@ -63,15 +64,23 @@ class HelpView : HasLifecycle { 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 + // Add title + val title = "Help: hotkeys" + val titleStartX = (maxWidth - title.length) / 2 + mvwaddstr(window, verticalPadding, titleStartX, title) + + // Add text state.text.forEachIndexed { index, line -> - mvwaddstr(window, index + padding, padding, line) + mvwaddstr( + window, + index + verticalPadding + 2, + horizontalPadding, + line, + ) // start from the third line } wrefresh(window) - - Logger.d("HELP view refreshed") } } diff --git a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/status/StatusView.kt b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/status/StatusView.kt index 8f35298..e7d0037 100644 --- a/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/status/StatusView.kt +++ b/src/nativeMain/kotlin/com/norsedreki/dogcat/app/ui/status/StatusView.kt @@ -72,6 +72,7 @@ class StatusView : HasLifecycle { updatePackageName(state.packageName) updateFilters(state.filters) updateAutoscroll(state.autoscroll) + updateHelp() wrefresh(window) @@ -135,6 +136,20 @@ class StatusView : HasLifecycle { wattroff(window, COLOR_PAIR(BLACK_ON_WHITE.colorPairCode)) } + private fun updateHelp() { + //wattron(window, COLOR_PAIR(GREEN_ON_WHITE.colorPairCode)) + wattron(window, COLOR_PAIR(BLACK_ON_WHITE.colorPairCode)) + //wattron(window, A_BOLD.toInt()) + + val s = "| (?) Help" + val offsetX = STATUS_VIEW_AUTOSCROLL_LEFT_MARGIN + NO_AUTOSCROLL.length + 2 + mvwprintw(window, 0, offsetX, s) + + //wattroff(window, A_BOLD.toInt()) + wattroff(window, COLOR_PAIR(BLACK_ON_WHITE.colorPairCode)) + //wattroff(window, COLOR_PAIR(GREEN_ON_WHITE.colorPairCode)) + } + private fun updateDevice(device: String, running: Boolean) { curs_set(0)