From fa7cad67bad0b20c99cf4ad73e3d00c914065872 Mon Sep 17 00:00:00 2001 From: BugJT <89973752+bugjt@users.noreply.github.com> Date: Tue, 17 Sep 2024 23:26:21 +0700 Subject: [PATCH] show menu when tapping after the editor is focused (#637) Co-authored-by: bugjt <@bugjt> --- ui/cryptomaterial/editor.go | 33 ++++++++++++++++++++++++--------- ui/cryptomaterial/theme.go | 20 ++++++++++++++++++++ ui/window.go | 5 +++-- 3 files changed, 47 insertions(+), 11 deletions(-) diff --git a/ui/cryptomaterial/editor.go b/ui/cryptomaterial/editor.go index a3c1eacee..a1d0ba300 100644 --- a/ui/cryptomaterial/editor.go +++ b/ui/cryptomaterial/editor.go @@ -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 @@ -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 { @@ -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()) } } @@ -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 } } diff --git a/ui/cryptomaterial/theme.go b/ui/cryptomaterial/theme.go index f06e03a80..aeca7c32c 100644 --- a/ui/cryptomaterial/theme.go +++ b/ui/cryptomaterial/theme.go @@ -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" @@ -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 { diff --git a/ui/window.go b/ui/window.go index f51d92b2b..894de2265 100644 --- a/ui/window.go +++ b/ui/window.go @@ -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.