Skip to content

Commit

Permalink
GUI + Provider: Filter on all display languages
Browse files Browse the repository at this point in the history
  • Loading branch information
jwijenbergh committed Dec 9, 2024
1 parent 2971449 commit 5c81e3e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
21 changes: 8 additions & 13 deletions cmd/geteduroam-gui/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,18 @@ type SelectList struct {
win *gtk.ScrolledWindow
list *gtk.ListView
activated func(int)
sorter func(a, b string) int
filter func(a string) bool
sorter func(a, b int) int
filter func(idx int) bool
store *gtk.StringList
cf *gtk.CustomFilter
cs *gtk.CustomSorter
}

func stringFromPtr(ptr uintptr) string {
func indexFromPtr(ptr uintptr) int {
// TODO: Remove this once we have proper callback type signatures
// The callback should already give a gobject.Binding
thisl := gobject.BindingNewFromInternalPtr(ptr)
var thisv gobject.Value
thisl.GetProperty("string", &thisv)
// Purego makes a copy of the string when we call .GetString()
// So we can safely unset after the return
defer thisv.Unset()
return thisv.GetString()
return int(thisl.GetData("model-index"))
}

func setupList(item uintptr) {
Expand All @@ -53,7 +48,7 @@ func bindList(item uintptr) {
label.SetText(strobj.GetString())
}

func NewSelectList(win *gtk.ScrolledWindow, list *gtk.ListView, activated func(int), sorter func(a, b string) int) *SelectList {
func NewSelectList(win *gtk.ScrolledWindow, list *gtk.ListView, activated func(int), sorter func(a, b int) int) *SelectList {
return &SelectList{
win: win,
list: list,
Expand Down Expand Up @@ -92,7 +87,7 @@ func (s *SelectList) Hide() {
s.win.Hide()
}

func (s *SelectList) WithFiltering(filter func(a string) bool) *SelectList {
func (s *SelectList) WithFiltering(filter func(idx int) bool) *SelectList {
s.filter = filter
return s
}
Expand Down Expand Up @@ -123,7 +118,7 @@ func (s *SelectList) setupFactory() *gtk.SignalListItemFactory {

func (s *SelectList) setupSorter(base gio.ListModel) gio.ListModel {
sf := (glib.CompareDataFunc)(func(this uintptr, other uintptr, _ uintptr) int {
return s.sorter(stringFromPtr(this), stringFromPtr(other))
return s.sorter(indexFromPtr(this), indexFromPtr(other))
})

destroycb := (glib.DestroyNotify)(func(uintptr) {
Expand All @@ -139,7 +134,7 @@ func (s *SelectList) setupSorter(base gio.ListModel) gio.ListModel {

func (s *SelectList) setupFilter(base gio.ListModel) gio.ListModel {
cf := (gtk.CustomFilterFunc)(func(item uintptr, _ uintptr) bool {
return s.filter(stringFromPtr(item))
return s.filter(indexFromPtr(item))
})
destroycb := (glib.DestroyNotify)(func(uintptr) {
// do nothing
Expand Down
32 changes: 28 additions & 4 deletions cmd/geteduroam-gui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ func (s *serverList) get(idx int, query string) (*provider.Provider, error) {
return &s.providers[idx], nil
}

func (s *serverList) getNames(idx int, query string) (*provider.LocalizedStrings, error) {
if s.custom && idx == len(s.providers) {
return &provider.LocalizedStrings{{Display: query}}, nil
}
if idx < 0 || idx > len(s.providers) {
return nil, errors.New("index out of range")
}
return &s.providers[idx].Name, nil
}

func (s *serverList) Fill() {
s.Lock()
defer s.Unlock()
Expand Down Expand Up @@ -252,12 +262,26 @@ func (m *mainState) initList() {
}()
}

sorter := func(a, b string) int {
return provider.SortNames(a, b, search.GetText())
sorter := func(a, b int) int {
query := search.GetText()
n1, err := m.servers.getNames(a, query)
if err != nil {
return -1
}
n2, err := m.servers.getNames(b, query)
if err != nil {
return -1
}
return provider.SortNames(*n1, *n2, query)
}

m.servers.list = NewSelectList(m.scroll, &list, activated, sorter).WithFiltering(func(a string) bool {
return provider.FilterSingle(a, search.GetText())
m.servers.list = NewSelectList(m.scroll, &list, activated, sorter).WithFiltering(func(idx int) bool {
query := search.GetText()
n, err := m.servers.getNames(idx, query)
if err != nil {
return false
}
return provider.FilterSingle(*n, query)
})

// Fill the servers in the select list
Expand Down
4 changes: 2 additions & 2 deletions cmd/geteduroam-gui/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (p *ProfileState) Initialize() {
defer label.Unref()
styleWidget(&label, "label")

sorter := func(a, b string) int {
sorter := func(a, b int) int {
// Here we have no search query
return provider.SortNames(a, b, "")
return provider.SortNames(p.profiles[a].Name, p.profiles[b].Name, "")
}
activated := func(idx int) {
go p.success(p.profiles[idx])
Expand Down
22 changes: 15 additions & 7 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ type LocalizedString struct {

type LocalizedStrings []LocalizedString

func (ls LocalizedStrings) Corpus() string {
var corpus strings.Builder
for _, v := range ls {
corpus.WriteString(v.Display)
}
return corpus.String()
}

var systemLanguage = language.English

func setSystemLanguage() {
Expand Down Expand Up @@ -78,9 +86,9 @@ type Provider struct {

type Providers []Provider

func SortNames(a string, b string, search string) int {
la := strings.ToLower(a)
lb := strings.ToLower(b)
func SortNames(a LocalizedStrings, b LocalizedStrings, search string) int {
la := strings.ToLower(a.Corpus())
lb := strings.ToLower(b.Corpus())
bd := strings.Compare(la, lb)
// compute the base difference which is based on alphabetical order
// if no search is defined return the base difference
Expand Down Expand Up @@ -109,13 +117,13 @@ type ByName struct {
func (s ByName) Len() int { return len(s.Providers) }
func (s ByName) Swap(i, j int) { s.Providers[i], s.Providers[j] = s.Providers[j], s.Providers[i] }
func (s ByName) Less(i, j int) bool {
diff := SortNames(s.Providers[i].Name.Get(), s.Providers[j].Name.Get(), s.Search)
diff := SortNames(s.Providers[i].Name, s.Providers[j].Name, s.Search)
// if i is less than j, diff returns less than 0
return diff < 0
}

func FilterSingle(name string, search string) bool {
l1, err1 := utils.RemoveDiacritics(strings.ToLower(name))
func FilterSingle(name LocalizedStrings, search string) bool {
l1, err1 := utils.RemoveDiacritics(strings.ToLower(name.Corpus()))
l2, err2 := utils.RemoveDiacritics(strings.ToLower(search))
if err1 != nil || err2 != nil {
return false
Expand All @@ -134,7 +142,7 @@ func (i *Providers) FilterSort(search string) *Providers {
Search: search,
}
for _, i := range *i {
if FilterSingle(i.Name.Get(), search) {
if FilterSingle(i.Name, search) {
x.Providers = append(x.Providers, i)
}
}
Expand Down

0 comments on commit 5c81e3e

Please sign in to comment.