Debian Codegraph @

app/partswitcher.go

raw
1 package app
2
3 import (
4 "math"
5
6 "git.sr.ht/~rjarry/aerc/config"
7 "git.sr.ht/~rjarry/aerc/lib/ui"
8 "github.com/mattn/go-runewidth"
9 "go.rockorager.dev/vaxis"
10 )
11
12 type PartSwitcher struct {
13 Scrollable
14 parts []*PartViewer
15 selected int
16
17 height int
18 offset int
19
20 uiConfig *config.UIConfig
21 }
22
23 func (ps *PartSwitcher) PreviousPart() {
24 for {
25 ps.selected--
26 if ps.selected < 0 {
27 ps.selected = len(ps.parts) - 1
28 }
29 if ps.parts[ps.selected].part.MIMEType != "multipart" {
30 break
31 }
32 }
33 }
34
35 func (ps *PartSwitcher) NextPart() {
36 for {
37 ps.selected++
38 if ps.selected >= len(ps.parts) {
39 ps.selected = 0
40 }
41 if ps.parts[ps.selected].part.MIMEType != "multipart" {
42 break
43 }
44 }
45 }
46
47 func (ps *PartSwitcher) SelectedPart() *PartViewer {
48 return ps.parts[ps.selected]
49 }
50
51 func (ps *PartSwitcher) AttachmentParts(all bool) []*PartInfo {
52 var attachments []*PartInfo
53 for _, p := range ps.parts {
54 if p.part.Disposition == "attachment" || (all && p.part.FileName() != "") {
55 pi := &PartInfo{
56 Index: p.index,
57 Msg: p.msg.MessageInfo(),
58 Part: p.part,
59 }
60 attachments = append(attachments, pi)
61 }
62 }
63 return attachments
64 }
65
66 func (ps *PartSwitcher) Invalidate() {
67 ui.Invalidate()
68 }
69
70 func (ps *PartSwitcher) Focus(focus bool) {
71 if ps.parts[ps.selected].term != nil {
72 ps.parts[ps.selected].term.Focus(focus)
73 }
74 }
75
76 func (ps *PartSwitcher) Show(visible bool) {
77 if ps.parts[ps.selected].term != nil {
78 ps.parts[ps.selected].term.Show(visible)
79 }
80 }
81
82 func (ps *PartSwitcher) Event(event vaxis.Event) bool {
83 return ps.parts[ps.selected].Event(event)
84 }
85
86 func (ps *PartSwitcher) Draw(ctx *ui.Context) {
87 uiConfig := ps.uiConfig
88 n := len(ps.parts)
89 part := ps.parts[ps.selected]
90 if n == 1 && !part.viewerConfig().AlwaysShowMime {
91 ps.offset = ctx.Height() // Off the screen <-> not really there.
92 part.Draw(ctx)
93 return
94 }
95
96 ps.height = part.viewerConfig().MaxMimeHeight
97 if ps.height <= 0 || n < ps.height {
98 ps.height = n
99 }
100 if ps.height > ctx.Height()/2 {
101 ps.height = ctx.Height() / 2
102 }
103
104 ps.UpdateScroller(ps.height, n)
105 ps.EnsureScroll(ps.selected)
106
107 var styleSwitcher, styleFile, styleMime vaxis.Style
108
109 scrollbarWidth := 0
110 if ps.NeedScrollbar() {
111 scrollbarWidth = 1
112 }
113
114 ps.offset = ctx.Height() - ps.height
115 y := ps.offset
116 row := ps.offset
117 ctx.Fill(0, y, ctx.Width(), ps.height, ' ', uiConfig.GetStyle(config.STYLE_PART_SWITCHER))
118 for i := ps.Scroll(); i < n; i++ {
119 part := ps.parts[i]
120 if ps.selected == i {
121 styleSwitcher = uiConfig.GetStyleSelected(config.STYLE_PART_SWITCHER)
122 styleFile = uiConfig.GetStyleSelected(config.STYLE_PART_FILENAME)
123 styleMime = uiConfig.GetStyleSelected(config.STYLE_PART_MIMETYPE)
124 } else {
125 styleSwitcher = uiConfig.GetStyle(config.STYLE_PART_SWITCHER)
126 styleFile = uiConfig.GetStyle(config.STYLE_PART_FILENAME)
127 styleMime = uiConfig.GetStyle(config.STYLE_PART_MIMETYPE)
128 }
129 ctx.Fill(0, row, ctx.Width(), 1, ' ', styleSwitcher)
130 left := len(part.index) * 2
131 if part.part.FileName() != "" {
132 name := runewidth.Truncate(part.part.FileName(),
133 ctx.Width()-left-1, "…")
134 left += ctx.Printf(left, row, styleFile, "%s ", name)
135 }
136 t := "(" + part.part.FullMIMEType() + ")"
137 t = runewidth.Truncate(t, ctx.Width()-left-scrollbarWidth, "…")
138 ctx.Printf(left, row, styleMime, "%s", t)
139 row++
140
141 if (i - ps.Scroll()) >= ps.height {
142 break
143 }
144 }
145 if ps.NeedScrollbar() {
146 ps.drawScrollbar(ctx.Subcontext(ctx.Width()-1, y, 1, ps.height))
147 }
148 ps.parts[ps.selected].Draw(ctx.Subcontext(
149 0, 0, ctx.Width(), ctx.Height()-ps.height))
150 }
151
152 func (ps *PartSwitcher) drawScrollbar(ctx *ui.Context) {
153 uiConfig := ps.uiConfig
154 gutterStyle := uiConfig.GetStyle(config.STYLE_MSGLIST_GUTTER)
155 pillStyle := uiConfig.GetStyle(config.STYLE_MSGLIST_PILL)
156
157 // gutter
158 ctx.Fill(0, 0, 1, ctx.Height(), ' ', gutterStyle)
159
160 // pill
161 pillSize := int(math.Ceil(float64(ctx.Height()) * ps.PercentVisible()))
162 pillOffset := int(math.Floor(float64(ctx.Height()) * ps.PercentScrolled()))
163 ctx.Fill(0, pillOffset, 1, pillSize, ' ', pillStyle)
164 }
165
166 func (ps *PartSwitcher) MouseEvent(localX int, localY int, event vaxis.Event) {
167 if localY < ps.offset && ps.parts[ps.selected].term != nil {
168 ps.parts[ps.selected].term.MouseEvent(localX, localY, event)
169 return
170 }
171
172 e, ok := event.(vaxis.Mouse)
173 if !ok {
174 return
175 }
176
177 if ps.parts[ps.selected].term != nil {
178 ps.parts[ps.selected].term.Focus(false)
179 }
180
181 switch e.Button {
182 case vaxis.MouseLeftButton:
183 i := localY - ps.offset + ps.Scroll()
184 if i < 0 || i >= len(ps.parts) {
185 break
186 }
187 if ps.parts[i].part.MIMEType == "multipart" {
188 break
189 }
190 ps.selected = i
191 ps.Invalidate()
192 case vaxis.MouseWheelDown:
193 ps.NextPart()
194 ps.Invalidate()
195 case vaxis.MouseWheelUp:
196 ps.PreviousPart()
197 ps.Invalidate()
198 }
199
200 if ps.parts[ps.selected].term != nil {
201 ps.parts[ps.selected].term.Focus(true)
202 }
203 }
204
205 func (ps *PartSwitcher) Cleanup() {
206 for _, partViewer := range ps.parts {
207 partViewer.Cleanup()
208 }
209 }