Skip to content

Commit

Permalink
show menu when tapping after the editor is focused (#637)
Browse files Browse the repository at this point in the history
Co-authored-by: bugjt <@bugjt>
  • Loading branch information
bugjt authored Sep 17, 2024
1 parent bc4285c commit fa7cad6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
33 changes: 24 additions & 9 deletions ui/cryptomaterial/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type Editor struct {

copy, paste Button
isDisableMenu bool
isShowMenu bool
focused bool

// add space for error lable if it is true
isSpaceError bool
Expand Down Expand Up @@ -240,12 +242,21 @@ func (e *Editor) Layout(gtx C) D {

func (e *Editor) update(gtx C) {
for {
_, ok := e.click.Update(gtx.Source)
ev, ok := e.click.Update(gtx.Source)
if !ok {
break
}
e.SetFocus()
if e.click.Pressed() {
if ev.NumClicks > 1 || (e.focused && !e.isShowMenu) {
e.isShowMenu = true
} else {
e.isShowMenu = false
}
}
}

e.focused = gtx.Source.Focused(e.Editor)

for {
ev, ok := e.Editor.Update(gtx)
if !ok {
Expand Down Expand Up @@ -378,24 +389,26 @@ func (e *Editor) editorLayout(gtx C) D {
}

func (e *Editor) editorMenusLayout(gtx C, editorHeight int) {
if gtx.Source.Focused(e.Editor) || e.copy.Hovered() || e.paste.Hovered() {
e.isShowMenu = e.isShowMenu && (gtx.Source.Focused(e.Editor) || e.copy.Hovered() || e.paste.Hovered())
if e.isShowMenu {
flexChilds := make([]layout.FlexChild, 0)
if len(e.Editor.Text()) > 0 {
if len(e.Editor.SelectedText()) > 0 {
flexChilds = append(flexChilds, layout.Rigid(e.copy.Layout))
flexChilds = append(flexChilds, layout.Rigid(e.t.Line(20, 1).Layout))
}
flexChilds = append(flexChilds, layout.Rigid(e.paste.Layout))
macro := op.Record(gtx.Ops)
gtxCopy := gtx
macro := op.Record(gtxCopy.Ops)
LinearLayout{
Width: WrapContent,
Height: WrapContent,
Background: e.t.Color.Surface,
Margin: layout.Inset{Top: gtx.Metric.PxToDp(-(editorHeight - 10))},
Margin: layout.Inset{Top: gtxCopy.Metric.PxToDp(-(editorHeight - 10))},
Padding: layout.UniformInset(values.MarginPadding5),
Alignment: layout.Middle,
Border: Border{Radius: Radius(8), Color: e.t.Color.Gray2, Width: unit.Dp(0.5)},
}.Layout(gtx, flexChilds...)
op.Defer(gtx.Ops, macro.Stop())
}.Layout(gtxCopy, flexChilds...)
op.Defer(gtxCopy.Ops, macro.Stop())
}
}

Expand Down Expand Up @@ -485,11 +498,13 @@ func (e *Editor) handleEvents(gtx C) {
}

if e.copy.Clicked(gtx) {
gtx.Execute(clipboard.WriteCmd{Data: io.NopCloser(strings.NewReader(e.Editor.Text()))})
gtx.Execute(clipboard.WriteCmd{Data: io.NopCloser(strings.NewReader(e.Editor.SelectedText()))})
e.isShowMenu = false
}

if e.paste.Clicked(gtx) {
gtx.Execute(clipboard.ReadCmd{Tag: e.Editor})
e.isShowMenu = false
}
}

Expand Down
20 changes: 20 additions & 0 deletions ui/cryptomaterial/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"image/color"

"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op/clip"
"gioui.org/op/paint"
Expand Down Expand Up @@ -223,6 +224,25 @@ func (t *Theme) closeAllDropdowns() {
}
}

func (t *Theme) ShowKeyboardIfEditorFocused(gtx C) bool {
for _, e := range t.allEditors {
for {
ev, ok := gtx.Event(pointer.Filter{
Target: e.Editor,
Kinds: pointer.Release,
})
if !ok {
break
}
if _, ok := ev.(pointer.Event); ok {
gtx.Execute(key.SoftKeyboardCmd{Show: true})
return true
}
}
}
return false
}

// isDropdownGroupCollapsed iterate over Dropdowns registered as a member
// of {group}, returns true if any of the drop down state is open.
func (t *Theme) isDropdownGroupCollapsed(group uint) bool {
Expand Down
5 changes: 3 additions & 2 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,13 +361,14 @@ func (win *Window) handleUserClick(gtx C) {
win.isDragging = true
case pointer.Release:
if win.isClick && !win.isDragging {
gtx.Execute(key.FocusCmd{})
gtx.Execute(key.SoftKeyboardCmd{Show: false})
}
win.isClick = false
win.isDragging = false
}

}

win.load.Theme.ShowKeyboardIfEditorFocused(gtx)
}

// handleShortKeys listen keys pressed.
Expand Down

0 comments on commit fa7cad6

Please sign in to comment.