| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package messages |
| 18 |
|
| 19 |
import ( |
| 20 |
"go.mau.fi/mauview" |
| •21 |
"github.com/gdamore/tcell/v2" |
| 22 |
|
| 23 |
"maunium.net/go/gomuks/matrix/muksevt" |
| 24 |
|
| 25 |
"maunium.net/go/gomuks/config" |
| 26 |
"maunium.net/go/gomuks/ui/messages/html" |
| 27 |
) |
| 28 |
|
| 29 |
type HTMLMessage struct { |
| 30 |
Root html.Entity |
| 31 |
TextColor tcell.Color |
| 32 |
} |
| 33 |
|
| 34 |
func NewHTMLMessage(evt *muksevt.Event, displayname string, root html.Entity) *UIMessage { |
| 35 |
return newUIMessage(evt, displayname, &HTMLMessage{ |
| 36 |
Root: root, |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 40 |
func (hw *HTMLMessage) Clone() MessageRenderer { |
| 41 |
return &HTMLMessage{ |
| 42 |
Root: hw.Root.Clone(), |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func (hw *HTMLMessage) Draw(screen mauview.Screen, msg *UIMessage) { |
| 47 |
if hw.TextColor != tcell.ColorDefault { |
| 48 |
hw.Root.AdjustStyle(func(style tcell.Style) tcell.Style { |
| 49 |
fg, _, _ := style.Decompose() |
| 50 |
if fg == tcell.ColorDefault { |
| 51 |
return style.Foreground(hw.TextColor) |
| 52 |
} |
| 53 |
return style |
| 54 |
}, html.AdjustStyleReasonNormal) |
| 55 |
} |
| 56 |
screen.Clear() |
| 57 |
hw.Root.Draw(screen, html.DrawContext{IsSelected: msg.IsSelected}) |
| 58 |
} |
| 59 |
|
| 60 |
func (hw *HTMLMessage) OnKeyEvent(event mauview.KeyEvent) bool { |
| 61 |
return false |
| 62 |
} |
| 63 |
|
| 64 |
func (hw *HTMLMessage) OnMouseEvent(event mauview.MouseEvent) bool { |
| 65 |
return false |
| 66 |
} |
| 67 |
|
| 68 |
func (hw *HTMLMessage) OnPasteEvent(event mauview.PasteEvent) bool { |
| 69 |
return false |
| 70 |
} |
| 71 |
|
| 72 |
func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width int, msg *UIMessage) { |
| 73 |
if width < 2 { |
| 74 |
return |
| 75 |
} |
| 76 |
// TODO account for bare messages in initial startX |
| 77 |
startX := 0 |
| 78 |
hw.TextColor = msg.TextColor() |
| 79 |
hw.Root.CalculateBuffer(width, startX, html.DrawContext{ |
| 80 |
IsSelected: msg.IsSelected, |
| 81 |
BareMessages: preferences.BareMessageView, |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
func (hw *HTMLMessage) Height() int { |
| 86 |
return hw.Root.Height() |
| 87 |
} |
| 88 |
|
| 89 |
func (hw *HTMLMessage) PlainText() string { |
| 90 |
return hw.Root.PlainText() |
| 91 |
} |
| 92 |
|
| 93 |
func (hw *HTMLMessage) NotificationContent() string { |
| 94 |
return hw.Root.PlainText() |
| 95 |
} |
| 96 |
|
| 97 |
func (hw *HTMLMessage) String() string { |
| 98 |
return hw.Root.String() |
| 99 |
} |