Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
diamondburned
GitHub Repository: diamondburned/gtkcord4
Path: blob/main/internal/window/quickswitcher/index.go
366 views
1
package quickswitcher
2
3
import (
4
"context"
5
"log/slog"
6
"slices"
7
8
"github.com/diamondburned/arikawa/v3/discord"
9
"github.com/diamondburned/gotkit/app"
10
"github.com/sahilm/fuzzy"
11
"libdb.so/dissent/internal/gtkcord"
12
)
13
14
type index struct {
15
items indexItems
16
buffer indexItems
17
}
18
19
const searchLimit = 25
20
21
var excludedChannelTypes = []discord.ChannelType{
22
discord.GuildCategory,
23
discord.GuildForum,
24
}
25
26
var allowedChannelTypes = slices.DeleteFunc(
27
slices.Clone(gtkcord.AllowedChannelTypes),
28
func(t discord.ChannelType) bool {
29
return slices.Contains(excludedChannelTypes, t)
30
},
31
)
32
33
func (idx *index) update(ctx context.Context) {
34
state := gtkcord.FromContext(ctx).Offline()
35
items := make([]indexItem, 0, 250)
36
37
dms, err := state.PrivateChannels()
38
if err != nil {
39
app.Error(ctx, err)
40
return
41
}
42
43
for i := range dms {
44
items = append(items, newChannelItem(state, nil, &dms[i]))
45
}
46
47
guilds, err := state.Guilds()
48
if err != nil {
49
app.Error(ctx, err)
50
return
51
}
52
53
for i, guild := range guilds {
54
chs, err := state.Channels(guild.ID, allowedChannelTypes)
55
if err != nil {
56
slog.Error(
57
"cannot populate channels for guild in quick switcher",
58
"guild", guild.Name,
59
"guild_id", guild.ID,
60
"err", err)
61
continue
62
}
63
64
items = append(items, newGuildItem(&guilds[i]))
65
for j := range chs {
66
items = append(items, newChannelItem(state, &guilds[i], &chs[j]))
67
}
68
}
69
70
idx.items = items
71
}
72
73
func (idx *index) search(str string) []indexItem {
74
if idx.items == nil {
75
return nil
76
}
77
78
idx.buffer = idx.buffer[:0]
79
if idx.buffer == nil {
80
idx.buffer = make([]indexItem, 0, searchLimit)
81
}
82
83
matches := fuzzy.FindFrom(str, idx.items)
84
for i := 0; i < len(matches) && i < searchLimit; i++ {
85
idx.buffer = append(idx.buffer, idx.items[matches[i].Index])
86
}
87
88
return idx.buffer
89
}
90
91