Simplify HelpModal

This commit is contained in:
Tulir Asokan 2020-09-13 17:16:47 +03:00
parent 2e5eeb22ed
commit 153aa34f2b
3 changed files with 26 additions and 123 deletions

2
go.mod
View File

@ -24,6 +24,6 @@ require (
gopkg.in/vansante/go-ffprobe.v2 v2.0.2 gopkg.in/vansante/go-ffprobe.v2 v2.0.2
gopkg.in/yaml.v2 v2.3.0 gopkg.in/yaml.v2 v2.3.0
maunium.net/go/mautrix v0.7.6 maunium.net/go/mautrix v0.7.6
maunium.net/go/mauview v0.1.1 maunium.net/go/mauview v0.1.2
maunium.net/go/tcell v0.2.0 maunium.net/go/tcell v0.2.0
) )

2
go.sum
View File

@ -121,5 +121,7 @@ maunium.net/go/mautrix v0.7.6 h1:jB9oCimPq0mVyolwQBC/9N1fu21AU+Ryq837cLf4gOo=
maunium.net/go/mautrix v0.7.6/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo= maunium.net/go/mautrix v0.7.6/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo=
maunium.net/go/mauview v0.1.1 h1:wfTXyPx3LGAGpTskh+UbBv/QItUWnEpaneHmywoYnfY= maunium.net/go/mauview v0.1.1 h1:wfTXyPx3LGAGpTskh+UbBv/QItUWnEpaneHmywoYnfY=
maunium.net/go/mauview v0.1.1/go.mod h1:3QBUiuLct9moP1LgDhCGIg0Ovxn38Bd2sGndnUOuj4o= maunium.net/go/mauview v0.1.1/go.mod h1:3QBUiuLct9moP1LgDhCGIg0Ovxn38Bd2sGndnUOuj4o=
maunium.net/go/mauview v0.1.2 h1:6Y3GpyckIlzCNkry6k025YhWg8oh5XJFj3RAMf4VwWo=
maunium.net/go/mauview v0.1.2/go.mod h1:3QBUiuLct9moP1LgDhCGIg0Ovxn38Bd2sGndnUOuj4o=
maunium.net/go/tcell v0.2.0 h1:1Q0kN3wCOGAIGu1r3QHADsjSUOPDylKREvCv3EzJpVg= maunium.net/go/tcell v0.2.0 h1:1Q0kN3wCOGAIGu1r3QHADsjSUOPDylKREvCv3EzJpVg=
maunium.net/go/tcell v0.2.0/go.mod h1:9Apcb3lNNS6C6lCqKT9UFp7BTRzHXfWE+/tgufsAMho= maunium.net/go/tcell v0.2.0/go.mod h1:9Apcb3lNNS6C6lCqKT9UFp7BTRzHXfWE+/tgufsAMho=

View File

@ -1,68 +1,23 @@
package ui package ui
import ( import (
"strings"
"maunium.net/go/mauview"
"maunium.net/go/tcell" "maunium.net/go/tcell"
"maunium.net/go/gomuks/debug"
"maunium.net/go/mauview"
) )
type HelpModal struct { const helpText = `# General
mauview.Component /help - Show this help dialog.
container *mauview.Box
text *mauview.TextView
scrollX int
scrollY int
maxScrollX int
maxScrollY int
textWidth int
textHeight int
parent *MainView
}
// There are only math.Min/math.Max for float64
func Max(a int, b int) int {
if a > b {
return a
}
return b
}
func Min(a int, b int) int {
if a < b {
return a
}
return b
}
func NewHelpModal(parent *MainView) *HelpModal {
hm := &HelpModal{
parent: parent,
scrollX: 0,
scrollY: 0,
maxScrollX: 0,
maxScrollY: 0,
}
helpText := `# General
/help - Show this "temporary" help message.
/quit - Quit gomuks. /quit - Quit gomuks.
/clearcache - Clear cache and quit gomuks. /clearcache - Clear cache and quit gomuks.
/logout - Log out of Matrix. /logout - Log out of Matrix.
/toggle <thing> - Temporary command to toggle various UI features. /toggle <thing> - Temporary command to toggle various UI features.
Things: rooms, users, baremessages, images, typingnotif, unverified
# Sending special messages # Sending special messages
/me <message> - Send an emote message. /me <message> - Send an emote message.
/notice <message> - Send a notice (generally used for bot messages). /notice <message> - Send a notice (generally used for bot messages).
/rainbow <message> - Send rainbow text (markdown not supported). /rainbow <message> - Send rainbow text.
/rainbowme <message> - Send rainbow text in an emote. /rainbowme <message> - Send rainbow text in an emote.
/reply [text] - Reply to the selected message. /reply [text] - Reply to the selected message.
/react <reaction> - React to the selected message. /react <reaction> - React to the selected message.
@ -105,101 +60,47 @@ Things: rooms, users, baremessages, images, typingnotif, unverified
/ban <user id> [reason] - Ban a user. /ban <user id> [reason] - Ban a user.
/unban <user id> - Unban a user.` /unban <user id> - Unban a user.`
split := strings.Split(helpText, "\n") type HelpModal struct {
hm.textHeight = len(split) mauview.FocusableComponent
hm.textWidth = 0 parent *MainView
}
for _, line := range split { func NewHelpModal(parent *MainView) *HelpModal {
hm.textWidth = Max(hm.textWidth, len(line)) hm := &HelpModal{parent: parent}
}
hm.text = mauview.NewTextView(). text := mauview.NewTextView().
SetText(helpText). SetText(helpText).
SetScrollable(true). SetScrollable(true).
SetWrap(false) SetWrap(false)
flex := mauview.NewFlex(). box := mauview.NewBox(text).
SetDirection(mauview.FlexRow).
AddProportionalComponent(hm.text, 1)
hm.container = mauview.NewBox(flex).
SetBorder(true). SetBorder(true).
SetTitle("Help"). SetTitle("Help").
SetBlurCaptureFunc(func() bool { SetBlurCaptureFunc(func() bool {
hm.parent.HideModal() hm.parent.HideModal()
return true return true
}) })
box.Focus()
hm.Component = mauview.Center(hm.container, 0, 0). hm.FocusableComponent = mauview.FractionalCenter(box, 42, 10, 0.5, 0.5)
SetAlwaysFocusChild(true)
return hm return hm
} }
func (hm *HelpModal) Focus() { func (hm *HelpModal) Focus() {
hm.container.Focus() debug.Print("focus")
hm.FocusableComponent.Focus()
} }
func (hm *HelpModal) Blur() { func (hm *HelpModal) Blur() {
hm.container.Blur() debug.Print("blur")
} hm.FocusableComponent.Blur()
func (hm *HelpModal) Draw(screen mauview.Screen) {
width, height := screen.Size()
width /= 2
if width < 42 {
width = 42
}
if height > 40 {
height -= 20
} else if height > 30 {
height -= 10
} else if height > 20 {
height -= 5
}
oldMaxScrollY := hm.maxScrollY
hm.maxScrollY = hm.textHeight - height + 2
hm.maxScrollX = hm.textWidth - width + 2
if hm.maxScrollY != oldMaxScrollY {
// Reset the scroll
// NOTE: Workarounds a problem where we can no longer scroll
// due to hm.scrollY being too big.
hm.scrollY = 0
hm.scrollX = 0
hm.text.ScrollTo(hm.scrollY, hm.scrollX)
}
hm.Component = mauview.Center(hm.container, width, height).
SetAlwaysFocusChild(true)
hm.Component.Draw(screen)
} }
func (hm *HelpModal) OnKeyEvent(event mauview.KeyEvent) bool { func (hm *HelpModal) OnKeyEvent(event mauview.KeyEvent) bool {
switch event.Key() { if event.Key() == tcell.KeyEscape || event.Rune() == 'q' {
case tcell.KeyUp:
hm.scrollY = Max(0, hm.scrollY-1)
hm.text.ScrollTo(hm.scrollY, hm.scrollX)
return true
case tcell.KeyDown:
hm.scrollY = Min(hm.maxScrollY, hm.scrollY+1)
hm.text.ScrollTo(hm.scrollY, hm.scrollX)
return true
case tcell.KeyLeft:
hm.scrollX = Max(0, hm.scrollX-1)
hm.text.ScrollTo(hm.scrollY, hm.scrollX)
return true
case tcell.KeyRight:
hm.scrollX = Min(hm.maxScrollX, hm.scrollX+1)
hm.text.ScrollTo(hm.scrollY, hm.scrollX)
return true
}
hm.parent.HideModal() hm.parent.HideModal()
return true return true
}
return hm.FocusableComponent.OnKeyEvent(event)
} }