Files
openchamber-android-native/third_party/mygo/internal/mobile/navigation.go
T
OpenChamber 6c0ee61750 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.
2026-11-18 13:22:59 +08:00

120 lines
2.9 KiB
Go

package mobile
import (
"github.com/egoist/mygo/internal/mobile/wire"
"github.com/egoist/mygo/internal/platform"
)
// The navigation of phones (platform.NavigationBackend): the host's tab
// bar, each tab a window, and stacks of windows pushed over others, under
// the host's navigation bar.
type tabBar struct {
b *Backend
id int
h platform.TabsHandler
}
func (b *Backend) NewTabs(tabs []platform.Tab, h platform.TabsHandler) (platform.TabBar, error) {
b.nextID++
t := &tabBar{b: b, id: b.nextID, h: h}
b.call(&wire.CreateTabs{ID: t.id}, nil)
old := b.front()
for i, tab := range tabs {
w := tab.Window.(*window)
w.visible = true
w.tabs, w.tabIndex = t, i
b.unshow(w)
b.shown = append(b.shown, w)
b.call(&wire.AddTab{ID: t.id, Window: w.id, Title: tab.Title, Symbol: tab.Symbol, Icon: tab.Icon}, nil)
}
// The first tab is in front.
first := tabs[0].Window.(*window)
b.unshow(first)
b.shown = append(b.shown, first)
b.tabs[t.id] = t
b.call(&wire.ShowTabs{ID: t.id}, nil)
b.refocus(old)
return t, nil
}
func (t *tabBar) Select(i int) {
t.fronted(i)
t.b.call(&wire.SelectTab{ID: t.id, Index: i}, nil)
}
// fronted makes the windows of tab i those in front, in the order they were.
func (t *tabBar) fronted(i int) {
b := t.b
old := b.front()
var mine, rest []*window
for _, w := range b.shown {
if w.tabs == t && w.tabIndex == i {
mine = append(mine, w)
} else {
rest = append(rest, w)
}
}
b.shown = append(rest, mine...)
b.refocus(old)
}
func (t *tabBar) SetBadge(i int, badge string) {
t.b.call(&wire.SetTabBadge{ID: t.id, Index: i, Badge: badge}, nil)
}
func (t *tabBar) Close() {
delete(t.b.tabs, t.id)
t.b.call(&wire.CloseTabs{ID: t.id}, nil)
}
func (b *Backend) PushWindow(parent, child platform.Window, animated bool) {
p, c := parent.(*window), child.(*window)
c.tabs, c.tabIndex = p.tabs, p.tabIndex
old := b.front()
c.visible = true
b.unshow(c)
b.shown = append(b.shown, c)
b.call(&wire.PushWindow{Parent: p.id, Child: c.id, Title: c.title, Animated: animated}, nil)
b.refocus(old)
}
func (b *Backend) PopWindow(w platform.Window, animated bool) {
b.call(&wire.PopWindow{ID: w.(*window).id, Animated: animated}, nil)
}
// handleNavigation handles the events of navigation, and reports whether
// op was one.
func (b *Backend) handleNavigation(op uint64, r *wire.Reader) bool {
switch op {
case wire.EventTabSelected:
var e wire.TabSelectedEvent
e.Decode(r)
if t := b.tabs[e.ID]; t != nil {
t.fronted(e.Index)
t.h.TabSelected(e.Index)
}
case wire.EventBarItemClicked:
var e wire.BarItemClickedEvent
e.Decode(r)
if w := b.windows[e.ID]; w != nil && e.Index >= 0 && e.Index < len(w.barItems) {
if fn := w.barItems[e.Index].OnClick; fn != nil {
fn()
}
}
case wire.EventPopped:
var e wire.PoppedEvent
e.Decode(r)
if w := b.windows[e.ID]; w != nil {
if h, ok := w.h.(platform.NavigationHandler); ok {
h.Popped()
} else {
w.Close()
}
}
default:
return false
}
return true
}