Skip to content

Commit

Permalink
Merge pull request #1874 from andydotxyz/fix/1863
Browse files Browse the repository at this point in the history
Allow Dialog.Resize(...) before Show()
  • Loading branch information
andydotxyz committed Jan 27, 2021
2 parents b418db0 + 21ca8bc commit 609e7f6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
11 changes: 8 additions & 3 deletions dialog/base.go
Expand Up @@ -31,9 +31,10 @@ type Dialog interface {
var _ Dialog = (*dialog)(nil)

type dialog struct {
callback func(bool)
title string
icon fyne.Resource
callback func(bool)
title string
icon fyne.Resource
desiredSize fyne.Size

win *widget.PopUp
bg *canvas.Rectangle
Expand Down Expand Up @@ -99,6 +100,9 @@ func (d *dialog) Hide() {
}

func (d *dialog) Show() {
if !d.desiredSize.IsZero() {
d.win.Resize(d.desiredSize)
}
d.win.Show()
}

Expand Down Expand Up @@ -141,6 +145,7 @@ func (d *dialog) Refresh() {

// Resize dialog, call this function after dialog show
func (d *dialog) Resize(size fyne.Size) {
d.desiredSize = size
maxSize := d.win.Size()
minSize := d.win.MinSize()
newWidth := size.Width
Expand Down
17 changes: 17 additions & 0 deletions dialog/base_test.go
Expand Up @@ -4,9 +4,12 @@ import (
"image/color"
"testing"

"github.com/stretchr/testify/assert"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)

Expand All @@ -28,3 +31,17 @@ func TestShowCustom_ApplyTheme(t *testing.T) {
test.ApplyTheme(t, test.NewTheme())
test.AssertImageMatches(t, "dialog-custom-ugly.png", w.Canvas().Capture())
}

func TestShowCustom_Resize(t *testing.T) {
w := test.NewWindow(canvas.NewRectangle(color.Transparent))
w.Resize(fyne.NewSize(300, 300))

label := widget.NewLabel("Content")
label.Alignment = fyne.TextAlignCenter
d := NewCustom("Title", "OK", label, w)

size := fyne.NewSize(200, 200)
d.Resize(size)
d.Show()
assert.Equal(t, size, d.(*dialog).win.Content.Size().Add(fyne.NewSize(theme.Padding()*2, theme.Padding()*2)))
}
12 changes: 6 additions & 6 deletions dialog/file.go
Expand Up @@ -47,7 +47,7 @@ type FileDialog struct {
parent fyne.Window
dialog *fileDialog
dismissText string
desiredSize *fyne.Size
desiredSize fyne.Size
// this will be applied to dialog.dir when it's loaded
startingLocation fyne.ListableURI
// this will be the initial filename in a FileDialog in save mode
Expand Down Expand Up @@ -459,9 +459,8 @@ func (f *FileDialog) Show() {
return
}
f.dialog = showFile(f)
if f.desiredSize != nil {
f.Resize(*f.desiredSize)
f.desiredSize = nil
if !f.desiredSize.IsZero() {
f.Resize(f.desiredSize)
}
}

Expand All @@ -470,10 +469,11 @@ func (f *FileDialog) Refresh() {
f.dialog.win.Refresh()
}

// Resize dialog, call this function after dialog show
// Resize dialog to the requested size, if there is sufficient space.
// If the parent window is not large enough then the size will be reduced to fit.
func (f *FileDialog) Resize(size fyne.Size) {
f.desiredSize = size
if f.dialog == nil {
f.desiredSize = &size
return
}
maxSize := f.dialog.win.Size()
Expand Down

0 comments on commit 609e7f6

Please sign in to comment.