152 lines
3.7 KiB
Go
152 lines
3.7 KiB
Go
package appui
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/egoist/mygo/ui"
|
||
|
|
"github.com/egoist/mygo/ui/mobile"
|
||
|
|
"github.com/openchamber/openchamber-native/internal/ocapi"
|
||
|
|
)
|
||
|
|
|
||
|
|
// sessionsView is the connected screen: the sessions list with its header.
|
||
|
|
func (a *App) sessionsView(c *ui.Context) {
|
||
|
|
t := c.Theme()
|
||
|
|
ui.Column(c).Fill().Background(t.Background).Children(func() {
|
||
|
|
mobile.TopBar(c, mobile.TopBarOptions{
|
||
|
|
Title: "会话",
|
||
|
|
Leading: func() {
|
||
|
|
// The single server entry: opens the instance list.
|
||
|
|
if a.iconButton(c, "server", "服务器").Clicked() {
|
||
|
|
a.instancesOpen = true
|
||
|
|
}
|
||
|
|
},
|
||
|
|
Trailing: func() {
|
||
|
|
if a.iconButton(c, "search", "搜索").Clicked() {
|
||
|
|
a.searchOpen = !a.searchOpen
|
||
|
|
if !a.searchOpen {
|
||
|
|
a.searchText = ""
|
||
|
|
a.loadSessions()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if a.iconButton(c, "plus", "新建会话").Clicked() {
|
||
|
|
a.newSessionForm()
|
||
|
|
}
|
||
|
|
},
|
||
|
|
})
|
||
|
|
if a.searchOpen {
|
||
|
|
ui.TextInput(c, &a.searchText).Placeholder("搜索会话").PaddingX(16).AutoFocus()
|
||
|
|
a.scheduleSearch()
|
||
|
|
}
|
||
|
|
if a.errorText != "" {
|
||
|
|
ui.Text(c, a.errorText).FontSize(13).TextColor(t.Danger).Padding(8, 16, 0, 16)
|
||
|
|
}
|
||
|
|
a.sessionList(c)
|
||
|
|
})
|
||
|
|
a.instancesSheet(c)
|
||
|
|
a.serverActionSheet(c)
|
||
|
|
a.deleteConfirmSheet(c)
|
||
|
|
a.newSessionSheet(c)
|
||
|
|
a.sessionActionSheet(c)
|
||
|
|
a.renameSheet(c)
|
||
|
|
a.sessionDeleteConfirm(c)
|
||
|
|
}
|
||
|
|
|
||
|
|
// iconButton is a small square button with an icon.
|
||
|
|
func (a *App) iconButton(c *ui.Context, icon, label string) ui.Element {
|
||
|
|
btn := ui.Button(c, "").Width(44).Height(44).Padding(0, 0)
|
||
|
|
btn.Children(func() {
|
||
|
|
bmp := iconBitmap(icon)
|
||
|
|
if bmp != nil {
|
||
|
|
ui.Image(c, bmp).Size(22, 22)
|
||
|
|
} else {
|
||
|
|
ui.Text(c, icon).FontSize(18)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
_ = label
|
||
|
|
return btn
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *App) sessionList(c *ui.Context) {
|
||
|
|
if a.sessionsLoad && len(a.sessions) == 0 {
|
||
|
|
ui.Column(c).Fill().Center().Children(func() {
|
||
|
|
ui.Spinner(c)
|
||
|
|
})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if len(a.sessions) == 0 && !a.sessionsLoad {
|
||
|
|
ui.Column(c).Fill().Center().Gap(8).Children(func() {
|
||
|
|
ui.Text(c, "暂无会话")
|
||
|
|
if ui.PrimaryButton(c, "新建会话").Clicked() {
|
||
|
|
a.newSessionForm()
|
||
|
|
}
|
||
|
|
})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
list := ui.List(c, &a.sessionListState, len(a.sessions))
|
||
|
|
list.ItemKey(func(i int) any { return a.sessions[i].ID })
|
||
|
|
list.Rows(func(r ui.ListRow) {
|
||
|
|
i := r.Index
|
||
|
|
s := a.sessions[i]
|
||
|
|
row := ui.Row(c).FillWidth().Gap(10).AlignItems(ui.Center).Padding(12, 16).
|
||
|
|
Role(ui.RoleButton).Label(s.Title)
|
||
|
|
row.Children(func() {
|
||
|
|
col := ui.Column(c).Grow(1).Basis(0).Gap(2)
|
||
|
|
col.Children(func() {
|
||
|
|
title := s.Title
|
||
|
|
if title == "" {
|
||
|
|
title = "(无标题会话)"
|
||
|
|
}
|
||
|
|
ui.Text(c, title).FontSize(16).FontWeight(600).MaxLines(1)
|
||
|
|
ui.Text(c, a.sessionSubtitle(s)).FontSize(13).TextColor(t(c).TextMuted).MaxLines(1)
|
||
|
|
})
|
||
|
|
if a.active[s.ID] {
|
||
|
|
ui.Text(c, "●").TextColor(t(c).Accent).FontSize(10)
|
||
|
|
}
|
||
|
|
if ui.Button(c, "⋯").Label("会话操作").Width(40).Clicked() {
|
||
|
|
a.openSessionActions(s)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
if row.Clicked() {
|
||
|
|
a.openChat(s)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
func t(c *ui.Context) *ui.Theme { return c.Theme() }
|
||
|
|
|
||
|
|
func (a *App) sessionSubtitle(s ocapi.Session) string {
|
||
|
|
parts := []string{}
|
||
|
|
if s.Agent != "" {
|
||
|
|
parts = append(parts, s.Agent)
|
||
|
|
}
|
||
|
|
if s.Model != nil {
|
||
|
|
parts = append(parts, a.modelName(s.Model))
|
||
|
|
}
|
||
|
|
if s.Time.Updated > 0 {
|
||
|
|
parts = append(parts, time.UnixMilli(s.Time.Updated).Format("01-02 15:04"))
|
||
|
|
}
|
||
|
|
out := ""
|
||
|
|
for i, p := range parts {
|
||
|
|
if i > 0 {
|
||
|
|
out += " · "
|
||
|
|
}
|
||
|
|
out += p
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
// scheduleSearch debounces search input.
|
||
|
|
func (a *App) scheduleSearch() {
|
||
|
|
if a.searchTimer != nil {
|
||
|
|
a.searchTimer.Stop()
|
||
|
|
}
|
||
|
|
a.searchTimer = time.AfterFunc(350*time.Millisecond, func() {
|
||
|
|
a.win.Update(func() {
|
||
|
|
if a.searchText != "" {
|
||
|
|
a.searchSessions(a.searchText)
|
||
|
|
} else {
|
||
|
|
a.loadSessions()
|
||
|
|
}
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}
|