Skip to content

Commit

Permalink
Draft for respecting cursor hold position
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseDreki committed Feb 23, 2024
1 parent 9c01570 commit f866877
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/nativeMain/kotlin/AppConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object AppConfig {

const val INPUT_KEY_DELAY_MILLIS = 10L
const val INPUT_KEY_DELAY_MILLIS = 30L

const val DEFAULT_TAG_WIDTH = 23

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@ import dogcat.LogFilter.ByPackage
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

data class AppState(
data class AppStateHolder(

val autoscroll: Boolean,

val packageFilter: Pair<ByPackage?, Boolean>,

val inputFilterLocation: Pair<Int, Int>
val inputFilterLocation: Pair<Int, Int>,

//val linesCount: Int,
val isCursorHeld: Boolean
)

interface AppStateFlow {
interface AppState {

val state: StateFlow<AppState>
val state: StateFlow<AppStateHolder>

fun autoscroll(on: Boolean)

fun filterByPackage(f: ByPackage?, enable: Boolean)

fun setInputFilterLocation(x: Int, y: Int)

fun holdCursor(hold: Boolean)
}

class InternalAppStateFlow : AppStateFlow {
class InternalAppState : AppState {

override val state = MutableStateFlow(AppState(false, null to false, 0 to 0))
override val state = MutableStateFlow(
AppStateHolder(
false,
null to false,
0 to 0,
false
)
)

override fun autoscroll(on: Boolean) {
state.value = state.value.copy(autoscroll = on)
Expand All @@ -36,12 +45,16 @@ class InternalAppStateFlow : AppStateFlow {
//if (f != null) {
// state.value = state.value.copy(packageFilter = f to true)
//} else {
//state.value = state.value.copy(packageFilter = state.value.packageFilter.first to enable)
state.value = state.value.copy(packageFilter = f to enable)
//state.value = state.value.copy(packageFilter = state.value.packageFilter.first to enable)
state.value = state.value.copy(packageFilter = f to enable)
//}
}

override fun setInputFilterLocation(x: Int, y: Int) {
state.value = state.value.copy(inputFilterLocation = x to y)
}

override fun holdCursor(hold: Boolean) {
state.value = state.value.copy(isCursorHeld = hold)
}
}
8 changes: 3 additions & 5 deletions src/nativeMain/kotlin/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package di

import AppStateFlow
import AppState
import BuildConfig
import FileLogger
import InternalAppStateFlow
import InternalAppState
import di.DogcatModule.dogcatModule
import kotlinx.coroutines.CloseableCoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import logger.CanLog
import logger.NoOpLogger
import org.kodein.di.DI
Expand All @@ -31,7 +29,7 @@ class AppModule(ui: CloseableCoroutineDispatcher) {
}

//bindSingleton<Input> { DefaultInput(Dispatchers.IO) }
bindSingleton<AppStateFlow> { InternalAppStateFlow() }
bindSingleton<AppState> { InternalAppState() }
bindSingleton<Input> { DefaultInput(instance()) }
bindSingleton<AppPresenter> {
AppPresenter(
Expand Down
2 changes: 0 additions & 2 deletions src/nativeMain/kotlin/di/DogcatModule.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package di

import AdbShell
import AppStateFlow
import InternalAppStateFlow
import dogcat.*
import dogcat.state.DefaultAppliedFiltersState
import kotlinx.coroutines.Dispatchers
Expand Down
15 changes: 7 additions & 8 deletions src/nativeMain/kotlin/ui/AppPresenter.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package ui

import AppStateFlow
import AppState
import dogcat.Command.*
import dogcat.Dogcat
import dogcat.LogFilter.*
import dogcat.LogLevel.*
import dogcat.state.PublicState
import dogcat.state.PublicState.Active
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.*
Expand All @@ -24,7 +23,7 @@ import kotlin.coroutines.coroutineContext

class AppPresenter(
private val dogcat: Dogcat,
private val appStateFlow: AppStateFlow,
private val appState: AppState,
private val input: Input,
private val logLinesPresenter: LogLinesPresenter,
private val statusPresenter: StatusPresenter,
Expand All @@ -38,7 +37,7 @@ class AppPresenter(
else -> dogcat(Start.PickAllApps)
}

appStateFlow.setInputFilterLocation("Filter: ".length, 49)
appState.setInputFilterLocation("Filter: ".length, 49)

view.start()

Expand Down Expand Up @@ -108,24 +107,24 @@ class AppPresenter(
when (Keymap.bindings[keyCode]) {

Autoscroll -> {
appStateFlow.autoscroll(!appStateFlow.state.value.autoscroll)
appState.autoscroll(!appState.state.value.autoscroll)
}

ClearLogs -> {
dogcat(ClearLogSource)
}

ToggleFilterByPackage -> {
val f = appStateFlow.state.value.packageFilter
val f = appState.state.value.packageFilter

if (f.second) {
Logger.d("${context()} !DeselectSelectAppByPackage")
appStateFlow.filterByPackage(f.first, false)
appState.filterByPackage(f.first, false)
dogcat(ResetFilter(ByPackage::class))
} else if (f.first != null) {
Logger.d("${context()} !SelectAppByPackage")
dogcat(Start.PickAppPackage(f.first!!.packageName))
appStateFlow.filterByPackage(f.first, true)
appState.filterByPackage(f.first, true)
}
}

Expand Down
25 changes: 17 additions & 8 deletions src/nativeMain/kotlin/ui/logLines/LogLinesPresenter.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ui.logLines

import AppStateFlow
import AppState
import dogcat.Dogcat
import dogcat.Unparseable
import dogcat.state.PublicState.*
Expand All @@ -9,6 +9,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import logger.Logger
import logger.context
Expand All @@ -20,7 +21,7 @@ import kotlin.coroutines.coroutineContext

class LogLinesPresenter(
private val dogcat: Dogcat,
private val appStateFlow: AppStateFlow,
private val appState: AppState,
private val input: Input,
) : HasLifecycle {
//views can come and go, when input disappears
Expand All @@ -38,6 +39,14 @@ class LogLinesPresenter(
scope.launch {
collectKeypresses()
}

scope.launch {
appState.state
.map { it.isCursorHeld }
.collect {
view.isCursorHeld = it
}
}
}

override suspend fun stop() {
Expand Down Expand Up @@ -99,36 +108,36 @@ class LogLinesPresenter(
Logger.d("${context()} Log lines key")
when (Keymap.bindings[it]) {
Home -> {
appStateFlow.autoscroll(false)
appState.autoscroll(false)
view.autoscroll = false
view.home()
}

End -> {
appStateFlow.autoscroll(true)
appState.autoscroll(true)
view.autoscroll = true
view.end()
}

LineUp -> {
appStateFlow.autoscroll(false)
appState.autoscroll(false)
view.autoscroll = false
view.lineUp()
}

LineDown -> {
appStateFlow.autoscroll(false)
appState.autoscroll(false)
view.autoscroll = false //?
view.lineDown(1)
}

PageDown -> {
val a = appStateFlow.state.value.autoscroll
val a = appState.state.value.autoscroll
view.pageDown()
}

PageUp -> {
appStateFlow.autoscroll(false)
appState.autoscroll(false)
view.autoscroll = false
view.pageUp()
}
Expand Down
53 changes: 45 additions & 8 deletions src/nativeMain/kotlin/ui/logLines/LogLinesView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import kotlin.math.min

@OptIn(ExperimentalForeignApi::class)
class LogLinesView {
var isCursorHeld: Boolean = false

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

Expand Down Expand Up @@ -182,18 +184,53 @@ class LogLinesView {

fun refresh() {
//logger.Logger.d("FVL $firstVisibleLine")
curs_set(0)
val notSeeingLastLine = firstVisibleLine <= linesCount - pageSize

/**/

/* when {
isCursorHeld -> {
Logger.d("cursor is held")
//curs_set(0)
}
notSeeingLastLine -> {
curs_set(0)
}
else -> {
curs_set(1)
}
}*/

prefresh(pad, firstVisibleLine, 0, position.startY, position.startX, position.endY, position.endX)
//call doupdate with pnoutrefresh

when {
isCursorHeld -> {
wmove(stdscr, 49, "Filter: ".length)
curs_set(1)
wrefresh(stdscr)
}

!notSeeingLastLine -> {
curs_set(1)
}

else -> {
curs_set(0)
}
}

val notSeeingLastLine = firstVisibleLine <= linesCount - pageSize
if (notSeeingLastLine) {
//curs_set(0)
//Logger.d("Cursor is hidden")
/*if (!isCursorHeld && !notSeeingLastLine) {
curs_set(1)
} else {
//wmove(pad, firstVisibleLine, 0)
//curs_set(1)
//Logger.d("Cursor is visible")
curs_set(0)
}
if (isCursorHeld) {
wmove(stdscr, 49, "Filter: ".length)
curs_set(1)
wrefresh(stdscr)
}*/
}

suspend fun recordLine(count: Int = 1) {
Expand Down
13 changes: 5 additions & 8 deletions src/nativeMain/kotlin/ui/status/StatusPresenter.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package ui.status

import AppStateFlow
import AppState
import userInput.Input
import userInput.Keymap.Actions.*
import dogcat.Command
import dogcat.Command.FilterBy
import dogcat.Dogcat
import dogcat.LogFilter
Expand All @@ -13,12 +11,11 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import logger.Logger
import logger.context
import userInput.Keymap
import kotlin.coroutines.coroutineContext

class StatusPresenter(
private val dogcat: Dogcat,
private val appStateFlow: AppStateFlow,
private val appState: AppState,
private val input: Input
) {
//views can come and go, when input disappears
Expand All @@ -40,7 +37,7 @@ class StatusPresenter(

Logger.d("${context()} Update filters in pres")
it[LogFilter.ByPackage::class]?.let {
appStateFlow.filterByPackage(it as LogFilter.ByPackage, true)
appState.filterByPackage(it as LogFilter.ByPackage, true)
}
}
.launchIn(scope)
Expand All @@ -51,7 +48,7 @@ class StatusPresenter(
.filterIsInstance<Active>()
.mapLatest { it }
.onEach {
view.updateAutoscroll(appStateFlow.state.value.autoscroll)
view.updateAutoscroll(appState.state.value.autoscroll)

Logger.d("${context()} !Emulator in pres ${it.deviceName}")
view.updateDevice(it.deviceName, true)
Expand Down Expand Up @@ -79,7 +76,7 @@ class StatusPresenter(



appStateFlow
appState
.state
.onEach {
Logger.d("${context()} autoscroll in pres ${it.autoscroll}")
Expand Down
Loading

0 comments on commit f866877

Please sign in to comment.