From 95aa89ee97872caa98669f2e64e6b718ce5cf2e8 Mon Sep 17 00:00:00 2001
From: Přemysl Eric Janouch <p@janouch.name>
Date: Sat, 10 Sep 2022 20:38:32 +0200
Subject: xP: bind M-h to toggle history, and adjust focus

---
 xP/public/xP.js | 52 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 31 insertions(+), 21 deletions(-)

(limited to 'xP/public')

diff --git a/xP/public/xP.js b/xP/public/xP.js
index c2e0e03..4c740ba 100644
--- a/xP/public/xP.js
+++ b/xP/public/xP.js
@@ -137,7 +137,7 @@ let bufferCurrent = undefined
 let bufferLog = undefined
 let bufferAutoscroll = true
 
-function resetBufferStats(b) {
+function bufferResetStats(b) {
 	b.newMessages = 0
 	b.newUnimportantMessages = 0
 	b.highlighted = false
@@ -147,6 +147,29 @@ function bufferActivate(name) {
 	rpc.send({command: 'BufferActivate', bufferName: name})
 }
 
+function bufferToggleLog() {
+	if (bufferLog) {
+		setTimeout(() =>
+			document.getElementById('input')?.focus())
+
+		bufferLog = undefined
+		m.redraw()
+		return
+	}
+
+	let name = bufferCurrent
+	rpc.send({
+		command: 'BufferLog',
+		bufferName: name,
+	}).then(resp => {
+		if (bufferCurrent !== name)
+			return
+
+		bufferLog = rpc.base64decode(resp.log)
+		m.redraw()
+	})
+}
+
 let connecting = true
 rpc.connect().then(result => {
 	buffers.clear()
@@ -175,7 +198,7 @@ rpc.addEventListener('BufferUpdate', event => {
 	let e = event.detail, b = buffers.get(e.bufferName)
 	if (b === undefined) {
 		buffers.set(e.bufferName, (b = {lines: []}))
-		resetBufferStats(b)
+		bufferResetStats(b)
 	}
 	b.hideUnimportant = e.hideUnimportant
 })
@@ -206,7 +229,7 @@ rpc.addEventListener('BufferRemove', event => {
 rpc.addEventListener('BufferActivate', event => {
 	let old = buffers.get(bufferCurrent)
 	if (old !== undefined)
-		resetBufferStats(old)
+		bufferResetStats(old)
 
 	bufferLast = bufferCurrent
 	let e = event.detail, b = buffers.get(e.bufferName)
@@ -307,26 +330,11 @@ let Toolbar = {
 		bufferAutoscroll = !bufferAutoscroll
 	},
 
-	toggleLog: () => {
-		if (bufferLog) {
-			bufferLog = undefined
-			return
-		}
-
-		rpc.send({
-			command: 'BufferLog',
-			bufferName: bufferCurrent,
-		}).then(resp => {
-			bufferLog = rpc.base64decode(resp.log)
-			m.redraw()
-		})
-	},
-
 	view: vnode => {
 		return m('.toolbar', {}, [
 			m('button', {onclick: Toolbar.toggleAutoscroll},
 				bufferAutoscroll ? 'Scroll lock' : 'Scroll unlock'),
-			m('button', {onclick: Toolbar.toggleLog},
+			m('button', {onclick: event => bufferToggleLog()},
 				bufferLog === undefined ? 'Show log' : 'Hide log'),
 		])
 	},
@@ -500,8 +508,8 @@ let Buffer = {
 
 let Log = {
 	oncreate: vnode => {
-		if (vnode.dom !== undefined)
-			vnode.dom.scrollTop = vnode.dom.scrollHeight
+		vnode.dom.scrollTop = vnode.dom.scrollHeight
+		vnode.dom.focus()
 	},
 
 	linkify: text => {
@@ -629,6 +637,8 @@ document.addEventListener('keydown', event => {
 	if (event.altKey && event.key == 'Tab') {
 		if (bufferLast !== undefined)
 			bufferActivate(bufferLast)
+	} else if (event.altKey && event.key == 'h') {
+		bufferToggleLog()
 	} else if (event.altKey && event.key == 'a') {
 		for (const [name, b] of buffers)
 			if (name !== bufferCurrent && b.newMessages) {
-- 
cgit v1.2.3-70-g09d2