Debian Codegraph @

ui/messages/html/spoiler.go

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // gomuks - A terminal Matrix client written in Go.
2 // Copyright (C) 2022 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 html
18
19 import (
20 "fmt"
21 "strings"
22
23 "go.mau.fi/mauview"
24 "go.mau.fi/tcell"
25 )
26
27 type SpoilerEntity struct {
28 reason string
29 hidden *ContainerEntity
30 visible *ContainerEntity
31 }
32
33 const SpoilerColor = tcell.ColorYellow
34
35 func NewSpoilerEntity(visible *ContainerEntity, reason string) *SpoilerEntity {
36 hidden := visible.Clone().(*ContainerEntity)
37 hidden.AdjustStyle(func(style tcell.Style) tcell.Style {
38 return style.Foreground(SpoilerColor).Background(SpoilerColor)
39 }, AdjustStyleReasonHideSpoiler)
40 if len(reason) > 0 {
41 reasonEnt := NewTextEntity(fmt.Sprintf("(%s)", reason))
42 hidden.Children = append([]Entity{reasonEnt}, hidden.Children...)
43 visible.Children = append([]Entity{reasonEnt}, visible.Children...)
44 }
45 return &SpoilerEntity{
46 reason: reason,
47 hidden: hidden,
48 visible: visible,
49 }
50 }
51
52 func (se *SpoilerEntity) Clone() Entity {
53 return &SpoilerEntity{
54 reason: se.reason,
55 hidden: se.hidden.Clone().(*ContainerEntity),
56 visible: se.visible.Clone().(*ContainerEntity),
57 }
58 }
59
60 func (se *SpoilerEntity) IsBlock() bool {
61 return false
62 }
63
64 func (se *SpoilerEntity) GetTag() string {
65 return "span"
66 }
67
68 func (se *SpoilerEntity) Draw(screen mauview.Screen, ctx DrawContext) {
69 if ctx.IsSelected {
70 se.visible.Draw(screen, ctx)
71 } else {
72 se.hidden.Draw(screen, ctx)
73 }
74 }
75
76 func (se *SpoilerEntity) AdjustStyle(fn AdjustStyleFunc, reason AdjustStyleReason) Entity {
77 if reason != AdjustStyleReasonHideSpoiler {
78 se.hidden.AdjustStyle(func(style tcell.Style) tcell.Style {
79 return fn(style).Foreground(SpoilerColor).Background(SpoilerColor)
80 }, reason)
81 se.visible.AdjustStyle(fn, reason)
82 }
83 return se
84 }
85
86 func (se *SpoilerEntity) PlainText() string {
87 if len(se.reason) > 0 {
88 return fmt.Sprintf("spoiler: %s", se.reason)
89 } else {
90 return "spoiler"
91 }
92 }
93
94 func (se *SpoilerEntity) String() string {
95 var buf strings.Builder
96 _, _ = fmt.Fprintf(&buf, `&html.SpoilerEntity{reason=%s`, se.reason)
97 buf.WriteString("\n visible=")
98 buf.WriteString(strings.Join(strings.Split(strings.TrimRight(se.visible.String(), "\n"), "\n"), "\n "))
99 buf.WriteString("\n hidden=")
100 buf.WriteString(strings.Join(strings.Split(strings.TrimRight(se.hidden.String(), "\n"), "\n"), "\n "))
101 buf.WriteString("\n]},")
102 return buf.String()
103 }
104
105 func (se *SpoilerEntity) Height() int {
106 return se.visible.Height()
107 }
108
109 func (se *SpoilerEntity) CalculateBuffer(width, startX int, ctx DrawContext) int {
110 se.hidden.CalculateBuffer(width, startX, ctx)
111 return se.visible.CalculateBuffer(width, startX, ctx)
112 }
113
114 func (se *SpoilerEntity) getStartX() int {
115 return se.visible.getStartX()
116 }
117
118 func (se *SpoilerEntity) IsEmpty() bool {
119 return se.visible.IsEmpty()
120 }