Skip to content

Commit

Permalink
fix mem leak in peers table due to fyne-io/fyne#735
Browse files Browse the repository at this point in the history
  • Loading branch information
julsemaan committed Dec 1, 2020
1 parent 1eb5811 commit 14eb008
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
4 changes: 2 additions & 2 deletions guiwrapper/main.go
Expand Up @@ -93,7 +93,7 @@ func checkTunnelStatus() {
}
} else if fails >= maxRpcFails {
statusLabel.SetText("Too many failures communicating with RPC server. Tunnel seems to be dead. Please restart the client.")
peersTable.SetContent(widget.NewLabel(""))
peersTableContainer.SetContent(widget.NewLabel(""))
} else {
fmt.Println("Failed to contact tunnel for status update")
statusLabel.SetText("Tunnel seems to be inactive...")
Expand All @@ -108,7 +108,7 @@ func checkTunnelStatus() {
return
} else {
statusLabel.SetText(messages[status])
//UpdatePeers(ctx, rpc)
UpdatePeers(ctx, rpc)
}
}

Expand Down
15 changes: 9 additions & 6 deletions guiwrapper/start_app.go
Expand Up @@ -20,7 +20,8 @@ import (
var spacePlaceholder = " "

var statusLabel *widget.Label
var peersTable *widget.Card
var peersTableContainer *widget.Card
var peersTable = NewTable()

var restartBtn *widget.Button

Expand Down Expand Up @@ -198,8 +199,8 @@ func PromptCredentials(tabs *container.AppTabs, callback func(bool)) {
func PostConnect(tabs *container.AppTabs) {
statusLabel = widget.NewLabel("Opening tunnel process")
statusLabel.Wrapping = fyne.TextWrapWord
peersTable = widget.NewCard("Peers", "", widget.NewVBox())
tabs.Items[0].Content = widget.NewVBox(statusLabel, restartBtn, peersTable)
peersTableContainer = widget.NewCard("Peers", "", widget.NewVBox())
tabs.Items[0].Content = widget.NewVBox(statusLabel, restartBtn, peersTableContainer)
if len(tabs.Items) > 1 {
tabs.RemoveIndex(1)
}
Expand All @@ -209,7 +210,7 @@ func PostConnect(tabs *container.AppTabs) {
func UpdatePeers(ctx context.Context, rpc wgrpc.WGServiceClient) {
peers, err := rpc.GetPeers(ctx, &wgrpc.PeersRequest{})
if err != nil {
peersTable.SetContent(widget.NewLabel("Failed to obtain peers from local wireguard server: " + err.Error()))
peersTableContainer.SetContent(widget.NewLabel("Failed to obtain peers from local wireguard server: " + err.Error()))
return
}

Expand All @@ -222,8 +223,10 @@ func UpdatePeers(ctx context.Context, rpc wgrpc.WGServiceClient) {
peersInfos = append(peersInfos, []string{peer.Hostname, peer.IpAddress, peer.Status})
}

peersTable.SetContent(makeTable(
peersTable.Update(
[]string{"Host", "IP address", "State"},
peersInfos,
))
)

peersTableContainer.SetContent(peersTable.GetContainer())
}
64 changes: 64 additions & 0 deletions guiwrapper/table.go
@@ -0,0 +1,64 @@
package main

import (
"fyne.io/fyne"
"fyne.io/fyne/container"
"fyne.io/fyne/widget"
)

type Table struct {
columnsRows [][]*widget.Label
columns []*fyne.Container
container *fyne.Container
}

func NewTable() *Table {
return &Table{columnsRows: [][]*widget.Label{}, columns: []*fyne.Container{}}
}

func (t *Table) Update(headings []string, rows [][]string) {
columnsData := rowsToColumns(headings, rows)
for i, col := range columnsData {
if len(t.columnsRows) <= i {
t.columnsRows = append(t.columnsRows, []*widget.Label{})
}
row := t.columnsRows[i]
columnData := append([]string{headings[i]}, col...)
for j, elem := range columnData {
if len(row) <= j {
row = append(row, widget.NewLabel(elem))
}
row[j].Text = elem
}
t.columnsRows[i] = row

if len(t.columns) <= i {
t.columns = append(t.columns, container.NewGridWithColumns(1, []fyne.CanvasObject{}...))
}

objects := []fyne.CanvasObject{}
for _, o := range t.columnsRows[i] {
objects = append(objects, o)
}

column := t.columns[i]
column.Objects = objects
column.Refresh()
}

objects := []fyne.CanvasObject{}
for _, o := range t.columns {
objects = append(objects, o)
}

if t.container == nil {
t.container = container.NewHBox(objects...)
} else {
t.container.Objects = objects
t.container.Refresh()
}
}

func (t *Table) GetContainer() *fyne.Container {
return t.container
}
21 changes: 0 additions & 21 deletions guiwrapper/util.go
@@ -1,26 +1,5 @@
package main

import (
"fyne.io/fyne"
"fyne.io/fyne/container"
"fyne.io/fyne/widget"
)

func makeTable(headings []string, rows [][]string) *fyne.Container {
columns := rowsToColumns(headings, rows)
objects := []fyne.CanvasObject{}
for i, col := range columns {
elems := []fyne.CanvasObject{
widget.NewLabelWithStyle(headings[i], fyne.TextAlignLeading, fyne.TextStyle{Bold: true}),
}
for _, elem := range col {
elems = append(elems, widget.NewLabel(elem))
}
objects = append(objects, container.NewGridWithColumns(1, elems...))
}
return container.NewHBox(objects...)
}

func rowsToColumns(headings []string, rows [][]string) [][]string {
columns := make([][]string, len(headings))
for _, row := range rows {
Expand Down

0 comments on commit 14eb008

Please sign in to comment.