patched away all errors

This commit is contained in:
diamondburned 2021-01-05 00:02:26 -08:00
parent ee041b3cc9
commit 907d1cdd1c
6 changed files with 57 additions and 17 deletions

2
go.mod
View File

@ -2,7 +2,7 @@ module github.com/diamondburned/cchat-gtk
go 1.14
replace github.com/gotk3/gotk3 => github.com/diamondburned/gotk3 v0.0.0-20210103063209-2bfa1d9dd9a6
replace github.com/gotk3/gotk3 => github.com/diamondburned/gotk3 v0.0.0-20210105074419-f7ce52e1018d
// replace github.com/diamondburned/cchat-discord => ../cchat-discord
// replace github.com/diamondburned/gotk3-tcmalloc => ../../gotk3-tcmalloc

2
go.sum
View File

@ -108,6 +108,8 @@ github.com/diamondburned/gotk3 v0.0.0-20201230071527-a77c32eb3876 h1:8Hacuan9iBg
github.com/diamondburned/gotk3 v0.0.0-20201230071527-a77c32eb3876/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/diamondburned/gotk3 v0.0.0-20210103063209-2bfa1d9dd9a6 h1:dJwNpjlgUP3YTPll5LvqyhpuaBPjITUuDes7Wq8HrTM=
github.com/diamondburned/gotk3 v0.0.0-20210103063209-2bfa1d9dd9a6/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/diamondburned/gotk3 v0.0.0-20210105074419-f7ce52e1018d h1:y08tIKgKqGqiIPGU63o2gKJimmG5FwaPaM8Pl7TQex4=
github.com/diamondburned/gotk3 v0.0.0-20210105074419-f7ce52e1018d/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/diamondburned/gspell v0.0.0-20200830182722-77e5d27d6894 h1:QgI21deaQbCUMnxKkQQUXzQolnAe1dMIXAWwqAyOp2g=
github.com/diamondburned/gspell v0.0.0-20200830182722-77e5d27d6894/go.mod h1:IoyMxPKSJOMoP0BiBuFwf2RDMeA4Uqx0HPKN5BzqTtA=
github.com/diamondburned/gspell v0.0.0-20201229064336-e43698fd5828 h1:Lm1F+GwrDdAaaMzrR7AYl4GGd/T+FE2OgOz25QWwsIg=

View File

@ -74,7 +74,7 @@ func NewListStore(ctrl Controller, constr Constructor) *ListStore {
}
func (c *ListStore) Reset() {
primitives.RemoveChildren(c.ListBox)
// Delegate removing children to the constructor.
c.messages = make(map[messageKey]*messageRow, BacklogLimit+1)
}
@ -128,6 +128,8 @@ func (c *ListStore) Around(id cchat.ID) (before, after MessageRow) {
}
func (c *ListStore) around(aroundID cchat.ID) (before, after *messageRow) {
c.ensureEmpty()
var last *messageRow
var next bool
@ -167,6 +169,8 @@ func (c *ListStore) LatestMessageFrom(userID string) (msgID string, ok bool) {
// findIndex searches backwards for id.
func (c *ListStore) findIndex(findID cchat.ID) (found *messageRow, index int) {
c.ensureEmpty()
// Faster implementation of findMessage: no map lookup is done until an ID
// match, so the worst case is a single string hash.
index = c.MessagesLen() - 1
@ -179,7 +183,7 @@ func (c *ListStore) findIndex(findID cchat.ID) (found *messageRow, index int) {
}
index--
return index == 0
return index <= 0
})
// Preserve old behavior.
@ -191,6 +195,8 @@ func (c *ListStore) findIndex(findID cchat.ID) (found *messageRow, index int) {
}
func (c *ListStore) findMessage(presend bool, fn func(*messageRow) bool) (*messageRow, int) {
c.ensureEmpty()
var r *messageRow
var i = c.MessagesLen() - 1
@ -208,7 +214,7 @@ func (c *ListStore) findMessage(presend bool, fn func(*messageRow) bool) (*messa
}
i--
return false
return i <= 0
})
// Preserve old behavior.
@ -287,9 +293,19 @@ func (c *ListStore) message(msgID cchat.ID, nonce string) *messageRow {
return nil
}
// ensureEmpty ensures that if the message map is empty, then the container
// should also be.
func (c *ListStore) ensureEmpty() {
if len(c.messages) == 0 {
primitives.RemoveChildren(c.ListBox)
}
}
// AddPresendMessage inserts an input.PresendMessage into the container and
// returning a wrapped widget interface.
func (c *ListStore) AddPresendMessage(msg input.PresendMessage) PresendMessageRow {
c.ensureEmpty()
before := c.LastMessage()
presend := c.Construct.NewPresendMessage(msg, before)
@ -331,6 +347,8 @@ func (c *ListStore) CreateMessageUnsafe(msg cchat.MessageCreate) MessageRow {
return msgc.MessageRow
}
c.ensureEmpty()
msgTime := msg.Time()
// Iterate and compare timestamp to find where to insert a message. Note
@ -413,6 +431,8 @@ func (c *ListStore) DeleteEarliest(n int) {
return
}
c.ensureEmpty()
// Since container/list nils out the next element, we can't just call Next
// after deleting, so we have to call Next manually before Removing.
primitives.ForeachChild(c.ListBox, func(v interface{}) (stop bool) {

View File

@ -63,10 +63,17 @@ func (f *Field) sendInput() {
return
}
// Derive the author. Prefer the author of the current user from the message
// buffer over the one in the username feed, unless we can't find any.
var author cchat.Author = f.ctrl.MessageAuthor(f.UserID)
if author == nil {
author = newAuthor(f)
}
f.SendMessage(SendMessageData{
time: time.Now().UTC(),
content: text,
author: newAuthor(f),
author: author,
nonce: f.generateNonce(),
replyID: f.replyingID,
files: attachments,

View File

@ -85,11 +85,6 @@ func (c *Container) Reset() {
}
c.Revealer.SetRevealChild(false)
for _, section := range c.Sections {
section.Destroy()
}
c.Sections = map[string]*Section{}
}
@ -129,6 +124,10 @@ type sectionInsert struct {
}
func (c *Container) SetSectionsUnsafe(sections []cchat.MemberSection) {
// Lazily invalidate the container. We could delegate removing old sections
// to this function instead of Reset to not halt for too long.
primitives.RemoveChildren(c.Main)
var newSections = make([]*Section, len(sections))
for i, section := range sections {
@ -143,8 +142,7 @@ func (c *Container) SetSectionsUnsafe(sections []cchat.MemberSection) {
}
// Remove all old sections.
for id, section := range c.Sections {
c.Main.Remove(section)
for id := range c.Sections {
delete(c.Sections, id)
}

View File

@ -25,16 +25,26 @@ type Container interface {
var _ Container = (*gtk.Container)(nil)
// RemoveChildren removes all children from the given container. Most of the
// time, DestroyChildren should be preferred if no children will be reused.
func RemoveChildren(w Container) {
// type destroyer interface {
// Destroy()
// }
w.GetChildren().FreeFull(func(child interface{}) {
w.Remove(child.(gtk.IWidget))
})
}
// DestroyChildren destroys all children of the given container, removing and
// freeing them at the same time.
func DestroyChildren(w Container) {
type destroyer interface {
Destroy()
}
w.GetChildren().FreeFull(func(child interface{}) {
child.(destroyer).Destroy()
})
}
// ChildrenLen gets the total count of children for the given container.
func ChildrenLen(w Container) int {
children := w.GetChildren()
@ -227,11 +237,14 @@ func MenuItem(label string, fn interface{}) *gtk.MenuItem {
type Connector interface {
Connect(string, interface{}) glib.SignalHandle
ConnectAfter(string, interface{}) glib.SignalHandle
HandlerDisconnect(glib.SignalHandle)
}
var _ Connector = (*glib.Object)(nil)
func HandleDestroyCtx(ctx context.Context, connector Connector) context.Context {
ctx, cancel := context.WithCancel(ctx)
connector.Connect("destroy", func(c Connector) { cancel() })
connector.Connect("destroy", func(interface{}) { cancel() })
return ctx
}