Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions widget/popup.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,18 @@ func (p *PopUp) ShowAtRelativePosition(rel fyne.Position, to fyne.CanvasObject)
withRelativePosition(rel, to, p.ShowAtPosition)
}

// Tapped is called when the user taps the popUp background - if not modal then dismiss this widget
func (p *PopUp) Tapped(_ *fyne.PointEvent) {
if !p.modal {
// Tapped is called when the user taps the popUp.
// If not modal and the tap is outside the content area, then dismiss this widget
func (p *PopUp) Tapped(e *fyne.PointEvent) {
if !p.modal && !p.isInsideContent(e.Position) {
p.Hide()
}
}

// TappedSecondary is called when the user right/alt taps the background - if not modal then dismiss this widget
func (p *PopUp) TappedSecondary(_ *fyne.PointEvent) {
if !p.modal {
// TappedSecondary is called when the user right/alt taps the popUp.
// If not modal and the tap is outside the content area, then dismiss this widget
func (p *PopUp) TappedSecondary(e *fyne.PointEvent) {
if !p.modal && !p.isInsideContent(e.Position) {
p.Hide()
}
}
Expand Down Expand Up @@ -119,6 +121,12 @@ func (p *PopUp) CreateRenderer() fyne.WidgetRenderer {
}
}

func (p *PopUp) isInsideContent(pos fyne.Position) bool {
return pos.X >= p.innerPos.X && pos.Y >= p.innerPos.Y &&
pos.X <= p.innerPos.X+p.innerSize.Width &&
pos.Y <= p.innerPos.Y+p.innerSize.Height
}

// ShowPopUpAtPosition creates a new popUp for the specified content at the specified absolute position.
// It will then display the popup on the passed canvas.
func ShowPopUpAtPosition(content fyne.CanvasObject, canvas fyne.Canvas, pos fyne.Position) {
Expand Down
21 changes: 21 additions & 0 deletions widget/popup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,27 @@ func TestModalPopUp_Resize(t *testing.T) {
assert.Equal(t, float32(80), popSize.Height)
}

func TestModalPopUp_TappedInside(t *testing.T) {
label := NewLabel("Hi")
win := test.NewWindow(NewLabel("OK"))
defer win.Close()
win.Resize(fyne.NewSize(80, 80))

pop := newPopUp(label, win.Canvas())
pop.Show()
defer test.Canvas().Overlays().Remove(pop)

size := fyne.NewSize(50, 48)
pop.Resize(size)
pop.Move(fyne.NewPos(10, 10))
assert.Equal(t, size.Subtract(fyne.NewSize(theme.InnerPadding(), theme.InnerPadding())), pop.Content.Size())

pop.Tapped(&fyne.PointEvent{Position: fyne.NewPos(30, 30)})
assert.False(t, pop.Hidden)
pop.Tapped(&fyne.PointEvent{Position: fyne.NewPos(5, 5)})
assert.True(t, pop.Hidden)
}

func TestModalPopUp_Resize_Constrained(t *testing.T) {
label := NewLabel("Hi")
win := test.NewTempWindow(t, NewLabel("OK"))
Expand Down
Loading