From 2960c15408414847fc7bb91057454ed24fb4c953 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Tue, 2 Nov 2021 02:11:18 -0700 Subject: [PATCH] Fix last channel map --- gtkcord/components/channel/channels.go | 27 ++++++++++++++++---------- gtkcord/components/message/input.go | 2 ++ gtkcord/components/message/messages.go | 10 ++++++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/gtkcord/components/channel/channels.go b/gtkcord/components/channel/channels.go index 4ca582a..b2d08ea 100644 --- a/gtkcord/components/channel/channels.go +++ b/gtkcord/components/channel/channels.go @@ -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) { @@ -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) { @@ -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) }) @@ -129,6 +130,12 @@ 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) @@ -136,8 +143,8 @@ func (chs *Channels) LoadGuild(guildID discord.GuildID) { // async 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) diff --git a/gtkcord/components/message/input.go b/gtkcord/components/message/input.go index 5331f56..a7a85ee 100644 --- a/gtkcord/components/message/input.go +++ b/gtkcord/components/message/input.go @@ -444,6 +444,8 @@ func (i *Input) send(content string) { } }) }() + + return } // If the content is empty but we're not editing, don't send. diff --git a/gtkcord/components/message/messages.go b/gtkcord/components/message/messages.go index a89c64d..61c20cb 100644 --- a/gtkcord/components/message/messages.go +++ b/gtkcord/components/message/messages.go @@ -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.