Skip to content

Commit

Permalink
Fix tabbook to default to first non-disabled tab.
Browse files Browse the repository at this point in the history
Add Initial selected TabBook configuration
Add Initial selected RadioGroup configuration
  • Loading branch information
Mark Carpenter committed Jun 4, 2023
1 parent c5caa61 commit 9969363
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
5 changes: 4 additions & 1 deletion _examples/demo/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,10 @@ func radioGroupPage(res *uiResources) *page {
for _, cb := range cbs {
elements = append(elements, cb)
}
widget.NewRadioGroup(widget.RadioGroupOpts.Elements(elements...))
widget.NewRadioGroup(
widget.RadioGroupOpts.Elements(elements...),
widget.RadioGroupOpts.InitialElement(elements[2]),
)

return &page{
title: "Radio Group",
Expand Down
4 changes: 4 additions & 0 deletions _examples/widget_demos/radiogroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func main() {
//If you have references to the individual elements you can add them like this since it is a
//variadic method.
widget.RadioGroupOpts.Elements(button1, button2, button3),

//This function allows you to select which element should be selected initialially
//widget.RadioGroupOpts.InitialElement(button2),

//This callback method gets called when the Active widget is changed.
widget.RadioGroupOpts.ChangedHandler(func(args *widget.RadioGroupChangedEventArgs) {
fmt.Println(args.Active.(*widget.Button).Text().Label)
Expand Down
21 changes: 16 additions & 5 deletions _examples/widget_demos/tabbook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
buttonImage, _ := loadButtonImage()

// load text font
face, _ := loadFont(20)
face, _ := loadFont(16)

// construct a new container that serves as the root of the UI hierarchy
rootContainer := widget.NewContainer(
Expand Down Expand Up @@ -55,7 +55,8 @@ func main() {
widget.ContainerOpts.Layout(widget.NewAnchorLayout()),
)
greenBtn := widget.NewText(
widget.TextOpts.Text("Green Tab Button", face, color.Black),
widget.TextOpts.Text("Green Tab Button\nThis is configured as the initial tab.", face, color.Black),
widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
widget.TextOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
HorizontalPosition: widget.AnchorLayoutPositionCenter,
VerticalPosition: widget.AnchorLayoutPositionCenter,
Expand Down Expand Up @@ -84,9 +85,15 @@ func main() {
})),
)
tabBlue.AddChild(blueBtn2)

tabDisabled := widget.NewTabBookTab("Disabled Tab",
widget.ContainerOpts.BackgroundImage(image.NewNineSliceColor(color.NRGBA{R: 80, G: 80, B: 140, A: 255})),
)
tabDisabled.Disabled = true

tabBook := widget.NewTabBook(
widget.TabBookOpts.TabButtonImage(buttonImage),
widget.TabBookOpts.TabButtonText(face, &widget.ButtonTextColor{Idle: color.White}),
widget.TabBookOpts.TabButtonText(face, &widget.ButtonTextColor{Idle: color.White, Disabled: color.White}),
widget.TabBookOpts.TabButtonSpacing(0),
widget.TabBookOpts.ContainerOpts(
widget.ContainerOpts.WidgetOpts(widget.WidgetOpts.LayoutData(widget.AnchorLayoutData{
Expand All @@ -99,9 +106,10 @@ func main() {
),
widget.TabBookOpts.TabButtonOpts(
widget.ButtonOpts.TextPadding(widget.NewInsetsSimple(5)),
widget.ButtonOpts.WidgetOpts(widget.WidgetOpts.MinSize(135, 0)),
widget.ButtonOpts.WidgetOpts(widget.WidgetOpts.MinSize(98, 0)),
),
widget.TabBookOpts.Tabs(tabRed, tabGreen, tabBlue),
widget.TabBookOpts.Tabs(tabDisabled, tabRed, tabGreen, tabBlue),
// widget.TabBookOpts.InitialTab(tabGreen),
)
// add the tabBook as a child of the container
rootContainer.AddChild(tabBook)
Expand Down Expand Up @@ -153,11 +161,14 @@ func loadButtonImage() (*widget.ButtonImage, error) {

pressedHover := image.NewNineSliceColor(color.NRGBA{R: 110, G: 110, B: 110, A: 255})

disabled := image.NewNineSliceColor(color.NRGBA{R: 80, G: 80, B: 140, A: 255})

return &widget.ButtonImage{
Idle: idle,
Hover: hover,
Pressed: pressed,
PressedHover: pressedHover,
Disabled: disabled,
}, nil
}

Expand Down
13 changes: 12 additions & 1 deletion widget/radiogroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type RadioGroup struct {

elements []RadioGroupElement
active RadioGroupElement
initial RadioGroupElement
listen bool
doneEvent *event.Event
}
Expand Down Expand Up @@ -81,6 +82,14 @@ func (o RadioGroupOptions) ChangedHandler(f RadioGroupChangedHandlerFunc) RadioG
}
}

// This function allows you to select which element should be selected initialially.
// Otherwise it will select the first element in the Elements array.
func (o RadioGroupOptions) InitialElement(e RadioGroupElement) RadioGroupOpt {
return func(r *RadioGroup) {
r.initial = e
}
}

func (r *RadioGroup) Active() RadioGroupElement {
return r.active
}
Expand Down Expand Up @@ -127,7 +136,9 @@ func (r *RadioGroup) create() {
})
}

if r.active == nil && len(r.elements) > 0 {
if r.initial != nil {
r.SetActive(r.initial)
} else if len(r.elements) > 0 {
r.SetActive(r.elements[0])
}
}
49 changes: 38 additions & 11 deletions widget/tabbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type TabBook struct {
tabToButton map[*TabBookTab]*Button
flipBook *FlipBook
tab *TabBookTab
initialTab *TabBookTab
}

type TabBookTab struct {
Expand Down Expand Up @@ -137,6 +138,12 @@ func (o TabBookOptions) Tabs(tabs ...*TabBookTab) TabBookOpt {
}
}

func (o TabBookOptions) InitialTab(tab *TabBookTab) TabBookOpt {
return func(t *TabBook) {
t.initialTab = tab
}
}

func (o TabBookOptions) TabSelectedHandler(f TabBookTabSelectedHandlerFunc) TabBookOpt {
return func(t *TabBook) {
t.TabSelectedEvent.AddHandler(func(args interface{}) {
Expand Down Expand Up @@ -198,7 +205,10 @@ func (t *TabBook) createWidget() {
ContainerOpts.Layout(NewRowLayout(
RowLayoutOpts.Spacing(t.buttonSpacing))))
t.container.AddChild(buttonsContainer)

btnElements := []RadioGroupElement{}
var firstTab *TabBookTab

for _, tab := range t.tabs {
tab := tab
btn := NewButton(
Expand All @@ -211,10 +221,22 @@ func (t *TabBook) createWidget() {
btnElements = append(btnElements, btn)
buttonsContainer.AddChild(btn)
t.tabToButton[tab] = btn
if firstTab == nil {
if t.initialTab == nil && !tab.Disabled {
firstTab = tab
} else if t.initialTab == tab && !tab.Disabled {
firstTab = t.initialTab
}
}
}
//If we cannot find an initial tab default to to the first one
if firstTab == nil {
firstTab = t.tabs[0]
}

NewRadioGroup(
RadioGroupOpts.Elements(btnElements...),
RadioGroupOpts.InitialElement(t.tabToButton[firstTab]),
RadioGroupOpts.ChangedHandler(func(args *RadioGroupChangedEventArgs) {
tab := args.Active.(*Button).GetWidget().CustomData.(*TabBookTab)
t.SetTab(tab)
Expand All @@ -228,14 +250,19 @@ func (t *TabBook) createWidget() {
t.container.AddChild(t.flipBook)
t.flipBookOpts = nil

t.setTab(t.tabs[0], false)
}

// Set the current tab for the tab book.
//
// Note: This method should only be called after the
// ui is running. To set the initial tab please use the
// TabBookOptions.InitialTab method during tabbook creation.
func (t *TabBook) SetTab(tab *TabBookTab) {
t.setTab(tab, true)
}
if tab.Disabled {
return
}
t.init.Do()

func (t *TabBook) setTab(tab *TabBookTab, fireEvent bool) {
if tab != t.tab {
previousTab := t.tab

Expand All @@ -251,16 +278,16 @@ func (t *TabBook) setTab(tab *TabBookTab, fireEvent bool) {
b.SetState(state)
}

if fireEvent {
t.TabSelectedEvent.Fire(&TabBookTabSelectedEventArgs{
TabBook: t,
Tab: tab,
PreviousTab: previousTab,
})
}
t.TabSelectedEvent.Fire(&TabBookTabSelectedEventArgs{
TabBook: t,
Tab: tab,
PreviousTab: previousTab,
})
}

}

// Return the currently selected tab
func (t *TabBook) Tab() *TabBookTab {
return t.tab
}

0 comments on commit 9969363

Please sign in to comment.