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
@@ -0,0 +1,24 @@
|
||||
// Package native is the update window of package updater drawn in native
|
||||
// UI (package ui) instead of a web page, for apps whose windows all show
|
||||
// native UI: it needs no webview, so neither WebKitGTK on Linux nor the
|
||||
// WebView2 Runtime on Windows. Use it in place of updater.Plugin:
|
||||
//
|
||||
// mygo.Use(native.Plugin) // or native.New(updater.Options{Interval: ..., Icon: ...})
|
||||
//
|
||||
// The rest is package updater's: updater.MenuItem and
|
||||
// updater.CheckForUpdates check from the app's menu, and the preferences
|
||||
// (updater.SetAutomaticChecks, updater.OnChange, …) work the same.
|
||||
package native
|
||||
|
||||
import (
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/plugins/updater"
|
||||
"github.com/egoist/mygo/plugins/updater/internal/frontend"
|
||||
)
|
||||
|
||||
// Plugin is the plugin with the default options.
|
||||
var Plugin = New(updater.Options{})
|
||||
|
||||
// New returns the plugin with options. Use one of them only, and neither
|
||||
// updater.Plugin nor updater.New.
|
||||
func New(opts updater.Options) mygo.Plugin { return frontend.Plugin(opts, open) }
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
package native
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"unicode"
|
||||
|
||||
"github.com/egoist/mygo/plugins/updater/internal/markdown"
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
// notes builds release notes, in the manner of the page of package
|
||||
// updater. Their text is selectable, and links open in the browser.
|
||||
func notes(c *ui.Context, t *ui.Theme, blocks []markdown.Block) {
|
||||
n := ¬esStyle{t: t, rtl: rightToLeft(blocks)}
|
||||
ui.Column(c).Children(func() { n.blocks(c, blocks, false, 0) })
|
||||
}
|
||||
|
||||
// notesStyle is how release notes look. Like the page's notes, whose dir
|
||||
// is auto, they are laid out from the right when the language they are
|
||||
// written in is written from right to left, whatever the window's.
|
||||
type notesStyle struct {
|
||||
t *ui.Theme
|
||||
rtl bool
|
||||
}
|
||||
|
||||
// rightToLeft reports whether the first strongly directional letter of
|
||||
// blocks is of a script written from right to left, as text does.
|
||||
func rightToLeft(blocks []markdown.Block) bool {
|
||||
var rtl, found bool
|
||||
var inlines func([]markdown.Inline)
|
||||
text := func(s string) {
|
||||
for _, r := range s {
|
||||
if found {
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case unicode.In(r, unicode.Hebrew, unicode.Arabic, unicode.Syriac, unicode.Thaana, unicode.Nko, unicode.Samaritan, unicode.Mandaic, unicode.Adlam):
|
||||
rtl, found = true, true
|
||||
case unicode.IsLetter(r):
|
||||
found = true
|
||||
}
|
||||
}
|
||||
}
|
||||
inlines = func(ins []markdown.Inline) {
|
||||
for _, in := range ins {
|
||||
text(in.Text)
|
||||
inlines(in.Children)
|
||||
}
|
||||
}
|
||||
var walk func([]markdown.Block)
|
||||
walk = func(bs []markdown.Block) {
|
||||
for _, b := range bs {
|
||||
inlines(b.Inlines)
|
||||
text(b.Text)
|
||||
for _, item := range b.Items {
|
||||
walk(item)
|
||||
}
|
||||
walk(b.Blocks)
|
||||
}
|
||||
}
|
||||
walk(blocks)
|
||||
return rtl
|
||||
}
|
||||
|
||||
// blocks builds blocks, apart by the room between paragraphs, or by none
|
||||
// in the items of a list (tight). depth counts the lists around them.
|
||||
func (n *notesStyle) blocks(c *ui.Context, blocks []markdown.Block, tight bool, depth int) {
|
||||
for i, b := range blocks {
|
||||
var gap float32
|
||||
switch {
|
||||
case i == 0 || tight:
|
||||
case b.Kind == markdown.Heading:
|
||||
gap = 12
|
||||
case blocks[i-1].Kind == markdown.Heading:
|
||||
gap = 4
|
||||
default:
|
||||
gap = 8
|
||||
}
|
||||
n.block(c, b, depth).Margin(gap, 0, 0, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// headingSizes are the sizes of headings of levels 1, 2 and the others,
|
||||
// relative to the text.
|
||||
var headingSizes = [...]float32{16.0 / 13, 14.0 / 13, 1}
|
||||
|
||||
func (n *notesStyle) block(c *ui.Context, b markdown.Block, depth int) ui.Element {
|
||||
t := n.t
|
||||
switch b.Kind {
|
||||
case markdown.Heading:
|
||||
size := headingSizes[min(b.Level, len(headingSizes))-1]
|
||||
return paragraph(c, t, b.Inlines).FontSize(t.Rem(size)).Bold()
|
||||
case markdown.List:
|
||||
return ui.Column(c).Children(func() {
|
||||
for i, item := range b.Items {
|
||||
row := ui.Row(c).AlignItems(ui.Start)
|
||||
if n.rtl {
|
||||
row.Reverse()
|
||||
}
|
||||
row.Children(func() {
|
||||
marker := bullet(depth)
|
||||
if b.Ordered {
|
||||
marker = strconv.Itoa(i+1) + "."
|
||||
}
|
||||
// The marker ends next to the item.
|
||||
m := ui.Text(c, marker).Width(20).Shrink(0)
|
||||
if n.rtl {
|
||||
m.Padding(0, 0, 0, 6)
|
||||
} else {
|
||||
m.TextAlign(ui.End).Padding(0, 6, 0, 0)
|
||||
}
|
||||
ui.Column(c).Grow(1).Children(func() { n.blocks(c, item, true, depth+1) })
|
||||
})
|
||||
}
|
||||
})
|
||||
case markdown.Quote:
|
||||
quote := ui.Column(c).BorderColor(t.Border).TextColor(t.TextMuted)
|
||||
if n.rtl {
|
||||
quote.BorderWidth(0, 3, 0, 0).Padding(0, 10, 0, 0)
|
||||
} else {
|
||||
quote.BorderWidth(0, 0, 0, 3).Padding(0, 0, 0, 10)
|
||||
}
|
||||
return quote.Children(func() { n.blocks(c, b.Blocks, false, depth) })
|
||||
case markdown.Code:
|
||||
return ui.ScrollHorizontal(c).Background(track(t)).Radius(3).Children(func() {
|
||||
ui.Text(c, b.Text).Font("monospace").FontSize(t.Rem(12.0/13)).NoWrap().Padding(6, 8).Selectable()
|
||||
})
|
||||
case markdown.Rule:
|
||||
return ui.Divider(c)
|
||||
}
|
||||
return paragraph(c, t, b.Inlines)
|
||||
}
|
||||
|
||||
// paragraph builds the text of a paragraph or heading, which the user may
|
||||
// select.
|
||||
func paragraph(c *ui.Context, t *ui.Theme, inlines []markdown.Inline) ui.Element {
|
||||
return ui.RichText(c).Children(func() { buildInlines(c, t, inlines) }).Selectable()
|
||||
}
|
||||
|
||||
// bullet returns the marker of the items of a list in depth lists, as
|
||||
// browsers draw them.
|
||||
func bullet(depth int) string {
|
||||
switch depth {
|
||||
case 0:
|
||||
return "•"
|
||||
case 1:
|
||||
return "◦"
|
||||
}
|
||||
return "▪"
|
||||
}
|
||||
|
||||
// buildInlines builds the text of a paragraph inside it: links open in the
|
||||
// browser.
|
||||
func buildInlines(c *ui.Context, t *ui.Theme, inlines []markdown.Inline) {
|
||||
for _, in := range inlines {
|
||||
switch in.Kind {
|
||||
case markdown.Text:
|
||||
ui.Text(c, in.Text)
|
||||
case markdown.CodeSpan:
|
||||
ui.Text(c, in.Text).Font("monospace").FontSize(t.Rem(12.0 / 13)).TextBackground(track(t))
|
||||
case markdown.Emphasis:
|
||||
ui.RichText(c).Italic().Children(func() { buildInlines(c, t, in.Children) })
|
||||
case markdown.Strong:
|
||||
ui.RichText(c).Bold().Children(func() { buildInlines(c, t, in.Children) })
|
||||
case markdown.Link:
|
||||
ui.Link(c, "", in.URL).Children(func() { buildInlines(c, t, in.Children) })
|
||||
}
|
||||
}
|
||||
}
|
||||
+267
@@ -0,0 +1,267 @@
|
||||
package native
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/egoist/mygo/plugins/updater/internal/frontend"
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
// window is the update window of a session.
|
||||
type window struct {
|
||||
s frontend.Session
|
||||
texts frontend.Texts
|
||||
icon *ui.Bitmap // nil without an icon
|
||||
|
||||
// What the view keeps from frame to frame, on the main thread: the
|
||||
// prompt of the view it shows, the prompt the user answered, and the
|
||||
// checkbox, which each prompt resets.
|
||||
prompt int
|
||||
answered int
|
||||
checked bool
|
||||
// built is what the last frame laid out, and fitted the size last
|
||||
// asked of the window for it.
|
||||
built layout
|
||||
fitted [2]int
|
||||
}
|
||||
|
||||
// layout is what the size of the window's content depends on.
|
||||
type layout struct {
|
||||
prompt int
|
||||
title, message, detail string
|
||||
bar, checkbox, release bool
|
||||
windowWidth, windowHeight float32
|
||||
}
|
||||
|
||||
// open creates the window of s, which shows at once.
|
||||
func open(s frontend.Session) {
|
||||
w := newWindow(s)
|
||||
// Changes made while the window is being created redraw it too.
|
||||
_, changed := s.Current()
|
||||
win := s.NewWindow(ui.View(w.view))
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-changed:
|
||||
_, changed = s.Current()
|
||||
win.Invalidate()
|
||||
case <-s.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
frontend.Show(win, s.User())
|
||||
}
|
||||
|
||||
func newWindow(s frontend.Session) *window {
|
||||
w := &window{s: s, texts: s.Texts(), prompt: -1, answered: -1}
|
||||
if png := s.Icon(); png != nil {
|
||||
w.icon, _ = ui.DecodeBitmap(png) // no icon when it is not an image
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
// The room around the window's content and between its parts, in DIPs, as
|
||||
// in the page of package updater.
|
||||
const (
|
||||
padTop, padSide, padBottom = 18, 20, 20
|
||||
iconSize, iconGap = 64, 18
|
||||
footGap = 12
|
||||
asideGap = 40
|
||||
)
|
||||
|
||||
// view builds the update window.
|
||||
func (w *window) view(c *ui.Context) {
|
||||
v, _ := w.s.Current()
|
||||
if v.Prompt != w.prompt {
|
||||
w.prompt, w.checked = v.Prompt, v.Checked
|
||||
}
|
||||
t := theme(c.Theme())
|
||||
c.SetTheme(t)
|
||||
w.keys(c, v)
|
||||
|
||||
var body, aside, buttons ui.Element
|
||||
ui.Column(c).Fill().Padding(padTop, padSide, padBottom).Gap(footGap).Children(func() {
|
||||
top := w.row(c).Grow(1).Gap(iconGap).AlignItems(ui.Start)
|
||||
if v.Release {
|
||||
top.AlignItems(ui.Stretch)
|
||||
}
|
||||
top.Children(func() {
|
||||
if w.icon != nil {
|
||||
ui.Image(c, w.icon).Size(iconSize, iconSize).Shrink(0).AlignSelf(ui.Start)
|
||||
}
|
||||
body = ui.Column(c).Grow(1).Children(func() { w.body(c, t, v) })
|
||||
})
|
||||
w.row(c).Gap(10).Shrink(0).Children(func() {
|
||||
aside = w.row(c).Gap(10).Shrink(0).Children(func() { w.buttons(c, t, v, true) })
|
||||
ui.Spacer(c)
|
||||
buttons = w.row(c).Gap(10).Shrink(0).Children(func() { w.buttons(c, t, v, false) })
|
||||
})
|
||||
})
|
||||
w.fit(c, v, body, aside, buttons)
|
||||
}
|
||||
|
||||
// row creates a row, laid out from the right in a window of a language
|
||||
// written from right to left.
|
||||
func (w *window) row(c *ui.Context) ui.Element {
|
||||
r := ui.Row(c)
|
||||
if w.texts.RTL {
|
||||
r.Reverse()
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// body builds the texts, progress bar, release notes and checkbox of the
|
||||
// view. Texts start on the side their own language starts, as the
|
||||
// page's unicode-bidi: plaintext does, since they may fall back to
|
||||
// English or be errors.
|
||||
func (w *window) body(c *ui.Context, t *ui.Theme, v frontend.View) {
|
||||
small := t.Rem(11.0 / 13)
|
||||
ui.Text(c.Key("title"), v.Title).Bold().Margin(0, 0, 4, 0)
|
||||
if v.Message != "" {
|
||||
ui.Text(c.Key("message"), v.Message).Margin(0, 0, 6, 0)
|
||||
}
|
||||
if v.Bar {
|
||||
bar := ui.Progress(c.Key("bar"), v.Progress).Margin(6, 0, 4, 0)
|
||||
if w.texts.RTL {
|
||||
bar.Reverse()
|
||||
}
|
||||
}
|
||||
if v.Detail != "" {
|
||||
// All of it, however long: the window grows to fit it, and a
|
||||
// tooltip would cover the title.
|
||||
ui.Text(c.Key("detail"), v.Detail).FontSize(small).TextColor(t.TextMuted).Selectable()
|
||||
}
|
||||
if len(v.Notes) > 0 {
|
||||
ui.Column(c.Key("notes")).Grow(1).Margin(6, 0, 0, 0).Children(func() {
|
||||
ui.Text(c, w.texts.ReleaseNotes).FontSize(small).Bold().Margin(0, 0, 4, 0)
|
||||
ui.Scroll(c).Grow(1).Background(panel(t)).Border(1, t.Border).Padding(8, 12).Children(func() {
|
||||
notes(c, t, v.Notes)
|
||||
})
|
||||
})
|
||||
}
|
||||
if v.Checkbox {
|
||||
w.row(c).Key("checkbox").Margin(10, 0, 0, 0).Children(func() {
|
||||
box := ui.Checkbox(c, &w.checked, w.texts.AutomaticDownloads)
|
||||
if w.texts.RTL {
|
||||
box.Reverse()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// buttons builds the buttons of the view set aside, or the others.
|
||||
func (w *window) buttons(c *ui.Context, t *ui.Theme, v frontend.View, aside bool) {
|
||||
height := float32(math.Round(float64(t.Rem(24.0 / 13))))
|
||||
for _, b := range v.Buttons {
|
||||
if b.Aside != aside {
|
||||
continue
|
||||
}
|
||||
// Keyed by action, so that the focus leaves with the button.
|
||||
var e ui.Element
|
||||
if b.Default {
|
||||
e = ui.PrimaryButton(c.Key(string(b.Action)), "")
|
||||
} else {
|
||||
e = ui.Button(c.Key(string(b.Action)), "").Shadow(0, 1, 1, 0, ui.RGBA(0, 0, 0, 0.08))
|
||||
}
|
||||
e.Padding(0, 14).Height(height).MinWidth(84).Disabled(w.off(v, b)).Children(func() {
|
||||
ui.Text(c, b.Label).SingleLine()
|
||||
})
|
||||
if e.Clicked() {
|
||||
w.respond(v, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// keys answers with the button of Escape, and that of Enter when no
|
||||
// button has the focus.
|
||||
func (w *window) keys(c *ui.Context, v frontend.View) {
|
||||
for _, b := range v.Buttons {
|
||||
if b.Cancel && c.Shortcut(0, ui.KeyEscape) || b.Default && c.Shortcut(0, ui.KeyEnter) {
|
||||
w.respond(v, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// off reports whether a button is disabled: by the view, or because the
|
||||
// user answered its prompt, until the next view.
|
||||
func (w *window) off(v frontend.View, b frontend.Button) bool {
|
||||
return b.Disabled || w.answered == v.Prompt
|
||||
}
|
||||
|
||||
func (w *window) respond(v frontend.View, b frontend.Button) {
|
||||
if w.off(v, b) {
|
||||
return
|
||||
}
|
||||
w.answered = v.Prompt
|
||||
w.s.Respond(v.Prompt, b.Action, w.checked)
|
||||
}
|
||||
|
||||
// fit asks for the size the content needs, as measured in the last frame
|
||||
// when it laid out the same view at the same size: the width of the
|
||||
// buttons, and for status views the height of the text.
|
||||
func (w *window) fit(c *ui.Context, v frontend.View, body, aside, buttons ui.Element) {
|
||||
ww, wh := c.Size()
|
||||
l := layout{v.Prompt, v.Title, v.Message, v.Detail, v.Bar, v.Checkbox, v.Release, ww, wh}
|
||||
if l != w.built {
|
||||
// Measure the layout of this frame in the next.
|
||||
w.built = l
|
||||
c.Invalidate()
|
||||
return
|
||||
}
|
||||
b, a, f := body.Bounds(), aside.Bounds(), buttons.Bounds()
|
||||
width := padSide + a.W + f.W + padSide
|
||||
if a.W > 0 {
|
||||
width += asideGap
|
||||
}
|
||||
height := padTop + b.H + footGap + max(a.H, f.H) + padBottom
|
||||
if w.icon != nil {
|
||||
height = max(height, padTop+iconSize+footGap+max(a.H, f.H)+padBottom)
|
||||
}
|
||||
size := [2]int{int(math.Ceil(float64(width))), int(math.Ceil(float64(height)))}
|
||||
if size != w.fitted {
|
||||
w.fitted = size
|
||||
go w.s.Fit(size[0], size[1])
|
||||
}
|
||||
}
|
||||
|
||||
// theme returns the look of the update window, that of the page of
|
||||
// package updater, over base.
|
||||
func theme(base *ui.Theme) *ui.Theme {
|
||||
t := *base
|
||||
light, dark := frontend.Background()
|
||||
if t.Dark {
|
||||
t.Background = ui.Hex(dark)
|
||||
t.Surface = ui.Hex("#5b5b5f")
|
||||
t.Border = ui.RGBA(255, 255, 255, 0.15)
|
||||
t.Text, t.TextMuted = ui.Hex("#f5f5f7"), ui.Hex("#98989d")
|
||||
t.Accent = ui.Hex("#0a84ff")
|
||||
} else {
|
||||
t.Background = ui.Hex(light)
|
||||
t.Surface = ui.Hex("#ffffff")
|
||||
t.Border = ui.RGBA(0, 0, 0, 0.15)
|
||||
t.Text, t.TextMuted = ui.Hex("#1d1d1f"), ui.Hex("#6e6e73")
|
||||
t.Accent = ui.Hex("#007aff")
|
||||
}
|
||||
black := ui.RGB(0, 0, 0)
|
||||
t.SurfaceHover, t.SurfacePressed = t.Surface.Mix(black, 0.05), t.Surface.Mix(black, 0.1)
|
||||
t.AccentHover, t.AccentPressed = t.Accent.Mix(black, 0.08), t.Accent.Mix(black, 0.16)
|
||||
t.Selection, t.Focus = t.Accent.Alpha(0.25), t.Accent.Alpha(0.55)
|
||||
return &t
|
||||
}
|
||||
|
||||
// panel returns the background of the release notes.
|
||||
func panel(t *ui.Theme) ui.Color {
|
||||
if t.Dark {
|
||||
return ui.Hex("#141414")
|
||||
}
|
||||
return ui.Hex("#ffffff")
|
||||
}
|
||||
|
||||
// track returns the background of code.
|
||||
func track(t *ui.Theme) ui.Color {
|
||||
if t.Dark {
|
||||
return ui.RGBA(255, 255, 255, 0.12)
|
||||
}
|
||||
return ui.RGBA(0, 0, 0, 0.1)
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
package native
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/png"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/internal/wait"
|
||||
"github.com/egoist/mygo/plugins/updater/internal/frontend"
|
||||
"github.com/egoist/mygo/plugins/updater/internal/markdown"
|
||||
"github.com/egoist/mygo/ui"
|
||||
)
|
||||
|
||||
// session is a frontend.Session that records what the window does.
|
||||
type session struct {
|
||||
texts frontend.Texts
|
||||
icon []byte
|
||||
|
||||
mu sync.Mutex
|
||||
view frontend.View
|
||||
changed chan struct{}
|
||||
responses []response
|
||||
fits chan [2]int
|
||||
}
|
||||
|
||||
type response struct {
|
||||
prompt int
|
||||
action frontend.Action
|
||||
automatic bool
|
||||
}
|
||||
|
||||
func newSession(v frontend.View) *session {
|
||||
return &session{
|
||||
texts: frontend.Texts{
|
||||
Title: "Software Update", ReleaseNotes: "Release Notes:",
|
||||
AutomaticDownloads: "Automatically download and install updates in the future", Lang: "en",
|
||||
},
|
||||
view: v, changed: make(chan struct{}), fits: make(chan [2]int, 100),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *session) NewWindow(mygo.Content) *mygo.Window { panic("no windows in tests") }
|
||||
func (s *session) Done() <-chan struct{} { return nil }
|
||||
func (s *session) Texts() frontend.Texts { return s.texts }
|
||||
func (s *session) Icon() []byte { return s.icon }
|
||||
func (s *session) User() bool { return true }
|
||||
func (s *session) Fit(width, height int) { s.fits <- [2]int{width, height} }
|
||||
|
||||
func (s *session) Current() (frontend.View, <-chan struct{}) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.view, s.changed
|
||||
}
|
||||
|
||||
func (s *session) set(v frontend.View) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.view = v
|
||||
close(s.changed)
|
||||
s.changed = make(chan struct{})
|
||||
}
|
||||
|
||||
func (s *session) Respond(prompt int, a frontend.Action, automatic bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.responses = append(s.responses, response{prompt, a, automatic})
|
||||
}
|
||||
|
||||
func (s *session) answers() []response {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return slices.Clone(s.responses)
|
||||
}
|
||||
|
||||
// fit returns the last size the window asked for.
|
||||
func (s *session) fit(t *testing.T) [2]int {
|
||||
t.Helper()
|
||||
var size [2]int
|
||||
select {
|
||||
case size = <-s.fits:
|
||||
case <-time.After(wait.Guard):
|
||||
t.Fatal("the window did not ask for a size")
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case size = <-s.fits:
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
return size
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func available(prompt int, notes string) frontend.View {
|
||||
return frontend.View{
|
||||
Prompt: prompt,
|
||||
Release: true,
|
||||
Title: "A new version of App is available!",
|
||||
Message: "App 2.0 is now available—you have 1.0. Would you like to install it now?",
|
||||
Notes: markdown.Parse(notes),
|
||||
Checkbox: true,
|
||||
Buttons: []frontend.Button{
|
||||
{Action: frontend.Skip, Label: "Skip This Version", Aside: true},
|
||||
{Action: frontend.Later, Label: "Remind Me Later", Cancel: true},
|
||||
{Action: frontend.Install, Label: "Install Update", Default: true},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func status(prompt int, title, message string) frontend.View {
|
||||
return frontend.View{Prompt: prompt, Title: title, Message: message, Buttons: []frontend.Button{
|
||||
{Action: frontend.OK, Label: "OK", Default: true, Cancel: true},
|
||||
}}
|
||||
}
|
||||
|
||||
func TestAvailable(t *testing.T) {
|
||||
s := newSession(available(1, "### Fixed\n\n- [The editor](https://example.com/editor) lost a **bug**\n - nested\n\n```\ncode\n```"))
|
||||
w := newWindow(s)
|
||||
tt := ui.NewTester(w.view, frontend.ReleaseWidth, frontend.ReleaseHeight)
|
||||
for _, want := range []string{"A new version of App is available!", "Release Notes:", "Fixed", "The editor lost a bug", "nested", "code", "•", "◦", s.texts.AutomaticDownloads} {
|
||||
if !tt.HasText(want) {
|
||||
t.Errorf("the window lacks %q: %q", want, tt.Texts())
|
||||
}
|
||||
}
|
||||
// The link of the notes opens in the browser.
|
||||
r, _ := tt.Find("The editor lost a bug")
|
||||
tt.ClickAt(r.X+10, r.Y+r.H/2)
|
||||
if got := tt.OpenedURLs(); !slices.Equal(got, []string{"https://example.com/editor"}) {
|
||||
t.Errorf("opened %v", got)
|
||||
}
|
||||
// The keyboard reaches it, and Enter opens it rather than installing.
|
||||
tt.Key(0, ui.KeyTab)
|
||||
if !tt.Focused(s.texts.AutomaticDownloads) {
|
||||
t.Fatal("Tab does not go from the link to the checkbox")
|
||||
}
|
||||
tt.Key(ui.Shift, ui.KeyTab)
|
||||
if !tt.Focused("The editor") {
|
||||
t.Fatal("Shift+Tab does not go back to the link of the notes")
|
||||
}
|
||||
tt.Key(0, ui.KeyEnter)
|
||||
if got := tt.OpenedURLs(); len(got) != 2 || len(s.answers()) != 0 {
|
||||
t.Fatalf("Enter on the link opened %v and answered %v", got, s.answers())
|
||||
}
|
||||
|
||||
if err := tt.Click(s.texts.AutomaticDownloads); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tt.Click("Install Update"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The answer disables the buttons until the next prompt.
|
||||
tt.Click("Remind Me Later")
|
||||
tt.Key(0, ui.KeyEscape)
|
||||
if got := s.answers(); !slices.Equal(got, []response{{1, frontend.Install, true}}) {
|
||||
t.Fatalf("responses %v", got)
|
||||
}
|
||||
|
||||
// A new prompt resets the checkbox, and takes an answer again.
|
||||
s.set(available(2, "Notes"))
|
||||
tt.Frame()
|
||||
tt.Key(0, ui.KeyEscape)
|
||||
if got := s.answers(); !slices.Equal(got[1:], []response{{2, frontend.Later, false}}) {
|
||||
t.Fatalf("responses %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeys(t *testing.T) {
|
||||
s := newSession(available(1, ""))
|
||||
tt := ui.NewTester(newWindow(s).view, frontend.StatusWidth, 200)
|
||||
tt.Key(0, ui.KeyEnter)
|
||||
if got := s.answers(); !slices.Equal(got, []response{{1, frontend.Install, false}}) {
|
||||
t.Fatalf("Enter answered %v", got)
|
||||
}
|
||||
s.set(available(2, ""))
|
||||
tt.Frame()
|
||||
tt.Key(0, ui.KeyEscape)
|
||||
if got := s.answers()[1:]; !slices.Equal(got, []response{{2, frontend.Later, false}}) {
|
||||
t.Fatalf("Escape answered %v", got)
|
||||
}
|
||||
// Disabled buttons do not answer.
|
||||
v := status(3, "Installing update…", "")
|
||||
v.Buttons = []frontend.Button{{Action: frontend.Cancel, Label: "Cancel", Cancel: true, Disabled: true}}
|
||||
s.set(v)
|
||||
tt.Frame()
|
||||
tt.Key(0, ui.KeyEscape)
|
||||
tt.Click("Cancel")
|
||||
if got := s.answers(); len(got) != 2 {
|
||||
t.Fatalf("a disabled button answered %v", got[2:])
|
||||
}
|
||||
}
|
||||
|
||||
func TestProgress(t *testing.T) {
|
||||
s := newSession(frontend.View{
|
||||
Prompt: 1, Title: "Downloading update…", Message: "5.0 MB of 20.0 MB", Bar: true, Progress: 0.25,
|
||||
Buttons: []frontend.Button{{Action: frontend.Cancel, Label: "Cancel", Cancel: true}},
|
||||
})
|
||||
tt := ui.NewTester(newWindow(s).view, frontend.StatusWidth, frontend.StatusHeight)
|
||||
if !tt.HasText("5.0 MB of 20.0 MB") {
|
||||
t.Errorf("texts %q", tt.Texts())
|
||||
}
|
||||
if err := tt.Click("Cancel"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := s.answers(); !slices.Equal(got, []response{{1, frontend.Cancel, false}}) {
|
||||
t.Fatalf("responses %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFit(t *testing.T) {
|
||||
s := newSession(status(1, "You’re up to date!", "App 1.0 is currently the newest version available."))
|
||||
tt := ui.NewTester(newWindow(s).view, frontend.StatusWidth, frontend.StatusHeight)
|
||||
short := s.fit(t)
|
||||
if short[0] > frontend.StatusWidth || short[1] < 60 || short[1] > frontend.StatusHeight+40 {
|
||||
t.Errorf("a short status asks for %v", short)
|
||||
}
|
||||
// A longer text asks for more height, at the window's width.
|
||||
v := status(2, "Update Error!", "An error occurred while checking for updates. Please try again later.")
|
||||
v.Detail = strings.Repeat("dial tcp: lookup updates.example.com: no such host ", 4)
|
||||
s.set(v)
|
||||
tt.Frame()
|
||||
long := s.fit(t)
|
||||
if long[1] <= short[1] {
|
||||
t.Errorf("an error asks for %v, a short status for %v", long, short)
|
||||
}
|
||||
// The session widens the window to the usual width at least.
|
||||
tt.SetSize(max(long[0], frontend.StatusWidth), long[1])
|
||||
select {
|
||||
case got := <-s.fits:
|
||||
t.Errorf("the window asks for %v once it has the size it asked for, %v", got, long)
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
}
|
||||
|
||||
// Long labels widen the window.
|
||||
v = available(3, "Notes")
|
||||
for i := range v.Buttons {
|
||||
v.Buttons[i].Label = strings.Repeat(v.Buttons[i].Label+" ", 3)
|
||||
}
|
||||
s.set(v)
|
||||
tt.SetSize(frontend.ReleaseWidth, frontend.ReleaseHeight)
|
||||
if wide := s.fit(t); wide[0] <= frontend.ReleaseWidth {
|
||||
t.Errorf("long labels ask for %v", wide)
|
||||
}
|
||||
}
|
||||
|
||||
// A long error shows in full, in a window that grows to fit it, and no
|
||||
// tooltip covers the title.
|
||||
func TestLongDetail(t *testing.T) {
|
||||
var icon bytes.Buffer
|
||||
png.Encode(&icon, image.NewRGBA(image.Rect(0, 0, 128, 128)))
|
||||
v := status(1, "Update Error!", "An error occurred while checking for updates. Please try again later.")
|
||||
v.Detail = "i/o timeout"
|
||||
s := newSession(v)
|
||||
s.icon = icon.Bytes()
|
||||
tt := ui.NewTester(newWindow(s).view, frontend.StatusWidth, frontend.StatusHeight)
|
||||
line, _ := tt.Find(v.Detail)
|
||||
// Three lines at the window's width.
|
||||
v.Detail = `mygo: checking for updates: Get "https://example.com/releases/latest/download/update-darwin-arm64.json": dial tcp 20.205.243.166:443: i/o timeout`
|
||||
s.set(v)
|
||||
tt.Frame()
|
||||
size := s.fit(t)
|
||||
tt.SetSize(max(size[0], frontend.StatusWidth), size[1])
|
||||
r, ok := tt.Find(v.Detail)
|
||||
if !ok || r.H < 2.5*line.H {
|
||||
t.Fatalf("the detail takes %v, a line of it %v", r, line)
|
||||
}
|
||||
if r.Y+r.H > float32(size[1]) {
|
||||
t.Errorf("the detail ends at %v in a window %v high", r.Y+r.H, size[1])
|
||||
}
|
||||
tt.Move(r.X+r.W/2, r.Y+r.H/2)
|
||||
tt.Advance(2 * time.Second)
|
||||
tt.Frame()
|
||||
if n := len(slices.DeleteFunc(tt.Texts(), func(s string) bool { return s != v.Detail })); n != 1 {
|
||||
t.Errorf("hovering the detail shows it %d times: %q", n, tt.Texts())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLayout(t *testing.T) {
|
||||
var icon bytes.Buffer
|
||||
png.Encode(&icon, image.NewRGBA(image.Rect(0, 0, 128, 128)))
|
||||
for _, rtl := range []bool{false, true} {
|
||||
s := newSession(available(1, "- An English item"))
|
||||
s.icon, s.texts.RTL = icon.Bytes(), rtl
|
||||
tt := ui.NewTester(newWindow(s).view, frontend.ReleaseWidth, frontend.ReleaseHeight)
|
||||
find := func(text string) ui.Rect {
|
||||
t.Helper()
|
||||
r, ok := tt.Find(text)
|
||||
if !ok {
|
||||
t.Fatalf("no %q", text)
|
||||
}
|
||||
return r
|
||||
}
|
||||
// The title, stretched across the column of texts, which the
|
||||
// icon leaves.
|
||||
body := find("A new version of App is available!")
|
||||
skip, later, install := find("Skip This Version"), find("Remind Me Later"), find("Install Update")
|
||||
// The checkbox's box sits between its label and the edge of the
|
||||
// texts' column where they start.
|
||||
label := find(s.texts.AutomaticDownloads)
|
||||
left, right := label.X-body.X, body.X+body.W-(label.X+label.W)
|
||||
bullet, item := find("•"), find("An English item")
|
||||
if !rtl {
|
||||
if body.X < padSide+iconSize+iconGap || skip.X > later.X || later.X > install.X {
|
||||
t.Errorf("left to right: texts at %v, Skip at %v, Later at %v, Install at %v", body, skip, later, install)
|
||||
}
|
||||
if left < 4 || left > right {
|
||||
t.Errorf("left to right: the checkbox's label at %v in %v", label, body)
|
||||
}
|
||||
} else {
|
||||
if body.X+body.W > frontend.ReleaseWidth-padSide-iconSize-iconGap+1 || skip.X < later.X || later.X < install.X {
|
||||
t.Errorf("right to left: texts at %v, Skip at %v, Later at %v, Install at %v", body, skip, later, install)
|
||||
}
|
||||
if right < 4 || right > left {
|
||||
t.Errorf("right to left: the checkbox's label at %v in %v", label, body)
|
||||
}
|
||||
}
|
||||
// Notes take the direction of their language, not the window's.
|
||||
if bullet.X > item.X {
|
||||
t.Errorf("rtl %v: the bullet of English notes at %v, the item at %v", rtl, bullet, item)
|
||||
}
|
||||
s.set(available(2, "- פריט בעברית"))
|
||||
tt.Frame()
|
||||
if bullet, item := find("•"), find("פריט בעברית"); bullet.X < item.X {
|
||||
t.Errorf("rtl %v: the bullet of Hebrew notes at %v, the item at %v", rtl, bullet, item)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user