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

enhance functionality of InnerWindow #5305

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 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
56 changes: 45 additions & 11 deletions container/innerwindow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package container

import (
"runtime"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
intWidget "fyne.io/fyne/v2/internal/widget"
Expand All @@ -18,6 +20,13 @@ var _ fyne.Widget = (*InnerWindow)(nil)
type InnerWindow struct {
widget.BaseWidget

// ButtonAlignment specifies where the window buttons (close, minimize, maximize) should be placed.
// The default is widget.ButtonAlignCenter which will auto select based on the OS.
// - On Windows and Linux this will be `widget.ButtonAlignTrailing`
roffe marked this conversation as resolved.
Show resolved Hide resolved
// - On Darwin this will be `widget.ButtonAlignLeading`
//
// Since: 2.6
ButtonAlignment widget.ButtonAlign
CloseIntercept func() `json:"-"`
OnDragged, OnResized func(*fyne.DragEvent) `json:"-"`
OnMinimized, OnMaximized, OnTappedBar, OnTappedIcon func() `json:"-"`
Expand All @@ -32,7 +41,11 @@ type InnerWindow struct {
//
// Since: 2.5
func NewInnerWindow(title string, content fyne.CanvasObject) *InnerWindow {
w := &InnerWindow{title: title, content: NewPadded(content)}
w := &InnerWindow{
title: title,
content: NewPadded(content),
ButtonAlignment: widget.ButtonAlignCenter,
roffe marked this conversation as resolved.
Show resolved Hide resolved
}
w.ExtendBaseWidget(w)
return w
}
Expand All @@ -53,15 +66,13 @@ func (w *InnerWindow) CreateRenderer() fyne.WidgetRenderer {
max.Disable()
}

buttons := NewHBox(
&widget.Button{Icon: theme.WindowCloseIcon(), Importance: widget.DangerImportance, OnTapped: func() {
if f := w.CloseIntercept; f != nil {
f()
} else {
w.Close()
}
}},
min, max)
close := &widget.Button{Icon: theme.WindowCloseIcon(), Importance: widget.DangerImportance, OnTapped: func() {
if f := w.CloseIntercept; f != nil {
f()
} else {
w.Close()
}
}}
roffe marked this conversation as resolved.
Show resolved Hide resolved

var icon fyne.CanvasObject
if w.Icon != nil {
Expand All @@ -74,12 +85,28 @@ func (w *InnerWindow) CreateRenderer() fyne.WidgetRenderer {
icon.(*widget.Button).Disable()
}
}

title := newDraggableLabel(w.title, w)
title.Truncation = fyne.TextTruncateEllipsis

var buttons *fyne.Container
var bar *fyne.Container

isLeading := w.ButtonAlignment == widget.ButtonAlignLeading || (w.ButtonAlignment == widget.ButtonAlignCenter && runtime.GOOS == "darwin")

if isLeading {
// Left side (darwin default or explicit left alignment)
buttons = NewHBox(close, min, max)
bar = NewBorder(nil, nil, buttons, icon, title)
} else {
// Right side (Windows/Linux default and explicit right alignment)
buttons = NewHBox(min, max, close)
bar = NewBorder(nil, nil, icon, buttons, title)
}

th := w.Theme()
v := fyne.CurrentApp().Settings().ThemeVariant()

bar := NewBorder(nil, nil, buttons, icon, title)
bg := canvas.NewRectangle(th.Color(theme.ColorNameOverlayBackground, v))
contentBG := canvas.NewRectangle(th.Color(theme.ColorNameBackground, v))
corner := newDraggableCorner(w)
Expand All @@ -104,6 +131,13 @@ func (w *InnerWindow) SetPadded(pad bool) {
w.content.Refresh()
}

// Title returns the current title of the window
//
// Since: 2.6
func (w *InnerWindow) Title() string {
return w.title
}
roffe marked this conversation as resolved.
Show resolved Hide resolved

func (w *InnerWindow) SetTitle(title string) {
w.title = title
w.Refresh()
Expand Down
73 changes: 72 additions & 1 deletion container/innerwindow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,55 @@ import (
"github.com/stretchr/testify/assert"
)

func TestInnerWindow_Close(t *testing.T) {
func TestInnerWindow_Title(t *testing.T) {
w := NewInnerWindow("Thing", widget.NewLabel("Content"))
w.SetTitle("New Title 123")
assert.Equal(t, "New Title 123", w.Title())
}

func TestInnerWindowIcon_Tap_Left(t *testing.T) {
w := NewInnerWindow("Thing", widget.NewLabel("Content"))
w.Icon = theme.GridIcon()

var testValue bool
w.OnTappedIcon = func() {
testValue = true
}
w.ButtonAlignment = widget.ButtonAlignLeading

outer := test.NewTempWindow(t, w)
outer.SetPadded(false)
outer.Resize(w.MinSize())
assert.True(t, w.Visible())

iconPos := fyne.NewPos(w.Size().Width-10, 10)
test.TapCanvas(outer.Canvas(), iconPos)
assert.True(t, testValue)
}

func TestInnerWindowIcon_Tap_Right(t *testing.T) {
w := NewInnerWindow("Thing", widget.NewLabel("Content"))
w.Icon = theme.GridIcon()

var testValue bool
w.OnTappedIcon = func() {
testValue = true
}
w.ButtonAlignment = widget.ButtonAlignTrailing

outer := test.NewTempWindow(t, w)
outer.SetPadded(false)
outer.Resize(w.MinSize())
assert.True(t, w.Visible())

iconPos := fyne.NewPos(10, 10)
test.TapCanvas(outer.Canvas(), iconPos)
assert.True(t, testValue)
}

func TestInnerWindow_Close_Left(t *testing.T) {
w := NewInnerWindow("Thing", widget.NewLabel("Content"))
w.ButtonAlignment = widget.ButtonAlignLeading
outer := test.NewTempWindow(t, w)
outer.SetPadded(false)
outer.Resize(w.MinSize())
Expand All @@ -36,6 +82,31 @@ func TestInnerWindow_Close(t *testing.T) {
assert.True(t, w.Visible())
}

func TestInnerWindow_Close_Right(t *testing.T) {
w := NewInnerWindow("Thing", widget.NewLabel("Content"))
w.ButtonAlignment = widget.ButtonAlignTrailing
outer := test.NewTempWindow(t, w)
outer.SetPadded(false)
outer.Resize(w.MinSize())
assert.True(t, w.Visible())

closePos := fyne.NewPos(w.Size().Width-10, 10)
test.TapCanvas(outer.Canvas(), closePos)
assert.False(t, w.Visible())

w.Show()
assert.True(t, w.Visible())

closing := true
w.CloseIntercept = func() {
closing = true
}

test.TapCanvas(outer.Canvas(), closePos)
assert.True(t, closing)
assert.True(t, w.Visible())
}

func TestInnerWindow_MinSize(t *testing.T) {
w := NewInnerWindow("Thing", widget.NewLabel("Content"))

Expand Down
Loading