Giter VIP home page Giter VIP logo

gio-compose's Introduction

Gio Compose

Gio UI Component solution inspired by Jetpack Compose and React Hooks

Why?

Gio UI is cool cross-platform GUI in Golang. However, with some Component solution, as ui developer used to be with, we could do more things.

Workflow

sequenceDiagram
    participant user as User

    box GUI
        participant vnode as VNode
        participant widget_node as Widget Node
        participant renderer as GPU
    end

    activate vnode
    vnode ->> vnode: Diff & Patch
    vnode ->> widget_node: Commit Changes
    deactivate vnode
    activate widget_node
    widget_node ->> renderer: Layout & Paint
    deactivate widget_node
    user -->> widget_node: Pointer & Input
    widget_node -->> vnode: notify to handler
    activate vnode
    vnode ->> vnode: Update if some stage chanaged
    vnode ->> vnode: Diff & Patch
    vnode ->> widget_node: Commit Changes
    deactivate vnode
    activate widget_node
    widget_node ->> renderer: Layout & Paint
    deactivate widget_node

Example

Components

package main

import (
	"fmt"

	"github.com/octohelm/gio-compose/pkg/compose/gesture"
	"github.com/octohelm/gio-compose/pkg/compose/modifier"
	"golang.org/x/image/colornames"

	. "github.com/octohelm/gio-compose/pkg/compose"
)


type Counter struct {
}

func (Counter) Build(b BuildContext) VNode {
	state := UseState(b, 0)

	//showTooltip := UseState(b, false)

	v :=
		Row(
			modifier.FillMaxHeight(),
			modifier.FillMaxSize(),
			modifier.PaddingAll(20),
			modifier.Align(alignment.Center),
		).Children(
			H(CounterButton{
				OnTap: func() {
					state.UpdateFunc(func(prev int) int {
						return prev + 1
					})
				},
			}).Children(
				Text(fmt.Sprint(state.Value()), modifier.TextAlign(text.Middle)),
			),
		)
	return v
}

type CounterButton struct {
	OnTap func()
}

func (b CounterButton) Build(c BuildContext) VNode {
	return Row(
		modifier.DetectGesture(
			gesture.OnTap(b.OnTap),
		),
		modifier.Height(80),
		modifier.FillMaxWidth(),
		modifier.RoundedAll(10),
		modifier.BackgroundColor(color.White),
		modifier.Shadow(2),
		modifier.Align(alignment.Center),
	).Children(
		c.ChildVNodes()...,
	)
}

App Main

package main

import (
	"context"
	"os"
	"log"

	"gioui.org/unit"
	"gioui.org/app"

	. "github.com/octohelm/gio-compose/pkg/compose"
	"github.com/octohelm/gio-compose/pkg/compose/renderer"
)

func main() {
	w := app.NewWindow(
		app.MinSize(unit.Dp(1280), unit.Dp(768)),
	)

	r := renderer.CreateRoot(w)

	// could inject singletons in to context
	ctx := context.Background()

	r.Render(ctx, H(Counter{}))

	go func() {
		if err := r.Loop(); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()

	app.Main()
}

See more https://github.com/octohelm/gio-compose/tree/main/cmd/example

gio-compose's People

Contributors

morlay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.