feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
This commit is contained in:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
Vendored
+238
@@ -0,0 +1,238 @@
|
||||
package mobile
|
||||
|
||||
import "github.com/egoist/mygo/ui"
|
||||
|
||||
// TopBarOptions are what a TopBar shows.
|
||||
type TopBarOptions struct {
|
||||
Title string
|
||||
// Leading and Trailing build what goes at its ends, as IconButton's.
|
||||
Leading, Trailing func()
|
||||
// Large shows the title large, under the bar, as UINavigationBar's large
|
||||
// titles.
|
||||
Large bool
|
||||
// Background is its color, the theme's Background by default.
|
||||
Background *ui.Color
|
||||
}
|
||||
|
||||
// TopBar shows a bar across the top of a screen, drawn by the app rather than
|
||||
// the system's (Window.Push): with its title centered on Cupertino and from
|
||||
// the start on Material, for screens of one window that route within it
|
||||
// (ui.Router).
|
||||
func TopBar(c *ui.Context, o TopBarOptions) ui.Element {
|
||||
t := c.Theme()
|
||||
bg := t.Background
|
||||
if o.Background != nil {
|
||||
bg = *o.Background
|
||||
}
|
||||
height := float32(44)
|
||||
if Current() == Material {
|
||||
height = 56
|
||||
}
|
||||
bar := ui.Column(c).FillWidth().Background(bg).Shrink(0).Role(ui.RoleGroup)
|
||||
bar.Children(func() {
|
||||
ui.Row(c).Height(height).AlignItems(ui.Center).PaddingX(4).Gap(4).Children(func() {
|
||||
ui.Row(c).AlignItems(ui.Center).MinWidth(48).Children(func() {
|
||||
if o.Leading != nil {
|
||||
o.Leading()
|
||||
}
|
||||
})
|
||||
title := ui.Row(c).Grow(1).Basis(0).AlignItems(ui.Center)
|
||||
if Current() == Cupertino {
|
||||
title.Justify(ui.Center)
|
||||
}
|
||||
title.Children(func() {
|
||||
if !o.Large {
|
||||
size := float32(17)
|
||||
if Current() == Material {
|
||||
size = 22
|
||||
}
|
||||
ui.Text(c, o.Title).FontSize(size).FontWeight(600).MaxLines(1).Role(ui.RoleHeading).Level(1)
|
||||
}
|
||||
})
|
||||
ui.Row(c).AlignItems(ui.Center).Justify(ui.End).MinWidth(48).Children(func() {
|
||||
if o.Trailing != nil {
|
||||
o.Trailing()
|
||||
}
|
||||
})
|
||||
})
|
||||
if o.Large {
|
||||
ui.Text(c, o.Title).FontSize(34).Bold().PaddingX(16).Padding(0, 16, 8, 16).Role(ui.RoleHeading).Level(1)
|
||||
}
|
||||
})
|
||||
return bar
|
||||
}
|
||||
|
||||
// keyboardAtLeast tells a keyboard from the system's bars in the window's
|
||||
// bottom inset: they are far less tall.
|
||||
const keyboardAtLeast = 120
|
||||
|
||||
// BarButton is a destination of a BottomBar.
|
||||
type BarButton struct {
|
||||
Label string
|
||||
// Icon draws it, in color, in r: lines and shapes (ui.Painter).
|
||||
Icon func(p *ui.Painter, r ui.Rect, color ui.Color)
|
||||
// Badge shows a count or a word on it.
|
||||
Badge string
|
||||
}
|
||||
|
||||
// BottomBar shows a bar of destinations across the bottom of a screen, choosing
|
||||
// *selected: UITabBar's on Cupertino, Material's navigation bar on Material,
|
||||
// drawn by the app rather than the system's (mygo.NewTabs), keeping above the
|
||||
// system's bars. Changed reports a new choice.
|
||||
func BottomBar(c *ui.Context, selected *int, buttons ...BarButton) ui.Element {
|
||||
t := c.Theme()
|
||||
_, _, bottom, _ := c.SafeArea()
|
||||
// Down to the screen's edge, under the system's bar: the window's root
|
||||
// keeps its content above it.
|
||||
bar := ui.Row(c).FillWidth().Background(t.Surface).Padding(0, 0, bottom, 0).Margin(0, 0, -bottom, 0).Shrink(0).Role(ui.RoleTabList)
|
||||
if bottom > keyboardAtLeast {
|
||||
// The keyboard covers it, as it does the system's.
|
||||
return bar.Hidden(true)
|
||||
}
|
||||
height := float32(49)
|
||||
if Current() == Material {
|
||||
height = 80
|
||||
}
|
||||
bar.Children(func() {
|
||||
for i, b := range buttons {
|
||||
on := i == *selected
|
||||
color := t.TextMuted
|
||||
if on {
|
||||
color = t.Accent
|
||||
}
|
||||
item := ui.Column(c.Key(i)).Grow(1).Basis(0).Height(height).AlignItems(ui.Center).Justify(ui.Center).Gap(2).
|
||||
Role(ui.RoleTab).Label(b.Label).Checked(on)
|
||||
item.Children(func() {
|
||||
icon := ui.Box(c).Size(28, 24)
|
||||
if Current() == Material && on {
|
||||
icon.Size(56, 32).Radius(16).Background(t.Accent.Alpha(0.18))
|
||||
}
|
||||
if b.Icon != nil {
|
||||
icon.Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
s := float32(24)
|
||||
b.Icon(p, ui.Rect{X: r.X + (r.W-s)/2, Y: r.Y + (r.H-s)/2, W: s, H: s}, color)
|
||||
})
|
||||
}
|
||||
if b.Badge != "" {
|
||||
icon.Children(func() {
|
||||
ui.Text(c, b.Badge).Absolute().Top(-2).Right(-6).FontSize(10).PaddingX(4).Radius(8).
|
||||
Background(t.Danger).TextColor(ui.RGB(255, 255, 255))
|
||||
})
|
||||
}
|
||||
size := float32(10)
|
||||
if Current() == Material {
|
||||
size = 12
|
||||
}
|
||||
ui.Text(c, b.Label).FontSize(size).TextColor(color).NoWrap()
|
||||
})
|
||||
if item.Clicked() && !on {
|
||||
*selected = i
|
||||
bar.SetChanged()
|
||||
}
|
||||
}
|
||||
})
|
||||
return bar
|
||||
}
|
||||
|
||||
// DrawerOptions are how a Drawer shows.
|
||||
type DrawerOptions struct {
|
||||
// Width is how wide it is, 300 by default.
|
||||
Width float32
|
||||
// End puts it at the end of the screen, rather than the start.
|
||||
End bool
|
||||
}
|
||||
|
||||
type drawerState struct {
|
||||
offset ui.Point
|
||||
shown, opened bool
|
||||
}
|
||||
|
||||
// Drawer shows a panel sliding in from the side of the window while *open, over
|
||||
// what is under it, dimmed, as Material's navigation drawer: a finger drags it
|
||||
// away, a tap outside and back close it, which sets *open to false.
|
||||
func Drawer(c *ui.Context, open *bool, o DrawerOptions, content func()) {
|
||||
st := ui.Local(c.Root(), drawerKey{open}, func() drawerState { return drawerState{} })
|
||||
if !*open && !st.shown {
|
||||
return
|
||||
}
|
||||
width := o.Width
|
||||
if width <= 0 {
|
||||
width = 300
|
||||
}
|
||||
gone := -width
|
||||
if o.End {
|
||||
gone = width
|
||||
}
|
||||
born := !st.shown
|
||||
if born {
|
||||
st.shown, st.opened = true, false
|
||||
st.offset = ui.Point{X: gone}
|
||||
}
|
||||
t := c.Theme()
|
||||
top, _, bottom, _ := c.SafeArea()
|
||||
ui.Overlay(c, func() {
|
||||
back := ui.Box(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Modal()
|
||||
if back.Clicked() || back.OverlayShortcut(0, ui.KeyEscape) {
|
||||
*open = false
|
||||
}
|
||||
var panel ui.Element
|
||||
back.Children(func() {
|
||||
panel = ui.Column(c).Absolute().Top(0).Bottom(0).Width(width).Background(t.Background).Padding(top, 0, bottom, 0).
|
||||
Shadow(0, 0, 16, 0, ui.RGBA(0, 0, 0, 0.2)).Role(ui.RoleDialog)
|
||||
if o.End {
|
||||
panel.Right(0)
|
||||
} else {
|
||||
panel.Left(0)
|
||||
}
|
||||
bounds := ui.Rect{X: min(gone, 0), W: width}
|
||||
panel.Draggable(&st.offset, ui.DraggableOptions{Axis: ui.Horizontal, Bounds: &bounds, Rigid: true,
|
||||
Snap: ui.Nearest(ui.Point{}, ui.Point{X: gone})})
|
||||
panel.Children(content)
|
||||
})
|
||||
switch {
|
||||
case born:
|
||||
c.AnimationFrame()
|
||||
case !*open:
|
||||
st.offset = ui.Point{X: gone}
|
||||
st.opened = false
|
||||
case !st.opened:
|
||||
st.opened = true
|
||||
st.offset = ui.Point{}
|
||||
case st.offset.X == gone && !panel.Dragging():
|
||||
*open = false // dragged away
|
||||
st.opened = false
|
||||
}
|
||||
shown := panel.DragOffset().X
|
||||
frac := 1 - abs(shown)/width
|
||||
back.Background(scrim(c, frac))
|
||||
if !*open && !born && abs(shown) >= width-0.5 {
|
||||
st.shown = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type drawerKey struct{ open *bool }
|
||||
|
||||
func abs(v float32) float32 {
|
||||
if v < 0 {
|
||||
return -v
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// FloatingButton shows Material's floating action button, or a round button on
|
||||
// Cupertino: place it with Absolute.
|
||||
func FloatingButton(c *ui.Context, label string, icon func(p *ui.Painter, r ui.Rect, color ui.Color)) ui.Element {
|
||||
t := c.Theme()
|
||||
e := ui.Box(c).Size(56, 56).Radius(16).Background(t.Accent).Shadow(0, 3, 8, 0, ui.RGBA(0, 0, 0, 0.3)).Role(ui.RoleButton).Label(label)
|
||||
if Current() == Cupertino {
|
||||
e.Radius(28)
|
||||
}
|
||||
press(e, t)
|
||||
if icon != nil {
|
||||
e.Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
icon(p, ui.Rect{X: r.X + 16, Y: r.Y + 16, W: 24, H: 24}, t.AccentText)
|
||||
})
|
||||
}
|
||||
return e
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
// Switch shows a switch of the system's size and color: UISwitch's on
|
||||
// Cupertino, green when on, Material's on Material. Changed reports a change.
|
||||
func Switch(c *ui.Context, on *bool) ui.Element {
|
||||
t := c.Theme()
|
||||
w, h := float32(51), float32(31)
|
||||
if Current() == Material {
|
||||
w, h = 52, 32
|
||||
}
|
||||
sw := ui.SwitchBase(c, on).Size(w, h).Radius(h / 2)
|
||||
if sw.Changed() {
|
||||
mygo.Haptics.Light()
|
||||
}
|
||||
pos := sw.Animate("knob", onFloat(*on), 180*time.Millisecond)
|
||||
bg := t.Border
|
||||
onColor := t.Accent
|
||||
if Current() == Cupertino {
|
||||
onColor = ui.Hex("#34C759")
|
||||
}
|
||||
sw.Background(bg.Mix(onColor, pos))
|
||||
sw.Draw(func(p *ui.Painter, r ui.Rect) {
|
||||
in := float32(2)
|
||||
d := r.H - 2*in
|
||||
if Current() == Material {
|
||||
// Grows as it turns on.
|
||||
d = 16 + 8*pos
|
||||
in = (r.H - d) / 2
|
||||
}
|
||||
k := ui.Rect{X: r.X + in + pos*(r.W-d-2*in), Y: r.Y + in, W: d, H: d}
|
||||
p.Shadow(k, d/2, 0, 1, 3, 0, ui.RGBA(0, 0, 0, 0.25))
|
||||
p.Fill(k, ui.RGB(255, 255, 255), d/2)
|
||||
})
|
||||
return sw
|
||||
}
|
||||
|
||||
func onFloat(on bool) float32 {
|
||||
if on {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
Vendored
+75
@@ -0,0 +1,75 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
// inkGrow is how long a ripple takes to cover what was pressed, and inkFade
|
||||
// how long it takes to go after the press.
|
||||
const (
|
||||
inkGrow = 225 * time.Millisecond
|
||||
inkFade = 150 * time.Millisecond
|
||||
)
|
||||
|
||||
type inkState struct {
|
||||
x, y float32
|
||||
at, gone time.Time
|
||||
on bool
|
||||
}
|
||||
|
||||
// Ink draws Material's ripple over the element as it is pressed, spreading
|
||||
// from where the finger went down, and returns it. The element clips it to its
|
||||
// corners if it clips.
|
||||
func Ink(e ui.Element) ui.Element {
|
||||
s := ui.Local(e, inkKey{}, func() inkState { return inkState{} })
|
||||
pressed := e.Pressed()
|
||||
switch {
|
||||
case pressed && !s.on:
|
||||
s.on = true
|
||||
s.x, s.y, _ = e.PointerPosition()
|
||||
s.at, s.gone = time.Time{}, time.Time{}
|
||||
case !pressed && s.on:
|
||||
s.on = false
|
||||
s.gone = time.Time{}
|
||||
}
|
||||
color := ui.RGBA(128, 128, 128, 0.2)
|
||||
e.DrawOver(func(p *ui.Painter, r ui.Rect) {
|
||||
now := p.Now()
|
||||
if s.at.IsZero() {
|
||||
if !s.on && s.gone.IsZero() {
|
||||
return
|
||||
}
|
||||
s.at = now
|
||||
}
|
||||
if !s.on && s.gone.IsZero() {
|
||||
s.gone = now
|
||||
}
|
||||
alpha := float32(1)
|
||||
if !s.gone.IsZero() {
|
||||
alpha = 1 - float32(now.Sub(s.gone))/float32(inkFade)
|
||||
if alpha <= 0 {
|
||||
s.at = time.Time{}
|
||||
return
|
||||
}
|
||||
}
|
||||
grow := min(1, float32(now.Sub(s.at))/float32(inkGrow))
|
||||
grow = 1 - (1-grow)*(1-grow)
|
||||
far := float32(math.Hypot(float64(max(s.x, r.W-s.x)), float64(max(s.y, r.H-s.y))))
|
||||
radius := far * grow
|
||||
c := color
|
||||
c.A = uint8(float32(c.A) * alpha)
|
||||
// The element's box holds the circle.
|
||||
p.Clip(r, 0, func() {
|
||||
var path ui.Path
|
||||
path.Circle(r.X+s.x, r.Y+s.y, radius)
|
||||
p.FillPath(&path, c)
|
||||
})
|
||||
p.After(16 * time.Millisecond)
|
||||
})
|
||||
return e
|
||||
}
|
||||
|
||||
type inkKey struct{}
|
||||
Vendored
+71
@@ -0,0 +1,71 @@
|
||||
package mobile
|
||||
|
||||
import "github.com/egoist/mygo/ui"
|
||||
|
||||
// RowOptions are what a ListRow shows.
|
||||
type RowOptions struct {
|
||||
Title, Subtitle string
|
||||
// Leading and Trailing build what goes at its ends.
|
||||
Leading, Trailing func()
|
||||
// Disclosure shows that it goes somewhere: a chevron on Cupertino.
|
||||
Disclosure bool
|
||||
}
|
||||
|
||||
// ListRow shows a row of a list, of a height a finger can tap: UITableViewCell's
|
||||
// on Cupertino and Material's list item on Material, with its look of being
|
||||
// pressed. Clicked reports a tap.
|
||||
func ListRow(c *ui.Context, o RowOptions) ui.Element {
|
||||
t := c.Theme()
|
||||
minH := float32(44)
|
||||
if Current() == Material {
|
||||
minH = 56
|
||||
}
|
||||
label := o.Title
|
||||
if o.Subtitle != "" {
|
||||
label += ", " + o.Subtitle // screen readers read the row as one
|
||||
}
|
||||
row := ui.Row(c).FillWidth().MinHeight(minH).AlignItems(ui.Center).Padding(10, 16).Gap(12).Role(ui.RoleButton).Label(label)
|
||||
press(row, t)
|
||||
row.Children(func() {
|
||||
if o.Leading != nil {
|
||||
o.Leading()
|
||||
}
|
||||
ui.Column(c).Grow(1).Basis(0).Gap(2).Children(func() {
|
||||
ui.Text(c, o.Title)
|
||||
if o.Subtitle != "" {
|
||||
ui.Text(c, o.Subtitle).FontSize(14).TextColor(t.TextMuted)
|
||||
}
|
||||
})
|
||||
if o.Trailing != nil {
|
||||
o.Trailing()
|
||||
}
|
||||
if o.Disclosure && Current() == Cupertino {
|
||||
ui.Text(c, "›").FontSize(20).TextColor(t.TextMuted)
|
||||
}
|
||||
})
|
||||
return row
|
||||
}
|
||||
|
||||
// Section shows rows together under a header: UITableView's inset grouped style
|
||||
// on Cupertino, with a line between them; on Material, a heading over the list.
|
||||
func Section(c *ui.Context, header string, rows func()) ui.Element {
|
||||
t := c.Theme()
|
||||
col := ui.Column(c).FillWidth().Gap(6)
|
||||
if Current() == Cupertino {
|
||||
col.PaddingX(16)
|
||||
}
|
||||
col.Children(func() {
|
||||
if header != "" {
|
||||
h := ui.Text(c, header).FontSize(13).TextColor(t.TextMuted)
|
||||
if Current() == Material {
|
||||
h.PaddingX(16)
|
||||
}
|
||||
}
|
||||
body := ui.Column(c).FillWidth().Dividers(0.5, t.Border)
|
||||
if Current() == Cupertino {
|
||||
body.Background(t.Surface).Radius(10).Clip()
|
||||
}
|
||||
body.Children(rows)
|
||||
})
|
||||
return col
|
||||
}
|
||||
Vendored
+62
@@ -0,0 +1,62 @@
|
||||
// Package mobile holds what phones' interfaces are made of and desktops'
|
||||
// aren't: sheets that come up from the bottom edge, pages and rows that a
|
||||
// finger swipes, wheels that it spins, bars at the screen's edges, and the
|
||||
// press of Material's ink, each looking as on its system.
|
||||
//
|
||||
// It is built on package ui's public API, as any app could be, and apps that
|
||||
// don't import it don't pay for it. It works with a mouse too, and on
|
||||
// desktops, with the look of the system that Current chooses.
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
// Look is the design a widget follows.
|
||||
type Look uint8
|
||||
|
||||
const (
|
||||
// Automatic chooses by the system the app runs on.
|
||||
Automatic Look = iota
|
||||
// Cupertino follows Apple's Human Interface Guidelines.
|
||||
Cupertino
|
||||
// Material follows Google's Material Design.
|
||||
Material
|
||||
)
|
||||
|
||||
var look = Automatic
|
||||
|
||||
// SetLook makes the widgets follow a design whatever the system, as an app
|
||||
// with one design everywhere, or to see the other system's on this one.
|
||||
func SetLook(l Look) { look = l }
|
||||
|
||||
// Current returns the look widgets follow: Cupertino on Apple's systems
|
||||
// unless SetLook said otherwise, Material elsewhere.
|
||||
func Current() Look {
|
||||
if look != Automatic {
|
||||
return look
|
||||
}
|
||||
switch runtime.GOOS {
|
||||
case "ios", "darwin":
|
||||
return Cupertino
|
||||
}
|
||||
return Material
|
||||
}
|
||||
|
||||
// overlayRadius is the corner of what comes up from the bottom of the screen.
|
||||
func overlayRadius() float32 {
|
||||
if Current() == Cupertino {
|
||||
return 14
|
||||
}
|
||||
return 28
|
||||
}
|
||||
|
||||
// scrim is the color that dims what is under a sheet or a drawer.
|
||||
func scrim(c *ui.Context, fraction float32) ui.Color {
|
||||
return ui.RGBA(0, 0, 0, 0.4*max(0, min(fraction, 1)))
|
||||
}
|
||||
|
||||
// separator is the color of the lines between rows.
|
||||
func separator(t *ui.Theme) ui.Color { return t.Border }
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
func advance(tt *ui.Tester, d time.Duration) {
|
||||
for range int(d / (16 * time.Millisecond)) {
|
||||
tt.Advance(16 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
// A sheet comes up to its detent, a finger drags it among them, and down and
|
||||
// away closes it.
|
||||
func TestSheet(t *testing.T) {
|
||||
open := true
|
||||
var top float32
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
ui.Box(c).Fill()
|
||||
Sheet(c, &open, SheetOptions{Detents: []float32{0.5, 1}}, func() {
|
||||
top = ui.Box(c).Fill().Bounds().Y
|
||||
})
|
||||
}, 400, 800)
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
if top < 390 || top > 420 {
|
||||
t.Fatalf("opened: its content shows at %v", top)
|
||||
}
|
||||
// Up to the tall detent.
|
||||
tt.FingerDown(200, 430)
|
||||
advance(tt, 16*time.Millisecond)
|
||||
for y := float32(420); y >= 40; y -= 20 {
|
||||
tt.FingerMove(200, y)
|
||||
tt.Frame()
|
||||
advance(tt, 16*time.Millisecond)
|
||||
}
|
||||
advance(tt, 200*time.Millisecond)
|
||||
tt.FingerUp(200, 40)
|
||||
advance(tt, 2*time.Second)
|
||||
if top > 40 {
|
||||
t.Errorf("dragged up: its content shows at %v", top)
|
||||
}
|
||||
// Down and away.
|
||||
tt.FingerDown(200, 60)
|
||||
advance(tt, 16*time.Millisecond)
|
||||
for y := float32(80); y <= 790; y += 30 {
|
||||
tt.FingerMove(200, y)
|
||||
tt.Frame()
|
||||
advance(tt, 16*time.Millisecond)
|
||||
}
|
||||
advance(tt, 200*time.Millisecond)
|
||||
tt.FingerUp(200, 790)
|
||||
advance(tt, 2*time.Second)
|
||||
if open {
|
||||
t.Error("dragged away, it stays open")
|
||||
}
|
||||
}
|
||||
|
||||
// A tap outside a sheet closes it.
|
||||
func TestSheetDismiss(t *testing.T) {
|
||||
open := true
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
Sheet(c, &open, SheetOptions{}, func() {})
|
||||
}, 400, 800)
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
tt.FingerDown(200, 100)
|
||||
tt.FingerUp(200, 100)
|
||||
tt.Frame()
|
||||
if open {
|
||||
t.Error("a tap outside left it open")
|
||||
}
|
||||
}
|
||||
|
||||
// A tap on what a sheet holds is that of what it holds.
|
||||
func TestSheetContent(t *testing.T) {
|
||||
open, clicks := true, 0
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
Sheet(c, &open, SheetOptions{}, func() {
|
||||
if ui.Box(c).Fill().Clicked() {
|
||||
clicks++
|
||||
}
|
||||
})
|
||||
}, 400, 800)
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
tt.FingerDown(200, 600)
|
||||
tt.FingerUp(200, 600)
|
||||
tt.Frame()
|
||||
if clicks != 1 || !open {
|
||||
t.Errorf("%d clicks, open %v", clicks, open)
|
||||
}
|
||||
}
|
||||
|
||||
// A throw goes to the next page; a short move stays.
|
||||
func TestPageView(t *testing.T) {
|
||||
var s PageState
|
||||
built := map[int]bool{}
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
PageView(c, &s, 5, func(i int) { built[i] = true; ui.Box(c).Fill() }).Fill()
|
||||
}, 400, 400)
|
||||
tt.Frame()
|
||||
tt.Frame()
|
||||
if built[3] {
|
||||
t.Errorf("built far pages: %v", built)
|
||||
}
|
||||
swipe := func(from, to float32) {
|
||||
tt.FingerDown(from, 200)
|
||||
advance(tt, 16*time.Millisecond)
|
||||
n := 6
|
||||
for i := 1; i <= n; i++ {
|
||||
tt.FingerMove(from+(to-from)*float32(i)/float32(n), 200)
|
||||
tt.Frame()
|
||||
tt.Advance(16 * time.Millisecond)
|
||||
}
|
||||
tt.FingerUp(to, 200)
|
||||
advance(tt, 2*time.Second)
|
||||
}
|
||||
swipe(350, 100)
|
||||
if s.Page != 1 {
|
||||
t.Fatalf("a swipe left: page %d", s.Page)
|
||||
}
|
||||
swipe(100, 350)
|
||||
if s.Page != 0 {
|
||||
t.Errorf("a swipe right: page %d", s.Page)
|
||||
}
|
||||
s.Page = 3
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
if s.Page != 3 {
|
||||
t.Errorf("set to 3: page %d", s.Page)
|
||||
}
|
||||
}
|
||||
|
||||
// A wheel lands on an item.
|
||||
func TestWheel(t *testing.T) {
|
||||
v, changes := 0, 0
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
if Wheel(c, &v, []string{"a", "b", "c", "d", "e", "f"}).Changed() {
|
||||
changes++
|
||||
}
|
||||
}, 200, 180)
|
||||
tt.Frame()
|
||||
tt.Frame()
|
||||
tt.FingerDown(100, 90)
|
||||
advance(tt, 16*time.Millisecond)
|
||||
for y := float32(80); y >= 10; y -= 10 {
|
||||
tt.FingerMove(100, y)
|
||||
tt.Frame()
|
||||
advance(tt, 16*time.Millisecond)
|
||||
}
|
||||
advance(tt, 200*time.Millisecond)
|
||||
tt.FingerUp(100, 10)
|
||||
advance(tt, 2*time.Second)
|
||||
if v != 2 || changes != 1 {
|
||||
t.Errorf("spun 80: item %d, %d changes", v, changes)
|
||||
}
|
||||
}
|
||||
|
||||
// Swiped, a row shows its actions, whose tap runs and closes it.
|
||||
func TestSwipeRow(t *testing.T) {
|
||||
var s SwipeState
|
||||
deleted := false
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
SwipeRow(c, &s, SwipeOptions{Trailing: []SwipeAction{{Label: "Delete", Destructive: true, OnTap: func() { deleted = true }}}},
|
||||
func() { ui.Box(c).Height(60).Shrink(0).FillWidth() })
|
||||
}, 300, 200)
|
||||
tt.Frame()
|
||||
tt.FingerDown(280, 30)
|
||||
advance(tt, 16*time.Millisecond)
|
||||
for x := float32(270); x >= 170; x -= 20 {
|
||||
tt.FingerMove(x, 30)
|
||||
tt.Frame()
|
||||
tt.Advance(16 * time.Millisecond)
|
||||
}
|
||||
advance(tt, 200*time.Millisecond)
|
||||
tt.FingerUp(170, 30)
|
||||
advance(tt, 2*time.Second)
|
||||
if !s.open {
|
||||
t.Fatalf("swiped: offset %+v", s.offset)
|
||||
}
|
||||
tt.FingerDown(260, 30)
|
||||
tt.FingerUp(260, 30)
|
||||
tt.Frame()
|
||||
if !deleted {
|
||||
t.Error("tapping the action did nothing")
|
||||
}
|
||||
}
|
||||
|
||||
// A sheet stays at its detent as the window turns, whose height is another.
|
||||
func TestSheetTurns(t *testing.T) {
|
||||
open := true
|
||||
var top float32
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
Sheet(c, &open, SheetOptions{Detents: []float32{0.5, 1}}, func() {
|
||||
top = ui.Box(c).Fill().Bounds().Y
|
||||
})
|
||||
}, 400, 800)
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
tt.SetSize(800, 300)
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
if !open {
|
||||
t.Fatal("turned: it closed")
|
||||
}
|
||||
if top < 130 || top > 170 {
|
||||
t.Errorf("turned: its content shows at %v of 300", top)
|
||||
}
|
||||
}
|
||||
|
||||
// Opened again as it closes, it opens.
|
||||
func TestSheetReopens(t *testing.T) {
|
||||
open := true
|
||||
var top float32
|
||||
tt := ui.NewTester(func(c *ui.Context) {
|
||||
Sheet(c, &open, SheetOptions{Detents: []float32{0.5}}, func() {
|
||||
top = ui.Box(c).Fill().Bounds().Y
|
||||
})
|
||||
}, 400, 800)
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
open = false
|
||||
tt.Frame()
|
||||
advance(tt, 50*time.Millisecond)
|
||||
open = true
|
||||
tt.Frame()
|
||||
advance(tt, 2*time.Second)
|
||||
if !open || top > 420 {
|
||||
t.Errorf("reopened: open %v, its content shows at %v", open, top)
|
||||
}
|
||||
}
|
||||
Vendored
+90
@@ -0,0 +1,90 @@
|
||||
package mobile
|
||||
|
||||
import "github.com/egoist/mygo/ui"
|
||||
|
||||
// PageState is what page of a PageView shows, kept in the app's state.
|
||||
type PageState struct {
|
||||
// Page is the page it rests at, or goes to. Set it to go there.
|
||||
Page int
|
||||
|
||||
offset ui.Point
|
||||
width float32
|
||||
last int
|
||||
}
|
||||
|
||||
// PageView shows one of count pages at a time, which a finger swipes between,
|
||||
// as UIPageViewController and ViewPager do, the page it lands on following the
|
||||
// throw. Only the pages next to what shows are built. It fills its parent:
|
||||
// size it with Height, Grow or Fill.
|
||||
//
|
||||
// mobile.PageView(c, &app.pages, 3, func(i int) { pages[i](c) }).Fill()
|
||||
func PageView(c *ui.Context, s *PageState, count int, page func(i int)) ui.Element {
|
||||
box := ui.Box(c).Clip().Role(ui.RoleGroup)
|
||||
width := box.Bounds().W
|
||||
if width <= 0 {
|
||||
// Laid out for the first time: its width shows next frame.
|
||||
c.AnimationFrame()
|
||||
return box
|
||||
}
|
||||
if count <= 0 {
|
||||
return box
|
||||
}
|
||||
s.Page = max(0, min(s.Page, count-1))
|
||||
box.Children(func() {
|
||||
points := make([]ui.Point, count)
|
||||
for i := range points {
|
||||
points[i] = ui.Point{X: -float32(i) * width}
|
||||
}
|
||||
track := ui.Row(c).Absolute().Left(0).Top(0).Bottom(0).Width(width * float32(count))
|
||||
if s.width != width {
|
||||
// A new width, as the device turned: the page stays.
|
||||
s.width = width
|
||||
s.offset = points[s.Page]
|
||||
}
|
||||
if s.last != s.Page {
|
||||
s.last = s.Page
|
||||
s.offset = points[s.Page]
|
||||
}
|
||||
track.Draggable(&s.offset, ui.DraggableOptions{
|
||||
Axis: ui.Horizontal,
|
||||
Bounds: &ui.Rect{X: -float32(count-1) * width, W: float32(count-1) * width},
|
||||
// One page a throw, as ViewPager's: from the page it left.
|
||||
Snap: func(p ui.Point) ui.Point {
|
||||
to := int(-p.X/width + 0.5)
|
||||
return points[max(0, min(to, min(s.Page+1, count-1)), max(0, s.Page-1))]
|
||||
},
|
||||
})
|
||||
shown := track.DragOffset().X
|
||||
at := int(-shown/width + 0.5)
|
||||
lo, hi := max(0, at-1), min(count-1, at+1)
|
||||
track.Children(func() {
|
||||
for i := range count {
|
||||
cell := ui.Box(c.Key(i)).Width(width).Shrink(0).FillHeight()
|
||||
if i >= lo && i <= hi {
|
||||
cell.Children(func() { page(i) })
|
||||
}
|
||||
}
|
||||
})
|
||||
if !track.Transitioning() && !track.Dragging() {
|
||||
if p := int(-s.offset.X/width + 0.5); p != s.Page && p >= 0 && p < count {
|
||||
s.Page, s.last = p, p
|
||||
c.AnimationFrame()
|
||||
}
|
||||
}
|
||||
})
|
||||
return box
|
||||
}
|
||||
|
||||
// PageDots shows which of count pages is chosen, as UIPageControl does.
|
||||
func PageDots(c *ui.Context, page, count int) ui.Element {
|
||||
t := c.Theme()
|
||||
return ui.Row(c).Gap(8).Justify(ui.Center).AlignItems(ui.Center).Padding(8).Children(func() {
|
||||
for i := range count {
|
||||
color := t.TextMuted.Alpha(0.35)
|
||||
if i == page {
|
||||
color = t.Text
|
||||
}
|
||||
ui.Box(c.Key(i)).Size(7, 7).Radius(3.5).Background(color)
|
||||
}
|
||||
})
|
||||
}
|
||||
Vendored
+252
@@ -0,0 +1,252 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
// SheetOptions are how a Sheet rests.
|
||||
type SheetOptions struct {
|
||||
// Detents are the heights it rests at, as fractions of the window's, in
|
||||
// any order: UISheetPresentationController's medium and large are
|
||||
// {0.5, 0.92}. It opens at the first, and one {0.5} when none.
|
||||
Detents []float32
|
||||
// NoGrabber hides the grabber at its top.
|
||||
NoGrabber bool
|
||||
// NoDismiss keeps it from going away by a drag, a tap outside or back.
|
||||
NoDismiss bool
|
||||
// Background is its color, the theme's Background by default.
|
||||
Background *ui.Color
|
||||
// Label names it for assistive technology.
|
||||
Label string
|
||||
}
|
||||
|
||||
type sheetState struct {
|
||||
offset ui.Point
|
||||
// shown says it is on screen, opened that it went up from below it.
|
||||
shown, opened bool
|
||||
// height is what it was laid out for, which detent, the one it rests at,
|
||||
// takes it back to as the window changes.
|
||||
height float32
|
||||
detent int
|
||||
}
|
||||
|
||||
// Sheet shows a panel coming up from the bottom of the window while *open,
|
||||
// over what is under it, dimmed: a finger drags it among its detents, and
|
||||
// down and away, which sets *open to false, as do a tap outside it and back
|
||||
// (Android's, and Escape). What it holds that scrolls scrolls first.
|
||||
//
|
||||
// mobile.Sheet(c, &app.filters, mobile.SheetOptions{Detents: []float32{0.5, 0.92}}, func() {
|
||||
// ui.Text(c, "Filters")
|
||||
// })
|
||||
func Sheet(c *ui.Context, open *bool, o SheetOptions, content func()) {
|
||||
s := ui.Local(c.Root(), sheetKey{open}, func() sheetState { return sheetState{} })
|
||||
if !*open && !s.shown {
|
||||
return
|
||||
}
|
||||
_, winH := c.Size()
|
||||
detents := o.Detents
|
||||
if len(detents) == 0 {
|
||||
detents = []float32{0.5}
|
||||
}
|
||||
tall := float32(0)
|
||||
for _, d := range detents {
|
||||
tall = max(tall, d)
|
||||
}
|
||||
height := winH * tall
|
||||
// The offset's 0 is the tallest detent; closed it is below the window.
|
||||
rest := func(d float32) ui.Point { return ui.Point{Y: height - winH*d} }
|
||||
closedY := height
|
||||
born := !s.shown
|
||||
if born {
|
||||
s.shown, s.opened = true, false
|
||||
s.offset = ui.Point{Y: closedY}
|
||||
}
|
||||
if s.opened && s.height != height && s.height > 0 {
|
||||
// The window turned: the same detent, in its new height.
|
||||
s.offset = rest(detents[min(s.detent, len(detents)-1)])
|
||||
}
|
||||
s.height = height
|
||||
points := make([]ui.Point, 0, len(detents)+1)
|
||||
for i, d := range detents {
|
||||
points = append(points, rest(d))
|
||||
if s.opened && s.offset == points[i] {
|
||||
s.detent = i
|
||||
}
|
||||
}
|
||||
if !o.NoDismiss {
|
||||
points = append(points, ui.Point{Y: closedY})
|
||||
}
|
||||
t := c.Theme()
|
||||
_, _, bottom, _ := c.SafeArea()
|
||||
ui.Overlay(c, func() {
|
||||
back := ui.Box(c).Absolute().Left(0).Top(0).Right(0).Bottom(0).Modal()
|
||||
if !o.NoDismiss && (back.Clicked() || back.OverlayShortcut(0, ui.KeyEscape)) {
|
||||
*open = false
|
||||
}
|
||||
bg := t.Background
|
||||
if o.Background != nil {
|
||||
bg = *o.Background
|
||||
}
|
||||
var panel ui.Element
|
||||
back.Children(func() {
|
||||
panel = ui.Column(c).Absolute().Left(0).Right(0).Bottom(0).Height(height).
|
||||
Background(bg).Radius(overlayRadius(), overlayRadius(), 0, 0).
|
||||
Shadow(0, -2, 16, 0, ui.RGBA(0, 0, 0, 0.18)).Role(ui.RoleDialog).Label(o.Label)
|
||||
// Clicks in it stay in it.
|
||||
panel.Clicked()
|
||||
panel.Draggable(&s.offset, ui.DraggableOptions{
|
||||
Axis: ui.Vertical,
|
||||
Bounds: &ui.Rect{H: closedY},
|
||||
Snap: ui.Nearest(points...),
|
||||
})
|
||||
panel.Children(func() {
|
||||
if !o.NoGrabber {
|
||||
ui.Box(c).Width(36).Height(5).Radius(2.5).Background(t.TextMuted.Alpha(0.35)).AlignSelf(ui.Center).Margin(8, 0, 4, 0)
|
||||
}
|
||||
ui.Column(c).Grow(1).Basis(0).Padding(0, 0, bottom, 0).Children(content)
|
||||
})
|
||||
})
|
||||
switch {
|
||||
case born:
|
||||
c.AnimationFrame() // shows below the window, to go up from there
|
||||
case !*open:
|
||||
s.offset = ui.Point{Y: closedY}
|
||||
s.opened = false
|
||||
case !s.opened:
|
||||
s.opened, s.detent = true, 0
|
||||
s.offset = rest(detents[0])
|
||||
case !o.NoDismiss && s.offset.Y >= closedY && !panel.Dragging():
|
||||
*open = false // dragged away
|
||||
s.opened = false
|
||||
}
|
||||
shown := panel.DragOffset()
|
||||
back.Background(scrim(c, 1-shown.Y/max(closedY, 1)))
|
||||
if !*open && !born && shown.Y >= closedY-0.5 {
|
||||
s.shown = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type sheetKey struct{ open *bool }
|
||||
|
||||
// Action is a choice of an ActionSheet.
|
||||
type Action struct {
|
||||
Label string
|
||||
// Destructive shows that it destroys something, in the theme's Danger.
|
||||
Destructive bool
|
||||
// Disabled grays it, and it does nothing.
|
||||
Disabled bool
|
||||
// OnTap runs as it is chosen, on the main thread; the sheet then closes.
|
||||
OnTap func()
|
||||
}
|
||||
|
||||
// ActionSheetOptions are what an ActionSheet says.
|
||||
type ActionSheetOptions struct {
|
||||
Title, Message string
|
||||
// Cancel is what its button of going back says, "Cancel" by default.
|
||||
Cancel string
|
||||
}
|
||||
|
||||
// ActionSheet shows a list of choices coming up from the bottom of the window
|
||||
// while *open: iOS's action sheet on Cupertino, a bottom sheet's list on
|
||||
// Material. Choosing one, or going back, sets *open to false.
|
||||
func ActionSheet(c *ui.Context, open *bool, o ActionSheetOptions, actions ...Action) {
|
||||
if o.Cancel == "" {
|
||||
o.Cancel = "Cancel"
|
||||
}
|
||||
t := c.Theme()
|
||||
_, winH := c.Size()
|
||||
if Current() == Cupertino {
|
||||
bg := ui.RGBA(0, 0, 0, 0)
|
||||
rowH := float32(57)
|
||||
_, _, bottom, _ := c.SafeArea()
|
||||
need := rowH*float32(len(actions)+1) + 40 + bottom
|
||||
if o.Title != "" || o.Message != "" {
|
||||
need += 72
|
||||
}
|
||||
Sheet(c, open, SheetOptions{Detents: []float32{min(need/max(winH, 1), 0.9)}, NoGrabber: true, Background: &bg, Label: o.Title}, func() {
|
||||
ui.Column(c).Padding(0, 8, 8, 8).Gap(8).Grow(1).Justify(ui.End).Children(func() {
|
||||
ui.Column(c).Background(t.Surface).Radius(14).Clip().Children(func() {
|
||||
if o.Title != "" || o.Message != "" {
|
||||
ui.Column(c).Padding(14, 16).AlignItems(ui.Center).Gap(2).Children(func() {
|
||||
if o.Title != "" {
|
||||
ui.Text(c, o.Title).FontSize(13).Bold().TextColor(t.TextMuted)
|
||||
}
|
||||
if o.Message != "" {
|
||||
ui.Text(c, o.Message).FontSize(13).TextColor(t.TextMuted).TextAlign(ui.Center)
|
||||
}
|
||||
})
|
||||
ui.Divider(c)
|
||||
}
|
||||
for i, a := range actions {
|
||||
if i > 0 {
|
||||
ui.Divider(c)
|
||||
}
|
||||
actionRow(c, i, a, open, rowH, true)
|
||||
}
|
||||
})
|
||||
cancel := ui.Row(c).Height(rowH).AlignItems(ui.Center).Justify(ui.Center).Background(t.Surface).Radius(14).Role(ui.RoleButton).Label(o.Cancel)
|
||||
press(cancel, t)
|
||||
cancel.Children(func() { ui.Text(c, o.Cancel).Bold().TextColor(t.Accent).FontSize(20) })
|
||||
if cancel.Clicked() {
|
||||
*open = false
|
||||
}
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
rowH := float32(52)
|
||||
_, _, bottom, _ := c.SafeArea()
|
||||
need := rowH*float32(len(actions)) + 48 + bottom
|
||||
if o.Title != "" {
|
||||
need += 48
|
||||
}
|
||||
Sheet(c, open, SheetOptions{Detents: []float32{min(need/max(winH, 1), 0.9)}, Label: o.Title}, func() {
|
||||
ui.Column(c).Grow(1).Children(func() {
|
||||
if o.Title != "" {
|
||||
ui.Text(c, o.Title).TextColor(t.TextMuted).Padding(12, 24)
|
||||
}
|
||||
for i, a := range actions {
|
||||
actionRow(c, i, a, open, rowH, false)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func actionRow(c *ui.Context, i int, a Action, open *bool, height float32, centered bool) {
|
||||
t := c.Theme()
|
||||
color := t.Accent
|
||||
if !centered {
|
||||
color = t.Text
|
||||
}
|
||||
if a.Destructive {
|
||||
color = t.Danger
|
||||
}
|
||||
row := ui.Row(c.Key(i)).Height(height).AlignItems(ui.Center).PaddingX(24).Role(ui.RoleButton).Label(a.Label).Disabled(a.Disabled)
|
||||
if centered {
|
||||
row.Justify(ui.Center)
|
||||
}
|
||||
press(row, t)
|
||||
size := float32(16)
|
||||
if centered {
|
||||
size = 20
|
||||
}
|
||||
row.Children(func() { ui.Text(c, a.Label).TextColor(color).FontSize(size) })
|
||||
if row.Clicked() {
|
||||
*open = false
|
||||
if a.OnTap != nil {
|
||||
a.OnTap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// press shows the look of a press: Material's ink, or Cupertino's highlight.
|
||||
func press(e ui.Element, t *ui.Theme) {
|
||||
if Current() == Material {
|
||||
Ink(e)
|
||||
return
|
||||
}
|
||||
if e.Pressed() {
|
||||
e.Background(t.SurfacePressed)
|
||||
}
|
||||
}
|
||||
Vendored
+107
@@ -0,0 +1,107 @@
|
||||
package mobile
|
||||
|
||||
import "github.com/egoist/mygo/ui"
|
||||
|
||||
// SwipeAction is a button a SwipeRow reveals.
|
||||
type SwipeAction struct {
|
||||
Label string
|
||||
// Color is its background, the theme's Danger for one that is
|
||||
// Destructive, or else its Accent.
|
||||
Color *ui.Color
|
||||
Destructive bool
|
||||
OnTap func()
|
||||
}
|
||||
|
||||
// SwipeState is how a SwipeRow rests, kept by the app per row where rows may
|
||||
// go away; a nil state keeps it in the element.
|
||||
type SwipeState struct {
|
||||
offset ui.Point
|
||||
open bool
|
||||
}
|
||||
|
||||
// Close puts the row back, hiding its actions.
|
||||
func (s *SwipeState) Close() { s.offset, s.open = ui.Point{}, false }
|
||||
|
||||
const swipeActionWidth = 80
|
||||
|
||||
// SwipeRow shows content that a finger swipes aside to reveal actions behind
|
||||
// it, as UITableView's swipe actions: Trailing as it goes left, Leading as it
|
||||
// goes right. A long swipe chooses the first action of that side. A finger
|
||||
// going down the list scrolls the list instead.
|
||||
//
|
||||
// mobile.SwipeRow(c.Key(i), nil, mobile.SwipeOptions{
|
||||
// Trailing: []mobile.SwipeAction{{Label: "Delete", Destructive: true, OnTap: func() { app.remove(i) }}},
|
||||
// }, func() { ui.Text(c, row.Title).Padding(14) })
|
||||
func SwipeRow(c *ui.Context, s *SwipeState, o SwipeOptions, content func()) ui.Element {
|
||||
t := c.Theme()
|
||||
// One cell, which holds the actions and the content over them.
|
||||
box := ui.Grid(c).Columns(1).Clip().FillWidth()
|
||||
if s == nil {
|
||||
s = ui.Local(box, swipeKey{}, func() SwipeState { return SwipeState{} })
|
||||
}
|
||||
box.Children(func() {
|
||||
trailing := float32(len(o.Trailing)) * swipeActionWidth
|
||||
leading := float32(len(o.Leading)) * swipeActionWidth
|
||||
// Behind the content.
|
||||
actions := func(list []SwipeAction, end bool) {
|
||||
if len(list) == 0 {
|
||||
return
|
||||
}
|
||||
row := ui.Row(c).ColumnStart(1).RowStart(1).AlignSelf(ui.Stretch).AlignItems(ui.Stretch)
|
||||
if end {
|
||||
row.JustifySelf(ui.End)
|
||||
} else {
|
||||
row.JustifySelf(ui.Start)
|
||||
}
|
||||
row.Children(func() {
|
||||
for i, a := range list {
|
||||
bg := t.Accent
|
||||
if a.Destructive {
|
||||
bg = t.Danger
|
||||
}
|
||||
if a.Color != nil {
|
||||
bg = *a.Color
|
||||
}
|
||||
b := ui.Row(c.Key(i)).Width(swipeActionWidth).AlignItems(ui.Center).Justify(ui.Center).
|
||||
Background(bg).Role(ui.RoleButton).Label(a.Label).Children(func() {
|
||||
ui.Text(c, a.Label).TextColor(ui.RGB(255, 255, 255)).FontSize(14).TextAlign(ui.Center)
|
||||
})
|
||||
if b.Clicked() {
|
||||
s.Close()
|
||||
if a.OnTap != nil {
|
||||
a.OnTap()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
actions(o.Leading, false)
|
||||
actions(o.Trailing, true)
|
||||
// The content, over them.
|
||||
var points []ui.Point
|
||||
points = append(points, ui.Point{})
|
||||
if trailing > 0 {
|
||||
points = append(points, ui.Point{X: -trailing})
|
||||
}
|
||||
if leading > 0 {
|
||||
points = append(points, ui.Point{X: leading})
|
||||
}
|
||||
face := ui.Box(c).ColumnStart(1).RowStart(1).Background(t.Background).Draggable(&s.offset, ui.DraggableOptions{
|
||||
Axis: ui.Horizontal,
|
||||
Bounds: &ui.Rect{X: -trailing, W: trailing + leading},
|
||||
Snap: ui.Nearest(points...),
|
||||
})
|
||||
face.Children(content)
|
||||
if !face.Transitioning() && !face.Dragging() {
|
||||
s.open = s.offset.X != 0
|
||||
}
|
||||
})
|
||||
return box
|
||||
}
|
||||
|
||||
type swipeKey struct{}
|
||||
|
||||
// SwipeOptions are the actions a SwipeRow reveals.
|
||||
type SwipeOptions struct {
|
||||
Leading, Trailing []SwipeAction
|
||||
}
|
||||
Vendored
+167
@@ -0,0 +1,167 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
const wheelRow = 36
|
||||
|
||||
// WheelState is how a Wheel shows, kept in the element.
|
||||
type wheelState struct {
|
||||
offset ui.Point
|
||||
// value is the value it shows, and ticked the item it last ticked for.
|
||||
value, ticked int
|
||||
once bool
|
||||
}
|
||||
|
||||
// Wheel shows a wheel of items a finger spins to choose one, as UIPickerView,
|
||||
// choosing *value's item, with a tick as the choice changes. Changed reports a
|
||||
// new choice.
|
||||
//
|
||||
// mobile.Wheel(c, &app.size, []string{"S", "M", "L", "XL"})
|
||||
func Wheel(c *ui.Context, value *int, items []string) ui.Element {
|
||||
t := c.Theme()
|
||||
box := ui.Box(c).Height(wheelRow * 5).Clip().Role(ui.RoleSlider)
|
||||
s := ui.Local(box, wheelKey{}, func() wheelState { return wheelState{} })
|
||||
n := len(items)
|
||||
if n == 0 {
|
||||
return box
|
||||
}
|
||||
*value = max(0, min(*value, n-1))
|
||||
at := func(i int) ui.Point { return ui.Point{Y: -float32(i) * wheelRow} }
|
||||
if !s.once || s.value != *value {
|
||||
s.once, s.value, s.ticked = true, *value, *value
|
||||
s.offset = at(*value)
|
||||
}
|
||||
box.Children(func() {
|
||||
// The band of the choice.
|
||||
ui.Box(c).Absolute().Left(4).Right(4).Top(wheelRow * 2).Height(wheelRow).Radius(8).Background(t.Surface)
|
||||
points := make([]ui.Point, n)
|
||||
for i := range points {
|
||||
points[i] = at(i)
|
||||
}
|
||||
track := ui.Column(c).Absolute().Left(0).Right(0).Top(wheelRow * 2).Height(float32(n) * wheelRow)
|
||||
track.Draggable(&s.offset, ui.DraggableOptions{
|
||||
Axis: ui.Vertical,
|
||||
Bounds: &ui.Rect{Y: -float32(n-1) * wheelRow, H: float32(n-1) * wheelRow},
|
||||
Snap: ui.Nearest(points...),
|
||||
})
|
||||
shown := track.DragOffset().Y
|
||||
cur := max(0, min(int(-shown/wheelRow+0.5), n-1))
|
||||
track.Children(func() {
|
||||
first, last := max(0, cur-4), min(n-1, cur+4)
|
||||
if first > 0 {
|
||||
ui.Box(c).Height(float32(first) * wheelRow).Shrink(0)
|
||||
}
|
||||
for i := first; i <= last; i++ {
|
||||
d := -shown/wheelRow - float32(i)
|
||||
if d < 0 {
|
||||
d = -d
|
||||
}
|
||||
alpha := max(0.2, 1-d*0.35)
|
||||
ui.Row(c.Key(i)).Height(wheelRow).Shrink(0).AlignItems(ui.Center).Justify(ui.Center).Children(func() {
|
||||
ui.Text(c, items[i]).TextColor(t.Text.Alpha(alpha)).NoWrap()
|
||||
})
|
||||
}
|
||||
})
|
||||
if cur != s.ticked {
|
||||
s.ticked = cur
|
||||
mygo.Haptics.Selection()
|
||||
}
|
||||
if !track.Transitioning() && !track.Dragging() {
|
||||
if p := max(0, min(int(-s.offset.Y/wheelRow+0.5), n-1)); p != *value {
|
||||
*value, s.value = p, p
|
||||
box.SetChanged()
|
||||
}
|
||||
}
|
||||
})
|
||||
return box
|
||||
}
|
||||
|
||||
type wheelKey struct{}
|
||||
|
||||
// numbers returns the labels of the numbers from lo to hi, formatted.
|
||||
func numbers(lo, hi int, format string) []string {
|
||||
out := make([]string, 0, hi-lo+1)
|
||||
for i := lo; i <= hi; i++ {
|
||||
out = append(out, fmt.Sprintf(format, i))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TimePicker shows wheels of hours and minutes choosing *value's time of day,
|
||||
// which keeps its date, as UIDatePicker's time mode: 24 hours or 12 as the
|
||||
// user's region says. Changed reports a new time.
|
||||
func TimePicker(c *ui.Context, value *time.Time) ui.Element {
|
||||
row := ui.Row(c).AlignItems(ui.Center).Gap(4)
|
||||
h24 := c.Region().HourCycle != 12
|
||||
row.Children(func() {
|
||||
hour, minute := value.Hour(), value.Minute()
|
||||
changed := false
|
||||
if h24 {
|
||||
changed = Wheel(c.Key("h"), &hour, numbers(0, 23, "%02d")).Grow(1).Basis(0).Changed()
|
||||
} else {
|
||||
// 12 is the first of a half of the day's, shown as the wheel's last.
|
||||
h12 := (hour + 11) % 12
|
||||
changed = Wheel(c.Key("h"), &h12, numbers(1, 12, "%d")).Grow(1).Basis(0).Changed()
|
||||
hour = (h12 + 1) % 12
|
||||
}
|
||||
if Wheel(c.Key("m"), &minute, numbers(0, 59, "%02d")).Grow(1).Basis(0).Changed() {
|
||||
changed = true
|
||||
}
|
||||
if !h24 {
|
||||
pm := 0
|
||||
if value.Hour() >= 12 {
|
||||
pm = 1
|
||||
}
|
||||
if Wheel(c.Key("a"), &pm, []string{"AM", "PM"}).Grow(1).Basis(0).Changed() {
|
||||
changed = true
|
||||
}
|
||||
hour += 12 * pm
|
||||
}
|
||||
if changed {
|
||||
v := *value
|
||||
*value = time.Date(v.Year(), v.Month(), v.Day(), hour, minute, v.Second(), v.Nanosecond(), v.Location())
|
||||
row.SetChanged()
|
||||
}
|
||||
})
|
||||
return row
|
||||
}
|
||||
|
||||
// DatePicker chooses *value's date, which keeps its time of day: wheels of
|
||||
// month, day and year on Cupertino, as UIDatePicker's wheels, the month's
|
||||
// calendar on Material. Changed reports a new date.
|
||||
func DatePicker(c *ui.Context, value *time.Time) ui.Element {
|
||||
if Current() == Material {
|
||||
return ui.Calendar(c, value)
|
||||
}
|
||||
row := ui.Row(c).AlignItems(ui.Center).Gap(4)
|
||||
row.Children(func() {
|
||||
v := *value
|
||||
year0 := v.Year() - 100
|
||||
month, day, year := int(v.Month())-1, v.Day()-1, v.Year()-year0
|
||||
months := make([]string, 12)
|
||||
for i := range months {
|
||||
months[i] = time.Month(i + 1).String()
|
||||
}
|
||||
last := time.Date(v.Year(), v.Month()+1, 0, 0, 0, 0, 0, v.Location()).Day()
|
||||
changed := Wheel(c.Key("mo"), &month, months).Grow(2).Basis(0).Changed()
|
||||
if Wheel(c.Key("d"), &day, numbers(1, last, "%d")).Grow(1).Basis(0).Changed() {
|
||||
changed = true
|
||||
}
|
||||
if Wheel(c.Key("y"), &year, numbers(year0, year0+200, "%d")).Grow(1).Basis(0).Changed() {
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
y, m := year0+year, time.Month(month+1)
|
||||
d := min(day+1, time.Date(y, m+1, 0, 0, 0, 0, 0, v.Location()).Day())
|
||||
*value = time.Date(y, m, d, v.Hour(), v.Minute(), v.Second(), v.Nanosecond(), v.Location())
|
||||
row.SetChanged()
|
||||
}
|
||||
})
|
||||
return row
|
||||
}
|
||||
Vendored
+96
@@ -0,0 +1,96 @@
|
||||
package mobile
|
||||
|
||||
import "github.com/egoist/mygo/ui"
|
||||
|
||||
// ZoomState is how a Zoomable shows its content, kept in the app's state.
|
||||
type ZoomState struct {
|
||||
// Scale is how large, 1 by default; X and Y how far the content moved, in
|
||||
// DIPs from its place; Rotation how far it turned, in radians, if
|
||||
// ZoomOptions.Rotate.
|
||||
Scale, X, Y, Rotation float32
|
||||
}
|
||||
|
||||
// Reset shows the content as it was.
|
||||
func (z *ZoomState) Reset() { *z = ZoomState{Scale: 1} }
|
||||
|
||||
// ZoomOptions are how far a Zoomable goes.
|
||||
type ZoomOptions struct {
|
||||
// Min and Max are the scales it goes from and to: 1 and 4 by default.
|
||||
Min, Max float32
|
||||
// DoubleTap is the scale a double tap zooms to from the smallest, and
|
||||
// back; 2 by default.
|
||||
DoubleTap float32
|
||||
// Rotate lets two fingers turn the content too.
|
||||
Rotate bool
|
||||
}
|
||||
|
||||
// Zoomable shows content that two fingers pinch to zoom and one drags, and a
|
||||
// double tap zooms, as UIScrollView's zooming and Flutter's InteractiveViewer,
|
||||
// for a photo or a map: on what pinches the system tells, around the point
|
||||
// between the fingers. It clips the content to its box. Size it: Fill, Height.
|
||||
func Zoomable(c *ui.Context, z *ZoomState, o ZoomOptions, content func()) ui.Element {
|
||||
if o.Min <= 0 {
|
||||
o.Min = 1
|
||||
}
|
||||
if o.Max <= 0 {
|
||||
o.Max = 4
|
||||
}
|
||||
if o.DoubleTap <= 0 {
|
||||
o.DoubleTap = 2
|
||||
}
|
||||
if z.Scale == 0 {
|
||||
z.Scale = 1
|
||||
}
|
||||
box := ui.Box(c).Clip().Role(ui.RoleImage)
|
||||
bounds := box.Bounds()
|
||||
clamp := func() {
|
||||
z.Scale = max(o.Min, min(z.Scale, o.Max))
|
||||
mx := bounds.W * (z.Scale - 1) / 2
|
||||
my := bounds.H * (z.Scale - 1) / 2
|
||||
z.X = max(-mx, min(z.X, mx))
|
||||
z.Y = max(-my, min(z.Y, my))
|
||||
}
|
||||
box.HandleInput(func(ev ui.InputEvent) bool {
|
||||
switch ev.Kind {
|
||||
case ui.InputZoom:
|
||||
// Around the point between the fingers.
|
||||
k := max(o.Min, min(z.Scale*ev.Scale, o.Max)) / z.Scale
|
||||
cx, cy := ev.X-bounds.W/2, ev.Y-bounds.H/2
|
||||
z.X = cx - (cx-z.X)*k
|
||||
z.Y = cy - (cy-z.Y)*k
|
||||
z.Scale *= k
|
||||
if o.Rotate {
|
||||
z.Rotation += ev.Rotation
|
||||
}
|
||||
clamp()
|
||||
return true
|
||||
case ui.InputScroll:
|
||||
if z.Scale <= 1 {
|
||||
return false
|
||||
}
|
||||
z.X -= ev.DX
|
||||
z.Y -= ev.DY
|
||||
clamp()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
})
|
||||
if box.DoubleClicked() {
|
||||
if z.Scale > o.Min {
|
||||
z.Scale, z.X, z.Y = o.Min, 0, 0
|
||||
} else {
|
||||
z.Scale = o.DoubleTap
|
||||
x, y, _ := box.PointerPosition()
|
||||
z.X = (bounds.W/2 - x) * (z.Scale - 1)
|
||||
z.Y = (bounds.H/2 - y) * (z.Scale - 1)
|
||||
clamp()
|
||||
}
|
||||
}
|
||||
scale := box.AnimateTo("scale", z.Scale, ui.Snappy)
|
||||
x := box.AnimateTo("x", z.X, ui.Snappy)
|
||||
y := box.AnimateTo("y", z.Y, ui.Snappy)
|
||||
box.Children(func() {
|
||||
ui.Box(c).Fill().Translate(x, y).Scale(scale, scale).Rotate(z.Rotation).Children(content)
|
||||
})
|
||||
return box
|
||||
}
|
||||
Reference in new issue
Block a user