From abe6eb03d817b6d88ef4acc872ba794bc3d44191 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Thu, 4 Apr 2019 10:16:23 +0200 Subject: ui: refactor TunnelsView to use the new status image painter Signed-off-by: Alexander Neumann Signed-off-by: Jason A. Donenfeld --- ui/tunnelsview.go | 47 +++++++++++++---------------------------------- 1 file changed, 13 insertions(+), 34 deletions(-) (limited to 'ui/tunnelsview.go') diff --git a/ui/tunnelsview.go b/ui/tunnelsview.go index 6f1deed0..08bc1f21 100644 --- a/ui/tunnelsview.go +++ b/ui/tunnelsview.go @@ -58,17 +58,18 @@ type TunnelsView struct { *walk.TableView model *TunnelModel - stoppedBrush *walk.SolidColorBrush - startingBrush *walk.SolidColorBrush - startedBrush *walk.SolidColorBrush - statusPen *walk.CosmeticPen + imageProvider *TunnelStatusImageProvider } func NewTunnelsView(parent walk.Container) (*TunnelsView, error) { + var disposables walk.Disposables + defer disposables.Treat() + tv, err := walk.NewTableView(parent) if err != nil { return nil, err } + disposables.Add(tv) model := &TunnelModel{} @@ -82,20 +83,15 @@ func NewTunnelsView(parent walk.Container) (*TunnelsView, error) { model: model, } - tunnelsView.stoppedBrush, _ = walk.NewSolidColorBrush(walk.RGB(239, 239, 239)) - tunnelsView.AddDisposable(tunnelsView.stoppedBrush) - - tunnelsView.startingBrush, _ = walk.NewSolidColorBrush(walk.RGB(255, 211, 31)) - tunnelsView.AddDisposable(tunnelsView.startingBrush) - - tunnelsView.startedBrush, _ = walk.NewSolidColorBrush(walk.RGB(0, 255, 0)) - tunnelsView.AddDisposable(tunnelsView.startedBrush) - - tunnelsView.statusPen, _ = walk.NewCosmeticPen(walk.PenSolid, walk.RGB(191, 191, 191)) - tunnelsView.AddDisposable(tunnelsView.statusPen) + if tunnelsView.imageProvider, err = NewTunnelStatusImageProvider(); err != nil { + return nil, err + } + tunnelsView.AddDisposable(tunnelsView.imageProvider) tv.SetCellStyler(tunnelsView) + disposables.Spare() + return tunnelsView, nil } @@ -105,20 +101,7 @@ func (tv *TunnelsView) StyleCell(style *walk.CellStyle) { return } - tunnel := tv.model.tunnels[style.Row()] - - var brush *walk.SolidColorBrush - state, _ := tunnel.State() - switch state { - case service.TunnelStarted: - brush = tv.startedBrush - - case service.TunnelStarting: - brush = tv.startingBrush - - default: - brush = tv.stoppedBrush - } + tunnel := &tv.model.tunnels[style.Row()] b := style.Bounds() @@ -126,13 +109,9 @@ func (tv *TunnelsView) StyleCell(style *walk.CellStyle) { b.Width -= b.Height canvas.DrawText(tunnel.Name, tv.Font(), 0, b, walk.TextVCenter) - b.X = 4 - b.Y += 4 - b.Height -= 8 b.Width = b.Height - canvas.FillEllipse(brush, b) - canvas.DrawEllipse(tv.statusPen, b) + tv.imageProvider.PaintForTunnel(tunnel, canvas, b) } func (tv *TunnelsView) CurrentTunnel() *service.Tunnel { -- cgit v1.2.3-59-g8ed1b