Skip to content

Commit

Permalink
update logic for editor
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeBoy committed Jun 26, 2024
1 parent 35bc200 commit c4557e8
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 126 deletions.
2 changes: 0 additions & 2 deletions ui/cryptomaterial/dropdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,6 @@ func (d *DropDown) Changed(gtx C) bool {

// If no dropdown item was clicked, check if there's a click on the
// backdrop and close all dropdowns.
//TODO07
// if len(d.theme.DropdownBackdrop.Clicks()) > 0 {
if d.theme.DropdownBackdrop.Clicked(gtx) {
d.theme.closeAllDropdowns()
}
Expand Down
127 changes: 64 additions & 63 deletions ui/cryptomaterial/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
package cryptomaterial

import (
"image"
"image/color"
"io"
"strings"

"gioui.org/gesture"
"gioui.org/io/clipboard"
"gioui.org/io/key"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
Expand Down Expand Up @@ -68,10 +72,10 @@ type Editor struct {
m2 unit.Dp
m5 unit.Dp

clickable *widget.Clickable
// clickable *widget.Clickable
click gesture.Click

copy, paste Button
eventKey int
isShowMenu bool
isDisableMenu bool

Expand Down Expand Up @@ -165,9 +169,9 @@ func (t *Theme) Editor(editor *widget.Editor, hint string) Editor {
t.Styles.IconButtonColorStyle,
},
CustomButton: t.Button(""),
clickable: new(widget.Clickable),
copy: t.Button(values.String(values.StrCopy)),
paste: t.Button(values.String(values.StrPaste)),
// clickable: new(widget.Clickable),
copy: t.Button(values.String(values.StrCopy)),
paste: t.Button(values.String(values.StrPaste)),
}
newEditor.copy.TextSize = values.TextSize10
newEditor.copy.Background = color.NRGBA{}
Expand All @@ -186,34 +190,47 @@ func (t *Theme) Editor(editor *widget.Editor, hint string) Editor {
}

func (e *Editor) Pressed(gtx C) bool {
return e.clickable.Pressed() || e.copy.Clicked(gtx) || e.paste.Clicked(gtx)
// return e.clickable.Pressed() || e.copy.Clicked(gtx) || e.paste.Clicked(gtx)
return e.copy.Clicked(gtx) || e.paste.Clicked(gtx)
}

func (e *Editor) FirstPressed(gtx C) bool {
// TODO07
// return !e.Editor.Focused() && e.clickable.Pressed()
return !gtx.Source.Focused(&e.Editor) && e.clickable.Pressed()
// return !gtx.Source.Focused(&e.Editor) && e.clickable.Pressed()
return !gtx.Source.Focused(e.Editor)

}

func (e *Editor) Layout(gtx C) D {
e.handleEvents(gtx)
clicks1, _ := e.clickable.Update(gtx)
// clicks := e.clickable.Clicks()
// if len(clicks) > 0 {
if clicks1.NumClicks == 2 {
e.isShowMenu = true
}
if clicks1.NumClicks == 1 {
gtx.Execute(key.FocusCmd{Tag: &e.Editor})
// e.Editor.Focus()
}
if clicks1.NumClicks != 2 && clicks1.NumClicks > 0 {
e.isShowMenu = false
}
e.update(gtx)
return e.layout(gtx)
}

func (e *Editor) update(gtx C) {
for {
ev, ok := e.click.Update(gtx.Source)
if !ok {
break
}

switch ev.Kind {
case gesture.KindPress:
gtx.Execute(key.FocusCmd{Tag: e.Editor})
}

switch ev.NumClicks {
case 1:
gtx.Execute(key.FocusCmd{Tag: e.Editor})
case 2:
e.isShowMenu = true
default:
e.isShowMenu = false
}
}
}

func (e *Editor) layout(gtx C) D {
e.LineColor, e.TitleLabel.Color = e.t.Color.Gray2, e.t.Color.GrayText3
if e.Editor.Len() > 0 && len(e.Hint) > 0 {
Expand All @@ -223,10 +240,8 @@ func (e *Editor) layout(gtx C) D {
e.TitleLabel.Text = ""
}

// TODO07
focused := gtx.Source.Focused(&e.Editor)
focused := gtx.Source.Focused(e.Editor)
if focused {
// if e.Editor.Focused() {
e.TitleLabel.Text = e.Hint
e.TitleLabel.Color, e.LineColor = e.t.Color.Primary, e.t.Color.Primary
e.Hint = ""
Expand Down Expand Up @@ -257,24 +272,22 @@ func (e *Editor) layout(gtx C) D {
layout.Rigid(func(gtx C) D {
return layout.Stack{}.Layout(gtx,
layout.Stacked(func(gtx C) D {
return e.clickable.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(e.editorLayout),
layout.Rigid(func(gtx C) D {
if e.errorLabel.Text != "" {
inset := layout.Inset{
Top: e.m2,
Left: e.m5,
}
return inset.Layout(gtx, e.errorLabel.Layout)
}
if e.isSpaceError {
return layout.Spacer{Height: values.MarginPadding18}.Layout(gtx)
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(e.editorLayout),
layout.Rigid(func(gtx C) D {
if e.errorLabel.Text != "" {
inset := layout.Inset{
Top: e.m2,
Left: e.m5,
}
return D{}
}),
)
})
return inset.Layout(gtx, e.errorLabel.Layout)
}
if e.isSpaceError {
return layout.Spacer{Height: values.MarginPadding18}.Layout(gtx)
}
return D{}
}),
)
}),
layout.Stacked(func(gtx C) D {
if e.IsTitleLabel {
Expand All @@ -287,6 +300,14 @@ func (e *Editor) layout(gtx C) D {
}
return D{}
}),
layout.Expanded(func(gtx C) D {
defer pointer.PassOp{}.Push(gtx.Ops).Pop()
defer clip.Rect(image.Rectangle{
Max: gtx.Constraints.Min,
}).Push(gtx.Ops).Pop()
e.click.Add(gtx.Ops)
return D{}
}),
layout.Stacked(overLay),
)
}),
Expand Down Expand Up @@ -323,8 +344,7 @@ func (e *Editor) editorLayout(gtx C) D {
}

func (e *Editor) editorMenusLayout(gtx C, editorHeight int) {
// TODO07
e.isShowMenu = e.isShowMenu && (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 {
Expand Down Expand Up @@ -374,15 +394,7 @@ func (e *Editor) editor(gtx C) D {
return D{}
}),
layout.Flexed(1, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
inset := layout.Inset{
Top: e.m5,
Bottom: e.m5,
}
return inset.Layout(gtx, e.EditorStyle.Layout)
}),
)
return layout.Inset{Top: e.m5, Bottom: e.m5}.Layout(gtx, e.EditorStyle.Layout)
}),
layout.Rigid(func(gtx C) D {
if e.ExtraText == "" {
Expand Down Expand Up @@ -440,28 +452,17 @@ func (e *Editor) handleEvents(gtx C) {
e.EditorIconButtonEvent()
}

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

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

// TODO07
// func (e *Editor) processEvent(gtx C) {
// for _, event := range gtx.Events(&e.eventKey) {
// switch eventType := event.(type) {
// case clipboard.Event:
// e.Editor.Insert(eventType.Text)
// }
// }
// }

func (re RestoreEditor) Layout(gtx C) D {
width := int(gtx.Metric.PxPerDp * 2.0)
height := int(gtx.Metric.PxPerDp * float32(re.height))
Expand Down
35 changes: 3 additions & 32 deletions ui/cryptomaterial/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type Slider struct {
SelectedIndicatorColor color.NRGBA // this is a full color no opacity
slideAction *SlideAction
clicker gesture.Click
clicked bool
disableButtonDirection bool
ControlInset layout.Inset
// clicked bool
disableButtonDirection bool
ControlInset layout.Inset
}

var m4 = values.MarginPadding4
Expand Down Expand Up @@ -208,12 +208,6 @@ func (s *Slider) RefreshItems() {
s.isSliderItemsSet = false
}

func (s *Slider) Clicked() bool {
clicked := s.clicked
s.clicked = false
return clicked
}

func (s *Slider) handleClickEvent(gtx C) {
if s.nextButton.Clicked(gtx) {
s.handleActionEvent(true)
Expand All @@ -238,29 +232,6 @@ func (s *Slider) handleClickEvent(gtx C) {
break
}
}

// TODO07
for {
e, ok := s.clicker.Update(gtx.Source)
if !ok {
break
}
switch e.Kind {
case gesture.KindClick:
if !s.clicked {
s.clicked = true
}
}
}

// for _, events := range s.clicker.Events(gtx) {
// switch events.Type {
// case gesture.TypeClick:
// if !s.clicked {
// s.clicked = true
// }
// }
// }
}

func (s *Slider) handleActionEvent(isNext bool) {
Expand Down
1 change: 0 additions & 1 deletion ui/cryptomaterial/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func (s *Switch) Layout(gtx layout.Context) layout.Dimensions {
return layout.Dimensions{Size: dims}
}

// TODO07
func (s *Switch) Changed(gtx C) bool {
return s.clk.Update(gtx)
}
Expand Down
24 changes: 12 additions & 12 deletions ui/cryptomaterial/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,18 @@ func approxLuminance(c color.NRGBA) byte {

func HandleEditorEvents(gtx C, editors ...*widget.Editor) (bool, bool) {
var submit, changed bool
for _, editor := range editors {
evt, ok := editor.Update(gtx)
if !ok {
continue
}
switch evt.(type) {
case widget.ChangeEvent:
changed = true
case widget.SubmitEvent:
submit = true
}
}
// for _, editor := range editors {
// evt, ok := editor.Update(gtx)
// if !ok {
// continue
// }
// switch evt.(type) {
// case widget.ChangeEvent:
// changed = true
// case widget.SubmitEvent:
// submit = true
// }
// }
return submit, changed
}

Expand Down
4 changes: 2 additions & 2 deletions ui/page/components/restore_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (pg *Restore) HandleUserInteractions(gtx C) {

if pg.confirmSeedButton.Clicked(gtx) {
if !pg.restoreInProgress {
go pg.restoreFromSeedEditor(gtx)
go pg.restoreFromSeedEditor()
}
}

Expand Down Expand Up @@ -283,7 +283,7 @@ func (pg *Restore) HandleKeyPress(gtx C, evt *key.Event) {
}
}

func (pg *Restore) restoreFromSeedEditor(gtx C) {
func (pg *Restore) restoreFromSeedEditor() {
pg.restoreInProgress = true
clearEditor := func() {
pg.restoreInProgress = false
Expand Down
4 changes: 2 additions & 2 deletions ui/page/components/seed_restore_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func (pg *SeedRestore) validateSeeds() (bool, string) {
return true, seedPhrase
}

func (pg *SeedRestore) verifySeeds(gtx C) bool {
func (pg *SeedRestore) verifySeeds() bool {
isValid, seedphrase := pg.validateSeeds()
if isValid {
pg.seedPhrase = seedphrase
Expand Down Expand Up @@ -536,7 +536,7 @@ func (pg *SeedRestore) HandleUserInteractions(gtx C) {
}

if pg.validateSeed.Clicked(gtx) {
if !pg.verifySeeds(gtx) {
if !pg.verifySeeds() {
return
}

Expand Down
2 changes: 1 addition & 1 deletion ui/page/components/wallet_setup_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func (pg *CreateWallet) HandleUserInteractions(gtx C) {
}

// editor event listener
isSubmit, isChanged := cryptomaterial.HandleEditorEvents(gtx, pg.walletName.Editor, pg.watchOnlyWalletHex.Editor, pg.passwordEditor.Editor, pg.confirmPasswordEditor.Editor)
isSubmit, isChanged := cryptomaterial.HandleEditorEvents(gtx, pg.watchOnlyWalletHex.Editor, pg.passwordEditor.Editor, pg.confirmPasswordEditor.Editor)
if isChanged {
// reset error when any editor is modified
pg.walletName.SetError("")
Expand Down
Loading

0 comments on commit c4557e8

Please sign in to comment.