goui

package module
v0.0.0-...-57faf36 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2020 License: MIT Imports: 14 Imported by: 0

README

GoUI

We are currently refactoring the project, and it is not ready to use.

Go Report Card

GoUI is a lightweight cross-platform Go "GUI" framework. It is not really a GUI library, instead, it displays things through web view. That is, you can write cross-platform applications using JavaScript, HTML and CSS, and Go of cause.

The basic idea is:

  1. Write your app with HTML, JavaScript, and CSS like it is a single page web application. You may adopt whatever javascript framework you like, angular, backbone, etc.
  2. Wrap the web application to desktop or mobile application.
  3. Plus, write some backend services with Go, then your Javascript code can access them like make an AJAX request
  4. Plus plus, you may also write some frontend services with Javascript for your Go code to access.

That's how GoUI came from. And now, it has some unique merits compares to other library.

  1. It provides two way bindings between Go and Javascript. Both sides can register services for the other side to access.

  2. Your application won't get too large because you don't need to include Chromium or any other browser into your package, since it uses Cocoa/WebKit for macOS, MSHTML for Windows and Gtk/WebKit for Linux.
    The macOS package "hello.app" of example "hello" only takes 2.6MB.

  3. It is extremely easy To Use. The api is super simple, and intentionally designed adapts to web developers. Register a service is like registering a REST service and request one is like making a jquery AJAX call.

  4. It is powerful, not because itself but because it brings two powerful tools together.

    • The UI You can use any frontend technologies to make your web page shinning. And conveniently, you can open the Web Inspector to help you.

    • The Logic With Javascript + Go, you can do anything.

Basic Usage

The backend - Go
  1. Install the package by go get
go get github.com/fipress/GoUI
  1. import the package
import (
   	"github.com/fipress/GoUI"
   )
  1. Register services with goui.Service, for Javascript to access
goui.Service("hello", func(context *goui.Context) {
	context.Success("Hello world!")
})
  1. Create window with goui.Create
goui.Create(goui.Settings{Title: "Hello",
		Left:      200,
		Top:       50,
		Width:     400,
		Height:    510,
		Resizable: true,
		Debug:     true})
The frontend - Javascript
  1. add goui.js in your web page

  2. request backend services

goui.request({url: "hello",
            success: function(data) {
                        document.getElementById("result").innerText = data;
             }});
The backend can access frontend services
  1. The frontend register services with goui.service

    goui.service("chat/:msg",function() {
         console.log(msg);
    })
    
  2. The backend invoke frontend service by goui.RequestJSService

goui.RequestJSService(goui.JSServiceOptions{
		Url: "chat/"+msg,
	})
Menu

It is extremely easy to create menu with GoUI: just define your menuDefs, and create window with them,

menuDefs = []goui.MenuDef{{Title: "File", Type: goui.Container, Children: []goui.MenuDef{
			{Title: "New", Type: goui.Custom, Action: "new", Handler: func() {
				println("new file")
			}},
			{Title: "Open", Type: goui.Custom, Action: "open"},
			{Type: goui.Separator},
			{Title: "Quit", Type: goui.Standard, Action: "quit"},
			...
			}
		}
		
goui.CreateWithMenu(settings,menuDefs)

For a complete demonstration, please check out the example "texteditor".

Debugging and packaging

Debugging

I personally recommend GoLand to debug Go code, or IntelliJ IDEA which contains GoLand. You may need to set the output directory to your working folder so that your debugging binary can read the web folder. Or other solution to make sure the binary can read the page you provided.

To debug javascript code or check web page elements, you should set Debug settings to true, then you can open the inspector from the context menu.

Inspector

Note You may use remote debugging of Safari or Chrome to debug iOS or Android web page.

Inspector

Packaging

The easiest way to package GoUI applications would be through GoUI-CLI, which will packaging native applications for all the supported platforms, macOS, Ubuntu, Windows, iOS and Android.

Examples

Under the example directory, there are some examples for you to get started with.

hello

This is the GoUI version of "Hello world". It's pretty much a starting point for you to go, so let's take a look at it in detail.

The project only contains 3 files.

  ├─ui
  | ├─css
  | |  └─index.css
  | ├─img
  | |  └─goui.png
  | ├─js
  | |  └─goui.js 
  | |  └─index.js
  | └─index.html
  └─main.go

goui.js - The frontend library GoUI provides for you.

main.go - provides a service named hello, and then create a window.

goui.Service("hello", func(context *goui.Context) {
		context.Success("Hello world!")
	})

	//create and open a window
	goui.Create(goui.Settings{Title: "Hello",
		Url:       "./ui/hello.html",
		Left:      20,
		Top:       30,
		Width:     300,
		Height:    200,
		Resizable: true,
		Debug:     true})

index.html - has a button on it, if a user clicks the button, it will request the above hello service. index.js - frontend logic of index.html.

goui.request({url: "hello",
              success: function(data) {
                  document.getElementById("result").innerText = data;
             }});

Here are some screenshots.

macOS hello-macOS

Ubuntu hello-ubuntu

iOS hello-ios

Android hello-android

chat

Demonstrate how backend requests frontend service.

texteditor

Demonstrate how to setup menu for desktop applications, on macOS, ubuntu and windows.

macOS editor-macOS

Ubuntu editor-ubuntu

Progress

Currently, the basic functions are working on macOS, Ubuntu, iOS and Android. We are working on Windows now, and it will be done soon. Then we will start adding features. If you have any suggestion, please don't hesitate to tell us.

Documentation

Overview

Package goui provides a cross platform GUI solution for Go developers.

It uses Cocoa/WebKit for macOS, MSHTML for Windows and Gtk/WebKit for Linux.

It provides two way bindings between Go and Javascript.

Index

Constants

This section is empty.

Variables

View Source
var Config = initConfig()
View Source
var Platform string

Platform is for packager to inject for different platforms

Functions

func ActivateWindow

func ActivateWindow()

func BoolToCInt

func BoolToCInt(b bool) (i C.int)

func Create

func Create(settings Settings) (err error)

Create is to create a native window with a webview

func CreateWithMenu

func CreateWithMenu(settings Settings, menuDefs []MenuDef) (err error)

func Log

func Log(args ...interface{})

func Logf

func Logf(format string, args ...interface{})

func OpenDevTools

func OpenDevTools()

InvokeJavascriptFunc is for the backend to invoke frontend javascript directly. params:

name - javascript function name
params - the parameters
func InvokeJavascriptFunc(name string, params ...interface{})  {
	js := fiputil.MkString(params,name + "(",",",")")
	worker.invokeJS(js)
}

func RegisterWidgets

func RegisterWidgets(widgets ...Widget)

func RequestJSService

func RequestJSService(options JSServiceOptions) (err error)

RequestJSService is to send a request to the front end from the main thread

func RequestJSServiceFromBackground

func RequestJSServiceFromBackground(options JSServiceOptions) (err error)

RequestJSServiceFromBackground is to send a request to the front end from a background thread

func Service

func Service(url string, handler func(*Context))

Service is to add a backend service for frontend to invoke. params:

url - the url act as an unique identifier of a service, for example, "user/login", "blog/get/:id".
handler - the function that handle the client request.

func Storage

func Storage() *storage

Types

type Context

type Context struct {
	Data            string `json:"data"`
	SuccessCallback string `json:"success"`
	ErrorCallback   string `json:"error"`
	// contains filtered or unexported fields
}

func (*Context) Error

func (ctx *Context) Error(err interface{})

func (*Context) Fail

func (ctx *Context) Fail()

func (*Context) GetBoolParam

func (ctx *Context) GetBoolParam(name string) (b bool, err error)

GetBoolParam get a bool parameter from the url

func (*Context) GetEntity

func (ctx *Context) GetEntity(v interface{}) (err error)

GetParam get an entity from the requested data

func (*Context) GetFloatParam

func (ctx *Context) GetFloatParam(name string) (f float64, err error)

GetFloatParam get a float parameter from the url

func (*Context) GetIntParam

func (ctx *Context) GetIntParam(name string) (i int, err error)

GetIntParam get a int parameter from the url

func (*Context) GetIntParamOr

func (ctx *Context) GetIntParamOr(name string, defaultVal int) (i int)

func (*Context) GetParam

func (ctx *Context) GetParam(name string) (val string)

GetParam get a string parameter from the url

func (*Context) Ok

func (ctx *Context) Ok()

func (*Context) Success

func (ctx *Context) Success(feedback interface{})

type JSServiceOptions

type JSServiceOptions struct {
	Url     string      `json:"url"`
	Data    interface{} `json:"data"`
	Success string      `json:"success"`
	Error   string      `json:"error"`
}
type MenuDef struct {
	Type     MenuType
	Title    string
	HotKey   string
	Action   string
	Handler  func()
	Children []MenuDef
}

MenuDef is to define a menu item

type MenuType int

MenuType is an enum of menu type

const (
	Container MenuType = iota //just a container item for sub items
	Custom
	Standard
	Separator
)

type Request

type Request struct {
	Url        string `json:"url"`
	IsCallback bool   `json:"isCallback"`
	Context    `json:",inline"`
}

type Settings

type Settings struct {
	Title     string //Title of the application window
	UIDir     string //Directory of the UI/Web related files, default: "ui"
	Index     string //Index html file, default: "index.html"
	Url       string //Full url address if you don't use WebDir + Index
	Left      int
	Top       int
	Width     int
	Height    int
	Resizable bool
	Debug     bool
}

Settings is to configure the window's appearance

type Widget

type Widget interface {
	Register()
}

Directories

Path Synopsis
example
widget

Jump to

Keyboard shortcuts

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