299 lines
8.4 KiB
Go
299 lines
8.4 KiB
Go
package appui
|
||||
|
|
|
|||
|
|
import (
|
|||
|
|
"net/url"
|
|||
|
|
|
|||
|
|
"github.com/egoist/mygo/ui"
|
|||
|
|
"github.com/egoist/mygo/ui/mobile"
|
|||
|
|
"github.com/openchamber/openchamber-native/internal/storage"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// serversView is the first screen: the list of saved servers, or the
|
|||
|
|
// connect form when one is being edited or needs a password.
|
|||
|
|
func (a *App) serversView(c *ui.Context) {
|
|||
|
|
t := c.Theme()
|
|||
|
|
if a.phase == "form" {
|
|||
|
|
a.connectFormView(c)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
ui.Column(c).Fill().Background(t.Background).Children(func() {
|
|||
|
|
header := ui.Column(c).FillWidth().Padding(16, 20, 8, 20)
|
|||
|
|
header.Children(func() {
|
|||
|
|
ui.Text(c, "OpenChamber").FontSize(28).Bold().Role(ui.RoleHeading).Level(1)
|
|||
|
|
ui.Text(c, "连接你的 OpenChamber 服务器").FontSize(14).TextColor(t.TextMuted)
|
|||
|
|
})
|
|||
|
|
if a.errorText != "" {
|
|||
|
|
ui.Text(c, a.errorText).FontSize(13).TextColor(t.Danger).Padding(12, 16, 4, 16)
|
|||
|
|
}
|
|||
|
|
scroll := ui.Scroll(c).Grow(1).PaddingX(16)
|
|||
|
|
scroll.Children(func() {
|
|||
|
|
ui.Column(c).FillWidth().Gap(10).Children(func() {
|
|||
|
|
conns := a.store.Connections()
|
|||
|
|
for i := range conns {
|
|||
|
|
conn := &conns[i]
|
|||
|
|
a.serverCard(c, conn)
|
|||
|
|
}
|
|||
|
|
add := ui.Row(c).FillWidth().Gap(10).AlignItems(ui.Center).Padding(14, 14).
|
|||
|
|
Radius(14).Border(0.5, t.Border).Role(ui.RoleButton).Label("添加服务器")
|
|||
|
|
add.Children(func() {
|
|||
|
|
ui.Text(c, "+").FontSize(20).TextColor(t.Accent).Width(30).TextAlign(ui.Center)
|
|||
|
|
ui.Text(c, "添加服务器").FontSize(16)
|
|||
|
|
})
|
|||
|
|
if add.Clicked() {
|
|||
|
|
a.newServerForm()
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (a *App) serverCard(c *ui.Context, conn *storage.Connection) {
|
|||
|
|
t := c.Theme()
|
|||
|
|
connected := a.client != nil && a.conn.ID == conn.ID
|
|||
|
|
card := ui.Row(c).FillWidth().Gap(10).AlignItems(ui.Center).Padding(14, 14).
|
|||
|
|
Background(t.Surface).Radius(14).Role(ui.RoleButton).Label("服务器 " + conn.Label)
|
|||
|
|
card.Children(func() {
|
|||
|
|
info := ui.Column(c).Grow(1).Basis(0).Gap(2)
|
|||
|
|
info.Children(func() {
|
|||
|
|
ui.Text(c, conn.Label).FontSize(16).FontWeight(600).MaxLines(1)
|
|||
|
|
ui.Text(c, conn.URL).FontSize(13).TextColor(t.TextMuted).MaxLines(1)
|
|||
|
|
})
|
|||
|
|
if connected {
|
|||
|
|
ui.Text(c, "已连接").FontSize(13).TextColor(t.Accent)
|
|||
|
|
} else if ui.Button(c, "连接").Clicked() {
|
|||
|
|
a.connectTo(*conn, "", false)
|
|||
|
|
}
|
|||
|
|
if ui.Button(c, "⋯").Label("服务器选项").Width(40).Clicked() {
|
|||
|
|
a.serverActions(conn)
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// serverActions opens a connection's menu: connect, edit, delete.
|
|||
|
|
func (a *App) serverActions(conn *storage.Connection) {
|
|||
|
|
open := true
|
|||
|
|
a.serverActionOpen = &open
|
|||
|
|
a.serverActionTarget = conn
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// serverActionSheet draws the open connection menu.
|
|||
|
|
func (a *App) serverActionSheet(c *ui.Context) {
|
|||
|
|
if a.serverActionOpen == nil || !*a.serverActionOpen {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
conn := a.serverActionTarget
|
|||
|
|
if conn == nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
mobile.ActionSheet(c, a.serverActionOpen, mobile.ActionSheetOptions{
|
|||
|
|
Title: conn.Label,
|
|||
|
|
Cancel: "取消",
|
|||
|
|
}, mobile.Action{
|
|||
|
|
Label: "连接",
|
|||
|
|
OnTap: func() { a.connectTo(*conn, "", false) },
|
|||
|
|
}, mobile.Action{
|
|||
|
|
Label: "编辑",
|
|||
|
|
OnTap: func() { a.editServerForm(conn) },
|
|||
|
|
}, mobile.Action{
|
|||
|
|
Label: "删除",
|
|||
|
|
Destructive: true,
|
|||
|
|
OnTap: func() { a.deleteServer(conn) },
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// deleteServer asks and removes a saved server.
|
|||
|
|
func (a *App) deleteServer(conn *storage.Connection) {
|
|||
|
|
a.confirmDelete = true
|
|||
|
|
a.deleteTarget = conn
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// deleteConfirmSheet asks "删除服务器?".
|
|||
|
|
func (a *App) deleteConfirmSheet(c *ui.Context) {
|
|||
|
|
if !a.confirmDelete {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
buttons := ui.AlertDialog(c, &a.confirmDelete, "删除服务器?",
|
|||
|
|
"“"+a.deleteTarget.Label+"”将从列表移除,设备令牌也会删除。", "取消", "删除")
|
|||
|
|
if buttons == 1 {
|
|||
|
|
if a.client != nil && a.conn.ID == a.deleteTarget.ID {
|
|||
|
|
a.disconnect()
|
|||
|
|
}
|
|||
|
|
_ = a.store.ClearToken(*a.deleteTarget)
|
|||
|
|
conns := a.store.Connections()
|
|||
|
|
next := conns[:0]
|
|||
|
|
for _, x := range conns {
|
|||
|
|
if x.ID != a.deleteTarget.ID {
|
|||
|
|
next = append(next, x)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
a.store.SaveConnections(next)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// connectFormView is the add/edit form with the password field.
|
|||
|
|
func (a *App) connectFormView(c *ui.Context) {
|
|||
|
|
t := c.Theme()
|
|||
|
|
ui.Column(c).Fill().Background(t.Background).Children(func() {
|
|||
|
|
head := ui.Row(c).FillWidth().Padding(12, 12).AlignItems(ui.Center).Gap(8)
|
|||
|
|
head.Children(func() {
|
|||
|
|
if ui.Button(c, "‹").Label("返回").Width(40).Clicked() {
|
|||
|
|
a.phase = ""
|
|||
|
|
a.formEditing = nil
|
|||
|
|
a.formError = ""
|
|||
|
|
}
|
|||
|
|
ui.Text(c, a.formTitle).FontSize(18).FontWeight(600).Grow(1)
|
|||
|
|
})
|
|||
|
|
scroll := ui.Scroll(c).Grow(1).PaddingX(16)
|
|||
|
|
scroll.Children(func() {
|
|||
|
|
ui.Column(c).FillWidth().Gap(10).Children(func() {
|
|||
|
|
a.fieldLabel(c, "名称")
|
|||
|
|
ui.TextInput(c, &a.formLabel).Placeholder("如 我的电脑")
|
|||
|
|
a.fieldLabel(c, "服务器地址")
|
|||
|
|
ui.TextInput(c, &a.formURL).Placeholder("http://192.168.1.10:3000").Keyboard(ui.KeyboardURL)
|
|||
|
|
a.fieldLabel(c, "代理(可选)")
|
|||
|
|
ui.TextInput(c, &a.formProxy).Placeholder("http://… 或 socks5://…").Keyboard(ui.KeyboardURL)
|
|||
|
|
a.fieldLabel(c, "自签证书指纹(可选)")
|
|||
|
|
ui.TextInput(c, &a.formFinger).Placeholder("SHA-256 十六进制")
|
|||
|
|
row := ui.Row(c).FillWidth().AlignItems(ui.Center)
|
|||
|
|
row.Children(func() {
|
|||
|
|
ui.Text(c, "信任自签名证书").Grow(1).FontSize(15)
|
|||
|
|
ui.Switch(c, &a.formInsecure)
|
|||
|
|
})
|
|||
|
|
if a.formEditing != nil {
|
|||
|
|
a.fieldLabel(c, "密码")
|
|||
|
|
ui.TextInput(c, &a.formPassword).Password().Placeholder("服务器密码")
|
|||
|
|
}
|
|||
|
|
if a.formError != "" {
|
|||
|
|
ui.Text(c, a.formError).FontSize(13).TextColor(t.Danger)
|
|||
|
|
}
|
|||
|
|
if a.busy {
|
|||
|
|
ui.Row(c).FillWidth().Gap(10).AlignItems(ui.Center).Padding(4, 0).Children(func() {
|
|||
|
|
ui.Spinner(c)
|
|||
|
|
ui.Text(c, "连接中…").TextColor(t.TextMuted)
|
|||
|
|
})
|
|||
|
|
} else if ui.PrimaryButton(c, "连接").FillWidth().Padding(8, 0).Clicked() {
|
|||
|
|
a.submitForm()
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (a *App) fieldLabel(c *ui.Context, s string) {
|
|||
|
|
t := c.Theme()
|
|||
|
|
ui.Text(c, s).FontSize(13).TextColor(t.TextMuted)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (a *App) submitForm() {
|
|||
|
|
conn := a.formEditing
|
|||
|
|
if conn == nil {
|
|||
|
|
conn = &storage.Connection{ID: storage.NewID()}
|
|||
|
|
}
|
|||
|
|
conn.Label = trimOr(a.formLabel, hostOf(a.formURL))
|
|||
|
|
conn.URL = a.formURL
|
|||
|
|
conn.Proxy = a.formProxy
|
|||
|
|
conn.Insecure = a.formInsecure
|
|||
|
|
conn.Fingerprint = a.formFinger
|
|||
|
|
a.connectTo(*conn, a.formPassword, false)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (a *App) editServerForm(conn *storage.Connection) {
|
|||
|
|
a.phase = "form"
|
|||
|
|
a.formEditing = conn
|
|||
|
|
a.formTitle = "编辑 " + conn.Label
|
|||
|
|
a.formLabel = conn.Label
|
|||
|
|
a.formURL = conn.URL
|
|||
|
|
a.formProxy = conn.Proxy
|
|||
|
|
a.formInsecure = conn.Insecure
|
|||
|
|
a.formFinger = conn.Fingerprint
|
|||
|
|
a.formPassword = ""
|
|||
|
|
a.formError = ""
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (a *App) newServerForm() {
|
|||
|
|
a.phase = "form"
|
|||
|
|
a.formEditing = nil
|
|||
|
|
a.formTitle = "添加服务器"
|
|||
|
|
a.formLabel = ""
|
|||
|
|
a.formURL = ""
|
|||
|
|
a.formProxy = ""
|
|||
|
|
a.formInsecure = false
|
|||
|
|
a.formFinger = ""
|
|||
|
|
a.formPassword = ""
|
|||
|
|
a.formError = ""
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// -------------------------------------------------------------- instances
|
|||
|
|
|
|||
|
|
// instancesSheet shows the instance list: switch servers, disconnect, add.
|
|||
|
|
func (a *App) instancesSheet(c *ui.Context) {
|
|||
|
|
if !a.instancesOpen {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
t := c.Theme()
|
|||
|
|
mobile.Sheet(c, &a.instancesOpen, mobile.SheetOptions{
|
|||
|
|
Detents: []float32{0.6},
|
|||
|
|
Label: "服务器",
|
|||
|
|
}, func() {
|
|||
|
|
ui.Column(c).FillWidth().Padding(8, 16, 24, 16).Gap(2).Children(func() {
|
|||
|
|
ui.Text(c, "服务器").FontSize(20).Bold().Padding(0, 0, 0, 6)
|
|||
|
|
conns := a.store.Connections()
|
|||
|
|
for i := range conns {
|
|||
|
|
conn := &conns[i]
|
|||
|
|
connected := a.client != nil && a.conn.ID == conn.ID
|
|||
|
|
row := mobile.ListRow(c, mobile.RowOptions{
|
|||
|
|
Title: conn.Label,
|
|||
|
|
Subtitle: conn.URL,
|
|||
|
|
Trailing: func() {
|
|||
|
|
if connected {
|
|||
|
|
ui.Text(c, "已连接").FontSize(13).TextColor(t.Accent)
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
if row.Clicked() && !connected {
|
|||
|
|
a.instancesOpen = false
|
|||
|
|
a.switchTo(*conn)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
row := mobile.ListRow(c, mobile.RowOptions{Title: "+ 添加服务器"})
|
|||
|
|
if row.Clicked() {
|
|||
|
|
a.instancesOpen = false
|
|||
|
|
a.newServerForm()
|
|||
|
|
}
|
|||
|
|
if a.client != nil {
|
|||
|
|
row := mobile.ListRow(c, mobile.RowOptions{Title: "断开当前连接"})
|
|||
|
|
if row.Clicked() {
|
|||
|
|
a.instancesOpen = false
|
|||
|
|
a.disconnect()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (a *App) switchTo(conn storage.Connection) {
|
|||
|
|
if a.chat != nil {
|
|||
|
|
a.closeChat()
|
|||
|
|
}
|
|||
|
|
a.stopStream()
|
|||
|
|
a.client = nil
|
|||
|
|
a.conn = storage.Connection{}
|
|||
|
|
a.phase = ""
|
|||
|
|
a.sessions = nil
|
|||
|
|
a.connectTo(conn, "", true)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func trimOr(s, fallback string) string {
|
|||
|
|
if s == "" {
|
|||
|
|
return fallback
|
|||
|
|
}
|
|||
|
|
return s
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func hostOf(raw string) string {
|
|||
|
|
u, err := url.Parse(raw)
|
|||
|
|
if err != nil || u.Host == "" {
|
|||
|
|
return raw
|
|||
|
|
}
|
|||
|
|
return u.Host
|
|||
|
|
}
|