~eliasnaur/gio

8b2f6dbf13c798b6a5be643bb915092cf77622a5 — Elias Naur 1 year, 7 months ago 354976b
ui: add doc.go

Signed-off-by: Elias Naur <mail@eliasnaur.com>
4 files changed, 45 insertions(+), 15 deletions(-)

M README.md
A ui/doc.go
M ui/ui.go
M ui/unit.go
M README.md => README.md +1 -1
@@ 3,7 3,7 @@
Gio implements portable immediate mode GUI programs in Go. Gio programs run on all the major platforms:
iOS/tvOS, Android, Linux (Wayland), macOS and Windows.

[![GoDoc](https://godoc.org/gioui.org/ui/app?status.svg)](https://godoc.org/gioui.org/ui/app)
[![GoDoc](https://godoc.org/gioui.org/ui?status.svg)](https://godoc.org/gioui.org/ui)

## Quickstart


A ui/doc.go => ui/doc.go +9 -0
@@ 0,0 1,9 @@
/*

Package ui implements portable desktop and mobile GUI programs.

See https://gioui.org for instructions to setup dependencies and
run Gio programs.

*/
package ui

M ui/ui.go => ui/ui.go +27 -1
@@ 8,26 8,52 @@ import (
	"gioui.org/ui/f32"
)

// Config contain the context for updating and
// drawing a user interface.
type Config struct {
	// Device pixels per dp.
	PxPerDp float32
	// Device pixels per sp.
	PxPerSp float32
	Now     time.Time
	// The current time for animation.
	Now time.Time
}

// Pixels converts a value to unitless device pixels.
func (c *Config) Pixels(v Value) float32 {
	switch v.U {
	case UnitPx:
		return v.V
	case UnitDp:
		return c.PxPerDp * v.V
	case UnitSp:
		return c.PxPerSp * v.V
	default:
		panic("unknown unit")
	}
}

// Op is implemented by all known drawing and control
// operations.
type Op interface {
	ImplementsOp()
}

// OpLayer represents a semantic layer of UI.
type OpLayer struct {
	Op Op
}

// OpRedraw requests a redraw at the given time. Use
// the zero value to request an immediate redraw.
type OpRedraw struct {
	At time.Time
}

// Ops is the operation for a list of ops.
type Ops []Op

// OpTransform transforms an op.
type OpTransform struct {
	Transform Transform
	Op        Op

M ui/unit.go => ui/unit.go +8 -13
@@ 2,16 2,24 @@

package ui

// Value is a value with a unit.
type Value struct {
	V float32
	U Unit
}

// Unit represents a unit for a Value.
type Unit uint8

const (
	// UnitPx represent device pixels in the resolution of
	// the underlying display.
	UnitPx Unit = iota
	// UnitDp represents device independent pixels. 1 dp will
	// have the same apparent size across platforms and
	// display resolutions.
	UnitDp
	// UnitSp is like UnitDp but for font sizes.
	UnitSp
)



@@ 26,16 34,3 @@ func Dp(v float32) Value {
func Sp(v float32) Value {
	return Value{V: v, U: UnitSp}
}

func (c *Config) Pixels(v Value) float32 {
	switch v.U {
	case UnitPx:
		return v.V
	case UnitDp:
		return c.PxPerDp * v.V
	case UnitSp:
		return c.PxPerSp * v.V
	default:
		panic("unknown unit")
	}
}