mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-04 00:45:35 +00:00
example: rename apps module
The new name emphasize the nature of the programs and won't be confused with package `app`. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
module gioui.org/example
|
||||
|
||||
go 1.13
|
||||
|
||||
require (
|
||||
gioui.org v0.0.0-20191006072004-529fd4d307e8
|
||||
github.com/google/go-github/v24 v24.0.1
|
||||
golang.org/x/exp v0.0.0-20190627132806-fd42eb6b336f
|
||||
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
|
||||
)
|
||||
@@ -0,0 +1,41 @@
|
||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
gioui.org v0.0.0-20191006072004-529fd4d307e8 h1:3hGhb8saeDm0LKKL/Zdg8LhGH4CZcz6hHCaGZIRayAs=
|
||||
gioui.org v0.0.0-20191006072004-529fd4d307e8/go.mod h1:+CEjc9B//HrBfWsQOVxjCyih7HGIj3Pww1xFHVDZyyk=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
|
||||
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
|
||||
github.com/google/go-github/v24 v24.0.1 h1:KCt1LjMJEey1qvPXxa9SjaWxwTsCWSq6p2Ju57UR4Q4=
|
||||
github.com/google/go-github/v24 v24.0.1/go.mod h1:CRqaW1Uns1TCkP0wqTpxYyRxRjxwvKU/XSS44u6X74M=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/exp v0.0.0-20190627132806-fd42eb6b336f h1:F3VDpCbV+46wJMDIwbFSefCwLlvK2CoEKVEYHO8p5Os=
|
||||
golang.org/x/exp v0.0.0-20190627132806-fd42eb6b336f/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
|
||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9 h1:uc17S921SPw5F2gJo7slQ3aqvr2RwpL7eb3+DZncu3s=
|
||||
golang.org/x/image v0.0.0-20190703141733-d6a02ce849c9/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180824143301-4910a1d54f87/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
@@ -0,0 +1,246 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package main
|
||||
|
||||
// A Gio program that displays Go contributors from GitHub. See https://gioui.org for more information.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"image"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
|
||||
_ "net/http/pprof"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/gesture"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/unit"
|
||||
|
||||
"github.com/google/go-github/v24/github"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
w *app.Window
|
||||
|
||||
ui *UI
|
||||
|
||||
updateUsers chan []*user
|
||||
commitsResult chan []*github.Commit
|
||||
ctx context.Context
|
||||
ctxCancel context.CancelFunc
|
||||
}
|
||||
|
||||
var (
|
||||
prof = flag.Bool("profile", false, "serve profiling data at http://localhost:6060")
|
||||
stats = flag.Bool("stats", false, "show rendering statistics")
|
||||
token = flag.String("token", "", "Github authentication token")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
initProfiling()
|
||||
if *token == "" {
|
||||
fmt.Println("The quota for anonymous GitHub API access is very low. Specify a token with -token to avoid quota errors.")
|
||||
fmt.Println("See https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line.")
|
||||
}
|
||||
go func() {
|
||||
w := app.NewWindow(
|
||||
app.WithWidth(unit.Dp(400)),
|
||||
app.WithHeight(unit.Dp(800)),
|
||||
app.WithTitle("Gophers"),
|
||||
)
|
||||
if err := newApp(w).run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
app.Main()
|
||||
}
|
||||
|
||||
func initProfiling() {
|
||||
if !*prof {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
log.Println(http.ListenAndServe("localhost:6060", nil))
|
||||
}()
|
||||
}
|
||||
|
||||
func (a *App) run() error {
|
||||
a.ui.profiling = *stats
|
||||
gtx := &layout.Context{
|
||||
Queue: a.w.Queue(),
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case users := <-a.updateUsers:
|
||||
a.ui.users = users
|
||||
a.ui.userClicks = make([]gesture.Click, len(users))
|
||||
a.w.Invalidate()
|
||||
case commits := <-a.commitsResult:
|
||||
a.ui.selectedUser.commits = commits
|
||||
a.w.Invalidate()
|
||||
case e := <-a.w.Events():
|
||||
switch e := e.(type) {
|
||||
case key.Event:
|
||||
switch e.Name {
|
||||
case key.NameEscape:
|
||||
os.Exit(0)
|
||||
case 'P':
|
||||
if e.Modifiers.Contain(key.ModCommand) {
|
||||
a.ui.profiling = !a.ui.profiling
|
||||
a.w.Invalidate()
|
||||
}
|
||||
}
|
||||
case app.DestroyEvent:
|
||||
return e.Err
|
||||
case app.StageEvent:
|
||||
if e.Stage >= app.StageRunning {
|
||||
if a.ctxCancel == nil {
|
||||
a.ctx, a.ctxCancel = context.WithCancel(context.Background())
|
||||
}
|
||||
if a.ui.users == nil {
|
||||
go a.fetchContributors()
|
||||
}
|
||||
} else {
|
||||
if a.ctxCancel != nil {
|
||||
a.ctxCancel()
|
||||
a.ctxCancel = nil
|
||||
}
|
||||
}
|
||||
case *app.CommandEvent:
|
||||
switch e.Type {
|
||||
case app.CommandBack:
|
||||
if a.ui.selectedUser != nil {
|
||||
a.ui.selectedUser = nil
|
||||
e.Cancel = true
|
||||
a.w.Invalidate()
|
||||
}
|
||||
}
|
||||
case app.UpdateEvent:
|
||||
gtx.Reset(&e.Config, e.Size)
|
||||
a.ui.Layout(gtx)
|
||||
a.w.Update(gtx.Ops)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newApp(w *app.Window) *App {
|
||||
a := &App{
|
||||
w: w,
|
||||
updateUsers: make(chan []*user),
|
||||
commitsResult: make(chan []*github.Commit, 1),
|
||||
}
|
||||
fetch := func(u string) {
|
||||
a.fetchCommits(a.ctx, u)
|
||||
}
|
||||
a.ui = newUI(fetch)
|
||||
return a
|
||||
}
|
||||
|
||||
func githubClient(ctx context.Context) *github.Client {
|
||||
var tc *http.Client
|
||||
if *token != "" {
|
||||
ts := oauth2.StaticTokenSource(
|
||||
&oauth2.Token{AccessToken: *token},
|
||||
)
|
||||
tc = oauth2.NewClient(ctx, ts)
|
||||
}
|
||||
return github.NewClient(tc)
|
||||
}
|
||||
|
||||
func (a *App) fetchContributors() {
|
||||
client := githubClient(a.ctx)
|
||||
cons, _, err := client.Repositories.ListContributors(a.ctx, "golang", "go", nil)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "github: failed to fetch contributors: %v\n", err)
|
||||
return
|
||||
}
|
||||
var users []*user
|
||||
userErrs := make(chan error, len(cons))
|
||||
avatarErrs := make(chan error, len(cons))
|
||||
for _, con := range cons {
|
||||
con := con
|
||||
avatar := con.GetAvatarURL()
|
||||
if avatar == "" {
|
||||
continue
|
||||
}
|
||||
u := &user{
|
||||
login: con.GetLogin(),
|
||||
}
|
||||
users = append(users, u)
|
||||
go func() {
|
||||
guser, _, err := client.Users.Get(a.ctx, u.login)
|
||||
if err != nil {
|
||||
avatarErrs <- err
|
||||
return
|
||||
}
|
||||
u.name = guser.GetName()
|
||||
u.company = guser.GetCompany()
|
||||
avatarErrs <- nil
|
||||
}()
|
||||
go func() {
|
||||
a, err := fetchImage(avatar)
|
||||
u.avatar = a
|
||||
userErrs <- err
|
||||
}()
|
||||
}
|
||||
for i := 0; i < len(cons); i++ {
|
||||
if err := <-userErrs; err != nil {
|
||||
fmt.Fprintf(os.Stderr, "github: failed to fetch user: %v\n", err)
|
||||
}
|
||||
if err := <-avatarErrs; err != nil {
|
||||
fmt.Fprintf(os.Stderr, "github: failed to fetch avatar: %v\n", err)
|
||||
}
|
||||
}
|
||||
// Drop users with no avatar or name.
|
||||
for i := len(users) - 1; i >= 0; i-- {
|
||||
if u := users[i]; u.name == "" || u.avatar == nil {
|
||||
users = append(users[:i], users[i+1:]...)
|
||||
}
|
||||
}
|
||||
a.updateUsers <- users
|
||||
}
|
||||
|
||||
func fetchImage(url string) (image.Image, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetchImage: http.Get(%q): %v", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
img, _, err := image.Decode(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetchImage: image decode failed: %v", err)
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
func (a *App) fetchCommits(ctx context.Context, user string) {
|
||||
go func() {
|
||||
gh := githubClient(ctx)
|
||||
repoCommits, _, err := gh.Repositories.ListCommits(ctx, "golang", "go", &github.CommitsListOptions{
|
||||
Author: user,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("failed to fetch commits: %v", err)
|
||||
return
|
||||
}
|
||||
var commits []*github.Commit
|
||||
for _, commit := range repoCommits {
|
||||
if c := commit.GetCommit(); c != nil {
|
||||
commits = append(commits, c)
|
||||
}
|
||||
}
|
||||
a.commitsResult <- commits
|
||||
}()
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gioui.org/io/event"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/unit"
|
||||
)
|
||||
|
||||
type queue struct{}
|
||||
|
||||
type config struct{}
|
||||
|
||||
func BenchmarkUI(b *testing.B) {
|
||||
fetch := func(_ string) {}
|
||||
u := newUI(fetch)
|
||||
cfg := new(config)
|
||||
gtx := &layout.Context{
|
||||
Queue: new(queue),
|
||||
}
|
||||
for i := 0; i < b.N; i++ {
|
||||
gtx.Reset(cfg, image.Point{800, 600})
|
||||
u.Layout(gtx)
|
||||
}
|
||||
}
|
||||
|
||||
func (queue) Events(k event.Key) []event.Event {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (config) Now() time.Time {
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func (config) Px(v unit.Value) int {
|
||||
return int(v.V + .5)
|
||||
}
|
||||
@@ -0,0 +1,549 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package main
|
||||
|
||||
// A Gio program that displays Go contributors from GitHub. See https://gioui.org for more information.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"runtime"
|
||||
|
||||
"golang.org/x/image/draw"
|
||||
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
|
||||
_ "net/http/pprof"
|
||||
|
||||
"gioui.org/f32"
|
||||
"gioui.org/gesture"
|
||||
"gioui.org/io/key"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/io/profile"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/text"
|
||||
"gioui.org/text/shape"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget"
|
||||
"golang.org/x/exp/shiny/iconvg"
|
||||
|
||||
"github.com/google/go-github/v24/github"
|
||||
"golang.org/x/image/font/gofont/gobold"
|
||||
"golang.org/x/image/font/gofont/goitalic"
|
||||
"golang.org/x/image/font/gofont/gomono"
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/sfnt"
|
||||
|
||||
"golang.org/x/exp/shiny/materialdesign/icons"
|
||||
)
|
||||
|
||||
type UI struct {
|
||||
fab *ActionButton
|
||||
usersList *layout.List
|
||||
users []*user
|
||||
userClicks []gesture.Click
|
||||
selectedUser *userPage
|
||||
edit, edit2 *text.Editor
|
||||
fetchCommits func(u string)
|
||||
|
||||
// Profiling.
|
||||
profiling bool
|
||||
profile profile.Event
|
||||
lastMallocs uint64
|
||||
}
|
||||
|
||||
type userPage struct {
|
||||
user *user
|
||||
commitsList *layout.List
|
||||
commits []*github.Commit
|
||||
}
|
||||
|
||||
type user struct {
|
||||
name string
|
||||
login string
|
||||
company string
|
||||
avatar image.Image
|
||||
}
|
||||
|
||||
type icon struct {
|
||||
src []byte
|
||||
size unit.Value
|
||||
|
||||
// Cached values.
|
||||
img image.Image
|
||||
imgSize int
|
||||
}
|
||||
|
||||
type ActionButton struct {
|
||||
Open bool
|
||||
icons []*icon
|
||||
sendIco *icon
|
||||
}
|
||||
|
||||
var families struct {
|
||||
primary *shape.Family
|
||||
mono *shape.Family
|
||||
}
|
||||
|
||||
var theme struct {
|
||||
text op.MacroOp
|
||||
tertText op.MacroOp
|
||||
brand op.MacroOp
|
||||
white op.MacroOp
|
||||
}
|
||||
|
||||
func colorMaterial(ops *op.Ops, color color.RGBA) op.MacroOp {
|
||||
var mat op.MacroOp
|
||||
mat.Record(ops)
|
||||
paint.ColorOp{Color: color}.Add(ops)
|
||||
mat.Stop()
|
||||
return mat
|
||||
}
|
||||
|
||||
func init() {
|
||||
families.primary = &shape.Family{
|
||||
Regular: mustLoadFont(goregular.TTF),
|
||||
Bold: mustLoadFont(gobold.TTF),
|
||||
Italic: mustLoadFont(goitalic.TTF),
|
||||
}
|
||||
families.mono = &shape.Family{
|
||||
Regular: mustLoadFont(gomono.TTF),
|
||||
}
|
||||
var ops op.Ops
|
||||
theme.text = colorMaterial(&ops, rgb(0x333333))
|
||||
theme.tertText = colorMaterial(&ops, rgb(0xbbbbbb))
|
||||
theme.brand = colorMaterial(&ops, rgb(0x62798c))
|
||||
theme.white = colorMaterial(&ops, rgb(0xffffff))
|
||||
}
|
||||
|
||||
func newUI(fetchCommits func(string)) *UI {
|
||||
u := &UI{
|
||||
fetchCommits: fetchCommits,
|
||||
}
|
||||
u.usersList = &layout.List{
|
||||
Axis: layout.Vertical,
|
||||
}
|
||||
u.fab = &ActionButton{
|
||||
sendIco: &icon{src: icons.ContentSend, size: unit.Dp(24)},
|
||||
icons: []*icon{},
|
||||
}
|
||||
u.edit2 = &text.Editor{
|
||||
Family: families.primary,
|
||||
Face: text.Face{
|
||||
Style: text.Italic,
|
||||
},
|
||||
Size: unit.Sp(14),
|
||||
//Alignment: text.End,
|
||||
SingleLine: true,
|
||||
Hint: "Hint",
|
||||
HintMaterial: theme.tertText,
|
||||
Material: theme.text,
|
||||
}
|
||||
u.edit2.SetText("Single line editor. Edit me!")
|
||||
u.edit = &text.Editor{
|
||||
Family: families.primary,
|
||||
Size: unit.Sp(16),
|
||||
Material: theme.text,
|
||||
//Alignment: text.End,
|
||||
//SingleLine: true,
|
||||
}
|
||||
u.edit.SetText(longTextSample)
|
||||
return u
|
||||
}
|
||||
|
||||
func mustLoadFont(fontData []byte) *sfnt.Font {
|
||||
fnt, err := sfnt.Parse(fontData)
|
||||
if err != nil {
|
||||
panic("failed to load font")
|
||||
}
|
||||
return fnt
|
||||
}
|
||||
|
||||
func rgb(c uint32) color.RGBA {
|
||||
return argb((0xff << 24) | c)
|
||||
}
|
||||
|
||||
func argb(c uint32) color.RGBA {
|
||||
return color.RGBA{A: uint8(c >> 24), R: uint8(c >> 16), G: uint8(c >> 8), B: uint8(c)}
|
||||
}
|
||||
|
||||
func (u *UI) layoutTimings(gtx *layout.Context) {
|
||||
if !u.profiling {
|
||||
return
|
||||
}
|
||||
for _, e := range gtx.Events(u) {
|
||||
if e, ok := e.(profile.Event); ok {
|
||||
u.profile = e
|
||||
}
|
||||
}
|
||||
profile.Op{Key: u}.Add(gtx.Ops)
|
||||
var mstats runtime.MemStats
|
||||
runtime.ReadMemStats(&mstats)
|
||||
mallocs := mstats.Mallocs - u.lastMallocs
|
||||
u.lastMallocs = mstats.Mallocs
|
||||
layout.Align(layout.NE).Layout(gtx, func() {
|
||||
layout.Inset{Top: unit.Dp(16)}.Layout(gtx, func() {
|
||||
txt := fmt.Sprintf("m: %d %s", mallocs, u.profile.Timings)
|
||||
text.Label{Material: theme.text, Size: unit.Sp(10), Text: txt}.Layout(gtx, families.mono)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (u *UI) Layout(gtx *layout.Context) {
|
||||
for i := range u.userClicks {
|
||||
click := &u.userClicks[i]
|
||||
for _, e := range click.Events(gtx) {
|
||||
if e.Type == gesture.TypeClick {
|
||||
u.selectedUser = u.newUserPage(u.users[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
if u.selectedUser == nil {
|
||||
u.layoutUsers(gtx)
|
||||
} else {
|
||||
u.selectedUser.Layout(gtx)
|
||||
}
|
||||
u.layoutTimings(gtx)
|
||||
}
|
||||
|
||||
func (u *UI) newUserPage(user *user) *userPage {
|
||||
up := &userPage{
|
||||
user: user,
|
||||
commitsList: &layout.List{Axis: layout.Vertical},
|
||||
}
|
||||
u.fetchCommits(user.login)
|
||||
return up
|
||||
}
|
||||
|
||||
func (up *userPage) Layout(gtx *layout.Context) {
|
||||
l := up.commitsList
|
||||
if l.Dragging() {
|
||||
key.HideInputOp{}.Add(gtx.Ops)
|
||||
}
|
||||
l.Layout(gtx, len(up.commits), func(i int) {
|
||||
up.commit(gtx, i)
|
||||
})
|
||||
}
|
||||
|
||||
func (up *userPage) commit(gtx *layout.Context, index int) {
|
||||
u := up.user
|
||||
msg := up.commits[index].GetMessage()
|
||||
label := text.Label{Material: theme.text, Size: unit.Sp(12), Text: msg}
|
||||
in := layout.Inset{Top: unit.Dp(16), Right: unit.Dp(8), Left: unit.Dp(8)}
|
||||
in.Layout(gtx, func() {
|
||||
f := layout.Flex{Axis: layout.Horizontal}
|
||||
c1 := f.Rigid(gtx, func() {
|
||||
sz := gtx.Px(unit.Dp(48))
|
||||
cc := clipCircle{}
|
||||
cc.Layout(gtx, func() {
|
||||
gtx.Constraints = layout.RigidConstraints(gtx.Constraints.Constrain(image.Point{X: sz, Y: sz}))
|
||||
widget.Image{Src: u.avatar, Rect: u.avatar.Bounds()}.Layout(gtx)
|
||||
})
|
||||
})
|
||||
c2 := f.Flex(gtx, 1, func() {
|
||||
gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
|
||||
layout.Inset{Left: unit.Dp(8)}.Layout(gtx, func() {
|
||||
label.Layout(gtx, families.primary)
|
||||
})
|
||||
})
|
||||
f.Layout(gtx, c1, c2)
|
||||
})
|
||||
}
|
||||
|
||||
func (u *UI) layoutUsers(gtx *layout.Context) {
|
||||
var st layout.Stack
|
||||
c2 := st.Rigid(gtx, func() {
|
||||
layout.Align(layout.SE).Layout(gtx, func() {
|
||||
in := layout.UniformInset(unit.Dp(16))
|
||||
in.Layout(gtx, func() {
|
||||
u.fab.Layout(gtx)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
c1 := st.Expand(gtx, func() {
|
||||
f := layout.Flex{Axis: layout.Vertical}
|
||||
|
||||
c1 := f.Rigid(gtx, func() {
|
||||
gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
|
||||
layout.UniformInset(unit.Dp(16)).Layout(gtx, func() {
|
||||
sz := gtx.Px(unit.Dp(200))
|
||||
cs := gtx.Constraints
|
||||
gtx.Constraints = layout.RigidConstraints(cs.Constrain(image.Point{X: sz, Y: sz}))
|
||||
u.edit.Layout(gtx)
|
||||
})
|
||||
})
|
||||
|
||||
c2 := f.Rigid(gtx, func() {
|
||||
gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
|
||||
in := layout.Inset{Bottom: unit.Dp(16), Left: unit.Dp(16), Right: unit.Dp(16)}
|
||||
in.Layout(gtx, func() {
|
||||
u.edit2.Layout(gtx)
|
||||
})
|
||||
})
|
||||
|
||||
c3 := f.Rigid(gtx, func() {
|
||||
gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
|
||||
s := layout.Stack{Alignment: layout.Center}
|
||||
c2 := s.Rigid(gtx, func() {
|
||||
grey := colorMaterial(gtx.Ops, rgb(0x888888))
|
||||
in := layout.Inset{Top: unit.Dp(16), Right: unit.Dp(8), Bottom: unit.Dp(8), Left: unit.Dp(8)}
|
||||
in.Layout(gtx, func() {
|
||||
lbl := text.Label{Material: grey, Size: unit.Sp(11), Text: "GOPHERS"}
|
||||
lbl.Layout(gtx, families.primary)
|
||||
})
|
||||
})
|
||||
c1 := s.Expand(gtx, func() {
|
||||
fill{colorMaterial(gtx.Ops, rgb(0xf2f2f2))}.Layout(gtx)
|
||||
})
|
||||
s.Layout(gtx, c1, c2)
|
||||
})
|
||||
|
||||
c4 := f.Flex(gtx, 1, func() {
|
||||
gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
|
||||
u.layoutContributors(gtx)
|
||||
})
|
||||
f.Layout(gtx, c1, c2, c3, c4)
|
||||
})
|
||||
st.Layout(gtx, c1, c2)
|
||||
}
|
||||
|
||||
func (a *ActionButton) Layout(gtx *layout.Context) {
|
||||
f := layout.Flex{Axis: layout.Vertical, Alignment: layout.End}
|
||||
f.Layout(gtx, f.Rigid(gtx, func() {
|
||||
layout.Inset{Top: unit.Dp(4)}.Layout(gtx, func() {
|
||||
fab(gtx, a.sendIco.image(gtx), theme.brand, gtx.Px(unit.Dp(56)))
|
||||
pointer.EllipseAreaOp{Rect: image.Rectangle{Max: gtx.Dimensions.Size}}.Add(gtx.Ops)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func (u *UI) layoutContributors(gtx *layout.Context) {
|
||||
l := u.usersList
|
||||
if l.Dragging() {
|
||||
key.HideInputOp{}.Add(gtx.Ops)
|
||||
}
|
||||
l.Layout(gtx, len(u.users), func(i int) {
|
||||
u.user(gtx, i)
|
||||
})
|
||||
}
|
||||
|
||||
func (u *UI) user(gtx *layout.Context, index int) {
|
||||
user := u.users[index]
|
||||
elem := layout.Flex{Axis: layout.Vertical}
|
||||
c1 := elem.Rigid(gtx, func() {
|
||||
in := layout.UniformInset(unit.Dp(8))
|
||||
in.Layout(gtx, func() {
|
||||
f := centerRowOpts()
|
||||
c1 := f.Rigid(gtx, func() {
|
||||
in := layout.Inset{Right: unit.Dp(8)}
|
||||
cc := clipCircle{}
|
||||
in.Layout(gtx, func() {
|
||||
cc.Layout(gtx, func() {
|
||||
sz := image.Point{X: gtx.Px(unit.Dp(48)), Y: gtx.Px(unit.Dp(48))}
|
||||
gtx.Constraints = layout.RigidConstraints(gtx.Constraints.Constrain(sz))
|
||||
widget.Image{Src: user.avatar, Rect: user.avatar.Bounds()}.Layout(gtx)
|
||||
})
|
||||
})
|
||||
})
|
||||
c2 := f.Rigid(gtx, func() {
|
||||
f := column()
|
||||
c1 := f.Rigid(gtx, func() {
|
||||
f := baseline()
|
||||
c1 := f.Rigid(gtx, func() {
|
||||
text.Label{Material: theme.text, Size: unit.Sp(13), Text: user.name}.Layout(gtx, families.primary)
|
||||
})
|
||||
c2 := f.Flex(gtx, 1, func() {
|
||||
gtx.Constraints.Width.Min = gtx.Constraints.Width.Max
|
||||
layout.Align(layout.E).Layout(gtx, func() {
|
||||
layout.Inset{Left: unit.Dp(2)}.Layout(gtx, func() {
|
||||
lbl := text.Label{Material: theme.text, Size: unit.Sp(10), Text: "3 hours ago"}
|
||||
lbl.Layout(gtx, families.primary)
|
||||
})
|
||||
})
|
||||
})
|
||||
f.Layout(gtx, c1, c2)
|
||||
})
|
||||
c2 := f.Rigid(gtx, func() {
|
||||
in := layout.Inset{Top: unit.Dp(4)}
|
||||
in.Layout(gtx, func() {
|
||||
text.Label{Material: theme.tertText, Size: unit.Sp(12), Text: user.company}.Layout(gtx, families.primary)
|
||||
})
|
||||
})
|
||||
f.Layout(gtx, c1, c2)
|
||||
})
|
||||
f.Layout(gtx, c1, c2)
|
||||
})
|
||||
pointer.RectAreaOp{Rect: image.Rectangle{Max: gtx.Dimensions.Size}}.Add(gtx.Ops)
|
||||
click := &u.userClicks[index]
|
||||
click.Add(gtx.Ops)
|
||||
})
|
||||
elem.Layout(gtx, c1)
|
||||
}
|
||||
|
||||
type fill struct {
|
||||
material op.MacroOp
|
||||
}
|
||||
|
||||
func (f fill) Layout(gtx *layout.Context) {
|
||||
cs := gtx.Constraints
|
||||
d := image.Point{X: cs.Width.Max, Y: cs.Height.Max}
|
||||
dr := f32.Rectangle{
|
||||
Max: f32.Point{X: float32(d.X), Y: float32(d.Y)},
|
||||
}
|
||||
f.material.Add(gtx.Ops)
|
||||
paint.PaintOp{Rect: dr}.Add(gtx.Ops)
|
||||
gtx.Dimensions = layout.Dimensions{Size: d, Baseline: d.Y}
|
||||
}
|
||||
|
||||
func column() layout.Flex {
|
||||
return layout.Flex{Axis: layout.Vertical}
|
||||
}
|
||||
|
||||
func centerRowOpts() layout.Flex {
|
||||
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}
|
||||
}
|
||||
|
||||
func baseline() layout.Flex {
|
||||
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Baseline}
|
||||
}
|
||||
|
||||
type clipCircle struct {
|
||||
}
|
||||
|
||||
func (c *clipCircle) Layout(gtx *layout.Context, w layout.Widget) {
|
||||
var m op.MacroOp
|
||||
m.Record(gtx.Ops)
|
||||
w()
|
||||
m.Stop()
|
||||
dims := gtx.Dimensions
|
||||
max := dims.Size.X
|
||||
if dy := dims.Size.Y; dy > max {
|
||||
max = dy
|
||||
}
|
||||
szf := float32(max)
|
||||
rr := szf * .5
|
||||
var stack op.StackOp
|
||||
stack.Push(gtx.Ops)
|
||||
rrect(gtx.Ops, szf, szf, rr, rr, rr, rr)
|
||||
m.Add(gtx.Ops)
|
||||
stack.Pop()
|
||||
}
|
||||
|
||||
func fab(gtx *layout.Context, ico image.Image, mat op.MacroOp, size int) {
|
||||
dp := image.Point{X: (size - ico.Bounds().Dx()) / 2, Y: (size - ico.Bounds().Dy()) / 2}
|
||||
dims := image.Point{X: size, Y: size}
|
||||
rr := float32(size) * .5
|
||||
rrect(gtx.Ops, float32(size), float32(size), rr, rr, rr, rr)
|
||||
mat.Add(gtx.Ops)
|
||||
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{X: float32(size), Y: float32(size)}}}.Add(gtx.Ops)
|
||||
paint.ImageOp{Src: ico, Rect: ico.Bounds()}.Add(gtx.Ops)
|
||||
paint.PaintOp{
|
||||
Rect: toRectF(ico.Bounds().Add(dp)),
|
||||
}.Add(gtx.Ops)
|
||||
gtx.Dimensions = layout.Dimensions{Size: dims}
|
||||
}
|
||||
|
||||
func toRectF(r image.Rectangle) f32.Rectangle {
|
||||
return f32.Rectangle{
|
||||
Min: f32.Point{X: float32(r.Min.X), Y: float32(r.Min.Y)},
|
||||
Max: f32.Point{X: float32(r.Max.X), Y: float32(r.Max.Y)},
|
||||
}
|
||||
}
|
||||
|
||||
func (ic *icon) image(cfg unit.Converter) image.Image {
|
||||
sz := cfg.Px(ic.size)
|
||||
if sz == ic.imgSize {
|
||||
return ic.img
|
||||
}
|
||||
m, _ := iconvg.DecodeMetadata(ic.src)
|
||||
dx, dy := m.ViewBox.AspectRatio()
|
||||
img := image.NewRGBA(image.Rectangle{Max: image.Point{X: sz, Y: int(float32(sz) * dy / dx)}})
|
||||
var ico iconvg.Rasterizer
|
||||
ico.SetDstImage(img, img.Bounds(), draw.Src)
|
||||
// Use white for icons.
|
||||
m.Palette[0] = color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff}
|
||||
iconvg.Decode(&ico, ic.src, &iconvg.DecodeOptions{
|
||||
Palette: &m.Palette,
|
||||
})
|
||||
ic.img = img
|
||||
ic.imgSize = sz
|
||||
return img
|
||||
}
|
||||
|
||||
// https://pomax.github.io/bezierinfo/#circles_cubic.
|
||||
func rrect(ops *op.Ops, width, height, se, sw, nw, ne float32) {
|
||||
w, h := float32(width), float32(height)
|
||||
const c = 0.55228475 // 4*(sqrt(2)-1)/3
|
||||
var b paint.Path
|
||||
b.Begin(ops)
|
||||
b.Move(f32.Point{X: w, Y: h - se})
|
||||
b.Cube(f32.Point{X: 0, Y: se * c}, f32.Point{X: -se + se*c, Y: se}, f32.Point{X: -se, Y: se}) // SE
|
||||
b.Line(f32.Point{X: sw - w + se, Y: 0})
|
||||
b.Cube(f32.Point{X: -sw * c, Y: 0}, f32.Point{X: -sw, Y: -sw + sw*c}, f32.Point{X: -sw, Y: -sw}) // SW
|
||||
b.Line(f32.Point{X: 0, Y: nw - h + sw})
|
||||
b.Cube(f32.Point{X: 0, Y: -nw * c}, f32.Point{X: nw - nw*c, Y: -nw}, f32.Point{X: nw, Y: -nw}) // NW
|
||||
b.Line(f32.Point{X: w - ne - nw, Y: 0})
|
||||
b.Cube(f32.Point{X: ne * c, Y: 0}, f32.Point{X: ne, Y: ne - ne*c}, f32.Point{X: ne, Y: ne}) // NE
|
||||
b.End()
|
||||
}
|
||||
|
||||
const longTextSample = `1. I learned from my grandfather, Verus, to use good manners, and to
|
||||
put restraint on anger. 2. In the famous memory of my father I had a
|
||||
pattern of modesty and manliness. 3. Of my mother I learned to be
|
||||
pious and generous; to keep myself not only from evil deeds, but even
|
||||
from evil thoughts; and to live with a simplicity which is far from
|
||||
customary among the rich. 4. I owe it to my great-grandfather that I
|
||||
did not attend public lectures and discussions, but had good and able
|
||||
teachers at home; and I owe him also the knowledge that for things of
|
||||
this nature a man should count no expense too great.
|
||||
|
||||
5. My tutor taught me not to favour either green or blue at the
|
||||
chariot races, nor, in the contests of gladiators, to be a supporter
|
||||
either of light or heavy armed. He taught me also to endure labour;
|
||||
not to need many things; to serve myself without troubling others; not
|
||||
to intermeddle in the affairs of others, and not easily to listen to
|
||||
slanders against them.
|
||||
|
||||
6. Of Diognetus I had the lesson not to busy myself about vain things;
|
||||
not to credit the great professions of such as pretend to work
|
||||
wonders, or of sorcerers about their charms, and their expelling of
|
||||
Demons and the like; not to keep quails (for fighting or divination),
|
||||
nor to run after such things; to suffer freedom of speech in others,
|
||||
and to apply myself heartily to philosophy. Him also I must thank for
|
||||
my hearing first Bacchius, then Tandasis and Marcianus; that I wrote
|
||||
dialogues in my youth, and took a liking to the philosopher's pallet
|
||||
and skins, and to the other things which, by the Grecian discipline,
|
||||
belong to that profession.
|
||||
|
||||
7. To Rusticus I owe my first apprehensions that my nature needed
|
||||
reform and cure; and that I did not fall into the ambition of the
|
||||
common Sophists, either by composing speculative writings or by
|
||||
declaiming harangues of exhortation in public; further, that I never
|
||||
strove to be admired by ostentation of great patience in an ascetic
|
||||
life, or by display of activity and application; that I gave over the
|
||||
study of rhetoric, poetry, and the graces of language; and that I did
|
||||
not pace my house in my senatorial robes, or practise any similar
|
||||
affectation. I observed also the simplicity of style in his letters,
|
||||
particularly in that which he wrote to my mother from Sinuessa. I
|
||||
learned from him to be easily appeased, and to be readily reconciled
|
||||
with those who had displeased me or given cause of offence, so soon as
|
||||
they inclined to make their peace; to read with care; not to rest
|
||||
satisfied with a slight and superficial knowledge; nor quickly to
|
||||
assent to great talkers. I have him to thank that I met with the
|
||||
discourses of Epictetus, which he furnished me from his own library.
|
||||
|
||||
8. From Apollonius I learned true liberty, and tenacity of purpose; to
|
||||
regard nothing else, even in the smallest degree, but reason always;
|
||||
and always to remain unaltered in the agonies of pain, in the losses
|
||||
of children, or in long diseases. He afforded me a living example of
|
||||
how the same man can, upon occasion, be most yielding and most
|
||||
inflexible. He was patient in exposition; and, as might well be seen,
|
||||
esteemed his fine skill and ability in teaching others the principles
|
||||
of philosophy as the least of his endowments. It was from him that I
|
||||
learned how to receive from friends what are thought favours without
|
||||
seeming humbled by the giver or insensible to the gift.`
|
||||
@@ -0,0 +1,61 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package main
|
||||
|
||||
// A simple Gio program. See https://gioui.org for more information.
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"log"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op"
|
||||
"gioui.org/op/paint"
|
||||
"gioui.org/text"
|
||||
"gioui.org/text/shape"
|
||||
"gioui.org/unit"
|
||||
|
||||
"golang.org/x/image/font/gofont/goregular"
|
||||
"golang.org/x/image/font/sfnt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
w := app.NewWindow()
|
||||
if err := loop(w); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}()
|
||||
app.Main()
|
||||
}
|
||||
|
||||
func loop(w *app.Window) error {
|
||||
regular, err := sfnt.Parse(goregular.TTF)
|
||||
if err != nil {
|
||||
panic("failed to load font")
|
||||
}
|
||||
family := &shape.Family{
|
||||
Regular: regular,
|
||||
}
|
||||
maroon := color.RGBA{127, 0, 0, 255}
|
||||
message := "Hello, Gio"
|
||||
gtx := &layout.Context{
|
||||
Queue: w.Queue(),
|
||||
}
|
||||
for {
|
||||
e := <-w.Events()
|
||||
switch e := e.(type) {
|
||||
case app.DestroyEvent:
|
||||
return e.Err
|
||||
case app.UpdateEvent:
|
||||
gtx.Reset(&e.Config, e.Size)
|
||||
var material op.MacroOp
|
||||
material.Record(gtx.Ops)
|
||||
paint.ColorOp{Color: maroon}.Add(gtx.Ops)
|
||||
material.Stop()
|
||||
text.Label{Material: material, Size: unit.Sp(72), Alignment: text.Middle, Text: message}.Layout(gtx, family)
|
||||
w.Update(gtx.Ops)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user