Skip to content

Commit e521837

Browse files
author
Cedric BAIL
committed
Add support for jumping to lease (brittle).
1 parent 50ff8fc commit e521837

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

binding.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ func (m *MikrotikRouter) IP() string {
160160
}
161161

162162
type MikrotikDataItem struct {
163-
id string
163+
router string
164+
id string
164165

165166
properties map[string]binding.String
166167

@@ -214,7 +215,7 @@ func NewMikrotikData(dial func(ctx context.Context, network, address string) (ne
214215
listen := false
215216

216217
for _, s := range r.Re {
217-
item := newMikrotikDataItem(s)
218+
item := newMikrotikDataItem(s, host)
218219
m.items[item.id] = item
219220
m.itemsList = append(m.itemsList, item)
220221
if item.id != "" {
@@ -243,7 +244,7 @@ func NewMikrotikData(dial func(ctx context.Context, network, address string) (ne
243244
item, ok := m.items[id]
244245
if !ok {
245246
if id != "" {
246-
item = newMikrotikDataItem(s)
247+
item = newMikrotikDataItem(s, host)
247248
m.items[id] = item
248249
m.itemsList = append(m.itemsList, item)
249250
m.listeners.Range(func(key, value interface{}) bool {
@@ -332,6 +333,10 @@ func (m *MikrotikDataTable) RemoveListener(l binding.DataListener) {
332333
m.listeners.Delete(l)
333334
}
334335

336+
func (m *MikrotikDataItem) Router() string {
337+
return m.router
338+
}
339+
335340
func (m *MikrotikDataItem) Get(key string) (binding.String, error) {
336341
if b, ok := m.properties[key]; ok {
337342
return b, nil
@@ -368,8 +373,8 @@ func getID(r *proto.Sentence) string {
368373
return ""
369374
}
370375

371-
func newMikrotikDataItem(r *proto.Sentence) *MikrotikDataItem {
372-
item := &MikrotikDataItem{properties: map[string]binding.String{}}
376+
func newMikrotikDataItem(r *proto.Sentence, host string) *MikrotikDataItem {
377+
item := &MikrotikDataItem{router: host, properties: map[string]binding.String{}}
373378
for _, p := range r.List {
374379
if p.Key == ".id" {
375380
item.id = p.Value

button.go

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ refreshDisable:
9090
}
9191

9292
func getString(data binding.String) string {
93+
if data == nil {
94+
return ""
95+
}
96+
9397
v, err := data.Get()
9498
if err != nil {
9599
return ""
@@ -98,6 +102,10 @@ func getString(data binding.String) string {
98102
}
99103

100104
func getBool(data binding.Bool) bool {
105+
if data == nil {
106+
return false
107+
}
108+
101109
v, err := data.Get()
102110
if err != nil {
103111
return false

table.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"fyne.io/fyne/v2/widget"
1212
)
1313

14-
func (a *appData) NewTableWithDataColumn(column []RouterOSHeader, data *MikrotikDataTable) *widget.Table {
14+
func (a *appData) NewTableWithDataColumn(jumpToTab func(host, view string), column []RouterOSHeader, data *MikrotikDataTable) *widget.Table {
1515
t := widget.NewTable(func() (int, int) {
1616
return data.Length(), len(column)
1717
}, func() fyne.CanvasObject {
1818
var button *Button
19-
button = NewButton("MAC Address", a.lookupIP(button))
19+
button = NewButton("MAC Address", a.lookupIP(jumpToTab, button))
2020
button.Hide()
2121
button.Importance = widget.LowImportance
2222

@@ -60,7 +60,7 @@ func (a *appData) NewTableWithDataColumn(column []RouterOSHeader, data *Mikrotik
6060
exist = append(exist, router.leaseBinding.Exist("mac-address", button.Text))
6161
}
6262
button.Icon = nil
63-
button.OnTapped = a.lookupIP(button)
63+
button.OnTapped = a.lookupIP(jumpToTab, button)
6464
button.BindDisable(NewNot(NewOr(exist...)))
6565
} else if column[i.Col].copy {
6666
button.Icon = theme.ContentCopyIcon()
@@ -88,7 +88,7 @@ func (a *appData) NewTableWithDataColumn(column []RouterOSHeader, data *Mikrotik
8888
return t
8989
}
9090

91-
func (a *appData) lookupIP(button *Button) func() {
91+
func (a *appData) lookupIP(jumpToTab func(host, view string), button *Button) func() {
9292
return func() {
9393
dl := []binding.DataList{}
9494

@@ -107,34 +107,40 @@ func (a *appData) lookupIP(button *Button) func() {
107107

108108
merged := NewMergeDataList(dl)
109109

110+
var d *dialog.CustomDialog
111+
110112
list := widget.NewListWithData(merged, func() fyne.CanvasObject {
111-
return widget.NewLabel("hostname.somewhere.com (255.255.255.255)")
113+
return NewButton("hostname.somewhere.com (255.255.255.255)", func() {})
112114
}, func(di binding.DataItem, co fyne.CanvasObject) {
113115
lookup := di.(*MikrotikDataItem)
114-
label := co.(*widget.Label)
116+
btn := co.(*Button)
115117

116118
ipString, _ := lookup.Get("active-address")
117119
hostnameString, _ := lookup.Get("host-name")
118120

121+
btn.OnTapped = func() {
122+
d.Hide()
123+
jumpToTab(lookup.Router(), "DHCP Server")
124+
}
119125
if len(getString(hostnameString)) > 0 {
120126
if len(getString(ipString)) > 0 {
121-
label.Bind(binding.NewSprintf("%s (%s)\n", hostnameString, ipString))
127+
btn.Bind(binding.NewSprintf("%s (%s)", hostnameString, ipString))
122128
} else {
123-
label.Bind(binding.NewSprintf("%s (-)\n", hostnameString))
129+
btn.Bind(binding.NewSprintf("%s (-)", hostnameString))
124130
}
125131
} else if len(getString(ipString)) > 0 {
126-
label.Bind(binding.NewSprintf("%s\n", ipString))
132+
btn.Bind(binding.NewSprintf("%s", ipString))
127133
} else {
128-
label.Unbind()
129-
label.SetText("unknown")
134+
btn.OnTapped = func() {}
135+
btn.Unbind()
136+
btn.SetText("unknown")
130137
}
131138

132139
})
133140

134-
d := dialog.NewCustom("Matching information for "+button.Text, "OK", container.New(&moreSpace{a.win}, list), a.win)
141+
d = dialog.NewCustom("Matching information for "+button.Text, "OK", container.New(&moreSpace{a.win}, list), a.win)
135142
d.SetOnClosed(func() {
136143
merged.Close()
137-
d.Hide()
138144
})
139145
d.Show()
140146
}

ui.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,15 @@ func (a *appData) createUI(lastHost string) {
8484
footer.SetText("")
8585
}
8686

87+
var sel *widget.Select
88+
8789
tree := widget.NewTreeWithStrings(routerOStree)
90+
jumpToTab := func(host, view string) {
91+
sel.SetSelected(host)
92+
tree.OnSelected(view)
93+
}
8894
tree.OnSelected = func(id string) {
89-
err := a.buildView(tabs, id)
95+
err := a.buildView(tabs, jumpToTab, id)
9096
if err != nil {
9197
updateStatus(nil, false, err)
9298
return
@@ -96,7 +102,7 @@ func (a *appData) createUI(lastHost string) {
96102
updateStatus(a.identity, a.current.ssl, nil)
97103
}
98104

99-
sel := widget.NewSelect([]string{}, a.selectHost(tabs, updateStatus))
105+
sel = widget.NewSelect([]string{}, a.selectHost(tabs, updateStatus, jumpToTab))
100106

101107
var useTailScale *widget.Check
102108
updateTailScale := func(b bool) {
@@ -279,7 +285,7 @@ func (a *appData) newHost(sel *widget.Select, ip string) {
279285
a.win.Canvas().Focus(host)
280286
}
281287

282-
func (a *appData) selectHost(tabs *container.AppTabs, updateStatus func(identity binding.String, ssl bool, err error)) func(s string) {
288+
func (a *appData) selectHost(tabs *container.AppTabs, updateStatus func(identity binding.String, ssl bool, err error), jumpToTab func(host, view string)) func(s string) {
283289
return func(s string) {
284290
for _, b := range a.bindings {
285291
b.Close()
@@ -308,7 +314,7 @@ func (a *appData) selectHost(tabs *container.AppTabs, updateStatus func(identity
308314
a.identity = identity
309315

310316
if a.currentView != "" {
311-
err := a.buildView(tabs, a.currentView)
317+
err := a.buildView(tabs, jumpToTab, a.currentView)
312318
if err != nil {
313319
updateStatus(nil, false, err)
314320
return
@@ -388,7 +394,7 @@ func (a *appData) getPassword(lastHost string, sel *widget.Select) {
388394
a.win.Canvas().Focus(password)
389395
}
390396

391-
func (a *appData) buildView(tabs *container.AppTabs, view string) error {
397+
func (a *appData) buildView(tabs *container.AppTabs, jumpToTab func(host, view string), view string) error {
392398
a.currentView = view
393399

394400
if a.current == nil {
@@ -413,7 +419,7 @@ func (a *appData) buildView(tabs *container.AppTabs, view string) error {
413419
if a.currentTab == cmd.title {
414420
selectIndex = len(tabs.Items)
415421
}
416-
tabs.Items = append(tabs.Items, container.NewTabItem(cmd.title, a.NewTableWithDataColumn(cmd.headers, b)))
422+
tabs.Items = append(tabs.Items, container.NewTabItem(cmd.title, a.NewTableWithDataColumn(jumpToTab, cmd.headers, b)))
417423
}
418424
tabs.SelectIndex(selectIndex)
419425
tabs.Refresh()

0 commit comments

Comments
 (0)