Skip to content

Commit

Permalink
review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Philemon Ukane <[email protected]>
  • Loading branch information
ukane-philemon committed Nov 2, 2023
1 parent da24348 commit 93882ce
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 179 deletions.
142 changes: 85 additions & 57 deletions ui/cryptomaterial/dropdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type DropDown struct {
Position uint
revs bool
selectedIndex int
Color color.NRGBA
background color.NRGBA
dropdownIcon *widget.Icon
navigationIcon *widget.Icon
clickable *Clickable
Expand All @@ -38,12 +36,16 @@ type DropDown struct {
padding layout.Inset
shadow *Shadow

extraDisplay func(gtx C) D
FontWeight font.Weight
BorderWidth unit.Dp
BorderColor *color.NRGBA
NavigationIconColor *color.NRGBA
Hoverable bool
extraDisplay func(gtx C) D
FontWeight font.Weight
BorderWidth unit.Dp
BorderColor *color.NRGBA
Background *color.NRGBA
SelectedItemIconColor *color.NRGBA
MakeSelectedItemHoverable bool
SelectedItemDirection layout.Direction
Stack bool
OpenedLayoutInset *layout.Inset
}

type DropDownItem struct {
Expand All @@ -65,8 +67,6 @@ func (t *Theme) DropDown(items []DropDownItem, group uint, pos uint) *DropDown {
isOpen: false,
Position: pos,
selectedIndex: 0,
Color: t.Color.Gray2,
background: t.Color.Surface,
dropdownIcon: t.dropDownIcon,
navigationIcon: t.navigationCheckIcon,
clickable: t.NewClickable(true),
Expand All @@ -80,8 +80,9 @@ func (t *Theme) DropDown(items []DropDownItem, group uint, pos uint) *DropDown {
Height: WrapContent,
Border: Border{Radius: Radius(8)},
},
padding: layout.Inset{Top: values.MarginPadding8, Bottom: values.MarginPadding8},
shadow: t.Shadow(),
padding: layout.Inset{Top: values.MarginPadding8, Bottom: values.MarginPadding8},
shadow: t.Shadow(),
SelectedItemDirection: layout.W,
}

d.clickable.ChangeStyle(t.Styles.DropdownClickableStyle)
Expand Down Expand Up @@ -154,22 +155,21 @@ func (d *DropDown) layoutActiveIcon(gtx C, index int) D {
var icon *Icon
if !d.isOpen {
icon = NewIcon(d.dropdownIcon)
icon.Color = d.theme.Color.Gray1
} else if index == d.selectedIndex {
icon = NewIcon(d.navigationIcon)
if d.NavigationIconColor != nil {
icon.Color = *d.NavigationIconColor
} else {
icon.Color = d.theme.Color.Gray1
}
}

return layout.E.Layout(gtx, func(gtx C) D {
if icon == nil {
return D{}
}
return icon.Layout(gtx, values.MarginPadding20)
})
if icon == nil {
return D{} // return early
}

if !d.isOpen || d.SelectedItemIconColor == nil {
icon.Color = d.theme.Color.Gray1
} else {
icon.Color = *d.SelectedItemIconColor
}

return icon.Layout(gtx, values.MarginPadding20)
}

func (d *DropDown) layoutOption(gtx C, itemIndex int) D {
Expand All @@ -179,22 +179,16 @@ func (d *DropDown) layoutOption(gtx C, itemIndex int) D {
if !d.isOpen {
radius = Radius(8)
clickable = d.clickable
clickable.Hoverable = d.Hoverable
clickable.Hoverable = d.MakeSelectedItemHoverable
}

padding := values.MarginPadding10
if item.Icon != nil {
padding = values.MarginPadding8
}

dropdownWidth := gtx.Dp(d.Width)
dropdownItemWidth := dropdownWidth
if dropdownWidth <= 0 {
dropdownWidth = gtx.Dp(defaultDropdownWidth(d.revs))
}

return LinearLayout{
Width: dropdownWidth,
Width: MatchParent,
Height: WrapContent,
Clickable: clickable,
Padding: layout.UniformInset(padding),
Expand All @@ -205,20 +199,25 @@ func (d *DropDown) layoutOption(gtx C, itemIndex int) D {
return D{}
}

dropdownItemWidth -= gtx.Dp(values.MarginPadding24) // account for the dropdown Icon
return item.Icon.Layout24dp(gtx)
}),
layout.Rigid(func(gtx C) D {
gtx.Constraints.Max.X = dropdownItemWidth - gtx.Dp(values.MarginPadding50) // give some space for the dropdown Icon
gtx.Constraints.Min.X = gtx.Constraints.Max.X
if item.DisplayFn != nil {
return item.DisplayFn(gtx)
layout.Flexed(1, func(gtx C) D {
ll := LinearLayout{
Width: MatchParent,
Height: WrapContent,
Padding: layout.Inset{Right: values.MarginPadding5},
Direction: layout.W,
}

return layout.Inset{
Right: unit.Dp(5),
Left: unit.Dp(5),
}.Layout(gtx, func(gtx C) D {
if !d.isOpen {
ll.Direction = d.SelectedItemDirection
}

return ll.Layout2(gtx, func(gtx C) D {
if item.DisplayFn != nil {
return item.DisplayFn(gtx)
}

lbl := d.theme.Body2(item.Text)
if !d.isOpen && len(item.Text) > 20 {
lbl.Text = item.Text[:20-3] + "..."
Expand Down Expand Up @@ -255,6 +254,37 @@ func (d *DropDown) Layout(gtx C, dropPos int, reversePos bool) D {
iRight = dropPos
}

if d.OpenedLayoutInset == nil {
d.OpenedLayoutInset = &layout.Inset{
Left: unit.Dp(float32(iLeft)),
Right: unit.Dp(float32(iRight)),
}
}

if d.Stack {
isOpen := d.isOpen
return layout.Stack{Alignment: alig}.Layout(gtx,
layout.Expanded(func(gtx layout.Context) layout.Dimensions {
d.isOpen = false
display := d.closedLayout(gtx, iLeft, iRight)
d.isOpen = isOpen
return display
}),
layout.Stacked(func(gtx layout.Context) layout.Dimensions {
maxY := unit.Dp(len(d.items))*values.MarginPadding50 + d.OpenedLayoutInset.Top
if d.extraDisplay != nil {
maxY += values.MarginPadding50
}

gtx.Constraints.Max.Y = gtx.Dp(maxY)
if !d.isOpen {
return D{}
}
return d.openedLayout(gtx, iLeft, iRight)
}),
)
}

if d.Position == DropdownBasePos && d.isOpenDropdownGroup(d.group) {
maxY := unit.Dp(len(d.items)) * values.MarginPadding50
if d.extraDisplay != nil {
Expand Down Expand Up @@ -297,10 +327,8 @@ func (d *DropDown) Layout(gtx C, dropPos int, reversePos bool) D {

// openedLayout computes dropdown layout when dropdown is opened.
func (d *DropDown) openedLayout(gtx C, iLeft int, iRight int) D {

Check failure on line 329 in ui/cryptomaterial/dropdown.go

View workflow job for this annotation

GitHub Actions / Build

unused-parameter: parameter 'iLeft' seems to be unused, consider removing or renaming it as _ (revive)
return layout.Inset{
Left: unit.Dp(float32(iLeft)),
Right: unit.Dp(float32(iRight)),
}.Layout(gtx, func(gtx C) D {
inset := *d.OpenedLayoutInset
return inset.Layout(gtx, func(gtx C) D {
return d.dropDownItemMenu(gtx)
})
}
Expand All @@ -322,9 +350,6 @@ func (d *DropDown) closedLayout(gtx C, iLeft int, iRight int) D {
}
return d.layoutOption(gtx, d.selectedIndex)
}))
if d.Width <= 0 {
d.Width = defaultDropdownWidth(d.revs)
}
return lay
})
})
Expand All @@ -341,12 +366,6 @@ func (d *DropDown) dropDownItemMenu(gtx C) D {
return d.layoutOption(gtx, index)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
dropdownWidth := gtx.Dp(d.Width)
if dropdownWidth <= 0 {
dropdownWidth = gtx.Dp(defaultDropdownWidth(d.revs))
}

gtx.Constraints.Max.X = dropdownWidth
return d.extraDisplay(gtx)
}),
)
Expand All @@ -361,16 +380,25 @@ func (d *DropDown) dropDownItemMenu(gtx C) D {

// drawLayout wraps the page tx and sync section in a card layout
func (d *DropDown) drawLayout(gtx C, body layout.Widget) D {
if d.Width <= 0 {
d.Width = defaultDropdownWidth(d.revs)
}
d.linearLayout.Width = gtx.Dp(d.Width)

if d.isOpen {
d.linearLayout.Background = d.background
d.linearLayout.Background = d.theme.Color.Surface
d.linearLayout.Padding = d.padding
d.linearLayout.Shadow = d.shadow
} else {
d.linearLayout.Background = d.Color
d.linearLayout.Background = d.theme.Color.Gray2
d.linearLayout.Padding = layout.Inset{}
d.linearLayout.Shadow = nil
}

if d.Background != nil {
d.linearLayout.Background = *d.Background
}

if d.BorderWidth > 0 {
d.linearLayout.Border.Width = d.BorderWidth
}
Expand Down
20 changes: 14 additions & 6 deletions ui/cryptomaterial/segmented_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type SegmentedControl struct {
leftNavBtn,
rightNavBtn *Clickable

Padding *layout.Inset

selectedIndex int
segmentTitles []string

Expand Down Expand Up @@ -57,14 +59,20 @@ func (sc *SegmentedControl) Layout(gtx C) D {
txt.Color = sc.theme.Color.Text
border = Border{Radius: Radius(8)}
}
return LinearLayout{

ll := LinearLayout{
Width: WrapContent,
Height: WrapContent,
Padding: layout.UniformInset(values.MarginPadding8),
Background: bg,
Margin: layout.UniformInset(values.MarginPadding5),
Border: border,
}.Layout2(gtx, txt.Layout)
}
if sc.Padding != nil {
ll.Padding = *sc.Padding
} else {
ll.Padding = layout.UniformInset(values.MarginPadding8)
}
return ll.Layout2(gtx, txt.Layout)
})
})
}),
Expand Down Expand Up @@ -166,11 +174,11 @@ func (sc *SegmentedControl) Changed() bool {
return changed
}

func (sc *SegmentedControl) SelectedSegmentAtIndex(index int) {
func (sc *SegmentedControl) SetSelectedSegment(segment string) {
sc.mu.Lock()
defer sc.mu.Unlock()
for i := range sc.segmentTitles {
if i == index {
for i, item := range sc.segmentTitles {
if item == segment {
sc.selectedIndex = i
break
}
Expand Down
26 changes: 11 additions & 15 deletions ui/page/dcrdex/dcrdex_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func NewDEXPage(l *load.Load) *DEXPage {
MasterPage: app.NewMasterPage(DCRDEXID),
openTradeMainPage: l.Theme.NewClickable(false),
}
// TODO: Setdp.inited
dp.inited = true
return dp
}
Expand All @@ -51,30 +52,25 @@ func (pg *DEXPage) ID() string {
// Part of the load.Page interface.
func (pg *DEXPage) OnNavigatedTo() {
pg.ctx, pg.ctxCancel = context.WithCancel(context.TODO())
// TODO: set pg.inited
if !pg.inited {
pg.Display(NewDEXOnboarding(pg.Load))
} else if pg.CurrentPage() == nil {
if pg.CurrentPage() != nil {
pg.CurrentPage().OnNavigatedTo()
} else if pg.inited {
pg.Display(NewDEXMarketPage(pg.Load))
} else {
pg.CurrentPage().OnNavigatedTo()
pg.Display(NewDEXOnboarding(pg.Load))
}
}

// Layout draws the page UI components into the provided layout context to be
// eventually drawn on screen.
// Part of the load.Page interface.
func (pg *DEXPage) Layout(gtx C) D {
return layout.Stack{}.Layout(gtx,
layout.Expanded(func(gtx C) D {
return cryptomaterial.LinearLayout{
Width: cryptomaterial.MatchParent,
Height: cryptomaterial.MatchParent,
Orientation: layout.Vertical,
}.Layout(gtx,
layout.Flexed(1, pg.CurrentPage().Layout),
)
}),
return cryptomaterial.LinearLayout{
Width: cryptomaterial.MatchParent,
Height: cryptomaterial.MatchParent,
Orientation: layout.Vertical,
}.Layout(gtx,
layout.Flexed(1, pg.CurrentPage().Layout),
)
}

Expand Down
5 changes: 2 additions & 3 deletions ui/page/dcrdex/dex_onboarding_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ func (pg *DEXOnboarding) Layout(gtx C) D {
Orientation: layout.Vertical,
Background: pg.Theme.Color.Surface,
Margin: layout.Inset{
Bottom: values.MarginPadding50,
Right: u20,
Left: u20,
Right: u20,
Left: u20,
},
Border: cryptomaterial.Border{
Radius: cryptomaterial.Radius(r),
Expand Down
Loading

0 comments on commit 93882ce

Please sign in to comment.