Skip to content
This repository has been archived by the owner on Apr 5, 2022. It is now read-only.

Commit

Permalink
Fix last channel map
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Nov 2, 2021
1 parent de66905 commit 2960c15
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
27 changes: 17 additions & 10 deletions gtkcord/components/channel/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Channels struct {
OnSelect func(ch *Channel)
GuildID discord.GuildID

lastSelected discord.ChannelID
lastSelected map[discord.GuildID]discord.ChannelID
}

func NewChannels(state *ningen.State, onSelect func(ch *Channel)) (chs *Channels) {
Expand All @@ -61,12 +61,13 @@ func NewChannels(state *ningen.State, onSelect func(ch *Channel)) (chs *Channels
page.SetChild(cs)

chs = &Channels{
Page: page,
Scroll: cs,
Main: main,
ChList: cl,
state: state,
OnSelect: onSelect,
Page: page,
Scroll: cs,
Main: main,
ChList: cl,
state: state,
OnSelect: onSelect,
lastSelected: make(map[discord.GuildID]discord.ChannelID),
}

cl.Connect("row-activated", func(l *gtk.ListBox, r *gtk.ListBoxRow) {
Expand All @@ -75,7 +76,7 @@ func NewChannels(state *ningen.State, onSelect func(ch *Channel)) (chs *Channels
}

chs.Selected = chs.Channels[r.Index()]
chs.lastSelected = chs.Selected.ID
chs.lastSelected[chs.GuildID] = chs.Selected.ID
chs.OnSelect(chs.Selected)
})

Expand Down Expand Up @@ -129,15 +130,21 @@ func (chs *Channels) LoadGuild(guildID discord.GuildID) { // async
}

glib.IdleAdd(func() {
// Ensure that the guild ID is still the same, in that the user
// hasn't clicked away while we were loading.
if guildID != chs.GuildID {
return
}

chs.SetDone()
chs.Channels = transformChannels(chs.state, channels)

for _, ch := range chs.Channels {
chs.ChList.Insert(ch, -1)
}

if chs.lastSelected.IsValid() {
lastCh := chs.FindByID(chs.lastSelected)
if lastChID := chs.lastSelected[guildID]; lastChID.IsValid() {
lastCh := chs.FindByID(lastChID)
if lastCh != nil {
// Restore the last accessed channel.
chs.ChList.SelectRow(lastCh.Row)
Expand Down
2 changes: 2 additions & 0 deletions gtkcord/components/message/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ func (i *Input) send(content string) {
}
})
}()

return
}

// If the content is empty but we're not editing, don't send.
Expand Down
10 changes: 8 additions & 2 deletions gtkcord/components/message/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,14 @@ func (m *Messages) Load(channelID discord.ChannelID) {
})

glib.IdleAdd(func() {
// Allocate a new empty slice. This is a trade-off to re-using the old
// slice to re-use messages.
// Ensure that the channel ID is still the same, in that the user
// hasn't clicked away while we were loading.
if m.channelID != channelID {
return
}

// Allocate a new empty slice. This is a trade-off to re-using the
// old slice to re-use messages.
m.messages = make([]*Message, 0, m.fetch)

// Iterate from earliest to latest, in a thread-safe function.
Expand Down

0 comments on commit 2960c15

Please sign in to comment.