Skip to content

Commit

Permalink
feat: add search
Browse files Browse the repository at this point in the history
  • Loading branch information
jgillich committed Jan 22, 2024
1 parent d431e02 commit 375018b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
54 changes: 27 additions & 27 deletions ui/list_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ui
import (
"context"
"strconv"
"strings"
"time"

"github.com/diamondburned/gotk4-adwaita/pkg/adw"
Expand All @@ -18,7 +19,6 @@ import (
type ListView struct {
*gtk.Box
root *ClusterWindow
list *gtk.StringList
resource *metav1.APIResource
items []client.Object
selection *gtk.SingleSelection
Expand All @@ -34,32 +34,21 @@ func NewListView(root *ClusterWindow) *ListView {
header.AddCSSClass("flat")
header.SetShowEndTitleButtons(false)
header.SetShowStartTitleButtons(false)
search := gtk.NewSearchBar()
entry := gtk.NewSearchEntry()
search.ConnectEntry(entry)
entry.Show()
search.Show()
b := gtk.NewBox(gtk.OrientationVertical, 0)
b.Append(search)
b.Append(entry)
header.SetTitleWidget(b)
header.SetTitleWidget(NewSearchBar(root))
l.Append(header)

l.list = gtk.NewStringList([]string{})
l.selection = gtk.NewSingleSelection(l.list)
l.selection = l.createModel()
l.columnView = gtk.NewColumnView(l.selection)
l.columnView.SetMarginStart(16)
l.columnView.SetMarginEnd(16)
sw := gtk.NewScrolledWindow()
sw.SetVExpand(true)
sw.SetPolicy(gtk.PolicyNever, gtk.PolicyAutomatic)
sw.SetHExpand(true)
sw.SetSizeRequest(500, 0)
// sw.SetPolicy(gtk.PolicyNever, gtk.PolicyAutomatic)
sw.SetChild(l.columnView)
l.Append(sw)

l.selection.ConnectSelectionChanged(func(_, _ uint) {
root.detailView.SetObject(l.items[l.selection.Selected()])
})

return &l
}

Expand All @@ -71,15 +60,8 @@ func (l *ListView) SetResource(gvr schema.GroupVersionResource) error {
}
}

for {
length := uint(len(l.items))
if length > 0 {
l.list.Remove(length - 1)
l.items = l.items[:length-1]
} else {
break
}
}
l.selection = l.createModel()
l.columnView.SetModel(l.selection)

for _, column := range l.columns {
l.columnView.RemoveColumn(column)
Expand All @@ -95,7 +77,7 @@ func (l *ListView) SetResource(gvr schema.GroupVersionResource) error {
}
l.items = list
for i, _ := range list {
l.list.Append(strconv.Itoa(i))
l.selection.Model().Cast().(*gtk.StringList).Append(strconv.Itoa(i))
}

if len(l.items) > 0 {
Expand All @@ -107,6 +89,16 @@ func (l *ListView) SetResource(gvr schema.GroupVersionResource) error {

}

func (l *ListView) SetFilter(filter SearchFilter) {
l.selection = l.createModel()
l.columnView.SetModel(l.selection)
for i, object := range l.items {
if strings.Contains(object.GetName(), filter.Name) || len(filter.Name) == 0 {
l.selection.Model().Cast().(*gtk.StringList).Append(strconv.Itoa(i))
}
}
}

func (l *ListView) createColumns() []*gtk.ColumnViewColumn {
var columns []*gtk.ColumnViewColumn

Expand Down Expand Up @@ -273,3 +265,11 @@ func (l *ListView) listResource(ctx context.Context, gvr schema.GroupVersionReso
return res, nil
}
}

func (l *ListView) createModel() *gtk.SingleSelection {
selection := gtk.NewSingleSelection(gtk.NewStringList([]string{}))
selection.ConnectSelectionChanged(func(_, _ uint) {
l.root.detailView.SetObject(l.items[l.selection.Selected()])
})
return selection
}
27 changes: 27 additions & 0 deletions ui/search_bar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ui

import (
"github.com/diamondburned/gotk4/pkg/gtk/v4"
)

// TODO more sophisticated search - maybe using bleve? https://github.com/blevesearch/bleve
type SearchFilter struct {
Name string
}

type SearchBar struct {
*gtk.SearchEntry
}

func NewSearchBar(root *ClusterWindow) *SearchBar {
entry := gtk.NewSearchEntry()
bar := gtk.NewSearchBar()

bar.ConnectEntry(entry)

entry.ConnectSearchChanged(func() {
root.listView.SetFilter(SearchFilter{Name: entry.Text()})
})

return &SearchBar{entry}
}

0 comments on commit 375018b

Please sign in to comment.