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

internal/ui: clean up some iterator stuff #153

Merged
merged 5 commits into from
Aug 22, 2024
Merged
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
15 changes: 8 additions & 7 deletions internal/ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ui

import (
"context"
"iter"
"log/slog"
"os"
"slices"
Expand All @@ -11,7 +10,6 @@ import (
"deedles.dev/mk"
"deedles.dev/trayscale/internal/tray"
"deedles.dev/trayscale/internal/tsutil"
"deedles.dev/xiter"
"github.com/diamondburned/gotk4-adwaita/pkg/adw"
"github.com/diamondburned/gotk4/pkg/gdk/v4"
"github.com/diamondburned/gotk4/pkg/gio/v2"
Expand Down Expand Up @@ -129,11 +127,14 @@ func (a *App) updatePeers(status tsutil.Status) {
}

peerMap := status.Status.Peer
peers := slices.SortedFunc(iter.Seq[key.NodePublic](xiter.Filter(xiter.MapKeys(status.Status.Peer),
func(peer key.NodePublic) bool {
return !tsutil.IsMullvad(peerMap[peer])
})),
key.NodePublic.Compare)
peers := make([]key.NodePublic, 0, len(status.Status.Peer))
for k, p := range peerMap {
if tsutil.IsMullvad(p) {
continue
}
peers = append(peers, k)
}
slices.SortFunc(peers, key.NodePublic.Compare)

for key, page := range a.peerPages {
if _, ok := peerMap[key]; !ok {
Expand Down
52 changes: 37 additions & 15 deletions internal/ui/peerpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"cmp"
"context"
_ "embed"
"iter"
"log/slog"
"net/netip"
"slices"
Expand Down Expand Up @@ -225,23 +224,46 @@ func (page *PeerPage) Update(a *App, peer *ipnstate.PeerStatus, status tsutil.St
slices.SortFunc(peer.TailscaleIPs, netip.Addr.Compare)
page.addrRows.Update(peer.TailscaleIPs)

if peer.PrimaryRoutes != nil {
page.routes = peer.PrimaryRoutes.AsSlice()
routes := func(yield func(netip.Prefix) bool) {
if peer.PrimaryRoutes == nil {
return
}
for i := 0; i < peer.PrimaryRoutes.Len(); i++ {
r := peer.PrimaryRoutes.At(i)
if r.Bits() == 0 {
continue
}
if !yield(r) {
return
}
}
}
page.routes = slices.SortedFunc(iter.Seq[netip.Prefix](xiter.Filter(xiter.OfSlice(page.routes),
func(p netip.Prefix) bool { return p.Bits() != 0 })),
routes = xiter.Or(
routes,
xiter.Of(netip.Prefix{}),
)

clear(page.routes)
page.routes = page.routes[:0]
page.routes = slices.AppendSeq(page.routes, routes)
slices.SortFunc(
page.routes,
func(p1, p2 netip.Prefix) int {
return cmp.Or(p1.Addr().Compare(p2.Addr()), p1.Bits()-p2.Bits())
})
if len(page.routes) == 0 {
page.routes = append(page.routes, netip.Prefix{})
}
eroutes := make([]enum[netip.Prefix], 0, len(page.routes))
for i, r := range page.routes {
i = -1
eroutes = append(eroutes, enumerate(i, r))
return cmp.Or(
p1.Addr().Compare(p2.Addr()),
cmp.Compare(p1.Bits(), p2.Bits()),
)
},
)

eroutes := func(yield func(enum[netip.Prefix]) bool) {
for _, r := range page.routes {
if !yield(enumerate(-1, r)) {
return
}
}
}
page.routeRows.Update(eroutes)
page.routeRows.UpdateFromSeq(eroutes, len(page.routes))

page.ExitNodeRow.SetVisible(peer.ExitNodeOption)
page.ExitNodeRow.ActivatableWidget().(*gtk.Switch).SetState(peer.ExitNode)
Expand Down
11 changes: 9 additions & 2 deletions internal/ui/rowmanager.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ui

import (
"iter"
"slices"

"deedles.dev/xiter"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
)

Expand Down Expand Up @@ -31,9 +33,14 @@ func (m *rowManager[Data]) resize(size int) {
}

func (m *rowManager[Data]) Update(data []Data) {
m.resize(len(data))
m.UpdateFromSeq(slices.Values(data), len(data))
}

func (m *rowManager[Data]) UpdateFromSeq(data iter.Seq[Data], size int) {
m.resize(size)

for i, d := range data {
edata := xiter.Enumerate(xiter.Seq[Data](data))
for i, d := range edata {
if i < len(m.rows) {
m.rows[i].Update(d)
continue
Expand Down
59 changes: 41 additions & 18 deletions internal/ui/selfpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"cmp"
"context"
_ "embed"
"iter"
"log/slog"
"net/netip"
"slices"
Expand Down Expand Up @@ -354,12 +353,16 @@ func (page *SelfPage) init(a *App, peer *ipnstate.PeerStatus, status tsutil.Stat
page.PreferredDERP.SetText(dm.Regions[r.PreferredDERP].RegionName)

page.DERPLatencies.SetVisible(true)
namedLats := slices.SortedFunc(iter.Seq[xiter.Pair[string, time.Duration]](xiter.Map(xiter.ToPair(xiter.OfMap(r.RegionLatency)),
func(p xiter.Pair[int, time.Duration]) xiter.Pair[string, time.Duration] {
return xiter.P(dm.Regions[p.V1].RegionName, p.V2)
})),
func(p1, p2 xiter.Pair[string, time.Duration]) int { return cmp.Compare(p1.V2, p2.V2) })
latencyRows.Update(namedLats)
namedLats := func(yield func(latencyEntry) bool) {
for id, latency := range r.RegionLatency {
named := xiter.P(dm.Regions[id].RegionName, latency)
if !yield(named) {
return
}
}
}
sortedLats := slices.SortedFunc(namedLats, func(p1, p2 latencyEntry) int { return cmp.Compare(p1.V2, p2.V2) })
latencyRows.Update(sortedLats)
})
}

Expand All @@ -383,19 +386,39 @@ func (page *SelfPage) Update(a *App, peer *ipnstate.PeerStatus, status tsutil.St
page.fileRows.Update(status.Files)
page.FilesGroup.SetVisible(len(status.Files) > 0)

page.routes = slices.SortedFunc(iter.Seq[netip.Prefix](xiter.Filter(xiter.OfSlice(status.Prefs.AdvertiseRoutes),
func(p netip.Prefix) bool { return p.Bits() != 0 })), // Filter
func(p1, p2 netip.Prefix) int { // SortedFunc
return cmp.Or(p1.Addr().Compare(p2.Addr()), p1.Bits()-p2.Bits())
})
if len(page.routes) == 0 {
page.routes = append(page.routes, netip.Prefix{})
routes := func(yield func(netip.Prefix) bool) {
for _, r := range status.Prefs.AdvertiseRoutes {
if r.Bits() == 0 {
continue
}
if !yield(r) {
return
}
}
}
eroutes := make([]enum[netip.Prefix], 0, len(page.routes))
for i, r := range page.routes {
eroutes = append(eroutes, enumerate(i, r))
routes = xiter.Or(
routes,
xiter.Of(netip.Prefix{}),
)

clear(page.routes)
page.routes = page.routes[:0]
page.routes = slices.AppendSeq(page.routes, routes)
slices.SortFunc(page.routes, func(p1, p2 netip.Prefix) int {
return cmp.Or(
p1.Addr().Compare(p2.Addr()),
cmp.Compare(p1.Bits(), p2.Bits()),
)
})

eroutes := func(yield func(enum[netip.Prefix]) bool) {
for i, r := range page.routes {
if !yield(enumerate(i, r)) {
return
}
}
}
page.routeRows.Update(eroutes)
page.routeRows.UpdateFromSeq(eroutes, len(page.routes))
}

type addrRow struct {
Expand Down
Loading