Skip to content

Commit

Permalink
Refactoring refresh code out of theme apply code.
Browse files Browse the repository at this point in the history
Update some canvas helper functions too - let's not expose internal details
  • Loading branch information
andydotxyz committed Sep 3, 2018
1 parent 34773fc commit 126d1ac
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 49 deletions.
9 changes: 3 additions & 6 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,11 @@ type Canvas interface {
SetOnKeyDown(func(*KeyEvent))
}

// GetCanvas returns the canvas containing the passed CanvasObject.
// It will return nil if the CanvasObject has not been added to any Canvas.
func GetCanvas(obj CanvasObject) Canvas {
// RefreshObject instructs the containing canvas to refresh the specified obj.
func RefreshObject(obj CanvasObject) {
for _, window := range GetDriver().AllWindows() {
if window.Canvas() != nil && window.Canvas().Contains(obj) {
return window.Canvas()
window.Canvas().Refresh(obj)
}
}

return nil
}
10 changes: 5 additions & 5 deletions canvas/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (r *baseObject) CurrentSize() fyne.Size {
func (r *baseObject) Resize(size fyne.Size) {
r.Size = size

// TODO refresh?
fyne.RefreshObject(r)
}

// CurrentPosition gets the current position of this rectangle object, relative to it's parent / canvas
Expand All @@ -32,17 +32,17 @@ func (r *baseObject) CurrentPosition() fyne.Position {
func (r *baseObject) Move(pos fyne.Position) {
r.Position = pos

// TODO refresh?
fyne.RefreshObject(r)
}

// MinSize for a Rectangle simply returns Size{1, 1} as there is no
// explicit content
// MinSize returns the specified minumum size, if set, or {0, 0} otherwise
func (r *baseObject) MinSize() fyne.Size {
return r.min
}

// SetMinSize specifies the smallest size this object should be
func (r *baseObject) SetMinSize(size fyne.Size) {
r.min = size

// TODO refresh?
fyne.RefreshObject(r)
}
13 changes: 0 additions & 13 deletions canvas_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
package fyne

import "testing"

import "github.com/stretchr/testify/assert"

// TestGetMissingCanvas checks for the nil case as no canvas was preset.
// What we want to do is check for the right canvas but we can't call the test
// package that provides this due to import loops :-/
func TestGetMissingCanvas(t *testing.T) {
box := new(dummyObject)

setDriver(new(dummyDriver))
assert.Equal(t, nil, GetCanvas(box))
}
9 changes: 6 additions & 3 deletions examples/apps/life.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ func (g *gameRenderer) ApplyTheme() {
g.deadColor = theme.BackgroundColor()
}

func (g *gameRenderer) Refresh() {
fyne.RefreshObject(g.render)
}

func (g *gameRenderer) Objects() []fyne.CanvasObject {
return g.objects
}
Expand Down Expand Up @@ -259,7 +263,6 @@ func (g *game) toggleRun() {
func (g *game) animate() {
go func() {
tick := time.NewTicker(time.Second / 6)
canvas := fyne.GetCanvas(g)

for {
select {
Expand All @@ -270,7 +273,7 @@ func (g *game) animate() {

state := g.board.nextGen()
g.board.renderState(state)
canvas.Refresh(g)
g.Renderer().Refresh()
}
}
}()
Expand All @@ -291,7 +294,7 @@ func (g *game) OnMouseDown(ev *fyne.MouseEvent) {

g.board.cells[ypos][xpos] = !g.board.cells[ypos][xpos]

fyne.GetCanvas(g).Refresh(g)
g.Renderer().Refresh()
}

func newGame(b *board) *game {
Expand Down
2 changes: 1 addition & 1 deletion widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package fyne
type Widget interface {
CanvasObject

// TODO refresh?
Renderer() WidgetRenderer
}

Expand All @@ -17,6 +16,7 @@ type WidgetRenderer interface {
Layout(Size)
MinSize() Size

Refresh()
ApplyTheme()
Objects() []CanvasObject
}
8 changes: 8 additions & 0 deletions widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func (b *buttonRenderer) Layout(size fyne.Size) {
func (b *buttonRenderer) ApplyTheme() {
b.label.Color = theme.TextColor()

b.Refresh()
}

func (b *buttonRenderer) Refresh() {
b.label.Text = b.button.Text

if b.button.Style == PrimaryButton {
b.background.FillColor = theme.PrimaryColor()
} else {
Expand All @@ -57,6 +63,8 @@ func (b *buttonRenderer) ApplyTheme() {
if b.button.Icon != nil {
b.icon.File = b.button.Icon.CachePath()
}

fyne.RefreshObject(b.button)
}

func (b *buttonRenderer) Objects() []fyne.CanvasObject {
Expand Down
11 changes: 9 additions & 2 deletions widget/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ func (c *checkRenderer) ApplyTheme() {
c.label.Color = theme.TextColor()
c.background.FillColor = theme.BackgroundColor()

c.Refresh()
}

func (c *checkRenderer) Refresh() {
c.label.Text = c.check.Text

if c.check.Checked {
c.icon.File = theme.CheckedIcon().CachePath()
} else {
c.icon.File = theme.UncheckedIcon().CachePath()
}

fyne.RefreshObject(c.check)
}

func (c *checkRenderer) Objects() []fyne.CanvasObject {
Expand All @@ -65,12 +73,11 @@ type Check struct {
// OnMouseDown is called when a mouse down event is captured and triggers any change handler
func (c *Check) OnMouseDown(*fyne.MouseEvent) {
c.Checked = !c.Checked
c.Renderer().ApplyTheme()

if c.OnChanged != nil {
c.OnChanged(c.Checked)
}
fyne.GetCanvas(c).Refresh(c)
c.Renderer().Refresh()
}

func (c *Check) createRenderer() fyne.WidgetRenderer {
Expand Down
21 changes: 13 additions & 8 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@ func (e *entryRenderer) Layout(size fyne.Size) {

// ApplyTheme is called when the Entry may need to update it's look.
func (e *entryRenderer) ApplyTheme() {
e.label.Text = e.entry.Text
e.label.Color = theme.TextColor()
e.box.FillColor = theme.BackgroundColor()
e.Refresh()
}

func (e *entryRenderer) Refresh() {
e.label.Text = e.entry.Text

if e.entry.focused {
e.bg.FillColor = theme.FocusColor()
} else {
e.bg.FillColor = theme.ButtonColor()
}
e.box.FillColor = theme.BackgroundColor()

fyne.RefreshObject(e.entry)
}

func (e *entryRenderer) Objects() []fyne.CanvasObject {
Expand All @@ -73,22 +79,21 @@ func (e *Entry) SetText(text string) {
e.OnChanged(text)
}

e.ApplyTheme()
fyne.GetCanvas(e).Refresh(e)
e.Renderer().Refresh()
}

// OnFocusGained is called when the Entry has been given focus.
func (e *Entry) OnFocusGained() {
e.focused = true
e.ApplyTheme()
fyne.GetCanvas(e).Refresh(e)

e.Renderer().Refresh()
}

// OnFocusLost is called when the Entry has had focus removed.
func (e *Entry) OnFocusLost() {
e.focused = false
e.Renderer().ApplyTheme()
fyne.GetCanvas(e).Refresh(e)

e.Renderer().Refresh()
}

// Focused returns whether or not this Entry has focus.
Expand Down
9 changes: 7 additions & 2 deletions widget/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ func (l *labelRenderer) ApplyTheme() {
l.background.FillColor = theme.BackgroundColor()

l.text.Color = theme.TextColor()
}

func (l *labelRenderer) Refresh() {
l.text.Alignment = l.label.Alignment
l.text.TextStyle = l.label.TextStyle
l.text.Text = l.label.Text

fyne.RefreshObject(l.label)
}

// Label widget is a basic text component with appropriate padding and layout.
Expand All @@ -50,8 +55,8 @@ type Label struct {
// SetText updates the text of the label widget
func (l *Label) SetText(text string) {
l.Text = text
l.ApplyTheme()
fyne.GetCanvas(l).Refresh(l)

l.Renderer().Refresh()
}

func (l *Label) createRenderer() fyne.WidgetRenderer {
Expand Down
12 changes: 3 additions & 9 deletions widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ type List struct {
func (l *List) Prepend(object fyne.CanvasObject) {
l.Children = append([]fyne.CanvasObject{object}, l.Children...)

render := l.Renderer().(*listRenderer)
render.objects = l.Children
render.Layout(l.CurrentSize())

fyne.GetCanvas(l).Refresh(l)
l.Renderer().Refresh()
}

// Append adds a new CanvasObject to the end of the list
func (l *List) Append(object fyne.CanvasObject) {
l.Children = append(l.Children, object)

render := l.Renderer().(*listRenderer)

render.Refresh()
l.Renderer().Refresh()
}

func (l *List) createRenderer() fyne.WidgetRenderer {
Expand Down Expand Up @@ -85,5 +79,5 @@ func (l *listRenderer) Refresh() {
l.objects = l.list.Children
l.Layout(l.list.CurrentSize())

fyne.GetCanvas(l.list).Refresh(l.list)
fyne.RefreshObject(l.list)
}

0 comments on commit 126d1ac

Please sign in to comment.