package ui import ( "image" "image/color" "strconv" "strings" "time" "unicode" "gioui.org/font" "gioui.org/io/event" "gioui.org/io/pointer" "gioui.org/layout" "gioui.org/op" "gioui.org/op/clip" "gioui.org/unit" "gioui.org/widget" "gioui.org/widget/material" "100gex/internal/rex" "100gex/internal/theme" "100gex/internal/workspace" ) // The sidebar lists every host, with its sessions under it, for finding // work and going back to it. Collapsed, it is a rail of the hosts' // initials. Over it, in the title bar's corner, are its toggle and a // field that opens the session switcher. // railWide is the collapsed sidebar's width. const railWide unit.Dp = 56 // sidebar is the sidebar's drawing state. type sidebar struct { collapsed bool list widget.List toggle widget.Clickable jump widget.Clickable addHost widget.Clickable hosts map[string]*hostWidgets // by hostEntry.key sessions map[sessionKey]*sessionWidgets renaming sessionKey // the session whose label is being edited renameEditor widget.Editor renameFocus bool // give the editor focus on the next frame folded map[string]bool // hosts folded or unfolded by hand, by key } // hostWidgets are the clickables of one host's heading, or its rail // button. type hostWidgets struct { head, add, rail widget.Clickable menu tag // right clicks on the heading } // sessionKey names a session row in the sidebar: a session on a server. type sessionKey struct { srv *server id string } // sidebarWide is the sidebar's width, in pixels: the rail's, if collapsed. func (u *UI) sidebarWide(gtx layout.Context) int { if u.side.collapsed { return gtx.Dp(railWide) } return gtx.Dp(theme.SidebarWide) } func (u *UI) hostWidgets(key string) *hostWidgets { if u.side.hosts == nil { u.side.hosts = make(map[string]*hostWidgets) } w := u.side.hosts[key] if w == nil { w = new(hostWidgets) u.side.hosts[key] = w } return w } // isFolded reports whether a host's sessions are hidden: as folded by hand, // or else when it's offline. func (u *UI) isFolded(e hostEntry) bool { if f, ok := u.side.folded[e.key]; ok { return f } return e.state == hostOffline || e.state == hostIdle } func (u *UI) setFolded(e hostEntry, folded bool) { if u.side.folded == nil { u.side.folded = make(map[string]bool) } u.side.folded[e.key] = folded } // rowKind is what a row of the sidebar shows. type rowKind int const ( rowHost rowKind = iota // a host's heading rowNote // a line about a host, such as why it's offline rowSession // one of a host's sessions ) // sidebarRow is a row of the sidebar. type sidebarRow struct { kind rowKind host *hostEntry session *rex.Session note string noteFg color.NRGBA } // sidebarRows lists the sidebar's rows: each host's heading, then, unless // it's folded, its sessions, or a note on why there are none. func (u *UI) sidebarRows(entries []hostEntry, now time.Time) []sidebarRow { var rows []sidebarRow for i := range entries { e := &entries[i] rows = append(rows, sidebarRow{kind: rowHost, host: e}) if u.isFolded(*e) { continue } var sessions []rex.Session if e.srv != nil { sessions = e.srv.ws.Sessions() } switch { case e.state == hostOffline: note := "Offline" if d := e.retry.Sub(now).Round(time.Second); d > 0 { note += " · trying again in " + d.String() } rows = append(rows, sidebarRow{kind: rowNote, host: e, note: note, noteFg: theme.Red}) if e.err != nil { rows = append(rows, sidebarRow{kind: rowNote, host: e, note: e.err.Error(), noteFg: theme.Muted}) } continue case len(sessions) == 0 && e.state == hostConnecting: rows = append(rows, sidebarRow{kind: rowNote, host: e, note: "Connecting…", noteFg: theme.Muted}) case len(sessions) == 0 && e.state == hostOnline: rows = append(rows, sidebarRow{kind: rowNote, host: e, note: "No sessions", noteFg: theme.Muted}) } for j := range sessions { rows = append(rows, sidebarRow{kind: rowSession, host: e, session: &sessions[j]}) } } return rows } // openHost acts on a click on a host: shows it, or tries it again. func (u *UI) openHost(e hostEntry) { switch { case e.srv != nil && e.state == hostOnline: u.switchTo(e.srv, "") case e.state == hostOffline || e.state == hostIdle: u.retryHost(e.target) } } // layoutSidebar draws the hosts and their sessions, or the rail. func (u *UI) layoutSidebar(gtx layout.Context, r image.Rectangle) { border := gtx.Dp(theme.Border) theme.Fill(gtx, theme.White, r) theme.Fill(gtx, theme.Ink, image.Rect(r.Max.X-border, r.Min.Y, r.Max.X, r.Max.Y)) entries := u.hosts() if u.side.collapsed { u.layoutRail(gtx, entries, image.Rect(r.Min.X, r.Min.Y, r.Max.X-border, r.Max.Y)) return } if u.side.addHost.Clicked(gtx) { u.openPalette(paletteMachines) } rows := u.sidebarRows(entries, gtx.Now) current := "" if u.session != nil { current = u.session.ID() } // Clicks first, so that a fold takes effect in this frame's rows. for _, e := range entries { w := u.hostWidgets(e.key) if w.head.Clicked(gtx) { folded := u.isFolded(e) u.setFolded(e, !folded) if folded && e.state == hostOffline { u.retryHost(e.target) } } if w.add.Clicked(gtx) && e.srv != nil { u.newSessionOn(e.srv) } for { ev, ok := gtx.Event(pointer.Filter{Target: &w.menu, Kinds: pointer.Press}) if !ok { break } if pe, ok := ev.(pointer.Event); ok && pe.Buttons.Contain(pointer.ButtonSecondary) { u.openHostMenu(e) } } } // A click shows a session, a double click renames it, and a right // click opens its menu. renamed := false for _, row := range rows { if row.kind != rowSession { continue } k := sessionKey{row.host.srv, row.session.SessionID} w := u.sessionWidgets(k) for { c, ok := w.click.Update(gtx) if !ok { break } if c.NumClicks >= 2 { u.startRename(k) } else if u.side.renaming != k { u.switchTo(k.srv, k.id) } } for { ev, ok := gtx.Event(pointer.Filter{Target: &w.menu, Kinds: pointer.Press}) if !ok { break } if pe, ok := ev.(pointer.Event); ok && pe.Buttons.Contain(pointer.ButtonSecondary) { u.openSessionMenu(k) } } renamed = renamed || u.side.renaming == k } if u.renaming() && !renamed { u.side.renaming = sessionKey{} // the session is gone, or hidden } rows = u.sidebarRows(entries, gtx.Now) at(gtx, image.Rect(r.Min.X, r.Min.Y, r.Max.X-border, r.Max.Y), func(gtx layout.Context) layout.Dimensions { return layout.Inset{Top: 14, Left: 12, Right: 12 + theme.Shadow, Bottom: 16}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { u.side.list.Axis = layout.Vertical return layout.Flex{Axis: layout.Vertical}.Layout(gtx, layout.Flexed(1, func(gtx layout.Context) layout.Dimensions { return material.List(u.theme.Material, &u.side.list).Layout(gtx, len(rows), func(gtx layout.Context, i int) layout.Dimensions { row := rows[i] switch row.kind { case rowHost: top := unit.Dp(14) if i == 0 { top = 0 } return layout.Inset{Top: top, Bottom: 6}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { return u.hostHeading(gtx, *row.host) }) case rowNote: return layout.Inset{Left: 18, Bottom: 4}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { l := u.theme.Label(12, row.note, row.noteFg, font.Medium) l.MaxLines = 3 return l.Layout(gtx) }) } return layout.Inset{Bottom: 6}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { isCurrent := row.host.srv == u.srv && row.session.SessionID == current return u.sessionRow(gtx, row.host.srv, *row.session, isCurrent) }) }) }), layout.Rigid(layout.Spacer{Height: 12}.Layout), layout.Rigid(theme.Button{Theme: u.theme, Clickable: &u.side.addHost, Label: "Add host…"}.Layout), ) }) }) } // hostHeading draws a host's name, with a square saying how its // connection stands, and a button for a new session on it. The host shown // is highlighted. A right click opens its menu. func (u *UI) hostHeading(gtx layout.Context, e hostEntry) layout.Dimensions { w := u.hostWidgets(e.key) shown := e.srv != nil && e.srv == u.srv fg := theme.Ink if e.state == hostOffline || e.state == hostIdle { fg = theme.Muted } m := op.Record(gtx.Ops) gtx.Constraints.Min.X = gtx.Constraints.Max.X dims := layout.Flex{Alignment: layout.Middle}.Layout(gtx, layout.Flexed(1, func(gtx layout.Context) layout.Dimensions { return w.head.Layout(gtx, func(gtx layout.Context) layout.Dimensions { gtx.Constraints.Min.X = gtx.Constraints.Max.X m := op.Record(gtx.Ops) dims := layout.Inset{Top: 3, Bottom: 3, Left: 2, Right: 4}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { gtx.Constraints.Min.X = gtx.Constraints.Max.X children := []layout.FlexChild{ layout.Rigid(func(gtx layout.Context) layout.Dimensions { return stateSquare(gtx, e.state) }), layout.Rigid(layout.Spacer{Width: 8}.Layout), layout.Flexed(1, func(gtx layout.Context) layout.Dimensions { bar := color.NRGBA{} if shown { bar = theme.Yellow } l := u.theme.Display(17, e.name, fg) l.MaxLines = 1 return theme.HighlightedIn(gtx, l, bar) }), } if u.isFolded(e) && e.srv != nil { // Folded, it says how many sessions it hides, and // whether one asks for attention. if u.hostNeedsYou(e) { children = append(children, layout.Rigid(attentionSquare), layout.Rigid(layout.Spacer{Width: 6}.Layout)) } if n := len(e.srv.ws.Sessions()); n > 0 { children = append(children, layout.Rigid(u.theme.Label(12, strconv.Itoa(n), theme.Muted, font.Bold).Layout)) } } return layout.Flex{Alignment: layout.Middle}.Layout(gtx, children...) }) call := m.Stop() if w.head.Hovered() { theme.Fill(gtx, theme.Hover, image.Rectangle{Max: dims.Size}) } call.Add(gtx.Ops) return dims }) }), layout.Rigid(func(gtx layout.Context) layout.Dimensions { if e.state != hostOnline { return layout.Dimensions{} } return layout.Inset{Left: 6}.Layout(gtx, theme.IconButton{Clickable: &w.add, Icon: theme.IconPlus}.Layout) }), ) call := m.Stop() defer clip.Rect{Max: dims.Size}.Push(gtx.Ops).Pop() event.Op(gtx.Ops, &w.menu) call.Add(gtx.Ops) return dims } // stateSquare is a small square in the colour of a connection's state: // green when connected, lavender while connecting, red when offline, and // empty when not tried. func stateSquare(gtx layout.Context, s hostState) layout.Dimensions { side := gtx.Dp(10) r := image.Rect(0, 0, side, side) var fill color.NRGBA // none when not tried switch s { case hostOnline: fill = theme.Green case hostConnecting: fill = theme.Lavender case hostOffline: fill = theme.Red } theme.Bordered(gtx, fill, theme.Ink, r, gtx.Dp(theme.Border)) return layout.Dimensions{Size: r.Max} } // sessionRow draws a session: its label, what runs in it, who else has // it open, and a tag for what happened in it since it was last shown, or // what a program in it says. func (u *UI) sessionRow(gtx layout.Context, srv *server, s rex.Session, current bool) layout.Dimensions { k := sessionKey{srv, s.SessionID} w := u.sessionWidgets(k) click := &w.click sum := srv.ws.Summary(s.SessionID) detail := strings.Join(sum.Programs, " · ") if len(sum.Elsewhere) > 0 { on := "on " + strings.Join(sum.Elsewhere, ", ") if detail != "" { on = " — " + on } detail += on } tag, tagFill, tagFg := sessionTag(sum) rec := op.Record(gtx.Ops) dims := click.Layout(gtx, func(gtx layout.Context) layout.Dimensions { gtx.Constraints.Min.X = gtx.Constraints.Max.X content := func(gtx layout.Context) layout.Dimensions { weight := font.Medium if current { weight = font.Bold } label := u.theme.Label(14, s.Label, theme.Ink, weight) label.MaxLines = 1 top := []layout.FlexChild{layout.Flexed(1, label.Layout)} if u.side.renaming == k { top[0] = layout.Flexed(1, u.renameField) } if tag != "" { top = append(top, layout.Rigid(func(gtx layout.Context) layout.Dimensions { return layout.Inset{Left: 6}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { return theme.Tag(gtx, u.theme, tag, tagFill, tagFg) }) })) } children := []layout.FlexChild{layout.Rigid(func(gtx layout.Context) layout.Dimensions { return layout.Flex{Alignment: layout.Middle}.Layout(gtx, top...) })} if detail != "" { l := u.theme.Label(12, detail, theme.Muted, font.Normal) l.MaxLines = 1 children = append(children, layout.Rigid(l.Layout)) } return layout.Flex{Axis: layout.Vertical}.Layout(gtx, children...) } padding := layout.Inset{Top: 6, Bottom: 6, Left: 10, Right: 10} if current { return theme.Box{Fill: theme.White, Shadow: theme.Shadow, Accent: theme.Purple, Padding: padding}.Layout(gtx, content) } // The other rows keep room for the accent bar, so that a row's // text doesn't move when it becomes the current one. padding.Left += theme.AccentWide m := op.Record(gtx.Ops) dims := padding.Layout(gtx, content) call := m.Stop() if click.Hovered() { theme.Fill(gtx, theme.Hover, image.Rectangle{Max: dims.Size}) } call.Add(gtx.Ops) return dims }) call := rec.Stop() defer clip.Rect{Max: dims.Size}.Push(gtx.Ops).Pop() event.Op(gtx.Ops, &w.menu) call.Add(gtx.Ops) return dims } // sessionTag is the tag on a session's row, for the most pressing thing // about it: a program waiting for the person, one that failed, a bell, one // that exited, or one working. It is "" if there is nothing. func sessionTag(s workspace.Summary) (tag string, fill, fg color.NRGBA) { switch { case s.Waiting: return "WAITING", theme.Yellow, theme.Ink case s.Failed: return "FAILED", theme.Red, theme.White case s.Bell: return "BELL", theme.Yellow, theme.Ink case s.Exited: return "EXITED", theme.Cream, theme.Ink case s.Working: return "WORKING", theme.Lavender, theme.Ink } return "", color.NRGBA{}, color.NRGBA{} } // needsYou reports whether a session's row asks for attention: a program // is waiting, failed, or rang the bell. func needsYou(s workspace.Summary) bool { return s.Waiting || s.Failed || s.Bell } // layoutRail draws the collapsed sidebar: a button per host, with its // initial, in the colours of its state. A click shows the host. func (u *UI) layoutRail(gtx layout.Context, entries []hostEntry, r image.Rectangle) { at(gtx, r, func(gtx layout.Context) layout.Dimensions { children := []layout.FlexChild{} for _, e := range entries { w := u.hostWidgets(e.key) if w.rail.Clicked(gtx) { u.openHost(e) } children = append(children, layout.Rigid(func(gtx layout.Context) layout.Dimensions { return layout.Inset{Bottom: 10}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { return u.railButton(gtx, e, &w.rail) }) })) } return layout.Inset{Top: 14}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { return layout.N.Layout(gtx, func(gtx layout.Context) layout.Dimensions { return layout.Flex{Axis: layout.Vertical, Alignment: layout.Middle}.Layout(gtx, children...) }) }) }) } // railButton is a host's initial in a box: purple for the host shown, and // with a square in the corner for its state, unless it's connected. func (u *UI) railButton(gtx layout.Context, e hostEntry, click *widget.Clickable) layout.Dimensions { shown := e.srv != nil && e.srv == u.srv return click.Layout(gtx, func(gtx layout.Context) layout.Dimensions { side := gtx.Dp(32) box := image.Rect(0, 0, side, side) fill, fg := theme.White, theme.Ink switch { case shown: fill, fg = theme.Purple, theme.White case click.Hovered(): fill = theme.Hover } if e.state == hostOffline || e.state == hostIdle { fg = theme.Muted } shadow := gtx.Dp(theme.Shadow) / 2 theme.Fill(gtx, theme.Ink, box.Add(image.Pt(shadow, shadow))) theme.Bordered(gtx, fill, theme.Ink, box, gtx.Dp(theme.Border)) gtx.Constraints = layout.Exact(box.Size()) layout.Center.Layout(gtx, u.theme.Display(17, initial(e.name), fg).Layout) corner := op.Offset(image.Pt(side-gtx.Dp(7), -gtx.Dp(3))).Push(gtx.Ops) switch { case e.state != hostOnline: stateSquare(gtx, e.state) case u.hostNeedsYou(e): attentionSquare(gtx) } corner.Pop() return layout.Dimensions{Size: image.Pt(side+shadow, side+shadow)} }) } // hostNeedsYou reports whether any session on a host, other than the one // shown, asks for attention. func (u *UI) hostNeedsYou(e hostEntry) bool { if e.srv == nil { return false } for _, s := range e.srv.ws.Sessions() { if (e.srv != u.srv || u.session == nil || s.SessionID != u.session.ID()) && needsYou(e.srv.ws.Summary(s.SessionID)) { return true } } return false } // attentionSquare is stateSquare's square in yellow, for a host with a // session that asks for attention. func attentionSquare(gtx layout.Context) layout.Dimensions { side := gtx.Dp(10) r := image.Rect(0, 0, side, side) theme.Bordered(gtx, theme.Yellow, theme.Ink, r, gtx.Dp(theme.Border)) return layout.Dimensions{Size: r.Max} } // initial is the first letter of a name, in capitals. func initial(name string) string { for _, r := range name { if unicode.IsLetter(r) || unicode.IsDigit(r) { return string(unicode.ToUpper(r)) } } return "?" } // updateCorner acts on clicks on the corner's toggle and field. It runs // at the start of a frame, before the sidebar's width is used: the window // draws only when something changes, so a frame drawn at the old width // could stay up until something else did. func (u *UI) updateCorner(gtx layout.Context) { if u.side.toggle.Clicked(gtx) { u.side.collapsed = !u.side.collapsed } if u.side.jump.Clicked(gtx) { u.palette.toggle(paletteSessions) } } // layoutCorner draws the title bar's corner over the sidebar: the // sidebar's toggle, and, unless it's collapsed, a field that opens the // session switcher. Space around them drags the window. func (u *UI) layoutCorner(gtx layout.Context) layout.Dimensions { moveArea(gtx) // The directions centre in the corner's height; what they lay out // keeps its own. toggle := func(gtx layout.Context) layout.Dimensions { gtx.Constraints.Min = image.Point{} return theme.IconButton{Clickable: &u.side.toggle, Icon: theme.IconSidebar}.Layout(gtx) } if u.side.collapsed { return layout.Center.Layout(gtx, toggle) } return layout.W.Layout(gtx, func(gtx layout.Context) layout.Dimensions { gtx.Constraints.Min = image.Point{} return layout.Inset{Left: 12, Right: 12}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { return layout.Flex{Alignment: layout.Middle}.Layout(gtx, layout.Rigid(toggle), layout.Rigid(layout.Spacer{Width: 10}.Layout), layout.Flexed(1, u.jumpField), ) }) }) } // jumpField looks like a search field, and opens the session switcher. func (u *UI) jumpField(gtx layout.Context) layout.Dimensions { click := &u.side.jump return click.Layout(gtx, func(gtx layout.Context) layout.Dimensions { gtx.Constraints.Min.X = gtx.Constraints.Max.X fill := theme.White if click.Hovered() { fill = theme.Hover } return theme.Box{Fill: fill, Padding: layout.Inset{Top: 5, Bottom: 5, Left: 8, Right: 6}}.Layout(gtx, func(gtx layout.Context) layout.Dimensions { gtx.Constraints.Min.X = gtx.Constraints.Max.X l := u.theme.Label(13, "Jump to session…", theme.Muted, font.Medium) l.MaxLines = 1 return l.Layout(gtx) }) }) } // breadcrumb says which host and session are shown, for the title bar // while the sidebar is collapsed, with the connection's state if it isn't // connected. func (u *UI) breadcrumb(gtx layout.Context) layout.Dimensions { name, _ := u.serverName() session := "" if u.session != nil { for _, s := range u.ws.Sessions() { if s.SessionID == u.session.ID() { session = s.Label } } } children := []layout.FlexChild{ layout.Rigid(u.theme.Label(13, name, theme.Muted, font.SemiBold).Layout), } if session != "" { children = append(children, layout.Rigid(u.theme.Label(13, " › ", theme.Muted, font.SemiBold).Layout), layout.Rigid(u.theme.Label(13, session, theme.Ink, font.Bold).Layout), ) } if tag, fill, fg := u.connectionTag(); tag != "" { children = append(children, layout.Rigid(layout.Spacer{Width: 8}.Layout), layout.Rigid(func(gtx layout.Context) layout.Dimensions { return theme.Tag(gtx, u.theme, tag, fill, fg) }), ) } return layout.Flex{Alignment: layout.Middle}.Layout(gtx, children...) } // connectionTag says how the connection to the server shown stands, or "" // if it's connected. func (u *UI) connectionTag() (tag string, fill, fg color.NRGBA) { state, err := u.connectState() switch { case state == "reconnecting": return "RECONNECTING", theme.Lavender, theme.Ink case state != "" && err != nil: return "FAILED", theme.Red, theme.White case state != "": return "CONNECTING", theme.Lavender, theme.Ink case !u.connected(): return "OFFLINE", theme.Red, theme.White } return "", color.NRGBA{}, color.NRGBA{} }