Skip to content

Commit

Permalink
Support toggling mouse handling
Browse files Browse the repository at this point in the history
v1.15.1

We really only support mouse scroll, but it is pretty nice to have. I'm
not sure why we didn't have it enabled by default, but it's now
configurable (like vim) so....
  • Loading branch information
dhleong committed Feb 2, 2020
1 parent de917dc commit 6e4de46
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ ext.deps = [

ext.config = [
name: 'Judo',
version: '1.15.0',
version: '1.15.1',
]
1 change: 1 addition & 0 deletions core/src/main/kotlin/net/dhleong/judo/modes/BaseCmdMode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.coroutines.withContext
import net.dhleong.judo.ALL_SETTINGS
import net.dhleong.judo.IJudoCore
import net.dhleong.judo.JudoRendererInfo
import net.dhleong.judo.MOUSE
import net.dhleong.judo.Setting
import net.dhleong.judo.alias.AliasProcesser
import net.dhleong.judo.complete.CompletionSource
Expand Down
18 changes: 17 additions & 1 deletion jline/src/main/kotlin/net/dhleong/judo/jline/JLineRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package net.dhleong.judo.jline

import net.dhleong.judo.BlockingKeySource
import net.dhleong.judo.CursorType
import net.dhleong.judo.IStateMap
import net.dhleong.judo.JudoRendererEvent
import net.dhleong.judo.JudoRendererInfo
import net.dhleong.judo.MOUSE
import net.dhleong.judo.OnRendererEventListener
import net.dhleong.judo.StateKind
import net.dhleong.judo.StateMap
import net.dhleong.judo.WORD_WRAP
import net.dhleong.judo.inTransaction
Expand Down Expand Up @@ -44,10 +47,21 @@ const val KEY_TIMEOUT = -2
class JLineRenderer(
private val ids: IdManager,
override var settings: StateMap,
private val enableMouse: Boolean = false,
private val enableMouse: Boolean = settings[MOUSE],
private val renderSurface: JLineDisplay = JLineDisplay(0, 0)
) : IJLineRenderer, BlockingKeySource {

private val mouseEnabledListener = object : IStateMap.Listener<Boolean> {
override fun onChanged(key: StateKind<Boolean>, newValue: Boolean) = inTransaction {
if (newValue) {
terminal.trackMouse(Terminal.MouseTracking.Normal)
} else {
terminal.trackMouse(Terminal.MouseTracking.Off)
}
terminal.flush()
}
}

private val terminal = TerminalBuilder.terminal()!!
private val window = Display(terminal, true)
private val originalAttributes: Attributes
Expand Down Expand Up @@ -126,6 +140,8 @@ class JLineRenderer(
capabilities.add(JudoRendererInfo.Capabilities.COLOR_256)
}
}

settings.addListener(MOUSE, mouseEnabledListener)
}

override fun validate() {
Expand Down
3 changes: 3 additions & 0 deletions model/src/main/kotlin/net/dhleong/judo/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ val ALL_SETTINGS = mutableMapOf<String, Setting<*>>()
val CLIPBOARD = declareSetting("clipboard", "",
"If set to 'unnamed', cuts and pastes will default to the * register")

val MOUSE = declareSetting("mouse", false,
"If true, mouse scroll events will be handled")

val WORD_WRAP = declareSetting("wordwrap", true,
"If false, output will go up to the edge of the window and words will be split.")

Expand Down
36 changes: 35 additions & 1 deletion model/src/main/kotlin/net/dhleong/judo/StateMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ open class StateKind<E>(val name: String) {
}

interface IStateMap {
interface Listener<E : Any> {
fun onChanged(key: StateKind<E>, newValue: E)
}

fun <E : Any> addListener(key: StateKind<E>, listener: Listener<E>)
fun <E : Any> removeListener(key: StateKind<E>, listener: Listener<E>)

operator fun <E : Any> set(key: StateKind<E>, value: E)

operator fun <E : Any> get(key: StateKind<E>): E?
Expand All @@ -19,6 +26,14 @@ interface IStateMap {
}

object EmptyStateMap : IStateMap {
override fun <E : Any> addListener(key: StateKind<E>, listener: IStateMap.Listener<E>) {
// nop
}

override fun <E : Any> removeListener(key: StateKind<E>, listener: IStateMap.Listener<E>) {
// nop
}

override fun <E : Any> set(key: StateKind<E>, value: E) {}

override fun <E : Any> get(key: StateKind<E>): E? = null
Expand All @@ -39,14 +54,33 @@ object EmptyStateMap : IStateMap {
*/
@Suppress("UNCHECKED_CAST")
class StateMap() : IStateMap {
private val map = HashMap<StateKind<*>, Any>()
private val map = mutableMapOf<StateKind<*>, Any>()
private val listeners = mutableMapOf<StateKind<*>, MutableSet<IStateMap.Listener<*>>>()

constructor(vararg pairs: Pair<StateKind<*>, Any>): this() {
map.putAll(pairs)
}

override fun <E : Any> addListener(key: StateKind<E>, listener: IStateMap.Listener<E>) {
val set = listeners[key] ?: mutableSetOf<IStateMap.Listener<*>>().also {
listeners[key] = it
}

set += listener
}

override fun <E : Any> removeListener(key: StateKind<E>, listener: IStateMap.Listener<E>) {
val set = listeners[key] ?: return
set -= listener
}

override operator fun <E : Any> set(key: StateKind<E>, value: E) {
map[key] = value

val set = listeners[key] ?: return
for (l in set) {
(l as IStateMap.Listener<E>).onChanged(key, value)
}
}

override operator fun <E : Any> get(key: StateKind<E>): E? =
Expand Down

0 comments on commit 6e4de46

Please sign in to comment.