Skip to content

Commit

Permalink
README: Advertise declarative style only
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Oct 23, 2012
1 parent 8ecbf3c commit 23aa80f
Showing 1 changed file with 13 additions and 113 deletions.
126 changes: 13 additions & 113 deletions README.mdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,124 +16,24 @@ Now run `go get github.com/lxn/walk`
Using Walk
==========

There are three options to create GUIs with Walk:

1. Imperative code
==================

package main

import "github.com/lxn/walk"

func newMainWindow() (*walk.MainWindow, error) {
mw, err := walk.NewMainWindow()
if err != nil {
return nil, err
}
succeeded := false
defer func(){
if !succeeded {
mw.Dispose()
}
}()
if err := mw.SetTitle("My Cool App"); err != nil {
return nil, err
}
if err := mw.SetMinMaxSize(walk.Size{400, 300}, walk.Size{}); err != nil {
return nil, err
}
if err := mw.SetLayout(walk.NewVBoxLayout()); err != nil {
return nil, err
}
pb, err := walk.NewPushButton(mw)
if err != nil {
return nil, err
}
if err := pb.SetText("Don't Click Me!"); err != nil {
return nil, err
}
pb.Clicked().Attach(func() {
panic("Ouch!")
})
succeeded = true
return mw, nil
}

Using this approach, a lot of error handling is required. In this code
it is hard to see the hierarchical structure of the GUI.

2. Declarative code
===================
The preferred way to create GUIs with Walk is to use its declarative sub package,
as illustrated in this small example:

package main

import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
import . "github.com/lxn/walk/declarative"

func newMainWindow() (*walk.MainWindow, error) {
var mw *walk.MainWindow
return mw, MainWindow{
AssignTo: &mw,
Title: "My Cool App",
MinSize: Size{400, 300},
Layout: VBox{},
func main() {
MainWindow{
Title: "My Cool App",
MinSize: Size{400, 300},
Layout: HBox{},
Children: []Widget{
PushButton{
Text: "Don't Click Me!",
OnClicked: func() { panic("Ouch!") },
},
HSpacer{},
PushButton{Text: "Don't Click Me!", OnClicked: func() { panic("Ouch!") }},
HSpacer{},
},
}.Create()
}

This requires much less error handling and the hierarchical structure of the GUI is reflected in the code.

3. Qt Designer and ui2walk
==========================

The ui2walk tool generates Go code for use with Walk from Qt Designer ui files.

It generates .go files for every .ui file in the current working directory,
recursively.

If you e.g. have a file 'mydialog.ui', ui2walk will create 'mydialog_ui.go' in
the same directory. This file will get regenerated every time you run ui2walk,
so don't edit it.

If it doesn't already exist, ui2walk also creates a matching "logic" file
'mydialog.go', which will not be regenerated, so you can extend it:

package main

import "github.com/lxn/walk"

type MyDialog struct {
*walk.Dialog
ui myDialogUI
}

func RunMyDialog(owner walk.RootWidget) (int, error) {
dlg := new(MyDialog)
if err := dlg.init(owner); err != nil {
return 0, err
}

// TODO: Do further required setup, e.g. for event handling, here.

return dlg.Run(), nil
}.Run()
}

ui2walk emits i18n-ed strings when using the `-tr` flag. See
https://github.com/lxn/polyglot for more details.
There are some [examples](walk/tree/master/examples) that should get you started.

0 comments on commit 23aa80f

Please sign in to comment.