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

Once the filedialog opened and file selected, the window will lost all the binding events handling #5369

Open
2 tasks done
FreemanFeng opened this issue Jan 7, 2025 · 1 comment
Labels
information-needed Further information is requested unverified A bug that has been reported but not verified

Comments

@FreemanFeng
Copy link

FreemanFeng commented Jan 7, 2025

Checklist

  • I have searched the issue tracker for open issues that relate to the same problem, before opening a new one.
  • This issue only relates to a single bug. I will open new issues for any other problems.

Describe the bug

Below is the code, once the file dialog is opened and file is selected, it will copy the path to the input. When we click the Confirm Button, the click triggering event was not happended:

func ParsePrompt(pm common.PromptMessageType) map[string]string {
	if pm.Timeout == 0 {
		pm.Timeout = common.DefaultPromptTimeout
	}
	fm := map[string]bool{}
	for _, k := range pm.Files {
		fm[k] = true
	}
	im := map[string]bool{}
	for _, k := range pm.Keys {
		if _, ok := fm[k]; ok {
			continue
		}
		im[k] = true
	}
	r := map[string]string{}
	a := app.New()
	w := a.NewWindow(pm.Title)
	w.Resize(fyne.NewSize(600, 300))

	w.CenterOnScreen()

	var timer *time.Timer
	//cancel := false

	startTimer := func() {
		if timer != nil {
			timer.Stop()
		}
		timer = time.AfterFunc(time.Second*time.Duration(pm.Timeout), func() {
			w.Close()
			a.Quit()
		})
	}

	stopTimer := func() {
		if timer != nil {
			timer.Stop()
			timer = nil
		}
	}

	init := false
	cancel := false
	var inputs []*widget.Entry
	content := container.NewVBox()
	for i := 0; i < len(pm.Keys); i++ {
		label := widget.NewLabel(pm.Keys[i])
		label.TextStyle = fyne.TextStyle{Bold: true}
		content.Add(label)
		if _, ok := im[pm.Keys[i]]; ok {
		input := widget.NewEntry()
		input.SetPlaceHolder(pm.Texts[i])
		content.Add(input)
		inputs = append(inputs, input)
		continue
		}
		if _, ok := fm[pm.Keys[i]]; ok {
			input := widget.NewEntry()
			input.SetPlaceHolder(pm.Texts[i])
			openFileDialog := func() {
				stopTimer()
				fd := dialog.NewFileOpen(
					func(uc fyne.URIReadCloser, err error) {
						if err != nil {
							dialog.ShowError(err, w)
							return
						}
						if uc == nil {
							return
						}
						defer uc.Close()
						input.SetText(uc.URI().Path())
		
						//input.FocusGained()
					}, w)
				fd.Show()
			}
			openButton := widget.NewButton("Open File", openFileDialog)
			openButton.SetIcon(theme.FileIcon())
			openButton.Importance = widget.SuccessImportance
			openButton.FocusGained()
			openButton.FocusLost()
			buttonFileContainer := container.NewGridWithColumns(2,
				input,
				openButton,
			)
			content.Add(buttonFileContainer)
			inputs = append(inputs, input)
			continue
		}
	}
	content.Add(widget.NewLabel(""))

	confirmButton := widget.NewButton("Confirm", func() {
		cancel = false
		w.Close()
	})
	confirmButton.Resize(fyne.NewSize(100, 30))
	confirmButton.Importance = widget.HighImportance
	confirmButton.SetIcon(theme.ConfirmIcon())

	cancelButton := widget.NewButton("Cancel", func() {
		cancel = true
		w.Close()
	})
	cancelButton.Resize(fyne.NewSize(100, 30))
	cancelButton.Importance = widget.DangerImportance
	cancelButton.SetIcon(theme.CancelIcon())
	cancelButton.FocusGained()
	confirmButton.FocusGained()
	for _, input := range inputs {
		input.OnSubmitted = func(content string) {
			confirmButton.Tapped(&fyne.PointEvent{})
		}
		input.OnChanged = func(content string) {
			stopTimer()
			input.FocusGained()
		}
	}
	buttonContainer := container.NewGridWithColumns(4,
		widget.NewLabel(""),
		confirmButton,
		cancelButton,
		widget.NewLabel(""),
	)

	content.Add(buttonContainer)

	w.SetContent(content)
	w.SetIcon(theme.FyneLogo())

	w.Canvas().SetOnTypedKey(func(e *fyne.KeyEvent) {
		if e.Name == fyne.KeyReturn {
			confirmButton.Tapped(&fyne.PointEvent{})
		} else if e.Name == fyne.KeyEscape {
			cancel = true
			cancelButton.Tapped(&fyne.PointEvent{})
		}
		stopTimer()
	})
	w.Canvas().SetOnTypedRune(func(r rune) {
		if len(inputs) > 0 && !init {
			init = true
			robotgo.KeyPress(robotgo.Tab)
			robotgo.KeyPress(robotgo.Right)
			currentText := inputs[0].Text
			inputs[0].SetText(currentText + string(r))
		}
		stopTimer()
	})

	startTimer()

	w.ShowAndRun()
	if !cancel {
		for i, input := range inputs {
			key := pm.Keys[i]
			r[key] = input.Text
			if r[key] == common.EMPTY {
				r[key] = pm.Texts[i]
			}
		}
	}

	return r
}

How to reproduce

  1. Compile and Run
  2. click the file dialog button
  3. select file and its path will be copied to input box
  4. click Confirm button
  5. the click event handling was not called

Screenshots

No response

Example code

func ParsePrompt(pm common.PromptMessageType) map[string]string {
	if pm.Timeout == 0 {
		pm.Timeout = common.DefaultPromptTimeout
	}
	fm := map[string]bool{}
	for _, k := range pm.Files {
		fm[k] = true
	}
	im := map[string]bool{}
	for _, k := range pm.Keys {
		if _, ok := fm[k]; ok {
			continue
		}
		im[k] = true
	}
	r := map[string]string{}
	a := app.New()
	w := a.NewWindow(pm.Title)
	w.Resize(fyne.NewSize(600, 300))

	w.CenterOnScreen()

	var timer *time.Timer
	//cancel := false

	startTimer := func() {
		if timer != nil {
			timer.Stop()
		}
		timer = time.AfterFunc(time.Second*time.Duration(pm.Timeout), func() {
			w.Close()
			a.Quit()
		})
	}

	stopTimer := func() {
		if timer != nil {
			timer.Stop()
			timer = nil
		}
	}

	init := false
	cancel := false
	var inputs []*widget.Entry
	content := container.NewVBox()
	for i := 0; i < len(pm.Keys); i++ {
		label := widget.NewLabel(pm.Keys[i])
		label.TextStyle = fyne.TextStyle{Bold: true}
		content.Add(label)
		if _, ok := im[pm.Keys[i]]; ok {
		input := widget.NewEntry()
		input.SetPlaceHolder(pm.Texts[i])
		content.Add(input)
		inputs = append(inputs, input)
		continue
		}
		if _, ok := fm[pm.Keys[i]]; ok {
			input := widget.NewEntry()
			input.SetPlaceHolder(pm.Texts[i])
			openFileDialog := func() {
				stopTimer()
				fd := dialog.NewFileOpen(
					func(uc fyne.URIReadCloser, err error) {
						if err != nil {
							dialog.ShowError(err, w)
							return
						}
						if uc == nil {
							return
						}
						defer uc.Close()
						input.SetText(uc.URI().Path())
		
						//input.FocusGained()
					}, w)
				fd.Show()
			}
			openButton := widget.NewButton("Open File", openFileDialog)
			openButton.SetIcon(theme.FileIcon())
			openButton.Importance = widget.SuccessImportance
			openButton.FocusGained()
			openButton.FocusLost()
			buttonFileContainer := container.NewGridWithColumns(2,
				input,
				openButton,
			)
			content.Add(buttonFileContainer)
			inputs = append(inputs, input)
			continue
		}
	}
	content.Add(widget.NewLabel(""))

	confirmButton := widget.NewButton("Confirm", func() {
		cancel = false
		w.Close()
	})
	confirmButton.Resize(fyne.NewSize(100, 30))
	confirmButton.Importance = widget.HighImportance
	confirmButton.SetIcon(theme.ConfirmIcon())

	cancelButton := widget.NewButton("Cancel", func() {
		cancel = true
		w.Close()
	})
	cancelButton.Resize(fyne.NewSize(100, 30))
	cancelButton.Importance = widget.DangerImportance
	cancelButton.SetIcon(theme.CancelIcon())
	cancelButton.FocusGained()
	confirmButton.FocusGained()
	for _, input := range inputs {
		input.OnSubmitted = func(content string) {
			confirmButton.Tapped(&fyne.PointEvent{})
		}
		input.OnChanged = func(content string) {
			stopTimer()
			input.FocusGained()
		}
	}
	buttonContainer := container.NewGridWithColumns(4,
		widget.NewLabel(""),
		confirmButton,
		cancelButton,
		widget.NewLabel(""),
	)

	content.Add(buttonContainer)

	w.SetContent(content)
	w.SetIcon(theme.FyneLogo())

	w.Canvas().SetOnTypedKey(func(e *fyne.KeyEvent) {
		if e.Name == fyne.KeyReturn {
			confirmButton.Tapped(&fyne.PointEvent{})
		} else if e.Name == fyne.KeyEscape {
			cancel = true
			cancelButton.Tapped(&fyne.PointEvent{})
		}
		stopTimer()
	})
	w.Canvas().SetOnTypedRune(func(r rune) {
		if len(inputs) > 0 && !init {
			init = true
			robotgo.KeyPress(robotgo.Tab)
			robotgo.KeyPress(robotgo.Right)
			currentText := inputs[0].Text
			inputs[0].SetText(currentText + string(r))
		}
		stopTimer()
	})

	startTimer()

	w.ShowAndRun()
	if !cancel {
		for i, input := range inputs {
			key := pm.Keys[i]
			r[key] = input.Text
			if r[key] == common.EMPTY {
				r[key] = pm.Texts[i]
			}
		}
	}

	return r
}

Fyne version

2.5.3

Go compiler version

1.23.2

Operating system and version

macOS

Additional Information

No response

@FreemanFeng FreemanFeng added the unverified A bug that has been reported but not verified label Jan 7, 2025
@Jacalz Jacalz added the information-needed Further information is requested label Jan 7, 2025
@Jacalz
Copy link
Member

Jacalz commented Jan 7, 2025

Per the issue template, can you please produce a short and simple example code? We want code examples to only contain the code necessary to show the issue in question and we appreciate if it is runnable as is so we can just copy the whole code block into an editor and run it locally.

PS: Please use markdown code blocks like with Go syntax highlighting. It makes code a lot easier to read :)

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

No branches or pull requests

2 participants