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.
156 lines
5.0 KiB
Go
156 lines
5.0 KiB
Go
package mygo
|
|
|
|
import (
|
|
"encoding/json/v2"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"sync"
|
|
)
|
|
|
|
// The recent documents: those the app opened lately, newest first, which
|
|
// RecentDocuments returns and the Open Recent menu lists, kept in
|
|
// recent-documents.json in PathUserData. AddRecentDocument records them
|
|
// with the system too (the Dock menu on macOS, the jump list on Windows,
|
|
// the recently used files on Linux), as NSDocumentController does.
|
|
var recentDocs struct {
|
|
sync.Mutex
|
|
loaded bool
|
|
paths []string
|
|
}
|
|
|
|
// maxRecentDocuments is how many documents the list keeps, as
|
|
// NSDocumentController's maximumRecentDocumentCount.
|
|
const maxRecentDocuments = 10
|
|
|
|
func recentFile() (string, error) {
|
|
dir, err := App.Path(PathUserData)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(dir, "recent-documents.json"), nil
|
|
}
|
|
|
|
// loadRecentLocked reads the list once. recentDocs is locked.
|
|
func loadRecentLocked() {
|
|
if recentDocs.loaded {
|
|
return
|
|
}
|
|
recentDocs.loaded = true
|
|
if f, err := recentFile(); err == nil {
|
|
if data, err := readAppFile(f); err == nil {
|
|
_ = json.Unmarshal(data, &recentDocs.paths)
|
|
}
|
|
}
|
|
}
|
|
|
|
// AddRecentDocument records path as a document the user opened: it goes
|
|
// first in RecentDocuments and the Open Recent menu (RoleOpenRecent), and
|
|
// the system offers it again, in the app's Dock menu on macOS, in the
|
|
// recently used files of the user (the file chooser's and file manager's
|
|
// Recent) on Linux, and in the Recent category of the app's jump list on
|
|
// Windows. Documents (NewDocuments) record the files they open and save;
|
|
// other apps call it whenever they open a document or folder, including
|
|
// one the user chose with a file dialog:
|
|
//
|
|
// if err := mygo.App.AddRecentDocument(path); err != nil { … }
|
|
//
|
|
// A relative path is resolved against the working directory. Choosing an
|
|
// entry of the Dock menu, the jump list or the Open Recent menu opens the
|
|
// file with the app, which gets it through OnOpenFile, or opens it as a
|
|
// document.
|
|
func (a *Application) AddRecentDocument(path string) error {
|
|
needsApp("App.AddRecentDocument")
|
|
if path == "" {
|
|
return errors.New("mygo: App.AddRecentDocument needs a path")
|
|
}
|
|
abs, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return addRecentDocument(abs)
|
|
}
|
|
|
|
// addRecentDocument puts the absolute path first in the recent documents
|
|
// and records it with the system.
|
|
func addRecentDocument(path string) error {
|
|
recentDocs.Lock()
|
|
loadRecentLocked()
|
|
paths := slices.DeleteFunc(slices.Clone(recentDocs.paths), func(p string) bool { return p == path })
|
|
paths = slices.Insert(paths, 0, path)
|
|
if len(paths) > maxRecentDocuments {
|
|
paths = paths[:maxRecentDocuments]
|
|
}
|
|
recentDocs.paths = paths
|
|
err := saveRecentLocked()
|
|
recentDocs.Unlock()
|
|
return errors.Join(err, onMainValue(func() error { return backend().App().AddRecentDocument(path) }))
|
|
}
|
|
|
|
// ClearRecentDocuments empties RecentDocuments and the Open Recent menu,
|
|
// and removes the documents the app recorded with the system: the app's
|
|
// recent documents on macOS, the files the app recorded on Linux (the
|
|
// user's other recent files stay), and the Recent category of the app's
|
|
// jump list on Windows. The Open Recent menu's Clear Menu calls it.
|
|
func (a *Application) ClearRecentDocuments() error {
|
|
needsApp("App.ClearRecentDocuments")
|
|
recentDocs.Lock()
|
|
recentDocs.loaded = true
|
|
recentDocs.paths = nil
|
|
err := saveRecentLocked()
|
|
recentDocs.Unlock()
|
|
return errors.Join(err, onMainValue(func() error { return backend().App().ClearRecentDocuments() }))
|
|
}
|
|
|
|
// RecentDocuments returns the documents the app opened lately, newest
|
|
// first, which the Open Recent menu (RoleOpenRecent) lists: those of
|
|
// AddRecentDocument, also of earlier runs, up to ten. As in AppKit, files
|
|
// that no longer exist are left out, and come back if they do again.
|
|
func (a *Application) RecentDocuments() []string {
|
|
recentDocs.Lock()
|
|
loadRecentLocked()
|
|
paths := slices.Clone(recentDocs.paths)
|
|
recentDocs.Unlock()
|
|
return slices.DeleteFunc(paths, func(p string) bool {
|
|
_, err := os.Stat(p)
|
|
return errors.Is(err, os.ErrNotExist)
|
|
})
|
|
}
|
|
|
|
// saveRecentLocked writes the recent documents and updates the Open
|
|
// Recent menus. recentDocs is locked, so that writes keep the order of
|
|
// the changes.
|
|
func saveRecentLocked() error {
|
|
postMain(updateRecentMenus)
|
|
f, err := recentFile()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
paths := recentDocs.paths
|
|
return writeAppFile(f, func(w io.Writer) error { return json.MarshalWrite(w, paths) })
|
|
}
|
|
|
|
// openRecentDocument opens a document of the Open Recent menu: as a
|
|
// document of the kind of its extension, or the first kind, or else
|
|
// through OnOpenFile, as the system opens those of the Dock menu and the
|
|
// jump list. Main thread only.
|
|
func openRecentDocument(path string) {
|
|
_, open := linkedDocuments()
|
|
if open == nil {
|
|
fire1(&App.onOpenFile, path)
|
|
return
|
|
}
|
|
go open(path)
|
|
}
|
|
|
|
// addRecent records a document's file, as AddRecentDocument does, and logs
|
|
// what fails.
|
|
func addRecent(path string) {
|
|
if err := addRecentDocument(path); err != nil {
|
|
log.Printf("mygo: documents: %v", err)
|
|
}
|
|
}
|