Movatterモバイル変換


[0]ホーム

URL:


gowd

packagemodule
v0.0.0-...-4271bc0Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2022 License:MITImports:12Imported by:17

Details

Repository

github.com/dtylman/gowd

Links

README

gowd

Build cross platform GUI apps with GO and HTML/JS/CSS (powered bynwjs)

CircleCIGo Report CardGoDoc

How to use this library:
  1. Download and installnwjs
  2. Install this librarygo get github.com/dtylman/gowd
  3. Clone this repo.
  4. Placepackage.json,index.html,main.go andmain.js fromtemplate in a new folder.
  5. go build
  6. Editmain.js and setgoBinary to your your executable name:
    var goBinary = "./template"; //or template.exe
  7. Runnw ., the hello world template should appear:hello-world
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 = bodygowd.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 thebootstrap package.

First, add bootsrap css and js to yourindex.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.Elementfunc main() {//creates a new bootstrap fluid containerbody = bootstrap.NewContainer(false)// add some elements using the object modeldiv := bootstrap.NewElement("div", "well")row := bootstrap.NewRow(bootstrap.NewColumn(bootstrap.ColumnLarge, 6, div))body.AddElement(row)// add some other elements from HTMLdiv.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 barbtn := 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 loopgowd.Run(body)}// happens when the 'start' button is clickedfunc 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 eventsbody.Disable()// clean up - remove the added elementsdefer func() {body.RemoveElement(text)body.RemoveElement(progressBar.Element)body.Enable()}()// render the progress barfor 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 refreshedbody.Render()}}

This will yield the following app:

Simple

More a more advanced usage, see theTodo sample

TodoMVC

Documentation

Index

Constants

View Source
const (//OnClick onclick eventOnClick = "onclick"//OnChange onchange eventOnChange = "onchange"//OnKeyPress onkeypress eventOnKeyPress = "onkeypress")
View Source
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

View Source
var (//Order counts the number of elements rendered (for generating auto-ids)Orderint//Output output render target (configurable for unit-tests)Outputio.Writer =os.Stdout)

Functions

funcAlert

func Alert(textstring)

Alert calls javascript alert now

funcExecJS

func ExecJS(jsstring)

ExecJS executes JS code after a DOM update from gowd

funcExecJSNow

func ExecJSNow(jsstring)

ExecJSNow Executes JS code in NWJS without waiting for a DOM update to be finished.

funcRun

func Run(body *Element)error

Run starts the message loop with body as the root element. This function never exits.

Types

typeElement

type Element struct {//Parent the parent elementParent *Element//Kids child elementsKids []*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 renderedHiddenbool// contains filtered or unexported fields}

Element represents a DOM element and its state.

funcNewElement

func NewElement(tagstring) *Element

NewElement creates a new HTML element

funcNewElementFromNode

func NewElementFromNode(node *html.Node, emElementsMap) *Element

NewElementFromNode creates an element from existing node

funcNewStyledText

func NewStyledText(textstring, stylestring) *Element

NewStyledText creates new text element using a specific style

funcNewText

func NewText(textstring) *Element

NewText creates new text node (without HTML tag)

funcParseElement

func ParseElement(innerHTMLstring, emElementsMap) (*Element,error)

ParseElement parses an html with only one root tag, returns the root element.

funcParseElementFromFile

func ParseElementFromFile(fileNamestring, emElementsMap) (*Element,error)

ParseElementFromFile like ParseElement, but reads input from a file

funcParseElements

func ParseElements(rio.Reader, emElementsMap) ([]*Element,error)

ParseElements parse an html fragment and return a list of elements

func (*Element)AddElement

func (e *Element) AddElement(elem *Element) *Element

AddElement adds a child element

func (*Element)AddHTML

func (e *Element) AddHTML(innerHTMLstring, emElementsMap) ([]*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 thegiven ElementMap.

func (*Element)AutoFocus

func (e *Element) AutoFocus()

AutoFocus adds the auto-focus atribute to the element

func (*Element)Disable

func (e *Element) Disable()

Disable sets the `disabled` attribute

func (*Element)Enable

func (e *Element) Enable()

Enable unsets the `disabled` attribute

func (*Element)Find

func (e *Element) Find(idstring) *Element

Find returns the kid, or offspring with a specific `id` attribute value.

func (*Element)GetAttribute

func (e *Element) GetAttribute(keystring) (string,bool)

GetAttribute returns value for attribute

func (*Element)GetID

func (e *Element) GetID()string

GetID returns the value of the `id` attribute

func (*Element)GetValue

func (e *Element) GetValue()string

GetValue returns the value of the `value` attribute

func (*Element)Hide

func (e *Element) Hide()

Hide if set, will not render the element.

func (*Element)OnEvent

func (e *Element) OnEvent(eventstring, handlerEventHandler)

OnEvent register an DOM element event.

func (*Element)OnKeyPressEvent

func (e *Element) OnKeyPressEvent(eventstring, keyCodeint, handlerEventHandler)

OnKeyPressEvent register handler as an OnKeyPressed event.

func (*Element)ProcessEvent

func (e *Element) ProcessEvent(event *Event)

ProcessEvent fires the event provided

func (*Element)RemoveAttribute

func (e *Element) RemoveAttribute(keystring)

RemoveAttribute removes the provided attribute by name

func (*Element)RemoveElement

func (e *Element) RemoveElement(elem *Element)

RemoveElement remove a specific kid

func (*Element)RemoveElements

func (e *Element) RemoveElements()

RemoveElements remove all kids.

func (*Element)Render

func (e *Element) Render()error

Render renders the element.

func (*Element)SetAttribute

func (e *Element) SetAttribute(key, valstring)

SetAttribute adds or set the attribute

func (*Element)SetAttributes

func (e *Element) SetAttributes(event *EventElement)

SetAttributes sets attributes from an event element.

func (*Element)SetClass

func (e *Element) SetClass(classstring)

SetClass adds the given class name to the class attribute.

func (*Element)SetElement

func (e *Element) SetElement(elem *Element) *Element

SetElement removes all child elemens and adds elem as first child

func (*Element)SetID

func (e *Element) SetID(idstring)

SetID sets the `id` attribute

func (*Element)SetText

func (e *Element) SetText(textstring)

SetText Sets the element to hold ONLY the provided text

func (*Element)SetValue

func (e *Element) SetValue(valstring)

SetValue sets the 'value' attribute

func (*Element)Show

func (e *Element) Show()

Show if set, will render the element.

func (*Element)UnsetClass

func (e *Element) UnsetClass(classstring)

UnsetClass removes the given class name from the class attribute

typeElementsMap

type ElementsMap map[string]*Element

ElementsMap maps Elements by their `id` attributes

funcNewElementMap

func NewElementMap()ElementsMap

NewElementMap creates a new ElementMap

typeEvent

type Event struct {Namestring         `json:"name"`SenderEventElement   `json:"sender"`Inputs []EventElement `json:"inputs"`}

Event represents a DOM event

typeEventElement

type EventElement struct {Properties map[string]string `json:"properties"`}

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.

typeEventHandler

type EventHandler func(sender *Element, event *EventElement)

EventHandler handler for DOM event.

Source Files

View all Source files

Directories

PathSynopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp