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

138 lines
3.4 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 {
2021-01-05 02:05:33 +00:00
c := NewEmptyContainer()
2020-06-06 07:44:36 +00:00
c.nonce = msg.Nonce()
2021-01-05 02:05:33 +00:00
c.UpdateAuthor(msg.Author())
c.UpdateTimestamp(msg.Time())
2020-06-06 07:44:36 +00:00
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) {
2021-01-05 02:05:33 +00:00
m.Content.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
2021-01-05 02:05:33 +00:00
m.Content.SetTooltipText("")
2020-07-10 23:26:07 +00:00
// Remove everything in the content box.
m.clearBox()
// Re-add the content label.
2021-01-05 02:05:33 +00:00
m.Content.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)
2021-01-05 02:05:33 +00:00
m.Content.SetTooltipText("")
2020-07-10 23:26:07 +00:00
// Clear everything inside the content container.
m.clearBox()
// Add the content label.
2021-01-05 02:05:33 +00:00
m.Content.Add(m.ContentBody)
2020-07-10 23:26:07 +00:00
// Add the attachment progress box back in, if any.
if m.uploads != nil {
m.uploads.Show() // show the bars
2021-01-05 02:05:33 +00:00
m.Content.Add(m.uploads)
2020-07-10 23:26:07 +00:00
}
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
2021-01-05 02:05:33 +00:00
m.Content.SetTooltipText(err.Error())
2020-07-10 23:26:07 +00:00
// Remove everything again.
m.clearBox()
// Re-add the label.
2021-01-05 02:05:33 +00:00
m.Content.Add(m.ContentBody)
2020-07-10 23:26:07 +00:00
// 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()
2021-01-05 02:05:33 +00:00
m.Content.Add(errl)
2020-07-10 23:26:07 +00:00
}
// clearBox clears everything inside the content container.
func (m *GenericPresendContainer) clearBox() {
2021-01-05 02:05:33 +00:00
primitives.RemoveChildren(m.Content)
2020-06-06 07:44:36 +00:00
}