~whereswaldon/sprig

d38927ba44d5eb2cb5534617c26c5b02bd9c07a5 — Chris Waldon 4 days ago c852c94
wip: listen for keyboard shortcuts on desktop only

This commit implements listening for keyboard events, but uses
conditional compilation to suppress this behavior on mobile. Otherwise
the on-screen keyboard opens constantly because the application is
requesting keyboard input.
5 files changed, 41 insertions(+), 6 deletions(-)

M go.mod
M go.sum
M reply-view.go
A reply-view_desktop.go
A reply-view_mobile.go
M go.mod => go.mod +1 -1
@@ 7,7 7,7 @@ require (
	git.sr.ht/~whereswaldon/colorpicker v0.0.0-20200801012301-b0b7a5822cd7
	git.sr.ht/~whereswaldon/forest-go v0.0.0-20200625210621-d3d4a318419f
	git.sr.ht/~whereswaldon/materials v0.0.0-20200801012148-3e241edb74da
	git.sr.ht/~whereswaldon/niotify v0.0.4-0.20200801012408-6296d10fa0f8
	git.sr.ht/~whereswaldon/niotify v0.0.4-0.20200801210550-599c1e9a9702
	git.sr.ht/~whereswaldon/sprout-go v0.0.0-20200517010141-a4188845a9a8
	golang.org/x/crypto v0.0.0-20191122220453-ac88ee75c92c
	golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5

M go.sum => go.sum +2 -2
@@ 9,8 9,8 @@ git.sr.ht/~whereswaldon/forest-go v0.0.0-20200625210621-d3d4a318419f h1:GQDJ9ExB
git.sr.ht/~whereswaldon/forest-go v0.0.0-20200625210621-d3d4a318419f/go.mod h1:aGmm4R7ifFBvJWOHINDvZcKVOu+ODkD75NmNm/O0zME=
git.sr.ht/~whereswaldon/materials v0.0.0-20200801012148-3e241edb74da h1:3ZWEyZ+eFYs/fGt7irRQDv4RFJes6cLk5UgQBjyMlo8=
git.sr.ht/~whereswaldon/materials v0.0.0-20200801012148-3e241edb74da/go.mod h1:gnQ47FJyeM34hX5zLHGlQsJSNvcWsMoF+SQiyT+YLdU=
git.sr.ht/~whereswaldon/niotify v0.0.4-0.20200801012408-6296d10fa0f8 h1:YlIq/od6L6v1iumZ01bZswO0uycDF4TGXit1xtLpcec=
git.sr.ht/~whereswaldon/niotify v0.0.4-0.20200801012408-6296d10fa0f8/go.mod h1:NE3saRSb5uAz64TIkOlRTrFbctt4chJhlQpD59/nsko=
git.sr.ht/~whereswaldon/niotify v0.0.4-0.20200801210550-599c1e9a9702 h1:br/YWITnCKHPzUm0XyFr/CehQC6kH+fWi+p5DNNxraI=
git.sr.ht/~whereswaldon/niotify v0.0.4-0.20200801210550-599c1e9a9702/go.mod h1:NE3saRSb5uAz64TIkOlRTrFbctt4chJhlQpD59/nsko=
git.sr.ht/~whereswaldon/sprout-go v0.0.0-20200517010141-a4188845a9a8 h1:Cl6MTNMZJgVVxe4vBXeqjAp1zfiQMXejn7VHra55Oqc=
git.sr.ht/~whereswaldon/sprout-go v0.0.0-20200517010141-a4188845a9a8/go.mod h1:aMg78wuZrnmbesMseUyCaQdj6OG1laIYpR5HbDJfG7Y=
git.wow.st/gmp/jni v0.0.0-20200623180511-2e7d20a1b472 h1:YlBjj/ZrWg1Vs/h7ibZSXBpX9GUtvZ1ATNUVs+jhcXg=

M reply-view.go => reply-view.go +23 -3
@@ 5,6 5,7 @@ import (
	"log"

	"gioui.org/f32"
	"gioui.org/io/key"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"


@@ 57,6 58,9 @@ type ReplyListView struct {
	// filtered to only those related to the selected node
	Filtered          bool
	PrefilterPosition layout.Position

	HasKeyboardFocus           bool
	ShouldRequestKeyboardFocus bool
}

var _ View = &ReplyListView{}


@@ 155,10 159,20 @@ func (c *ReplyListView) dismissReplyContextMenu(gtx layout.Context) {
}

func (c *ReplyListView) Update(gtx layout.Context) {
	for _, event := range gtx.Events(c) {
		switch event := event.(type) {
		case key.FocusEvent:
			log.Println("focus", event)
			c.HasKeyboardFocus = event.Focus
		case key.Event:
			log.Println("key", event)
		}
	}
	overflowTag := c.manager.SelectedOverflowTag()
	for i := range c.ReplyStates {
		clickHandler := &c.ReplyStates[i]
		if clickHandler.Clicked() {
			c.requestKeyboardFocus()
			clickedOnFocused := clickHandler.Reply.Equals(c.Focused)
			if !clickedOnFocused {
				c.StateRefreshNeeded = true


@@ 211,9 225,11 @@ func (c *ReplyListView) Update(gtx layout.Context) {
			}
		}
		c.manager.DismissOverflow(gtx)
		c.ReplyEditor.Focus()
	}
	if c.CreateConversationButton.Clicked() || overflowTag == &c.CreateConversationButton {
		c.CreatingConversation = true
		c.ReplyEditor.Focus()
		c.manager.DismissOverflow(gtx)
	}
	if c.CancelReplyButton.Clicked() {


@@ 311,6 327,8 @@ func (c *ReplyListView) statusOf(reply *forest.Reply) sprigTheme.ReplyStatus {
}

func (c *ReplyListView) Layout(gtx layout.Context) layout.Dimensions {
	key.InputOp{Tag: c, Focus: c.ShouldRequestKeyboardFocus}.Add(gtx.Ops)
	c.ShouldRequestKeyboardFocus = false
	return layout.Stack{}.Layout(gtx,
		layout.Expanded(func(gtx C) D {
			paintOp := paint.ColorOp{Color: c.Theme.Background.Default}


@@ 355,6 373,7 @@ func (c *ReplyListView) layoutReplyList(gtx layout.Context) layout.Dimensions {
		if c.Focused == nil && len(replies) > 0 {
			c.Focused = replies[len(replies)-1].ID()
			c.StateRefreshNeeded = true
			c.requestKeyboardFocus()
		}
		dims = c.ReplyList.Layout(gtx, len(replies), func(gtx layout.Context, index int) layout.Dimensions {
			if stateIndex >= len(c.ReplyStates) {


@@ 536,9 555,10 @@ func (c *ReplyListView) layoutEditor(gtx layout.Context) layout.Dimensions {
										return layout.Dimensions{}
									}),
									layout.Stacked(func(gtx C) D {
										return layout.UniformInset(unit.Dp(6)).Layout(gtx,
											material.Editor(theme, &c.ReplyEditor, "type your reply here").Layout,
										)
										return layout.UniformInset(unit.Dp(6)).Layout(gtx, func(gtx C) D {
											editor := material.Editor(theme, &c.ReplyEditor, "type your reply here")
											return editor.Layout(gtx)
										})
									}),
								)
							})

A reply-view_desktop.go => reply-view_desktop.go +8 -0
@@ 0,0 1,8 @@
//+build !ios,!android

package main

func (r *ReplyListView) requestKeyboardFocus() {
	// on desktop, actually request keyboard focus
	r.ShouldRequestKeyboardFocus = true
}

A reply-view_mobile.go => reply-view_mobile.go +7 -0
@@ 0,0 1,7 @@
//+build ios android

package main

func (r *ReplyListView) requestKeyboardFocus() {
	// do nothing on mobile, otherwise we trigger the on-screen keyboard
}