Skip to content

Commit

Permalink
Refactor to remove 'Current' and 'Is' getter forms.
Browse files Browse the repository at this point in the history
Also move caching of widget renders out of widget implementation.
Involves moving Container.Size to Container.Size() to match
  • Loading branch information
andydotxyz committed Dec 9, 2018
1 parent 43d1397 commit a5538d3
Show file tree
Hide file tree
Showing 36 changed files with 711 additions and 464 deletions.
29 changes: 22 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package app
import (
"github.com/fyne-io/fyne"
"github.com/fyne-io/fyne/theme"
"github.com/fyne-io/fyne/widget"
)

type fyneApp struct {
Expand Down Expand Up @@ -37,18 +38,32 @@ func (app *fyneApp) Driver() fyne.Driver {
return app.driver
}

func (app *fyneApp) applyTheme(fyne.Settings) {
for _, window := range app.driver.AllWindows() {
content := window.Content()
func (app *fyneApp) applyThemeTo(content fyne.CanvasObject, canvas fyne.Canvas) {
if themed, ok := content.(fyne.ThemedObject); ok {
themed.ApplyTheme()
canvas.Refresh(content)
}
if wid, ok := content.(fyne.Widget); ok {
widget.Renderer(wid).ApplyTheme()
canvas.Refresh(content)

switch themed := content.(type) {
case fyne.ThemedObject:
themed.ApplyTheme()
window.Canvas().Refresh(content)
for _, o := range widget.Renderer(wid).Objects() {
app.applyThemeTo(o, canvas)
}
}
if c, ok := content.(*fyne.Container); ok {
for _, o := range c.Objects {
app.applyThemeTo(o, canvas)
}
}
}

func (app *fyneApp) applyTheme(fyne.Settings) {
for _, window := range app.driver.AllWindows() {
app.applyThemeTo(window.Content(), window.Canvas())
}
}

// NewAppWithDriver initialises a new Fyne application using the specified driver
// and returns a handle to that App.
// As this package has no default driver one must be provided.
Expand Down
28 changes: 9 additions & 19 deletions canvas/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,32 @@ package canvas
import "github.com/fyne-io/fyne"

type baseObject struct {
Size fyne.Size // The current size of the Rectangle
Position fyne.Position // The current position of the Rectangle
size fyne.Size // The current size of the Rectangle
position fyne.Position // The current position of the Rectangle
Options Options // Options to pass to the renderer
Hidden bool // Is this object currently hidden

min fyne.Size // The minimum size this object can be
}

// CurrentSize returns the current size of this rectangle object
func (r *baseObject) CurrentSize() fyne.Size {
return r.Size
func (r *baseObject) Size() fyne.Size {
return r.size
}

// Resize sets a new size for the rectangle object
func (r *baseObject) Resize(size fyne.Size) {
r.Size = size

Refresh(r)
r.size = size
}

// CurrentPosition gets the current position of this rectangle object, relative to it's parent / canvas
func (r *baseObject) CurrentPosition() fyne.Position {
return r.Position
func (r *baseObject) Position() fyne.Position {
return r.position
}

// Move the rectangle object to a new position, relative to it's parent / canvas
func (r *baseObject) Move(pos fyne.Position) {
r.Position = pos

Refresh(r)
r.position = pos
}

// MinSize returns the specified minimum size, if set, or {1, 1} otherwise
Expand All @@ -48,27 +44,21 @@ func (r *baseObject) MinSize() fyne.Size {
// SetMinSize specifies the smallest size this object should be
func (r *baseObject) SetMinSize(size fyne.Size) {
r.min = size

Refresh(r)
}

// IsVisible returns true if this object is visible, false otherwise
func (r *baseObject) IsVisible() bool {
func (r *baseObject) Visible() bool {
return !r.Hidden
}

// Show will set this object to be visible
func (r *baseObject) Show() {
r.Hidden = false

Refresh(r)
}

// Hide will set this object to not be visible
func (r *baseObject) Hide() {
r.Hidden = true

Refresh(r)
}

// Refresh instructs the containing canvas to refresh the specified obj.
Expand Down
14 changes: 7 additions & 7 deletions canvas/circle.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Circle struct {
StrokeWidth float32 // The stroke width of the circle
}

// CurrentSize returns the current size of bounding box for this circle object
func (l *Circle) CurrentSize() fyne.Size {
// Size returns the current size of bounding box for this circle object
func (l *Circle) Size() fyne.Size {
return fyne.NewSize(l.Position2.X-l.Position1.X, l.Position2.Y-l.Position1.Y)
}

Expand All @@ -26,14 +26,14 @@ func (l *Circle) Resize(size fyne.Size) {
l.Position2 = fyne.NewPos(l.Position1.X+size.Width, l.Position1.Y+size.Height)
}

// CurrentPosition gets the current top-left position of this circle object, relative to it's parent / canvas
func (l *Circle) CurrentPosition() fyne.Position {
// Position gets the current top-left position of this circle object, relative to it's parent / canvas
func (l *Circle) Position() fyne.Position {
return l.Position1
}

// Move the circle object to a new position, relative to it's parent / canvas
func (l *Circle) Move(pos fyne.Position) {
size := l.CurrentSize()
size := l.Size()
l.Position1 = pos
l.Position2 = fyne.NewPos(l.Position1.X+size.Width, l.Position1.Y+size.Height)
}
Expand All @@ -44,8 +44,8 @@ func (l *Circle) MinSize() fyne.Size {
return fyne.NewSize(1, 1)
}

// IsVisible returns true if this circle is visible, false otherwise
func (l *Circle) IsVisible() bool {
// Visible returns true if this circle is visible, false otherwise
func (l *Circle) Visible() bool {
return !l.Hidden
}

Expand Down
14 changes: 7 additions & 7 deletions canvas/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type Line struct {
StrokeWidth float32 // The stroke width of the line
}

// CurrentSize returns the current size of bounding box for this line object
func (l *Line) CurrentSize() fyne.Size {
// Size returns the current size of bounding box for this line object
func (l *Line) Size() fyne.Size {
return fyne.NewSize(int(math.Abs(float64(l.Position2.X))-math.Abs(float64(l.Position1.X))),
int(math.Abs(float64(l.Position2.Y))-math.Abs(float64(l.Position1.Y))))
}
Expand All @@ -29,14 +29,14 @@ func (l *Line) Resize(size fyne.Size) {
l.Position2 = fyne.NewPos(l.Position1.X+size.Width, l.Position1.Y+size.Height)
}

// CurrentPosition gets the current top-left position of this line object, relative to it's parent / canvas
func (l *Line) CurrentPosition() fyne.Position {
// Position gets the current top-left position of this line object, relative to it's parent / canvas
func (l *Line) Position() fyne.Position {
return fyne.NewPos(fyne.Min(l.Position1.X, l.Position2.X), fyne.Min(l.Position1.Y, l.Position2.Y))
}

// Move the line object to a new position, relative to it's parent / canvas
func (l *Line) Move(pos fyne.Position) {
size := l.CurrentSize()
size := l.Size()
l.Position1 = pos
l.Position2 = fyne.NewPos(l.Position1.X+size.Width, l.Position1.Y+size.Height)
}
Expand All @@ -47,8 +47,8 @@ func (l *Line) MinSize() fyne.Size {
return fyne.NewSize(1, 1)
}

// IsVisible returns true if this line// Show will set this circle to be visible is visible, false otherwise
func (l *Line) IsVisible() bool {
// Visible returns true if this line// Show will set this circle to be visible is visible, false otherwise
func (l *Line) Visible() bool {
return !l.Hidden
}

Expand Down
20 changes: 0 additions & 20 deletions canvas/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,6 @@ type Text struct {
TextStyle fyne.TextStyle // The style of the text content
}

// CurrentSize gets the current size of this text object
func (t *Text) CurrentSize() fyne.Size {
return t.Size
}

// Resize sets a new size for the text object
func (t *Text) Resize(size fyne.Size) {
t.Size = size
}

// CurrentPosition gets the current position of this text object, relative to it's parent / canvas
func (t *Text) CurrentPosition() fyne.Position {
return t.Position
}

// Move the text object to a new position, relative to it's parent / canvas
func (t *Text) Move(pos fyne.Position) {
t.Position = pos
}

// MinSize returns the minimum size of this text objet based on it's font size and content.
// This is normally determined by the render implementation.
func (t *Text) MinSize() fyne.Size {
Expand Down
6 changes: 3 additions & 3 deletions canvasobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ package fyne
// Resize(Size) or Move(Position).
type CanvasObject interface {
// geometry
CurrentSize() Size
Size() Size
Resize(Size)
CurrentPosition() Position
Position() Position
Move(Position)
MinSize() Size

// visibility
IsVisible() bool
Visible() bool
Show()
Hide()
}
Expand Down
34 changes: 17 additions & 17 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package fyne
// Container is a CanvasObject that contains a collection of child objects.
// The layout of the children is set by the specified Layout.
type Container struct {
Size Size // The current size of the Container
Position Position // The current position of the Container
size Size // The current size of the Container
position Position // The current position of the Container
Hidden bool // Is this Container hidden

Layout Layout // The Layout algorithm for arranging child CanvasObjects
Expand All @@ -13,35 +13,35 @@ type Container struct {

func (c *Container) layout() {
if c.Layout != nil {
c.Layout.Layout(c.Objects, c.Size)
c.Layout.Layout(c.Objects, c.size)
return
}

for _, child := range c.Objects {
child.Resize(c.Size)
child.Move(c.Position)
child.Resize(c.size)
child.Move(c.position)
}
}

// CurrentSize returns the current size of this container
func (c *Container) CurrentSize() Size {
return c.Size
// Size returns the current size of this container
func (c *Container) Size() Size {
return c.size
}

// Resize sets a new size for the Container
func (c *Container) Resize(size Size) {
c.Size = size
c.size = size
c.layout()
}

// CurrentPosition gets the current position of this Container, relative to it's parent
func (c *Container) CurrentPosition() Position {
return c.Position
// Position gets the current position of this Container, relative to it's parent
func (c *Container) Position() Position {
return c.position
}

// Move the container (and all it's children) to a new position, relative to it's parent
func (c *Container) Move(pos Position) {
c.Position = pos
c.position = pos
c.layout()
}

Expand All @@ -61,8 +61,8 @@ func (c *Container) MinSize() Size {

}

// IsVisible returns true if the container is currently visible, false otherwise.
func (c *Container) IsVisible() bool {
// Visible returns true if the container is currently visible, false otherwise.
func (c *Container) Visible() bool {
return !c.Hidden
}

Expand Down Expand Up @@ -105,7 +105,7 @@ func NewContainer(objects ...CanvasObject) *Container {
Objects: objects,
}

ret.Size = ret.MinSize()
ret.size = ret.MinSize()
ret.layout()

return ret
Expand All @@ -119,7 +119,7 @@ func NewContainerWithLayout(layout Layout, objects ...CanvasObject) *Container {
Layout: layout,
}

ret.Size = layout.MinSize(objects)
ret.size = layout.MinSize(objects)
ret.layout()
return ret
}
Loading

0 comments on commit a5538d3

Please sign in to comment.