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,212 @@
|
||||
// Package frontend is what the updater shares with the windows that show
|
||||
// its sessions: package updater shows them in a web page, package native
|
||||
// in native UI.
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
"github.com/egoist/mygo/plugins/updater/internal/markdown"
|
||||
)
|
||||
|
||||
// Session is a check for updates and what follows it, as its window sees
|
||||
// it.
|
||||
type Session interface {
|
||||
// NewWindow creates the session's window, hidden, showing content, or
|
||||
// a web page when content is nil. The session resizes it for its
|
||||
// views and closes it when it ends.
|
||||
NewWindow(content mygo.Content) *mygo.Window
|
||||
// Current returns the view to show and a channel closed when it
|
||||
// changes.
|
||||
Current() (View, <-chan struct{})
|
||||
// Done is closed when the session ends or its window closes.
|
||||
Done() <-chan struct{}
|
||||
// Respond answers the prompt of a view with a button, and tells
|
||||
// whether updates may be installed automatically from now on. Only
|
||||
// the first response to a prompt counts.
|
||||
Respond(prompt int, a Action, automaticDownloads bool)
|
||||
// Fit gives the window the width its buttons need, when they need
|
||||
// more than the usual width, and a status view the height of its
|
||||
// content, in DIPs.
|
||||
Fit(width, height int)
|
||||
// Texts returns the texts of the window that views leave out.
|
||||
Texts() Texts
|
||||
// Icon returns the PNG image of the window, or nil.
|
||||
Icon() []byte
|
||||
// User reports whether the user asked for the check: the window shows
|
||||
// in front.
|
||||
User() bool
|
||||
}
|
||||
|
||||
// Open creates the window of a session and shows it, at once or once it
|
||||
// is ready to.
|
||||
type Open func(s Session)
|
||||
|
||||
// Plugin returns the updater plugin with opts, an updater.Options, whose
|
||||
// windows open creates. Package updater sets it.
|
||||
var Plugin func(opts any, open Open) mygo.Plugin
|
||||
|
||||
// Show shows win as Sparkle shows its update alert: in front, with the
|
||||
// keyboard, when the user asked for the check or the app is active. A check
|
||||
// in the background takes the keyboard from no other app: on macOS win
|
||||
// waits until the app becomes active, unless the app has no Dock icon and
|
||||
// so may never be, which comes to the front; elsewhere it shows without
|
||||
// the keyboard.
|
||||
func Show(win *mygo.Window, user bool) { show(win, user) }
|
||||
|
||||
// The app as Show sees it, which tests swap.
|
||||
var (
|
||||
goos = runtime.GOOS
|
||||
appActive = mygo.App.IsActive
|
||||
activationPolicy = mygo.App.ActivationPolicy
|
||||
focusApp = mygo.App.Focus
|
||||
onAppActive = mygo.App.OnDidBecomeActive
|
||||
)
|
||||
|
||||
// window is what Show does with a *mygo.Window.
|
||||
type window interface {
|
||||
Show()
|
||||
ShowInactive()
|
||||
Focus()
|
||||
IsVisible() bool
|
||||
OnClosed(fn func()) (off func())
|
||||
}
|
||||
|
||||
func show(win window, user bool) {
|
||||
switch {
|
||||
case user || appActive():
|
||||
case goos != "darwin":
|
||||
win.ShowInactive()
|
||||
return
|
||||
case activationPolicy() != mygo.ActivationPolicyRegular:
|
||||
focusApp()
|
||||
default:
|
||||
showWhenActive(win)
|
||||
return
|
||||
}
|
||||
win.Show()
|
||||
win.Focus()
|
||||
}
|
||||
|
||||
// showWhenActive shows win in front once the app becomes active, unless
|
||||
// it closed first or the user had it shown already.
|
||||
func showWhenActive(win window) {
|
||||
var mu sync.Mutex
|
||||
var offs []func()
|
||||
done := false
|
||||
// end stops waiting, and shows win when front is set. Only its first
|
||||
// call counts.
|
||||
end := func(front bool) {
|
||||
mu.Lock()
|
||||
if done {
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
done = true
|
||||
for _, off := range offs {
|
||||
off()
|
||||
}
|
||||
mu.Unlock()
|
||||
if front && !win.IsVisible() {
|
||||
win.Show()
|
||||
win.Focus()
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
offs = append(offs, onAppActive(func() { end(true) }), win.OnClosed(func() { end(false) }))
|
||||
mu.Unlock()
|
||||
// The app may have become active before it was watched.
|
||||
if appActive() {
|
||||
end(true)
|
||||
}
|
||||
}
|
||||
|
||||
// Action is a button of the update window.
|
||||
type Action string
|
||||
|
||||
const (
|
||||
OK Action = "ok"
|
||||
Cancel Action = "cancel"
|
||||
Skip Action = "skip"
|
||||
Later Action = "later"
|
||||
Install Action = "install"
|
||||
Relaunch Action = "relaunch"
|
||||
// Close is the window closing, not a button.
|
||||
Close Action = "close"
|
||||
)
|
||||
|
||||
// View is what the update window shows.
|
||||
type View struct {
|
||||
// Prompt changes with the buttons: responses name the prompt they
|
||||
// answer, so that none answers a later one.
|
||||
Prompt int `json:"prompt"`
|
||||
// Release views show release notes, in a larger window.
|
||||
Release bool `json:"release,omitzero"`
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message,omitzero"`
|
||||
// Detail is the error of a failure.
|
||||
Detail string `json:"detail,omitzero"`
|
||||
// Notes are the release notes.
|
||||
Notes []markdown.Block `json:"-"`
|
||||
// Bar shows a progress bar with Progress, between 0 and 1, or -1 when
|
||||
// the progress is unknown.
|
||||
Bar bool `json:"bar,omitzero"`
|
||||
Progress float64 `json:"progress,omitzero"`
|
||||
// Checkbox offers to download and install updates automatically,
|
||||
// Checked or not.
|
||||
Checkbox bool `json:"checkbox,omitzero"`
|
||||
Checked bool `json:"checked,omitzero"`
|
||||
Buttons []Button `json:"buttons"`
|
||||
}
|
||||
|
||||
// Button is a button of a view.
|
||||
type Button struct {
|
||||
Action Action `json:"action"`
|
||||
Label string `json:"label"`
|
||||
// Default is the button of Enter, Cancel the one of Escape.
|
||||
Default bool `json:"default,omitzero"`
|
||||
Cancel bool `json:"cancel,omitzero"`
|
||||
// Aside sets the button apart, on the left.
|
||||
Aside bool `json:"aside,omitzero"`
|
||||
Disabled bool `json:"disabled,omitzero"`
|
||||
}
|
||||
|
||||
// Texts are the texts of the window around its views, in the language of
|
||||
// the window.
|
||||
type Texts struct {
|
||||
// Title is the title of the window, ReleaseNotes the heading of the
|
||||
// notes and AutomaticDownloads the label of the checkbox.
|
||||
Title, ReleaseNotes, AutomaticDownloads string
|
||||
// Lang is the language, such as "zh-Hans", and RTL is set for those
|
||||
// written from right to left.
|
||||
Lang string
|
||||
RTL bool
|
||||
}
|
||||
|
||||
// The size of the update window's content, for status views and for views
|
||||
// with release notes.
|
||||
const (
|
||||
StatusWidth, StatusHeight = 480, 148
|
||||
ReleaseWidth, ReleaseHeight = 620, 440
|
||||
)
|
||||
|
||||
// Size returns the size of the window's content for a status view or a
|
||||
// release view.
|
||||
func Size(release bool) (width, height int) {
|
||||
if release {
|
||||
return ReleaseWidth, ReleaseHeight
|
||||
}
|
||||
return StatusWidth, StatusHeight
|
||||
}
|
||||
|
||||
// Background returns the light and dark colors of the update window: the
|
||||
// gray of dialogs on macOS, and white elsewhere, where the title bar above
|
||||
// it is light.
|
||||
func Background() (light, dark string) {
|
||||
if runtime.GOOS == "darwin" {
|
||||
return "#ececec", "#1e1e1e"
|
||||
}
|
||||
return "#fff", "#1e1e1e"
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/egoist/mygo"
|
||||
)
|
||||
|
||||
type fakeWindow struct {
|
||||
visible, focused, inactive bool
|
||||
closed []func()
|
||||
}
|
||||
|
||||
func (w *fakeWindow) Show() { w.visible = true }
|
||||
func (w *fakeWindow) ShowInactive() { w.visible, w.inactive = true, true }
|
||||
func (w *fakeWindow) Focus() { w.focused = true }
|
||||
func (w *fakeWindow) IsVisible() bool { return w.visible }
|
||||
func (w *fakeWindow) OnClosed(fn func()) func() {
|
||||
w.closed = append(w.closed, fn)
|
||||
return func() { w.closed = nil }
|
||||
}
|
||||
|
||||
// fakeApp is the app as Show sees it, on GOOS os, until the test ends.
|
||||
type fakeApp struct {
|
||||
active, focused bool
|
||||
policy mygo.ActivationPolicy
|
||||
listeners []func()
|
||||
}
|
||||
|
||||
func useApp(t *testing.T, os string) *fakeApp {
|
||||
a := &fakeApp{policy: mygo.ActivationPolicyRegular}
|
||||
prev := [...]any{goos, appActive, activationPolicy, focusApp, onAppActive}
|
||||
goos = os
|
||||
appActive = func() bool { return a.active }
|
||||
activationPolicy = func() mygo.ActivationPolicy { return a.policy }
|
||||
focusApp = func() { a.focused = true }
|
||||
onAppActive = func(fn func()) func() {
|
||||
a.listeners = append(a.listeners, fn)
|
||||
return func() { a.listeners = nil }
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
goos = prev[0].(string)
|
||||
appActive = prev[1].(func() bool)
|
||||
activationPolicy = prev[2].(func() mygo.ActivationPolicy)
|
||||
focusApp = prev[3].(func())
|
||||
onAppActive = prev[4].(func(func()) func())
|
||||
})
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *fakeApp) activate() {
|
||||
a.active = true
|
||||
for _, fn := range a.listeners {
|
||||
fn()
|
||||
}
|
||||
}
|
||||
|
||||
// A background check while the app is active shows the window in front, with
|
||||
// the keyboard, as Sparkle does; it used to show it behind the app's window.
|
||||
func TestShowActiveApp(t *testing.T) {
|
||||
for _, os := range []string{"darwin", "windows", "linux"} {
|
||||
a := useApp(t, os)
|
||||
a.active = true
|
||||
w := &fakeWindow{}
|
||||
show(w, false)
|
||||
if !w.visible || !w.focused {
|
||||
t.Errorf("%s: visible %v, focused %v; want the window in front", os, w.visible, w.focused)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// On macOS a background check while another app is in front waits for the
|
||||
// app to become active, then shows the window in front, once.
|
||||
func TestShowWaitsForActivation(t *testing.T) {
|
||||
a := useApp(t, "darwin")
|
||||
w := &fakeWindow{}
|
||||
show(w, false)
|
||||
if w.visible {
|
||||
t.Fatal("the window showed while the app was in the background")
|
||||
}
|
||||
a.activate()
|
||||
if !w.visible || !w.focused || w.inactive {
|
||||
t.Fatalf("visible %v, focused %v, inactive %v once active; want it in front", w.visible, w.focused, w.inactive)
|
||||
}
|
||||
if len(a.listeners) != 0 || len(w.closed) != 0 {
|
||||
t.Error("still watching the app after showing the window")
|
||||
}
|
||||
}
|
||||
|
||||
// A window closed while it waits for the app, as when its session ends,
|
||||
// never shows.
|
||||
func TestShowClosedWhileWaiting(t *testing.T) {
|
||||
a := useApp(t, "darwin")
|
||||
w := &fakeWindow{}
|
||||
show(w, false)
|
||||
for _, fn := range w.closed {
|
||||
fn()
|
||||
}
|
||||
a.activate()
|
||||
if w.visible {
|
||||
t.Error("a closed window showed when the app became active")
|
||||
}
|
||||
}
|
||||
|
||||
// An app without a Dock icon may never become active by itself, so a
|
||||
// background check brings it to the front with the window, as Sparkle does.
|
||||
func TestShowAccessoryApp(t *testing.T) {
|
||||
a := useApp(t, "darwin")
|
||||
a.policy = mygo.ActivationPolicyAccessory
|
||||
w := &fakeWindow{}
|
||||
show(w, false)
|
||||
if !a.focused || !w.visible || !w.focused {
|
||||
t.Errorf("app focused %v, window visible %v, focused %v; want both in front", a.focused, w.visible, w.focused)
|
||||
}
|
||||
}
|
||||
|
||||
// Elsewhere a background check while another app is in front shows the
|
||||
// window without the keyboard.
|
||||
func TestShowInactiveElsewhere(t *testing.T) {
|
||||
useApp(t, "windows")
|
||||
w := &fakeWindow{}
|
||||
show(w, false)
|
||||
if !w.visible || w.focused {
|
||||
t.Errorf("visible %v, focused %v; want it shown without the keyboard", w.visible, w.focused)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user