1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-17 11:42:48 +00:00
cchat-gtk/internal/ui/messages/message/sending.go

145 lines
3.6 KiB
Go
Raw Normal View History

2020-06-06 07:44:36 +00:00
package message
import (
2020-07-10 23:26:07 +00:00
"fmt"
2020-06-06 07:44:36 +00:00
"html"
2020-07-10 23:26:07 +00:00
"github.com/diamondburned/cchat-gtk/internal/humanize"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/messages/input"
2020-07-10 23:26:07 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/messages/input/attachment"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-07-10 23:26:07 +00:00
"github.com/gotk3/gotk3/gtk"
"github.com/gotk3/gotk3/pango"
)
var EmptyContentPlaceholder = fmt.Sprintf(
`<span alpha="25%%">%s</span>`, html.EscapeString("<empty>"),
2020-06-06 07:44:36 +00:00
)
type PresendContainer interface {
SetDone(id string)
SetLoading()
2020-06-06 07:44:36 +00:00
SetSentError(err error)
}
// PresendGenericContainer is the generic container with extra methods
// implemented for stateful mutability of the generic message container.
2020-06-06 07:44:36 +00:00
type GenericPresendContainer struct {
*GenericContainer
2020-07-10 23:26:07 +00:00
// states; to be cleared on SetDone()
presend input.PresendMessage
uploads *attachment.MessageUploader
2020-06-06 07:44:36 +00:00
}
var _ PresendContainer = (*GenericPresendContainer)(nil)
func NewPresendContainer(msg input.PresendMessage) *GenericPresendContainer {
2020-06-07 04:27:28 +00:00
return WrapPresendContainer(NewEmptyContainer(), msg)
}
func WrapPresendContainer(c *GenericContainer, msg input.PresendMessage) *GenericPresendContainer {
2020-06-06 07:44:36 +00:00
c.nonce = msg.Nonce()
c.authorID = msg.AuthorID()
c.UpdateTimestamp(msg.Time())
2020-06-06 07:44:36 +00:00
c.UpdateAuthorName(msg.Author())
p := &GenericPresendContainer{
GenericContainer: c,
2020-07-10 23:26:07 +00:00
presend: msg,
uploads: attachment.NewMessageUploader(msg.Files()),
2020-06-06 07:44:36 +00:00
}
p.SetLoading()
2020-06-06 07:44:36 +00:00
return p
}
func (m *GenericPresendContainer) SetSensitive(sensitive bool) {
2020-07-10 23:26:07 +00:00
m.contentBox.SetSensitive(sensitive)
2020-06-06 07:44:36 +00:00
}
func (m *GenericPresendContainer) SetDone(id string) {
2020-07-10 23:26:07 +00:00
// Apply the received ID.
m.id = id
m.nonce = ""
2020-07-10 23:26:07 +00:00
// Reset the state to be normal. Especially setting presend to nil should
// free it from memory.
m.presend = nil
m.uploads = nil
m.contentBox.SetTooltipText("")
// Remove everything in the content box.
m.clearBox()
// Re-add the content label.
m.contentBox.Add(m.ContentBody)
// Set the sensitivity from false in SetLoading back to true.
m.SetSensitive(true)
2020-06-06 07:44:36 +00:00
}
func (m *GenericPresendContainer) SetLoading() {
m.SetSensitive(false)
2020-07-10 23:26:07 +00:00
m.contentBox.SetTooltipText("")
// Clear everything inside the content container.
m.clearBox()
// Add the content label.
m.contentBox.Add(m.ContentBody)
// Add the attachment progress box back in, if any.
if m.uploads != nil {
m.uploads.Show() // show the bars
m.contentBox.Add(m.uploads)
}
2020-07-10 23:26:07 +00:00
if content := m.presend.Content(); content != "" {
m.ContentBody.SetText(content)
} else {
// Use a placeholder content if the actual content is empty.
m.ContentBody.SetMarkup(EmptyContentPlaceholder)
}
}
2020-06-07 04:27:28 +00:00
func (m *GenericPresendContainer) SetSentError(err error) {
m.SetSensitive(true) // allow events incl right clicks
2020-07-10 23:26:07 +00:00
m.contentBox.SetTooltipText(err.Error())
// Remove everything again.
m.clearBox()
// Re-add the label.
m.contentBox.Add(m.ContentBody)
// Style the label appropriately by making it red.
var content = EmptyContentPlaceholder
if m.presend != nil && m.presend.Content() != "" {
content = m.presend.Content()
2020-07-10 23:26:07 +00:00
}
m.ContentBody.SetMarkup(fmt.Sprintf(`<span color="red">%s</span>`, content))
// Add a smaller label indicating an error.
errl, _ := gtk.LabelNew("")
errl.SetXAlign(0)
errl.SetLineWrap(true)
errl.SetLineWrapMode(pango.WRAP_WORD_CHAR)
errl.SetMarkup(fmt.Sprintf(
`<span size="small" color="red"><b>Error:</b> %s</span>`,
html.EscapeString(humanize.Error(err)),
))
errl.Show()
m.contentBox.Add(errl)
}
// clearBox clears everything inside the content container.
func (m *GenericPresendContainer) clearBox() {
primitives.ForeachChild(m.contentBox, func(v interface{}) (stop bool) {
2020-07-10 23:26:07 +00:00
m.contentBox.Remove(v.(gtk.IWidget))
return false
2020-07-10 23:26:07 +00:00
})
2020-06-06 07:44:36 +00:00
}