Add Gioui scaffolding with Label and ListView rendering
- Element interface with Region() and Visible() methods - Label and ListView element types with constructors - Renderer that clips and draws elements in slice order - Main app loop with Gioui window and frame events Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package ui
|
||||
|
||||
import "gioui.org/unit"
|
||||
|
||||
// Region defines a screen area in device-independent pixels (Dp).
|
||||
type Region struct {
|
||||
X, Y unit.Dp
|
||||
W, H unit.Dp
|
||||
}
|
||||
|
||||
// Element is the base interface for all UI elements.
|
||||
type Element interface {
|
||||
Region() Region
|
||||
Visible() bool
|
||||
}
|
||||
|
||||
// TextAlign specifies horizontal text alignment.
|
||||
type TextAlign int
|
||||
|
||||
const (
|
||||
AlignStart TextAlign = iota
|
||||
AlignCenter
|
||||
AlignEnd
|
||||
)
|
||||
|
||||
// Label displays static text.
|
||||
type Label struct {
|
||||
id string
|
||||
region Region
|
||||
visible bool
|
||||
Text string
|
||||
Align TextAlign
|
||||
FontSize unit.Sp // 0 = theme default
|
||||
Color Color // 0 = theme default
|
||||
Bold bool
|
||||
}
|
||||
|
||||
func (l Label) Region() Region { return l.region }
|
||||
func (l Label) Visible() bool { return l.visible }
|
||||
func (l Label) ID() string { return l.id }
|
||||
|
||||
// TextField accepts text input or displays multiline text.
|
||||
type TextField struct {
|
||||
id string
|
||||
region Region
|
||||
visible bool
|
||||
Value string
|
||||
Placeholder string
|
||||
Focused bool
|
||||
Multiline bool
|
||||
ScrollOffset unit.Dp
|
||||
}
|
||||
|
||||
func (tf TextField) Region() Region { return tf.region }
|
||||
func (tf TextField) Visible() bool { return tf.visible }
|
||||
func (tf TextField) ID() string { return tf.id }
|
||||
|
||||
// ListView displays a scrollable list of items.
|
||||
type ListView struct {
|
||||
id string
|
||||
region Region
|
||||
visible bool
|
||||
Items []ListItem
|
||||
ScrollOffset int
|
||||
Selected int
|
||||
}
|
||||
|
||||
func (lv ListView) Region() Region { return lv.region }
|
||||
func (lv ListView) Visible() bool { return lv.visible }
|
||||
func (lv ListView) ID() string { return lv.id }
|
||||
|
||||
// ListItem is a single entry in a ListView.
|
||||
type ListItem struct {
|
||||
Text string
|
||||
Subtext string
|
||||
Selected bool
|
||||
}
|
||||
|
||||
// Color is an RGBA color.
|
||||
type Color struct {
|
||||
R, G, B, A uint8
|
||||
}
|
||||
|
||||
// Theme holds styling defaults for the UI.
|
||||
type Theme struct {
|
||||
FontSize unit.Sp
|
||||
}
|
||||
|
||||
// NewLabel creates a visible Label element.
|
||||
func NewLabel(text string, fontSize unit.Sp, region Region) Label {
|
||||
return Label{
|
||||
region: region,
|
||||
visible: true,
|
||||
Text: text,
|
||||
FontSize: fontSize,
|
||||
}
|
||||
}
|
||||
|
||||
// NewListView creates a visible ListView element.
|
||||
func NewListView(region Region, items []ListItem) ListView {
|
||||
return ListView{
|
||||
region: region,
|
||||
visible: true,
|
||||
Items: items,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/clip"
|
||||
"gioui.org/text"
|
||||
"gioui.org/widget/material"
|
||||
)
|
||||
|
||||
// Renderer consumes a slice of elements and draws them.
|
||||
type Renderer struct {
|
||||
theme Theme
|
||||
shp *text.Shaper
|
||||
}
|
||||
|
||||
// New creates a new Renderer.
|
||||
func New(th Theme, shp *text.Shaper) *Renderer {
|
||||
return &Renderer{theme: th, shp: shp}
|
||||
}
|
||||
|
||||
// Draw iterates elements and draws each in slice order (back-to-front).
|
||||
func (r *Renderer) Draw(gtx layout.Context, elems []Element) {
|
||||
for _, e := range elems {
|
||||
if !e.Visible() {
|
||||
continue
|
||||
}
|
||||
switch v := e.(type) {
|
||||
case Label:
|
||||
r.drawLabel(gtx, v)
|
||||
case ListView:
|
||||
r.drawListView(gtx, v)
|
||||
default:
|
||||
// unknown element type, skip
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Renderer) drawLabel(gtx layout.Context, l Label) {
|
||||
reg := l.Region()
|
||||
c := clip.Rect{
|
||||
Min: image.Point{X: int(reg.X), Y: int(reg.Y)},
|
||||
Max: image.Point{X: int(reg.X + reg.W), Y: int(reg.Y + reg.H)},
|
||||
}.Push(gtx.Ops)
|
||||
|
||||
th := material.NewTheme()
|
||||
th.Shaper = r.shp
|
||||
size := l.FontSize
|
||||
if size == 0 {
|
||||
size = r.theme.FontSize
|
||||
}
|
||||
material.Label(th, size, l.Text).Layout(gtx)
|
||||
c.Pop()
|
||||
}
|
||||
|
||||
func (r *Renderer) drawListView(gtx layout.Context, lv ListView) {
|
||||
reg := lv.Region()
|
||||
c := clip.Rect{
|
||||
Min: image.Point{X: int(reg.X), Y: int(reg.Y)},
|
||||
Max: image.Point{X: int(reg.X + reg.W), Y: int(reg.Y + reg.H)},
|
||||
}.Push(gtx.Ops)
|
||||
|
||||
th := material.NewTheme()
|
||||
th.Shaper = r.shp
|
||||
for _, item := range lv.Items {
|
||||
material.Label(th, r.theme.FontSize, item.Text).Layout(gtx)
|
||||
}
|
||||
c.Pop()
|
||||
}
|
||||
Reference in New Issue
Block a user