app

package module
v0.0.0-...-f5d5a66 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2017 License: MIT Imports: 14 Imported by: 0

README

app

app

Build Status Go Report Card Coverage Status GoDoc

Build multiplatform apps with Go, HTML and CSS.

Table of Contents

  1. Requirements
  2. Install
  3. Getting started
  4. Documentation

Requirements:

  • MacOS: 10.12 (Sierra) and the latest version of Xcode (mandatory for Apple frameworks).

Install

  1. Install Golang:

  2. Get a driver:

    • MacOS: go get -u github.com/murlokswarm/mac
    • Windows: In progress
    • Linux: (Please contribute)
    • IOS: (Please contribute)
    • Android: (Please contribute)

Getting started

hello

Import a driver
import (
	_ "github.com/murlokswarm/mac"
)
Create a component
type Hello struct {
	Greeting string
}

func (h *Hello) Render() string {
	return `
<div class="WindowLayout">    
    <div class="HelloBox">
        <h1>
            Hello,
            <span>{{if .Greeting}}{{html .Greeting}}{{else}}World{{end}}</span>
        </h1>
        <input type="text" placeholder="What is your name?" onchange="OnInputChange" />
    </div>
</div>
    `
}

func (h *Hello) OnInputChange(arg app.ChangeArg) {
	h.Greeting = arg.Value
	app.Render(h)
}

func init() {
	app.RegisterComponent(&Hello{})
}
Write the main
func main() {
	app.OnLaunch = func() {
		win := app.NewWindow(app.Window{
			Title:          "Hello World",
			Width:          1280,
			Height:         720,
			TitlebarHidden: true,
		})

		hello := &Hello{}
		win.Mount(hello)
	}

	app.Run()
}
Style your component

Create a CSS file in [PACKAGE PATH]/resources/css/ and write your styles.

resources/css/hello.css:
body {
    background-image: url("../bg1.jpg");
    background-size: cover;
    background-position: center;
    color: white;
    overflow: hidden;
}

.WindowLayout {
    position: fixed;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.HelloBox {
    padding: 20pt;
}

h1 {
    font-weight: 300;
}

input {
    width: 100%;
    padding: 5pt;
    border: 0;
    border-left: 2px solid silver;
    outline: none;
    font-size: 14px;
    background: transparent;
    color: white;
}

input:focus {
    border-left-color: deepskyblue;
}

Try it by cloning the full example.

Go to the cloned directory and type:

go build
./hello

Documentation

More examples

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// OnLaunch is a handler which (if set) is called when the app is
	// initialized and ready.
	// The main window should be created here.
	OnLaunch func()

	// OnFocus is a handler which (if set) is called when the app became
	// focused.
	OnFocus func()

	// OnBlur is a handler which (if set) is called when the app lost the
	// focus.
	OnBlur func()

	// OnReopen is a handler which (if set) is called when the app is reopened.
	// Eg. when the dock icon is clicked.
	OnReopen func()

	// OnFilesOpen is a handler which (if set) is called when files are targeted
	// to be opened with the app.
	OnFilesOpen func(filenames []string)

	// OnURLOpen is a handler which (if set) is called when the app is opened by
	// an URL.
	OnURLOpen func(URL url.URL)

	// OnTerminate is a handler which (if set) is called when the app is
	// requested to terminates. Return false cancels the termination request.
	OnTerminate func() bool

	// OnFinalize is a handler which (if set) is called when the app is about
	// to be terminated.
	// It should be used to perform any final cleanup before the application
	// terminates.
	OnFinalize func()
)
View Source
var (
	// UIChan is a channel which take a func as payload.
	// When the app package is initialized, it creates a goroutine dedicated to
	// execute UI related tasks in order to avoid to deal with concurrency when
	// programming a component.
	// UIChan allows to enqueue UI related tasks that should be executed in this
	// goroutine.
	// When implementing a driver, driver and component callbacks should be
	// called through this channel.
	UIChan = make(chan func(), 256)
)

Functions

func ComponentID

func ComponentID(c Componer) uuid.UUID

ComponentID returns the id of c. Panic if c is not mounted.

func FileHaveExtension

func FileHaveExtension(name string, exts ...string) bool

FileHaveExtension returns a boolean indicating whether or not name have an extension defined in exts.

func FileIsSupportedIcon

func FileIsSupportedIcon(name string) bool

FileIsSupportedIcon returns a boolean indicating whether or not name is a supported icon.

func GetFilenamesFromDir

func GetFilenamesFromDir(dirname string, extension ...string) (names []string)

GetFilenamesFromDir returns the filenames within dirname. names are not prefixed by dirname.

func HandleEvent

func HandleEvent(msg string)

HandleEvent allows to call the component method or map the component field described in msg. Should be used only in a driver.

func MurlokJS

func MurlokJS() string

MurlokJS returns the javascript code allowing bidirectional communication between a context and it's webview. Should be used only in drivers implementations.

func RegisterComponent

func RegisterComponent(c Componer)

RegisterComponent allows the app to create a component of type c when found into a markup. Should be called in an init func following the component implementation.

func RegisterDriver

func RegisterDriver(d Driver)

RegisterDriver registers the driver to be used when using the app package.

Driver implementation: - Should be called once in an init() func.

func Render

func Render(c Componer)

Render renders a component. Update the rendering of c. c must be mounted into a context.

func Resources

func Resources() string

Resources returns the location of the resources directory. resources directory should contain files required by the UI. Its path should be used only for read only operations, otherwise it could mess up with the app signature.

func Run

func Run()

Run runs the app.

func Storage

func Storage() string

Storage returns the location of the app storage directory. Content generated (e.g. sqlite db) or downloaded (e.g. images, music) should be saved in this directory.

Types

type ChangeArg

type ChangeArg struct {
	Value  string
	Target DOMElement
}

ChangeArg represents the data passed in a onchange event.

type Componer

type Componer interface {
	// Render should returns a markup.
	// The markup can be a template string following the text/template standard
	// package rules.
	Render() string
}

Componer is the interface that describes a component.

func ComponentByID

func ComponentByID(id uuid.UUID) Componer

ComponentByID returns the component associated with id. Panic if no component with id is mounted.

type ContextMenu

type ContextMenu Menu

ContextMenu is a struct that describes a context menu. It will be used by a driver to create a context on the top of a native context menu.

type Contexter

type Contexter interface {
	Elementer

	// Mount mounts the component c into the context. The context is displayed
	// with the default appearance of the component.
	//
	// Driver implementation:
	// - Should call markup.Mount()
	Mount(c Componer)

	// Component returns the component mounted with Mount().
	// Returns nil if there is no component mounted.
	Component() Componer

	// Render renders the DOM node targeted into the sync description s.
	// s contains info specifying if the node and its children should be
	// replaced or just updated.
	// Should be called in a driver implementation.
	Render(s markup.Sync)
}

Contexter is the interface that describes an element where a component can be mounted and rendered. e.g. a window.

func Context

func Context(c Componer) Contexter

Context returns the context where c is mounted. Panic if there is no context where c is mounted.

func MenuBar() (menu Contexter, ok bool)

MenuBar returns the menu bar context. ok will be false if there is no menubar available.

func NewContextMenu

func NewContextMenu() Contexter

NewContextMenu creates a new context menu.

type DOMElement

type DOMElement struct {
	Tag   string // The tag of the element. e.g. div.
	ID    string // The id attribute.
	Class string // the class attribute.
	Value string // The value attribute.
	Index string // The data-murlok-index attribute.
}

DOMElement represents a DOM element.

type DeltaMode

type DeltaMode uint64

DeltaMode is an indication of the units of measurement for a delta value.

type Dismounter

type Dismounter interface {
	OnDismount()
}

Dismounter is the interface that wraps OnDismount method. OnDismount si called when a component is dismounted.

type Docker

type Docker interface {
	Contexter

	// SetIcon set the dock icon to the image targeted by imageName.
	SetIcon(imageName string)

	// SetBadge set the dock badge with the string value of v.
	SetBadge(v interface{})
}

Docker is the interface that describes a dock.

func Dock

func Dock() (d Docker, ok bool)

Dock returns the dock context. ok will be false if there is no dock available.

type Driver

type Driver interface {
	// Run runs the application.
	//
	// Driver implementation:
	// - Should start the app loop.
	Run()

	// NewElement create an app element. e should be a struct describing the
	// element (e.g. Window, ContextMenu).
	//
	// Driver implementation:
	// - Should check the type of e and then create the native element
	//   described.
	NewElement(e interface{}) Elementer

	// MenuBar returns the element that represents the menu bar.
	// ok will be false if there is no menubar available.
	//
	// Driver implementation:
	// - Should be created in a driver.
	MenuBar() (menu Contexter, ok bool)

	// Dock returns the element that represents the dock.
	// ok will be false if there is no dock available.
	//
	// Driver implementation:
	// - Should be created in the driver implementation.
	Dock() (d Docker, ok bool)

	// Resources returns the location of the resources directory.
	Resources() string

	// Storage returns the location of the app storage directory.
	Storage() string

	// JavascriptBridge is the javascript function to call when a driver want to
	// pass data to the native platform.
	JavascriptBridge() string
}

Driver is the interface that describes the implementation to handle platform specific rendering.

type ElementStorer

type ElementStorer interface {
	// Add adds an element in the store.
	// Should be called in a driver implementation when a native element is
	// created.
	Add(e Elementer)

	// Remove removes an element from the store.
	// Should be called in a driver implementation when a native element is
	// closed or removed.
	Remove(e Elementer)

	// Len returns the numbers of element in use.
	Len() int

	// Get returns the element with id.
	// ok will be false if there is no element with id.
	Get(id uuid.UUID) (e Elementer, ok bool)
}

ElementStorer is the interface that describes a element store. It keep a reference of the elements used by the app and allows to retrieve them when needed. Implementation should be thread safe.

func Elements

func Elements() ElementStorer

Elements returns the element store containing all the elements in use by the app.

type Elementer

type Elementer interface {
	// ID returns the identifier of the element.
	ID() uuid.UUID
}

Elementer is the interface that describes an app element. It wraps the ID method which allow to keep and retrieve a element.

Driver implementation: - Elements().Add() should be called when an element is created. - Elements().Remove() should be called when an element is closed.

func NewFilePicker

func NewFilePicker(p FilePicker) Elementer

NewFilePicker creates and opens a native file picker described by fp.

func NewShare

func NewShare(s Share) Elementer

NewShare creates a new sharing.

type EventArg

type EventArg struct {
	Target DOMElement
}

EventArg represents the data passed in events.

type FilePicker

type FilePicker struct {
	MultipleSelection bool
	NoDir             bool
	NoFile            bool
	OnPick            func(filenames []string)
}

FilePicker is a struct that describes a file picker. It will be used by a driver to create a native file picker that allow to select files and directories filenames.

type HTMLContext

type HTMLContext struct {
	ID       uuid.UUID
	Title    string
	Lang     string
	MurlokJS string
	JS       []string
	CSS      []string
}

HTMLContext contains the data required to generate the minimum HTML to setup a webview based context. Should be used only in drivers implementations.

func (HTMLContext) HTML

func (c HTMLContext) HTML() string

HTML generate the HTML based on the data of c.

type Hrefer

type Hrefer interface {
	OnHref(URL *url.URL)
}

Hrefer is the interface that wraps OnHref method. OnHref is called when the component is mounted from a click on a link that targets the component in the href attribute.

type KeyCode

type KeyCode uint8

KeyCode represents a system and implementation dependent numerical code identifying the unmodified value of the pressed key.

const (
	KeyBackspace          KeyCode = 8
	KeyTab                KeyCode = 9
	KeyEnter              KeyCode = 13
	KeyShift              KeyCode = 16
	KeyCtrl               KeyCode = 17
	KeyAlt                KeyCode = 18
	KeyPauseBreak         KeyCode = 19
	KeyCapsLock           KeyCode = 20
	KeyEsc                KeyCode = 27
	KeySpace              KeyCode = 32
	KeyPageUp             KeyCode = 33
	KeyPageDown           KeyCode = 34
	KeyEnd                KeyCode = 35
	KeyHome               KeyCode = 36
	KeyLeft               KeyCode = 37
	KeyUp                 KeyCode = 38
	KeyRight              KeyCode = 39
	KeyDown               KeyCode = 40
	KeyPrintScreen        KeyCode = 44
	KeyInsert             KeyCode = 45
	KeyDelete             KeyCode = 46
	Key0                  KeyCode = 48
	Key1                  KeyCode = 49
	Key2                  KeyCode = 50
	Key3                  KeyCode = 51
	Key4                  KeyCode = 52
	Key5                  KeyCode = 53
	Key6                  KeyCode = 54
	Key7                  KeyCode = 55
	Key8                  KeyCode = 56
	Key9                  KeyCode = 57
	KeyA                  KeyCode = 65
	KeyB                  KeyCode = 66
	KeyC                  KeyCode = 67
	KeyD                  KeyCode = 68
	KeyE                  KeyCode = 69
	KeyF                  KeyCode = 70
	KeyG                  KeyCode = 71
	KeyH                  KeyCode = 72
	KeyI                  KeyCode = 73
	KeyJ                  KeyCode = 74
	KeyK                  KeyCode = 75
	KeyL                  KeyCode = 76
	KeyM                  KeyCode = 77
	KeyN                  KeyCode = 78
	KeyO                  KeyCode = 79
	KeyP                  KeyCode = 80
	KeyQ                  KeyCode = 81
	KeyR                  KeyCode = 82
	KeyS                  KeyCode = 83
	KeyT                  KeyCode = 84
	KeyU                  KeyCode = 85
	KeyV                  KeyCode = 86
	KeyW                  KeyCode = 87
	KeyX                  KeyCode = 88
	KeyY                  KeyCode = 88
	KeyZ                  KeyCode = 90
	KeyMeta               KeyCode = 91
	KeyMenu               KeyCode = 93
	KeyNumPad0            KeyCode = 96
	KeyNumPad1            KeyCode = 97
	KeyNumPad2            KeyCode = 98
	KeyNumPad3            KeyCode = 99
	KeyNumPad4            KeyCode = 100
	KeyNumPad5            KeyCode = 101
	KeyNumPad6            KeyCode = 102
	KeyNumPad7            KeyCode = 103
	KeyNumPad8            KeyCode = 104
	KeyNumPad9            KeyCode = 105
	KeyNumPadMult         KeyCode = 106
	KeyNumPadPlus         KeyCode = 107
	KeyNumPadMin          KeyCode = 109
	KeyNumPadDot          KeyCode = 110
	KeyNumPadDecimal      KeyCode = 111
	KeyF1                 KeyCode = 112
	KeyF2                 KeyCode = 113
	KeyF3                 KeyCode = 114
	KeyF4                 KeyCode = 115
	KeyF5                 KeyCode = 116
	KeyF6                 KeyCode = 117
	KeyF7                 KeyCode = 118
	KeyF8                 KeyCode = 119
	KeyF9                 KeyCode = 120
	KeyF10                KeyCode = 121
	KeyF11                KeyCode = 122
	KeyF12                KeyCode = 123
	KeyNumLock            KeyCode = 144
	KeyMute               KeyCode = 173
	KeyVolumeDown         KeyCode = 174
	KeyVolumeUp           KeyCode = 175
	KeySemicolon          KeyCode = 186
	KeyEqual              KeyCode = 187
	KeyComa               KeyCode = 188
	KeyDash               KeyCode = 189
	KeyDot                KeyCode = 190
	KeySlash              KeyCode = 191
	KeyBackquote          KeyCode = 192
	KeySquareBracketLeft  KeyCode = 219
	KeyBackslash          KeyCode = 220
	KeySquareBracketRight KeyCode = 221
	KeyQuote              KeyCode = 222
)

Keyboard keys.

type KeyLocation

type KeyLocation uint8

KeyLocation represents the location of the key on the keyboard or other input device.

const (
	KeyLocationStandard KeyLocation = iota
	KeyLocationLeft
	KeyLocationRight
	KeyLocationNumpad
)

Keyboard locations.

type KeyboardArg

type KeyboardArg struct {
	CharCode rune
	KeyCode  KeyCode
	Location KeyLocation
	AltKey   bool
	CtrlKey  bool
	MetaKey  bool
	ShiftKey bool
	Target   DOMElement
}

KeyboardArg represents data fired when the keyboard is used.

type Menu struct{}

Menu is a struct that describes a menu. It will be used by a driver to create a menu on the top of a native menu.

type Mounter

type Mounter interface {
	OnMount()
}

Mounter is the interface that wraps OnMount method. OnMount si called when a component is mounted.

type MouseArg

type MouseArg struct {
	ClientX  float64
	ClientY  float64
	PageX    float64
	PageY    float64
	ScreenX  float64
	ScreenY  float64
	Button   int
	Detail   int
	AltKey   bool
	CtrlKey  bool
	MetaKey  bool
	ShiftKey bool
	Target   DOMElement
}

MouseArg represents data fired when interacting with a pointing device (such as a mouse).

type Share

type Share struct {
	Value interface{}
}

Share is a struct that describes a share. It will be used by a driver to create a native share panel.

type TemplateFuncMapper

type TemplateFuncMapper interface {
	// Allows to add custom functions to the template used to render the
	// component.
	// Note that funcs named json and time are already implemented to handle
	// structs as prop and time format. Overloads of these will be ignored.
	// See https://golang.org/pkg/text/template/#Template.Funcs for more details.
	FuncMaps() template.FuncMap
}

TemplateFuncMapper is the interface that wraps FuncMaps method.

type Vibrancy

type Vibrancy uint8

Vibrancy represents the NSVisualEffectView which will be applied to the background of the window. Only applicable on Apple apps. When set, BackgroundColor is ignored.

const (
	VibeNone Vibrancy = iota
	VibeLight
	VibeDark
	VibeTitlebar
	VibeSelection
	VibeMenu
	VibePopover
	VibeSidebar
	VibeMediumLight
	VibeUltraDark
)

Constants to specify the vibrancy to be used.

type WheelArg

type WheelArg struct {
	DeltaX    float64
	DeltaY    float64
	DeltaZ    float64
	DeltaMode DeltaMode
	Target    DOMElement
}

WheelArg represents data fired when a wheel button of a pointing device (usually a mouse) is rotated.

type Window

type Window struct {
	Title           string
	Lang            string
	X               float64
	Y               float64
	Width           float64
	Height          float64
	MinWidth        float64
	MinHeight       float64
	MaxWidth        float64
	MaxHeight       float64
	BackgroundColor string
	Vibrancy        Vibrancy
	Borderless      bool
	FixedSize       bool
	CloseHidden     bool
	MinimizeHidden  bool
	TitlebarHidden  bool

	OnMinimize       func()
	OnDeminimize     func()
	OnFullScreen     func()
	OnExitFullScreen func()
	OnMove           func(x float64, y float64)
	OnResize         func(width float64, height float64)
	OnFocus          func()
	OnBlur           func()
	OnClose          func() bool
}

Window is a struct that describes a window. It will be used by a driver to create a context on the top of a native window.

type Windower

type Windower interface {
	Contexter

	// Position returns the position of the window.
	Position() (x float64, y float64)

	Move(x float64, y float64)

	// Size returns the size of the window.
	Size() (width float64, height float64)

	// Resize resizes the window.
	Resize(width float64, height float64)

	// Close close`s the window.
	//
	// Driver implementation:
	// - Close should call the native way to close a window.
	// - Native windows often have a handler that is called before a window
	//   is destroyed. This handler should be implemented and call
	//   Elements().Remove() to free resources allocated on go side.
	//   markup.Dismount() should be also called to release the components
	//   mounted.
	Close()
}

Windower is the interface that describes a window context.

func NewWindow

func NewWindow(w Window) Windower

NewWindow creates a new window.

Directories

Path Synopsis
examples
nav

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL