Skip to content

Commit

Permalink
Remove some inefficient code and undesirable APIs
Browse files Browse the repository at this point in the history
This looks like a big refactor but basically pushes code
that could be optimised into the driver for optimisation.
Remove fyne.GetDriver() and add fyne.CurrentApp() as that is
friendlier and more future proof.
  • Loading branch information
andydotxyz committed Nov 29, 2018
1 parent f9b680a commit 277ae39
Show file tree
Hide file tree
Showing 19 changed files with 85 additions and 45 deletions.
16 changes: 16 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,20 @@ type App interface {
// Calling Quit on the application will cause the application to exit
// cleanly, closing all open windows.
Quit()

// Driver returns the driver that is rendering this application.
// Typically not needed for day to day work, mostly internal functionality.
Driver() Driver
}

var app App

// SetCurrentApp is an internal function to set the app instance currently running
func SetCurrentApp(current App) {
app = current
}

// CurrentApp returns the current application, for which there is only 1 per process.
func CurrentApp() App {
return app
}
9 changes: 6 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (app *fyneApp) Quit() {
app.driver.Quit()
}

func (app *fyneApp) Driver() fyne.Driver {
return app.driver
}

func (app *fyneApp) applyTheme(fyne.Settings) {
for _, window := range app.driver.AllWindows() {
content := window.Content()
Expand All @@ -40,9 +44,8 @@ func (app *fyneApp) applyTheme(fyne.Settings) {
// As this package has no default driver one must be provided.
// Helpers are available - see desktop.NewApp() and test.NewApp().
func NewAppWithDriver(d fyne.Driver) fyne.App {
newApp := &fyneApp{}
newApp.driver = d
fyne.SetDriver(d)
newApp := &fyneApp{driver: d}
fyne.SetCurrentApp(newApp)

listener := make(chan fyne.Settings)
fyne.GetSettings().AddChangeListener(listener)
Expand Down
4 changes: 4 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func TestDummyApp(t *testing.T) {
type dummyDriver struct {
}

func (d *dummyDriver) CanvasForObject(fyne.CanvasObject) fyne.Canvas {
return nil
}

func (d *dummyDriver) CreateWindow(string) fyne.Window {
return nil
}
Expand Down
1 change: 0 additions & 1 deletion canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ type Canvas interface {
Content() CanvasObject
SetContent(CanvasObject)
Refresh(CanvasObject)
Contains(CanvasObject) bool
Focus(FocusableObject)
Focused() FocusableObject

Expand Down
7 changes: 3 additions & 4 deletions canvas/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ func (r *baseObject) Hide() {
// Refresh instructs the containing canvas to refresh the specified obj.
// Use of this method directly is not recommended and it may be removed in a future release
func Refresh(obj fyne.CanvasObject) {
for _, window := range fyne.GetDriver().AllWindows() {
if window.Canvas() != nil && window.Canvas().Contains(obj) {
window.Canvas().Refresh(obj)
}
c := fyne.CurrentApp().Driver().CanvasForObject(obj)
if c != nil {
c.Refresh(obj)
}
}
2 changes: 1 addition & 1 deletion canvas/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (t *Text) Move(pos fyne.Position) {
// 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 {
return fyne.GetDriver().RenderedTextSize(t.Text, t.TextSize, t.TextStyle)
return fyne.CurrentApp().Driver().RenderedTextSize(t.Text, t.TextSize, t.TextStyle)
}

// NewText returns a new Text implementation
Expand Down
2 changes: 1 addition & 1 deletion dialog/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (d *dialog) MinSize(obj []fyne.CanvasObject) fyne.Size {
}

func newDialogWin(title string, _ fyne.Window) fyne.Window {
win := fyne.GetDriver().CreateWindow(title)
win := fyne.CurrentApp().Driver().CreateWindow(title)
win.SetFixedSize(true)
// win.SetParent(parent)

Expand Down
18 changes: 4 additions & 14 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,12 @@ type Driver interface {

// Return the size required to render the given string of specified font size and style
RenderedTextSize(string, int, TextStyle) Size

// Get the canvas that is associated with a given CanvasObject
CanvasForObject(CanvasObject) Canvas

// Start the main event loop of the driver
Run()
// Close the driver and open windows then exit the application
Quit()
}

var driver Driver

// GetDriver returns the current render driver.
// This is basically an internal detail - use with caution.
func GetDriver() Driver {
return driver
}

// SetDriver sets the application driver.
// This bridges internal modularity - do not call this method directly.
func SetDriver(d Driver) {
driver = d
}
4 changes: 0 additions & 4 deletions driver/efl/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,6 @@ func (c *eflCanvas) doRefresh(o fyne.CanvasObject) {
}
}

func (c *eflCanvas) Contains(obj fyne.CanvasObject) bool {
return c.native[obj] != nil
}

func (c *eflCanvas) Focus(obj fyne.FocusableObject) {
if c.focused != nil {
if c.focused == obj {
Expand Down
11 changes: 11 additions & 0 deletions driver/efl/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ const (
type eFLDriver struct {
}

func (d *eFLDriver) CanvasForObject(obj fyne.CanvasObject) fyne.Canvas {
for _, win := range windows {
canv := win.canvas.(*eflCanvas)
if canv.native[obj] != nil {
return canv
}
}

return nil
}

func (d *eFLDriver) Run() {
runEFL()
}
Expand Down
2 changes: 1 addition & 1 deletion driver/efl/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func onLogCallback(domain *C.char, level int, file *C.char, num int, message *C.
// DoQuit will cause the driver's Quit method to be called to terminate the app
//export DoQuit
func DoQuit() {
fyne.GetDriver().Quit()
fyne.CurrentApp().Driver().Quit()
}

// Quit will cause the render loop to end and the application to exit
Expand Down
4 changes: 2 additions & 2 deletions driver/efl/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ func nativeTextBounds(obj *C.Evas_Object) fyne.Size {
}

func (d *eFLDriver) RenderedTextSize(text string, size int, style fyne.TextStyle) fyne.Size {
if len(fyne.GetDriver().AllWindows()) == 0 {
if len(d.AllWindows()) == 0 {
return fyne.NewSize(0, 0)
}
c := fyne.GetDriver().AllWindows()[0].Canvas().(*eflCanvas)
c := d.AllWindows()[0].Canvas().(*eflCanvas)
var native fyne.Size

runOnMain(func() {
Expand Down
4 changes: 0 additions & 4 deletions driver/gl/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ func (c *glCanvas) Refresh(obj fyne.CanvasObject) {
c.setDirty()
}

func (c *glCanvas) Contains(fyne.CanvasObject) bool {
return true
}

func (c *glCanvas) Focus(obj fyne.FocusableObject) {
if c.focused != nil {
c.focused.(fyne.FocusableObject).OnFocusLost()
Expand Down
13 changes: 13 additions & 0 deletions driver/gl/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ type gLDriver struct {
done chan interface{}
}

func (d *gLDriver) CanvasForObject(obj fyne.CanvasObject) fyne.Canvas {
var found fyne.Canvas
for _, win := range d.windows {
walkObjects(win.Content(), fyne.NewPos(0, 0),
func(child fyne.CanvasObject, _ fyne.Position) {
if child == obj {
found = win.Canvas()
}
})
}
return found
}

// TODO for styles...
var fontRegular *truetype.Font

Expand Down
2 changes: 1 addition & 1 deletion driver/gl/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (w *window) Close() {

func (w *window) ShowAndRun() {
w.Show()
fyne.GetDriver().Run()
fyne.CurrentApp().Driver().Run()
}

func (w *window) Content() fyne.CanvasObject {
Expand Down
11 changes: 8 additions & 3 deletions test/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func init() {
}

type testApp struct {
driver *testDriver
}

func (a *testApp) NewWindow(title string) fyne.Window {
Expand All @@ -28,7 +29,7 @@ func (a *testApp) Quit() {
}

func (a *testApp) applyTheme(fyne.Settings) {
for _, window := range fyne.GetDriver().AllWindows() {
for _, window := range a.driver.AllWindows() {
content := window.Content()

switch themed := content.(type) {
Expand All @@ -39,11 +40,15 @@ func (a *testApp) applyTheme(fyne.Settings) {
}
}

func (a *testApp) Driver() fyne.Driver {
return a.driver
}

// NewApp returns a new dummy app used for testing..
// It loads a test driver which creates a virtual window in memory for testing.
func NewApp() fyne.App {
test := &testApp{}
fyne.SetDriver(NewTestDriver())
test := &testApp{driver: NewTestDriver().(*testDriver)}
fyne.SetCurrentApp(test)

listener := make(chan fyne.Settings)
fyne.GetSettings().AddChangeListener(listener)
Expand Down
4 changes: 0 additions & 4 deletions test/testcanvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func (c *testCanvas) SetContent(content fyne.CanvasObject) {
func (c *testCanvas) Refresh(fyne.CanvasObject) {
}

func (c *testCanvas) Contains(fyne.CanvasObject) bool {
return true
}

func (c *testCanvas) Focus(obj fyne.FocusableObject) {
c.focused = obj
obj.OnFocusGained()
Expand Down
5 changes: 5 additions & 0 deletions test/testdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import "github.com/fyne-io/fyne"
type testDriver struct {
}

func (d *testDriver) CanvasForObject(fyne.CanvasObject) fyne.Canvas {
// cheating as we only have a single test window
return windows[0].Canvas()
}

func (d *testDriver) CreateWindow(string) fyne.Window {
return NewTestWindow(nil)
}
Expand Down
11 changes: 9 additions & 2 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ type entryRenderer struct {
}

func emptyTextMinSize(style fyne.TextStyle) fyne.Size {
return fyne.GetDriver().RenderedTextSize("M", theme.TextSize(), style)
return textMinSize("M", theme.TextSize(), style)
}

func textMinSize(text string, size int, style fyne.TextStyle) fyne.Size {
label := canvas.NewText(text, color.Black)
label.TextSize = size
label.TextStyle = style
return label.MinSize()
}

// MinSize calculates the minimum size of an entry widget.
Expand All @@ -45,7 +52,7 @@ func (e *entryRenderer) cursorPosition() (int, int) {
e.entry.CursorColumn = len(str)
}
substr := str[0:e.entry.CursorColumn]
subSize := fyne.GetDriver().RenderedTextSize(substr, renderlabel.TextSize, e.label.TextStyle)
subSize := textMinSize(substr, renderlabel.TextSize, e.label.TextStyle)

return subSize.Width, e.entry.CursorRow * lineHeight
}
Expand Down

0 comments on commit 277ae39

Please sign in to comment.