Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tab buttons stop working after removing a tab #3050

Closed
leprechaun33 opened this issue Jun 8, 2022 · 5 comments
Closed

Tab buttons stop working after removing a tab #3050

leprechaun33 opened this issue Jun 8, 2022 · 5 comments
Labels
unverified A bug that has been reported but not verified

Comments

@leprechaun33
Copy link

Describe the bug:

When removing a tab from AppTabs the tabs that are to the right of the removed tab cannot be selected with mouse clicks.
If there are tabs 1, 2, 3, 4 and tab 2 is removed then the tab buttons for 3 and 4 will not respond to mouse clicks. If tabs 5 and 6 are then added, the buttons for tabs 5 and 6 will still work like normal.

To Reproduce:

Steps to reproduce the behaviour:
Running the below code, go to File -> New Tab two or three times.
Then select a tab that is not the last tab and click the "Close Tab" button, after this the tab buttons of tabs to the right of the removed tab will not work.
If another tab is added then removed then the last tab will be selected.

Example code:

package main

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	w := a.NewWindow()
	t := buildTabsCont()
	w.SetMainMenu(buildMenu(t))

	w.SetContent(t)
	w.ShowAndRun()
}

func buildMenu(t *container.AppTabs) *fyne.MainMenu {
	nt := fyne.NewMenuItem("New Tab", func() { newTab(t) })
	file := fyne.NewMenu("File", nt)
	m := fyne.NewMainMenu(file)
	return m
}

func buildTabsCont() *container.AppTabs {
	at := container.NewAppTabs(
		container.NewTabItem("Welcome", widget.NewLabel("Welcome info here")),
	)
	at.SetTabLocation(container.TabLocationTop)
	return at
}

func newTab(t *container.AppTabs) {
	b := widget.NewButton("Close Tab", func() { t.RemoveIndex(t.SelectedIndex()) })
	t.Append(container.NewTabItem("Tab Name", b))
}

Device:

  • OS: Linux/Pop_OS
  • Version: 22.04
  • Go version: 1.18.1
  • Fyne version: v2.1
@leprechaun33 leprechaun33 added the unverified A bug that has been reported but not verified label Jun 8, 2022
@Jacalz
Copy link
Member

Jacalz commented Jun 9, 2022

The latest version of Fyne is v2.2.0. Can you please try with the new version and see if the issue still persists?

@d4x1
Copy link
Contributor

d4x1 commented Jun 9, 2022

I think it's root cause it here: https://github.com/fyne-io/fyne/blob/develop/container/apptabs.go#L341-L349. When you remove a tab, the index bind to the tab button is not updated.

You can replace the codes like this, and it will work. @leprechaun33

        item := r.appTabs.Items[i]
	index := i // capture
	button := &tabButton{
		onTapped: func() { r.appTabs.SelectIndex(index) },
	}
	r.buttonCache[item] = button

Do you have any ideas about how to fix it elegantly? @Jacalz

@Jacalz
Copy link
Member

Jacalz commented Jun 9, 2022

Hmm. Nice catch @d4x1. The best fix would probably be to stop using indexes all together and find a better solution but I suspect that your fix might work as well.

@d4x1
Copy link
Contributor

d4x1 commented Jun 9, 2022

After reading the codes, a more convenient way to fix it is use r.appTabs.Select(item) when a tab is active. I will make a PR to fix this later.

andydotxyz added a commit that referenced this issue Jun 9, 2022
Fix #3050, update onTapped hooker for tabButton
@andydotxyz
Copy link
Member

Thanks so much @d4x1, this is now on develop and release/v2.2.x for the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unverified A bug that has been reported but not verified
Projects
None yet
Development

No branches or pull requests

4 participants