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

Various improvements to InnerWindow #4470

Merged
merged 4 commits into from
Dec 22, 2023
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
42 changes: 28 additions & 14 deletions container/innerwindow.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package container

import (
"image/color"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
intWidget "fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
Expand All @@ -25,15 +24,15 @@ type InnerWindow struct {
Icon fyne.Resource

title string
content fyne.CanvasObject
content *fyne.Container
}

// NewInnerWindow creates a new window border around the given `content`, displaying the `title` along the top.
// This will behave like a normal contain and will probably want to be added to a `MultipleWindows` parent.
//
// Since: 2.5
func NewInnerWindow(title string, content fyne.CanvasObject) *InnerWindow {
w := &InnerWindow{title: title, content: content}
w := &InnerWindow{title: title, content: NewPadded(content)}
w.ExtendBaseWidget(w)
return w
}
Expand Down Expand Up @@ -81,13 +80,28 @@ func (w *InnerWindow) CreateRenderer() fyne.WidgetRenderer {
bar := NewBorder(nil, nil, buttons, icon, title)
bg := canvas.NewRectangle(theme.OverlayBackgroundColor())
contentBG := canvas.NewRectangle(theme.BackgroundColor())
corner := newDraggableCorner(w.OnResized)
corner := newDraggableCorner(w)

objects := []fyne.CanvasObject{bg, contentBG, bar, corner, w.content}
objects := []fyne.CanvasObject{bg, contentBG, bar, w.content, corner}
return &innerWindowRenderer{ShadowingRenderer: intWidget.NewShadowingRenderer(objects, intWidget.DialogLevel),
win: w, bar: bar, bg: bg, corner: corner, contentBG: contentBG}
}

func (w *InnerWindow) SetContent(obj fyne.CanvasObject) {
w.content.Objects[0] = obj

w.content.Refresh()
}

func (w *InnerWindow) SetPadded(pad bool) {
if pad {
w.content.Layout = layout.NewPaddedLayout()
} else {
w.content.Layout = layout.NewStackLayout()
}
w.content.Refresh()
}

func (w *InnerWindow) SetTitle(title string) {
w.title = title
w.Refresh()
Expand Down Expand Up @@ -125,7 +139,7 @@ func (i *innerWindowRenderer) Layout(size fyne.Size) {
i.win.content.Resize(innerSize)

cornerSize := i.corner.MinSize()
i.corner.Move(pos.Add(size).Subtract(cornerSize))
i.corner.Move(pos.Add(size).Subtract(cornerSize).AddXY(1, 1))
i.corner.Resize(cornerSize)
}

Expand Down Expand Up @@ -181,24 +195,24 @@ func (d *draggableLabel) Tapped(ev *fyne.PointEvent) {

type draggableCorner struct {
widget.BaseWidget
drag func(*fyne.DragEvent)
win *InnerWindow
}

func newDraggableCorner(fn func(*fyne.DragEvent)) *draggableCorner {
d := &draggableCorner{drag: fn}
func newDraggableCorner(w *InnerWindow) *draggableCorner {
d := &draggableCorner{win: w}
d.ExtendBaseWidget(d)
return d
}

func (c *draggableCorner) CreateRenderer() fyne.WidgetRenderer {
prop := canvas.NewRectangle(color.Transparent)
prop.SetMinSize(fyne.NewSquareSize(20))
prop := canvas.NewImageFromResource(fyne.CurrentApp().Settings().Theme().Icon(theme.IconNameDragCornerIndicator))
prop.SetMinSize(fyne.NewSquareSize(16))
return widget.NewSimpleRenderer(prop)
}

func (c *draggableCorner) Dragged(ev *fyne.DragEvent) {
if f := c.drag; f != nil {
c.drag(ev)
if f := c.win.OnResized; f != nil {
c.win.OnResized(ev)
}
}

Expand Down
25 changes: 23 additions & 2 deletions container/innerwindow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,34 @@ func TestInnerWindow_MinSize(t *testing.T) {
labelMin := widget.NewLabel("Inner").MinSize()

winMin := w.MinSize()
assert.Equal(t, btnMin.Height+labelMin.Height+theme.Padding()*2, winMin.Height)
assert.Greater(t, winMin.Width, btnMin.Width*3+theme.Padding()*3)
assert.Equal(t, btnMin.Height+labelMin.Height+theme.Padding()*4, winMin.Height)
assert.Greater(t, winMin.Width, btnMin.Width*3+theme.Padding()*5)

w2 := NewInnerWindow("Much longer title that will truncate", widget.NewLabel("Content"))
assert.Equal(t, winMin, w2.MinSize())
}

func TestInnerWindow_SetContent(t *testing.T) {
w := NewInnerWindow("Title", widget.NewLabel("Content"))
r := cache.Renderer(w).(*innerWindowRenderer)
title := r.Objects()[4].(*fyne.Container)
assert.Equal(t, "Content", title.Objects[0].(*widget.Label).Text)

w.SetContent(widget.NewLabel("Content2"))
assert.Equal(t, "Content2", title.Objects[0].(*widget.Label).Text)
}

func TestInnerWindow_SetPadded(t *testing.T) {
w := NewInnerWindow("Title", widget.NewLabel("Content"))
minPadded := w.MinSize()

w.SetPadded(false)
assert.Less(t, w.MinSize().Height, minPadded.Height)

w.SetPadded(true)
assert.Equal(t, minPadded, w.MinSize())
}

func TestInnerWindow_SetTitle(t *testing.T) {
w := NewInnerWindow("Title1", widget.NewLabel("Content"))
r := cache.Renderer(w).(*innerWindowRenderer)
Expand Down
5 changes: 5 additions & 0 deletions theme/bundled-icons.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions theme/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ func main() {
bundleIcon("document-print", f)
bundleIcon("document-save", f)

bundleIcon("drag-corner-indicator", f)

bundleIcon("more-horizontal", f)
bundleIcon("more-vertical", f)

Expand Down
7 changes: 7 additions & 0 deletions theme/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ const (
// Since: 2.0
IconNameDocumentSave fyne.ThemeIconName = "documentSave"

// IconNameDragCornerIndicator is the name of the icon used in inner windows to indicate a draggable corner.
//
// Since: 2.5
IconNameDragCornerIndicator fyne.ThemeIconName = "dragCornerIndicator"

// IconNameMoreHorizontal is the name of theme lookup for horizontal more.
//
// Since 2.0
Expand Down Expand Up @@ -507,6 +512,8 @@ var (
IconNameDocumentPrint: NewThemedResource(documentprintIconRes),
IconNameDocumentSave: NewThemedResource(documentsaveIconRes),

IconNameDragCornerIndicator: NewThemedResource(dragcornerindicatorIconRes),

IconNameMoreHorizontal: NewThemedResource(morehorizontalIconRes),
IconNameMoreVertical: NewThemedResource(moreverticalIconRes),

Expand Down
11 changes: 11 additions & 0 deletions theme/icons/drag-corner-indicator.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading