Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring ui #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 27 additions & 32 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"sort"
"strings"

libui "git.sr.ht/~sircmpwn/aerc/lib/ui"
"github.com/gdamore/tcell"
libui "git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rockorager/vaxis"
)

type ClientView struct {
libui.Invalidatable
selected int
currentCategory string
client *Client
Expand Down Expand Up @@ -42,24 +41,24 @@ type DashboardDisplayable interface {

func (c *ClientView) Draw(ctx *libui.Context) {
c.viewportHeight = ctx.Height()
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', vaxis.Style{})

y := 0

printerWithStyle := func(style tcell.Style, formatter string, v ...interface{}) {
printerWithStyle := func(style vaxis.Style, formatter string, v ...interface{}) {
if y < c.scroll || y-c.scroll >= ctx.Height() {
y++
return
}
if c.selected == y {
style = style.Reverse(true)
style.Attribute = vaxis.AttrReverse
}
w := ctx.Printf(0, y-c.scroll, style, formatter, v...)
ctx.Fill(w, y-c.scroll, ctx.Width()-w, 1, ' ', style)
y++
}
printer := func(formatter string, v ...interface{}) {
style := tcell.StyleDefault
style := vaxis.Style{}
printerWithStyle(style, formatter, v...)
}

Expand All @@ -70,10 +69,11 @@ func (c *ClientView) Draw(ctx *libui.Context) {
status = client.Err.Error()
}

style := tcell.StyleDefault
statusStyle := style.Foreground(tcell.ColorGreen)
statusStyle := vaxis.Style {
Foreground: vaxis.RGBColor(0, 255, 0),
}
if client.Err != nil {
statusStyle = statusStyle.Foreground(tcell.ColorRed)
statusStyle.Foreground = vaxis.RGBColor(255, 0, 0)
}
printerWithStyle(statusStyle, "%s since %s rx: %-6d tx: %-6d globals: %-4d objects: %-4d",
status, client.Timestamp.Format("15:04:05"), len(client.RxLog), len(client.TxLog),
Expand Down Expand Up @@ -103,7 +103,8 @@ func (c *ClientView) Draw(ctx *libui.Context) {
if y == c.selected {
c.currentCategory = category
}
printerWithStyle(tcell.StyleDefault.Foreground(tcell.ColorYellow), category)
printerWithStyle(vaxis.Style { Foreground: vaxis.IndexColor(226) }, category)

if c.folded[category] {
continue
}
Expand All @@ -116,7 +117,7 @@ func (c *ClientView) Draw(ctx *libui.Context) {
}

func (client *ClientView) Invalidate() {
client.DoInvalidate(client)
libui.Invalidate()
}

func (client *ClientView) SelectNext(inc int) {
Expand Down Expand Up @@ -153,34 +154,28 @@ func (client *ClientView) Focus(focus bool) {
// This space deliberately left blank
}

func (client *ClientView) Event(event tcell.Event) bool {
switch event := event.(type) {
case *tcell.EventKey:
switch event.Key() {
case tcell.KeyDown:
func (client *ClientView) Event(event vaxis.Event) bool {
if key, ok := event.(vaxis.Key); ok {
switch {
case key.Matches(vaxis.KeyDown):
client.SelectNext(1)
return true
case tcell.KeyUp:
case key.Matches(vaxis.KeyUp):
client.SelectPrev(1)
return true
case tcell.KeyPgDn:
case key.Matches(vaxis.KeyPgDown):
client.SelectNext(client.viewportHeight)
return true
case tcell.KeyPgUp:
case key.Matches(vaxis.KeyPgUp):
client.SelectPrev(client.viewportHeight)
return true
case tcell.KeyRune:
switch event.Rune() {
case 'j':
client.SelectNext(1)
return true
case 'k':
client.SelectPrev(1)
return true
}
}
switch event.Rune() {
case ' ':
case key.Matches('j'):
client.SelectNext(1)
return true
case key.Matches('k'):
client.SelectPrev(1)
return true
case key.Matches(' '):
client.Toggle()
return true
}
Expand Down
48 changes: 23 additions & 25 deletions clients.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
libui "git.sr.ht/~sircmpwn/aerc/lib/ui"
"github.com/gdamore/tcell"
libui "git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rockorager/vaxis"
)

const (
Expand All @@ -20,7 +20,6 @@ Commands: exec, slow, fast, clear, block, unblock, quit
)

type ClientsView struct {
libui.Invalidatable
selected int
proxy *Proxy
}
Expand All @@ -32,11 +31,12 @@ func NewClientsView(proxy *Proxy) *ClientsView {
}

func (clients *ClientsView) Draw(ctx *libui.Context) {
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)

ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', vaxis.Style{})

proxy := clients.proxy
if len(proxy.Clients) == 0 {
ctx.Printf(0, 0, tcell.StyleDefault, "%s", intro)
ctx.Printf(0, 0, vaxis.Style{}, "%s", intro)
return
}

Expand All @@ -48,17 +48,19 @@ func (clients *ClientsView) Draw(ctx *libui.Context) {
if client.Err != nil {
status = client.Err.Error()
}
style := tcell.StyleDefault
style := vaxis.Style{}
if clients.selected == i {
style = style.Reverse(true)
style.Attribute = vaxis.AttrReverse
}
w := ctx.Printf(0, y, style,
"Client %d: %s", client.Pid(), status)
ctx.Fill(w, y, ctx.Width()-w, 1, ' ', style)
y++
statusStyle := style.Reverse(false).Foreground(tcell.ColorGreen)
statusStyle := style
statusStyle.Attribute = vaxis.AttrNone
statusStyle.Foreground = vaxis.RGBColor(0, 255, 0)
if client.Err != nil {
statusStyle = statusStyle.Foreground(tcell.ColorRed)
statusStyle.Foreground = vaxis.RGBColor(255, 0, 0)
}
w = ctx.Printf(0, y, statusStyle, " since %s ",
client.Timestamp.Format("15:04:05"))
Expand All @@ -72,7 +74,7 @@ func (clients *ClientsView) Draw(ctx *libui.Context) {
}

func (clients *ClientsView) Invalidate() {
clients.DoInvalidate(clients)
libui.Invalidate()
}

// TODO: Scrolling
Expand All @@ -96,25 +98,21 @@ func (clients *ClientsView) Focus(focus bool) {
// This space deliberately left blank
}

func (clients *ClientsView) Event(event tcell.Event) bool {
switch event := event.(type) {
case *tcell.EventKey:
switch event.Key() {
case tcell.KeyDown:
func (clients *ClientsView) Event(event vaxis.Event) bool {
if key, ok := event.(vaxis.Key); ok {
switch {
case key.Matches(vaxis.KeyDown):
clients.SelectNext()
return true
case key.Matches(vaxis.KeyUp):
clients.SelectPrev()
return true
case key.Matches('j'):
clients.SelectNext()
return true
case tcell.KeyUp:
case key.Matches('k'):
clients.SelectPrev()
return true
case tcell.KeyRune:
switch event.Rune() {
case 'j':
clients.SelectNext()
return true
case 'k':
clients.SelectPrev()
return true
}
}
}
return false
Expand Down
76 changes: 39 additions & 37 deletions dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"fmt"
"os/exec"

libui "git.sr.ht/~sircmpwn/aerc/lib/ui"
"github.com/gdamore/tcell"
libui "git.sr.ht/~rjarry/aerc/lib/ui"
config "git.sr.ht/~rjarry/aerc/config"
"github.com/google/shlex"
"git.sr.ht/~rockorager/vaxis"
)

type Dashboard struct {
Expand All @@ -22,24 +23,28 @@ type Dashboard struct {
func NewDashboard(proxy *Proxy) *Dashboard {
clients := NewClientsView(proxy)

tabs := libui.NewTabs()
tabs.Add(clients, "Connections")
config.Ui.LoadStyle()

status := libui.NewStack()
tabs := libui.NewTabs(func(d libui.Drawable) *config.UIConfig {
return config.Ui
})
tabs.Add(clients, "Connections", true)

status := libui.NewStack(config.Ui)
status.Push(libui.NewText(
fmt.Sprintf("WAYLAND_DISPLAY=%s -> %s",
proxy.ProxyDisplay(), proxy.RemoteDisplay())).
Reverse(true))
proxy.ProxyDisplay(), proxy.RemoteDisplay()),
config.Ui.GetStyle(config.STYLE_SUCCESS)))

grid := libui.NewGrid().Rows([]libui.GridSpec{
{libui.SIZE_EXACT, 1},
{libui.SIZE_WEIGHT, 1},
{libui.SIZE_EXACT, 1},
{Strategy: libui.SIZE_EXACT, Size: libui.Const(1)},
{Strategy: libui.SIZE_WEIGHT, Size: libui.Const(1)},
{Strategy: libui.SIZE_EXACT, Size: libui.Const(1)},
}).Columns([]libui.GridSpec{
{libui.SIZE_EXACT, 11},
{libui.SIZE_WEIGHT, 1},
{Strategy: libui.SIZE_EXACT, Size: libui.Const(11)},
{Strategy: libui.SIZE_WEIGHT, Size: libui.Const(1)},
})
grid.AddChild(libui.NewText(" wlhax ").Reverse(true))
grid.AddChild(libui.NewText(" wlhax ", config.Ui.GetStyle(config.STYLE_HEADER)))
grid.AddChild(tabs.TabStrip).At(0, 1)
grid.AddChild(tabs.TabContent).At(1, 0).Span(1, 2)
grid.AddChild(status).At(2, 0).Span(1, 2)
Expand Down Expand Up @@ -69,8 +74,7 @@ func NewDashboard(proxy *Proxy) *Dashboard {
}
v = NewClientView(c)
dash.tabMap[c] = v
tabs.Add(v, fmt.Sprintf("Client %d", c.Pid()))
tabs.Select(len(tabs.Tabs) - 1)
tabs.Add(v, fmt.Sprintf("Client %d", c.Pid()), false)
})
proxy.OnDisconnect(func(c *Client) {
clients.Invalidate()
Expand All @@ -87,44 +91,41 @@ func (dash *Dashboard) Draw(ctx *libui.Context) {
}

func (dash *Dashboard) OnInvalidate(callback func(d libui.Drawable)) {
dash.grid.OnInvalidate(func(d libui.Drawable) {
callback(dash)
})
//dash.grid.OnInvalidate(func(d libui.Drawable) {
// callback(dash)
//})
libui.Invalidate()
}

func (dash *Dashboard) Invalidate() {
dash.grid.Invalidate()
}

func (dash *Dashboard) Event(event tcell.Event) bool {
func (dash *Dashboard) Event(event vaxis.Event) bool {
if dash.focused != nil {
if dash.focused.Event(event) {
return true
}
}
interactive, ok := dash.tabs.Tabs[dash.tabs.Selected].
Content.(libui.Interactive)

interactive, ok := dash.tabs.Selected().Content.(libui.Interactive)
if ok {
if interactive.Event(event) {
return true
}
}
switch event := event.(type) {
case *tcell.EventKey:
switch event.Key() {
case tcell.KeyLeft:
if key, ok := event.(vaxis.Key); ok {
switch {
case key.Matches(vaxis.KeyLeft):
dash.tabs.PrevTab()
case tcell.KeyRight:
case key.Matches(vaxis.KeyRight):
dash.tabs.NextTab()
case tcell.KeyRune:
switch event.Rune() {
case 'h':
dash.tabs.PrevTab()
case 'l':
dash.tabs.NextTab()
case ':':
dash.BeginExCommand("")
}
case key.Matches('h'):
dash.tabs.PrevTab()
case key.Matches('l'):
dash.tabs.NextTab()
case key.Matches(':'):
dash.BeginExCommand("")
}
}
return false
Expand All @@ -150,8 +151,9 @@ func (dash *Dashboard) focus(item libui.Interactive) {
dash.focused.Focus(false)
}
dash.focused = item
interactive, ok := dash.tabs.Tabs[dash.tabs.Selected].
Content.(libui.Interactive)

interactive, ok := dash.tabs.Selected().Content.(libui.Interactive)
//tabs[dash.tabs.curIndex]
if item != nil {
item.Focus(true)
if ok {
Expand Down
Loading