Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

declarative: Make Dialog and MainWindow not implement Widget

  • Loading branch information...
commit 801b6a0a9c951955829c95c55296abd9d116ccd5 1 parent 2a34abc
@lxn authored
View
2  README.mdown
@@ -95,7 +95,7 @@ it is hard to see the hierarchical structure of the GUI.
OnClicked: func() { panic("Ouch!") },
},
},
- }.Create(nil)
+ }.Create()
}
This requires much less error handling and the hierarchical structure of the GUI is reflected in the code.
View
29 declarative/dialog.go
@@ -25,18 +25,25 @@ type Dialog struct {
CancelButton **walk.PushButton
}
-func (d Dialog) Create(parent walk.Container) error {
- var owner walk.RootWidget
- if o, ok := parent.(walk.RootWidget); ok {
- owner = o
- }
-
+func (d Dialog) Create(owner walk.RootWidget) error {
w, err := walk.NewDialog(owner)
if err != nil {
return err
}
- return InitWidget(d, w, func() error {
+ tlwi := topLevelWindowInfo{
+ Name: d.Name,
+ Disabled: d.Disabled,
+ Hidden: d.Hidden,
+ Font: d.Font,
+ MinSize: d.MinSize,
+ MaxSize: d.MaxSize,
+ ContextMenuActions: d.ContextMenuActions,
+ Layout: d.Layout,
+ Children: d.Children,
+ }
+
+ return InitWidget(tlwi, w, func() error {
if err := w.SetTitle(d.Title); err != nil {
return err
}
@@ -63,11 +70,3 @@ func (d Dialog) Create(parent walk.Container) error {
return nil
})
}
-
-func (d Dialog) WidgetInfo() (name string, disabled, hidden bool, font *Font, minSize, maxSize Size, stretchFactor, row, rowSpan, column, columnSpan int, contextMenuActions []*walk.Action) {
- return d.Name, d.Disabled, d.Hidden, &d.Font, d.MinSize, d.MaxSize, 0, 0, 0, 0, 0, d.ContextMenuActions
-}
-
-func (d Dialog) ContainerInfo() (Layout, []Widget) {
- return d.Layout, d.Children
-}
View
24 declarative/interfaces.go
@@ -24,3 +24,27 @@ type Container interface {
type MenuItem interface {
createAction(menu *walk.Menu) (*walk.Action, error)
}
+
+type topLevelWindowInfo struct {
+ Name string
+ Disabled bool
+ Hidden bool
+ Font Font
+ MinSize Size
+ MaxSize Size
+ ContextMenuActions []*walk.Action
+ Layout Layout
+ Children []Widget
+}
+
+func (topLevelWindowInfo) Create(parent walk.Container) error {
+ return nil
+}
+
+func (i topLevelWindowInfo) WidgetInfo() (name string, disabled, hidden bool, font *Font, minSize, maxSize Size, stretchFactor, row, rowSpan, column, columnSpan int, contextMenuActions []*walk.Action) {
+ return i.Name, i.Disabled, i.Hidden, &i.Font, i.MinSize, i.MaxSize, 0, 0, 0, 0, 0, i.ContextMenuActions
+}
+
+func (i topLevelWindowInfo) ContainerInfo() (Layout, []Widget) {
+ return i.Layout, i.Children
+}
View
24 declarative/mainwindow.go
@@ -25,13 +25,25 @@ type MainWindow struct {
ToolBarActions []*walk.Action
}
-func (mw MainWindow) Create(parent walk.Container) error {
+func (mw MainWindow) Create() error {
w, err := walk.NewMainWindow()
if err != nil {
return err
}
- return InitWidget(mw, w, func() error {
+ tlwi := topLevelWindowInfo{
+ Name: mw.Name,
+ Disabled: mw.Disabled,
+ Hidden: mw.Hidden,
+ Font: mw.Font,
+ MinSize: mw.MinSize,
+ MaxSize: mw.MaxSize,
+ ContextMenuActions: mw.ContextMenuActions,
+ Layout: mw.Layout,
+ Children: mw.Children,
+ }
+
+ return InitWidget(tlwi, w, func() error {
if err := w.SetTitle(mw.Title); err != nil {
return err
}
@@ -61,11 +73,3 @@ func (mw MainWindow) Create(parent walk.Container) error {
return nil
})
}
-
-func (mw MainWindow) WidgetInfo() (name string, disabled, hidden bool, font *Font, minSize, maxSize Size, stretchFactor, row, rowSpan, column, columnSpan int, contextMenuActions []*walk.Action) {
- return mw.Name, mw.Disabled, mw.Hidden, &mw.Font, mw.MinSize, mw.MaxSize, 0, 0, 0, 0, 0, mw.ContextMenuActions
-}
-
-func (mw MainWindow) ContainerInfo() (Layout, []Widget) {
- return mw.Layout, mw.Children
-}
View
2  examples/declarative/declarative.go
@@ -199,7 +199,7 @@ func main() {
},
},
},
- }.Create(nil)); err != nil {
+ }.Create()); err != nil {
log.Fatal(err)
}
Please sign in to comment.
Something went wrong with that request. Please try again.