README
¶
gowd
Build cross platform GUI apps with GO and HTML/JS/CSS (powered by nwjs)
How to use this library:
- Download and install nwjs
- Install this library
go get github.com/dtylman/gowd
- Clone this repo.
- Place
package.json
,index.html
,main.go
andmain.js
from template in a new folder. go build
- Edit
main.js
and setgoBinary
to your your executable name:var goBinary = "./template"; //or template.exe
- Run
nw .
, the hello world template should appear:
Usage
Simplest "hello world":
import "github.com/dtylman/gowd"
func main() {
body, err := gowd.ParseElement("<h1>Hello world</h1>", nil)
if err != nil {
panic(err)
}
gowd.Run(body)
}
Adding a button:
import (
"github.com/dtylman/gowd"
)
func main() {
body, err := gowd.ParseElement("<h1>Hello world</h1>", nil)
if err != nil {
panic(err)
}
p := body.AddElement(gowd.NewElement("p"))
btn := p.AddElement(gowd.NewElement("button"))
btn.SetText("Click me")
btn.OnEvent(gowd.OnClick, btnClicked)
gowd.Run(body)
}
func btnClicked(sender *gowd.Element, event *gowd.EventElement) {
sender.SetText("Clicked!")
}
Creating and binding from HTML:
import (
"github.com/dtylman/gowd"
"fmt"
)
func main() {
body, err := gowd.ParseElement("<h1>Hello world</h1>", nil)
if err != nil {
panic(err)
}
p := body.AddElement(gowd.NewElement("p"))
em := gowd.NewElementMap()
p.AddHtml(`<select id="select1">
<option value="" disabled="disabled" selected="selected">Please select a name</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>`, em)
em["select1"].OnEvent(gowd.OnChange, btnClicked)
em["select1"].Object = body
gowd.Run(body)
}
func btnClicked(sender *gowd.Element, event *gowd.EventElement) {
body := sender.Object.(*gowd.Element)
body.AddElement(gowd.NewStyledText(fmt.Sprintf("Selected %s", event.GetValue()), gowd.BoldText))
body.AddElement(gowd.NewElement("br"))
}
Using bootstrap:
'gowd' supports creating bootstrap elements using the bootstrap package.
First, add bootsrap css and js to your index.html
file:
<script type="text/javascript" src="js/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
Then you can create bootsrap items:
import (
"github.com/dtylman/gowd"
"github.com/dtylman/gowd/bootstrap"
"time"
"fmt"
)
var body *gowd.Element
func main() {
//creates a new bootstrap fluid container
body = bootstrap.NewContainer(false)
// add some elements using the object model
div := bootstrap.NewElement("div", "well")
row := bootstrap.NewRow(bootstrap.NewColumn(bootstrap.ColumnLarge, 6, div))
body.AddElement(row)
// add some other elements from HTML
div.AddHTML(`<div class="dropdown">
<button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
<span class="caret"></span></button>
<ul class="dropdown-menu" id="dropdown-menu">
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">JavaScript</a></li>
</ul>
</div>`, nil)
// add a button to show a progress bar
btn := bootstrap.NewButton(bootstrap.ButtonPrimary, "Start")
btn.OnEvent(gowd.OnClick, btnClicked)
row.AddElement(bootstrap.NewColumn(bootstrap.ColumnLarge, 4, bootstrap.NewElement("div", "well", btn)))
//start the ui loop
gowd.Run(body)
}
// happens when the 'start' button is clicked
func btnClicked(sender *gowd.Element, event *gowd.EventElement) {
// adds a text and progress bar to the body
text := body.AddElement(gowd.NewStyledText("Working...", gowd.BoldText))
progressBar := bootstrap.NewProgressBar()
body.AddElement(progressBar.Element)
// makes the body stop responding to user events
body.Disable()
// clean up - remove the added elements
defer func() {
body.RemoveElement(text)
body.RemoveElement(progressBar.Element)
body.Enable()
}()
// render the progress bar
for i := 0; i <= 123; i++ {
progressBar.SetValue(i, 123)
text.SetText(fmt.Sprintf("Working %v", i))
time.Sleep(time.Millisecond * 20)
// this will cause the body to be refreshed
body.Render()
}
}
This will yield the following app:
More a more advanced usage, see the Todo sample
Documentation
¶
Index ¶
- Constants
- Variables
- func Alert(text string)
- func ExecJS(js string)
- func ExecJSNow(js string)
- func Run(body *Element) error
- type Element
- func NewElement(tag string) *Element
- func NewElementFromNode(node *html.Node, em ElementsMap) *Element
- func NewStyledText(text string, style string) *Element
- func NewText(text string) *Element
- func ParseElement(innerHTML string, em ElementsMap) (*Element, error)
- func ParseElementFromFile(fileName string, em ElementsMap) (*Element, error)
- func ParseElements(r io.Reader, em ElementsMap) ([]*Element, error)
- func (e *Element) AddElement(elem *Element) *Element
- func (e *Element) AddHTML(innerHTML string, em ElementsMap) ([]*Element, error)
- func (e *Element) AutoFocus()
- func (e *Element) Disable()
- func (e *Element) Enable()
- func (e *Element) Find(id string) *Element
- func (e *Element) GetAttribute(key string) (string, bool)
- func (e *Element) GetID() string
- func (e *Element) GetValue() string
- func (e *Element) Hide()
- func (e *Element) OnEvent(event string, handler EventHandler)
- func (e *Element) OnKeyPressEvent(event string, keyCode int, handler EventHandler)
- func (e *Element) ProcessEvent(event *Event)
- func (e *Element) RemoveAttribute(key string)
- func (e *Element) RemoveElement(elem *Element)
- func (e *Element) RemoveElements()
- func (e *Element) Render() error
- func (e *Element) SetAttribute(key, val string)
- func (e *Element) SetAttributes(event *EventElement)
- func (e *Element) SetClass(class string)
- func (e *Element) SetElement(elem *Element) *Element
- func (e *Element) SetID(id string)
- func (e *Element) SetText(text string)
- func (e *Element) SetValue(val string)
- func (e *Element) Show()
- func (e *Element) UnsetClass(class string)
- type ElementsMap
- type Event
- type EventElement
- type EventHandler
Constants ¶
const ( //OnClick onclick event OnClick = "onclick" //OnChange onchange event OnChange = "onchange" //OnKeyPress onkeypress event OnKeyPress = "onkeypress" )
const ( //BoldText <b> BoldText = "b" //StrongText <strong> StrongText = "strong" //ItalicText <i> ItalicText = "i" //EmphasizedText <em> EmphasizedText = "em" //MarkedText <mark> MarkedText = "mark" //SmallText <small> SmallText = "small" //DeletedText <del> DeletedText = "del" //InsertedText <ins> InsertedText = "ins" //SubscriptText <sub> SubscriptText = "sub" //SuperscriptText <sup> SuperscriptText = "sup" //TitleText <title> TitleText = "title" //Paragraph <p> Paragraph = "p" //Heading1 <h1> Heading1 = "h1" //Heading2 <h2> Heading2 = "h2" //Heading3 <h3> Heading3 = "h3" //Heading4 <h4> Heading4 = "h4" //Heading5 <h5> Heading5 = "h5" //Heading6 <h6> Heading6 = "h6" )
Variables ¶
Functions ¶
Types ¶
type Element ¶
type Element struct { //Parent the parent element Parent *Element //Kids child elements Kids []*Element //Attributes element attributes... Attributes []html.Attribute //Object arbitrary user object that can be associated with element. Object interface{} //Hidden if true the element will not be rendered Hidden bool // contains filtered or unexported fields }
Element represents a DOM element and its state.
func NewElementFromNode ¶
func NewElementFromNode(node *html.Node, em ElementsMap) *Element
NewElementFromNode creates an element from existing node
func NewStyledText ¶
NewStyledText creates new text element using a specific style
func ParseElement ¶
func ParseElement(innerHTML string, em ElementsMap) (*Element, error)
ParseElement parses an html with only one root tag, returns the root element.
func ParseElementFromFile ¶
func ParseElementFromFile(fileName string, em ElementsMap) (*Element, error)
ParseElementFromFile like ParseElement, but reads input from a file
func ParseElements ¶
func ParseElements(r io.Reader, em ElementsMap) ([]*Element, error)
ParseElements parse an html fragment and return a list of elements
func (*Element) AddElement ¶
AddElement adds a child element
func (*Element) AddHTML ¶
func (e *Element) AddHTML(innerHTML string, em ElementsMap) ([]*Element, error)
AddHTML parses the provided element and adds it to the current element. Returns a list of root elements from `html`. If em is not nil, for each HTML tag that has the `id` attribute set the corresponding element will be stored in the given ElementMap.
func (*Element) AutoFocus ¶
func (e *Element) AutoFocus()
AutoFocus adds the auto-focus atribute to the element
func (*Element) GetAttribute ¶
GetAttribute returns value for attribute
func (*Element) OnEvent ¶
func (e *Element) OnEvent(event string, handler EventHandler)
OnEvent register an DOM element event.
func (*Element) OnKeyPressEvent ¶
func (e *Element) OnKeyPressEvent(event string, keyCode int, handler EventHandler)
OnKeyPressEvent register handler as an OnKeyPressed event.
func (*Element) ProcessEvent ¶
ProcessEvent fires the event provided
func (*Element) RemoveAttribute ¶
RemoveAttribute removes the provided attribute by name
func (*Element) RemoveElement ¶
RemoveElement remove a specific kid
func (*Element) SetAttribute ¶
SetAttribute adds or set the attribute
func (*Element) SetAttributes ¶
func (e *Element) SetAttributes(event *EventElement)
SetAttributes sets attributes from an event element.
func (*Element) SetElement ¶
SetElement removes all child elemens and adds elem as first child
func (*Element) UnsetClass ¶
UnsetClass removes the given class name from the class attribute
type ElementsMap ¶
ElementsMap maps Elements by their `id` attributes
type Event ¶
type Event struct { Name string `json:"name"` Sender EventElement `json:"sender"` Inputs []EventElement `json:"inputs"` }
Event represents a DOM event
type EventElement ¶
EventElement represents the DOM element sending an event
func (*EventElement) GetID ¶
func (e *EventElement) GetID() string
GetID get the id of the event sender.
func (*EventElement) GetValue ¶
func (e *EventElement) GetValue() string
GetValue gets the value of the event sender.
type EventHandler ¶
type EventHandler func(sender *Element, event *EventElement)
EventHandler handler for DOM event.