forked from fyne-io/fyne
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new SimpleRenderer to handle simple cases.
Easiest usage is to return NewSimpleRenderer(myObj) from CreateRenderer Fixes fyne-io#709
- Loading branch information
1 parent
e3a830d
commit 64aba40
Showing
10 changed files
with
153 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,71 @@ | ||
// Package main loads a very basic Hello World graphical application. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"image/color" | ||
"math/rand" | ||
"time" | ||
|
||
"fyne.io/fyne/v2" | ||
"fyne.io/fyne/v2/app" | ||
"fyne.io/fyne/v2/canvas" | ||
"fyne.io/fyne/v2/container" | ||
"fyne.io/fyne/v2/widget" | ||
"fyne.io/fyne/v2/theme" | ||
) | ||
|
||
func main() { | ||
a := app.New() | ||
w := a.NewWindow("Hello") | ||
|
||
hello := widget.NewLabel("Hello Fyne!") | ||
w.SetContent(container.NewVBox( | ||
hello, | ||
widget.NewButton("Hi!", func() { | ||
hello.SetText("Welcome :)") | ||
}), | ||
)) | ||
|
||
w.ShowAndRun() | ||
myApp := app.New() | ||
myWindow := myApp.NewWindow("Lines") | ||
|
||
c := container.NewWithoutLayout() | ||
lps := canvas.NewText("- lps", color.White) | ||
sig := canvas.NewRectangle(theme.BackgroundColor()) | ||
myWindow.SetContent(container.NewBorder(lps, nil, nil, nil, container.NewMax(sig, c))) | ||
myWindow.Resize(fyne.NewSize(400, 400)) | ||
myWindow.SetPadded(false) | ||
|
||
drawn := 0 | ||
r := uint8(0) | ||
g := uint8(0) | ||
go func() { | ||
pos := fyne.NewPos(0, 0) | ||
for { | ||
newPos := fyne.NewPos(float32(rand.Intn(400)), float32(rand.Intn(400))) | ||
|
||
line := canvas.NewLine(color.NRGBA{R: r, G: g, B: 250, A: 255}) | ||
g++ | ||
if g >= 255 { | ||
g = 0 | ||
} | ||
if g%10 == 0 { | ||
r++ | ||
if r >= 255 { | ||
r = 0 | ||
} | ||
} | ||
line.Position1 = pos | ||
line.Position2 = newPos | ||
|
||
c.Objects = append(c.Objects, line) | ||
if len(c.Objects) > 1024 { | ||
c.Objects = c.Objects[1:1025] | ||
} | ||
pos = newPos | ||
|
||
canvas.Refresh(sig) | ||
drawn++ | ||
time.Sleep(time.Millisecond / 8) | ||
} | ||
}() | ||
|
||
go func() { | ||
for { | ||
time.Sleep(time.Second) | ||
lps.Text = fmt.Sprintf("%d lps", drawn) | ||
lps.Refresh() | ||
drawn = 0 | ||
} | ||
}() | ||
|
||
myWindow.ShowAndRun() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package widget | ||
|
||
import "fyne.io/fyne/v2" | ||
|
||
var _ fyne.WidgetRenderer = (*SimpleRenderer)(nil) | ||
|
||
// SimpleRenderer is a basic renderer that satisfies widget.Renderer interface by wrapping | ||
// a single fyne.CanvasObject. | ||
// | ||
// Since: 2.1 | ||
type SimpleRenderer struct { | ||
object fyne.CanvasObject | ||
} | ||
|
||
// NewSimpleRenderer creates a new BaseRenderer. | ||
// | ||
// Since: 2.1 | ||
func NewSimpleRenderer(object fyne.CanvasObject) *SimpleRenderer { | ||
return &SimpleRenderer{object} | ||
} | ||
|
||
// Destroy does nothing in the base implementation. | ||
// | ||
// Implements: fyne.WidgetRenderer | ||
// | ||
// Since: 2.1 | ||
func (r *SimpleRenderer) Destroy() { | ||
} | ||
|
||
// Layout updates the contained object to be the requested size. | ||
// | ||
// Implements: fyne.WidgetRenderer | ||
// | ||
// Since: 2.1 | ||
func (r *SimpleRenderer) Layout(s fyne.Size) { | ||
r.object.Resize(s) | ||
} | ||
|
||
// MinSize returns the smallest size that this render can use, returned from the underlying object. | ||
// | ||
// Implements: fyne.WidgetRenderer | ||
// | ||
// Since: 2.1 | ||
func (r *SimpleRenderer) MinSize() fyne.Size { | ||
return r.object.MinSize() | ||
} | ||
|
||
// Objects returns the objects that should be rendered. | ||
// | ||
// Implements: fyne.WidgetRenderer | ||
// | ||
// Since: 2.1 | ||
func (r *SimpleRenderer) Objects() []fyne.CanvasObject { | ||
return []fyne.CanvasObject{r.object} | ||
} | ||
|
||
// Refresh requests the underlying object to redraw. | ||
// | ||
// Implements: fyne.WidgetRenderer | ||
// | ||
// Since: 2.1 | ||
func (r *SimpleRenderer) Refresh() { | ||
r.object.Refresh() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.