richtext_objects.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. package widget
  2. import (
  3. "image/color"
  4. "net/url"
  5. "strconv"
  6. "fyne.io/fyne/v2"
  7. "fyne.io/fyne/v2/canvas"
  8. "fyne.io/fyne/v2/internal/scale"
  9. "fyne.io/fyne/v2/theme"
  10. )
  11. var (
  12. // RichTextStyleBlockquote represents a quote presented in an indented block.
  13. //
  14. // Since: 2.1
  15. RichTextStyleBlockquote = RichTextStyle{
  16. ColorName: theme.ColorNameForeground,
  17. Inline: false,
  18. SizeName: theme.SizeNameText,
  19. TextStyle: fyne.TextStyle{Italic: true},
  20. }
  21. // RichTextStyleCodeBlock represents a code blog segment.
  22. //
  23. // Since: 2.1
  24. RichTextStyleCodeBlock = RichTextStyle{
  25. ColorName: theme.ColorNameForeground,
  26. Inline: false,
  27. SizeName: theme.SizeNameText,
  28. TextStyle: fyne.TextStyle{Monospace: true},
  29. }
  30. // RichTextStyleCodeInline represents an inline code segment.
  31. //
  32. // Since: 2.1
  33. RichTextStyleCodeInline = RichTextStyle{
  34. ColorName: theme.ColorNameForeground,
  35. Inline: true,
  36. SizeName: theme.SizeNameText,
  37. TextStyle: fyne.TextStyle{Monospace: true},
  38. }
  39. // RichTextStyleEmphasis represents regular text with emphasis.
  40. //
  41. // Since: 2.1
  42. RichTextStyleEmphasis = RichTextStyle{
  43. ColorName: theme.ColorNameForeground,
  44. Inline: true,
  45. SizeName: theme.SizeNameText,
  46. TextStyle: fyne.TextStyle{Italic: true},
  47. }
  48. // RichTextStyleHeading represents a heading text that stands on its own line.
  49. //
  50. // Since: 2.1
  51. RichTextStyleHeading = RichTextStyle{
  52. ColorName: theme.ColorNameForeground,
  53. Inline: false,
  54. SizeName: theme.SizeNameHeadingText,
  55. TextStyle: fyne.TextStyle{Bold: true},
  56. }
  57. // RichTextStyleInline represents standard text that can be surrounded by other elements.
  58. //
  59. // Since: 2.1
  60. RichTextStyleInline = RichTextStyle{
  61. ColorName: theme.ColorNameForeground,
  62. Inline: true,
  63. SizeName: theme.SizeNameText,
  64. }
  65. // RichTextStyleParagraph represents standard text that should appear separate from other text.
  66. //
  67. // Since: 2.1
  68. RichTextStyleParagraph = RichTextStyle{
  69. ColorName: theme.ColorNameForeground,
  70. Inline: false,
  71. SizeName: theme.SizeNameText,
  72. }
  73. // RichTextStylePassword represents standard sized text where the characters are obscured.
  74. //
  75. // Since: 2.1
  76. RichTextStylePassword = RichTextStyle{
  77. ColorName: theme.ColorNameForeground,
  78. Inline: true,
  79. SizeName: theme.SizeNameText,
  80. concealed: true,
  81. }
  82. // RichTextStyleStrong represents regular text with a strong emphasis.
  83. //
  84. // Since: 2.1
  85. RichTextStyleStrong = RichTextStyle{
  86. ColorName: theme.ColorNameForeground,
  87. Inline: true,
  88. SizeName: theme.SizeNameText,
  89. TextStyle: fyne.TextStyle{Bold: true},
  90. }
  91. // RichTextStyleSubHeading represents a sub-heading text that stands on its own line.
  92. //
  93. // Since: 2.1
  94. RichTextStyleSubHeading = RichTextStyle{
  95. ColorName: theme.ColorNameForeground,
  96. Inline: false,
  97. SizeName: theme.SizeNameSubHeadingText,
  98. TextStyle: fyne.TextStyle{Bold: true},
  99. }
  100. )
  101. // HyperlinkSegment represents a hyperlink within a rich text widget.
  102. //
  103. // Since: 2.1
  104. type HyperlinkSegment struct {
  105. Alignment fyne.TextAlign
  106. Text string
  107. URL *url.URL
  108. // OnTapped overrides the default `fyne.OpenURL` call when the link is tapped
  109. //
  110. // Since: 2.4
  111. OnTapped func()
  112. }
  113. // Inline returns true as hyperlinks are inside other elements.
  114. func (h *HyperlinkSegment) Inline() bool {
  115. return true
  116. }
  117. // Textual returns the content of this segment rendered to plain text.
  118. func (h *HyperlinkSegment) Textual() string {
  119. return h.Text
  120. }
  121. // Visual returns the hyperlink widget required to render this segment.
  122. func (h *HyperlinkSegment) Visual() fyne.CanvasObject {
  123. link := NewHyperlink(h.Text, h.URL)
  124. link.Alignment = h.Alignment
  125. link.OnTapped = h.OnTapped
  126. return &fyne.Container{Layout: &unpadTextWidgetLayout{}, Objects: []fyne.CanvasObject{link}}
  127. }
  128. // Update applies the current state of this hyperlink segment to an existing visual.
  129. func (h *HyperlinkSegment) Update(o fyne.CanvasObject) {
  130. link := o.(*fyne.Container).Objects[0].(*Hyperlink)
  131. link.Text = h.Text
  132. link.URL = h.URL
  133. link.Alignment = h.Alignment
  134. link.OnTapped = h.OnTapped
  135. link.Refresh()
  136. }
  137. // Select tells the segment that the user is selecting the content between the two positions.
  138. func (h *HyperlinkSegment) Select(begin, end fyne.Position) {
  139. // no-op: this will be added when we progress to editor
  140. }
  141. // SelectedText should return the text representation of any content currently selected through the Select call.
  142. func (h *HyperlinkSegment) SelectedText() string {
  143. // no-op: this will be added when we progress to editor
  144. return ""
  145. }
  146. // Unselect tells the segment that the user is has cancelled the previous selection.
  147. func (h *HyperlinkSegment) Unselect() {
  148. // no-op: this will be added when we progress to editor
  149. }
  150. // ImageSegment represents an image within a rich text widget.
  151. //
  152. // Since: 2.3
  153. type ImageSegment struct {
  154. Source fyne.URI
  155. Title string
  156. // Alignment specifies the horizontal alignment of this image segment
  157. // Since: 2.4
  158. Alignment fyne.TextAlign
  159. }
  160. // Inline returns false as images in rich text are blocks.
  161. func (i *ImageSegment) Inline() bool {
  162. return false
  163. }
  164. // Textual returns the content of this segment rendered to plain text.
  165. func (i *ImageSegment) Textual() string {
  166. return "Image " + i.Title
  167. }
  168. // Visual returns the image widget required to render this segment.
  169. func (i *ImageSegment) Visual() fyne.CanvasObject {
  170. return newRichImage(i.Source, i.Alignment)
  171. }
  172. // Update applies the current state of this image segment to an existing visual.
  173. func (i *ImageSegment) Update(o fyne.CanvasObject) {
  174. newer := canvas.NewImageFromURI(i.Source)
  175. img := o.(*richImage)
  176. // one of the following will be used
  177. img.img.File = newer.File
  178. img.img.Resource = newer.Resource
  179. img.setAlign(i.Alignment)
  180. img.Refresh()
  181. }
  182. // Select tells the segment that the user is selecting the content between the two positions.
  183. func (i *ImageSegment) Select(begin, end fyne.Position) {
  184. // no-op: this will be added when we progress to editor
  185. }
  186. // SelectedText should return the text representation of any content currently selected through the Select call.
  187. func (i *ImageSegment) SelectedText() string {
  188. // no-op: images have no text rendering
  189. return ""
  190. }
  191. // Unselect tells the segment that the user is has cancelled the previous selection.
  192. func (i *ImageSegment) Unselect() {
  193. // no-op: this will be added when we progress to editor
  194. }
  195. // ListSegment includes an itemised list with the content set using the Items field.
  196. //
  197. // Since: 2.1
  198. type ListSegment struct {
  199. Items []RichTextSegment
  200. Ordered bool
  201. }
  202. // Inline returns false as a list should be in a block.
  203. func (l *ListSegment) Inline() bool {
  204. return false
  205. }
  206. // Segments returns the segments required to draw bullets before each item
  207. func (l *ListSegment) Segments() []RichTextSegment {
  208. out := make([]RichTextSegment, len(l.Items))
  209. for i, in := range l.Items {
  210. txt := "• "
  211. if l.Ordered {
  212. txt = strconv.Itoa(i+1) + "."
  213. }
  214. bullet := &TextSegment{Text: txt + " ", Style: RichTextStyleStrong}
  215. out[i] = &ParagraphSegment{Texts: []RichTextSegment{
  216. bullet,
  217. in,
  218. }}
  219. }
  220. return out
  221. }
  222. // Textual returns no content for a list as the content is in sub-segments.
  223. func (l *ListSegment) Textual() string {
  224. return ""
  225. }
  226. // Visual returns no additional elements for this segment.
  227. func (l *ListSegment) Visual() fyne.CanvasObject {
  228. return nil
  229. }
  230. // Update doesnt need to change a list visual.
  231. func (l *ListSegment) Update(fyne.CanvasObject) {
  232. }
  233. // Select does nothing for a list container.
  234. func (l *ListSegment) Select(_, _ fyne.Position) {
  235. }
  236. // SelectedText returns the empty string for this list.
  237. func (l *ListSegment) SelectedText() string {
  238. return ""
  239. }
  240. // Unselect does nothing for a list container.
  241. func (l *ListSegment) Unselect() {
  242. }
  243. // ParagraphSegment wraps a number of text elements in a paragraph.
  244. // It is similar to using a list of text elements when the final style is RichTextStyleParagraph.
  245. //
  246. // Since: 2.1
  247. type ParagraphSegment struct {
  248. Texts []RichTextSegment
  249. }
  250. // Inline returns false as a paragraph should be in a block.
  251. func (p *ParagraphSegment) Inline() bool {
  252. return false
  253. }
  254. // Segments returns the list of text elements in this paragraph.
  255. func (p *ParagraphSegment) Segments() []RichTextSegment {
  256. return p.Texts
  257. }
  258. // Textual returns no content for a paragraph container.
  259. func (p *ParagraphSegment) Textual() string {
  260. return ""
  261. }
  262. // Visual returns the no extra elements.
  263. func (p *ParagraphSegment) Visual() fyne.CanvasObject {
  264. return nil
  265. }
  266. // Update doesnt need to change a paragraph container.
  267. func (p *ParagraphSegment) Update(fyne.CanvasObject) {
  268. }
  269. // Select does nothing for a paragraph container.
  270. func (p *ParagraphSegment) Select(_, _ fyne.Position) {
  271. }
  272. // SelectedText returns the empty string for this paragraph container.
  273. func (p *ParagraphSegment) SelectedText() string {
  274. return ""
  275. }
  276. // Unselect does nothing for a paragraph container.
  277. func (p *ParagraphSegment) Unselect() {
  278. }
  279. // SeparatorSegment includes a horizontal separator in a rich text widget.
  280. //
  281. // Since: 2.1
  282. type SeparatorSegment struct{}
  283. // Inline returns false as a separator should be full width.
  284. func (s *SeparatorSegment) Inline() bool {
  285. return false
  286. }
  287. // Textual returns no content for a separator element.
  288. func (s *SeparatorSegment) Textual() string {
  289. return ""
  290. }
  291. // Visual returns the separator element for this segment.
  292. func (s *SeparatorSegment) Visual() fyne.CanvasObject {
  293. return NewSeparator()
  294. }
  295. // Update doesnt need to change a separator visual.
  296. func (s *SeparatorSegment) Update(fyne.CanvasObject) {
  297. }
  298. // Select does nothing for a separator.
  299. func (s *SeparatorSegment) Select(_, _ fyne.Position) {
  300. }
  301. // SelectedText returns the empty string for this separator.
  302. func (s *SeparatorSegment) SelectedText() string {
  303. return "" // TODO maybe return "---\n"?
  304. }
  305. // Unselect does nothing for a separator.
  306. func (s *SeparatorSegment) Unselect() {
  307. }
  308. // RichTextStyle describes the details of a text object inside a RichText widget.
  309. //
  310. // Since: 2.1
  311. type RichTextStyle struct {
  312. Alignment fyne.TextAlign
  313. ColorName fyne.ThemeColorName
  314. Inline bool
  315. SizeName fyne.ThemeSizeName
  316. TextStyle fyne.TextStyle
  317. // an internal detail where we obscure password fields
  318. concealed bool
  319. }
  320. // RichTextSegment describes any element that can be rendered in a RichText widget.
  321. //
  322. // Since: 2.1
  323. type RichTextSegment interface {
  324. Inline() bool
  325. Textual() string
  326. Update(fyne.CanvasObject)
  327. Visual() fyne.CanvasObject
  328. Select(pos1, pos2 fyne.Position)
  329. SelectedText() string
  330. Unselect()
  331. }
  332. // TextSegment represents the styling for a segment of rich text.
  333. //
  334. // Since: 2.1
  335. type TextSegment struct {
  336. Style RichTextStyle
  337. Text string
  338. }
  339. // Inline should return true if this text can be included within other elements, or false if it creates a new block.
  340. func (t *TextSegment) Inline() bool {
  341. return t.Style.Inline
  342. }
  343. // Textual returns the content of this segment rendered to plain text.
  344. func (t *TextSegment) Textual() string {
  345. return t.Text
  346. }
  347. // Visual returns the graphical elements required to render this segment.
  348. func (t *TextSegment) Visual() fyne.CanvasObject {
  349. obj := canvas.NewText(t.Text, t.color())
  350. t.Update(obj)
  351. return obj
  352. }
  353. // Update applies the current state of this text segment to an existing visual.
  354. func (t *TextSegment) Update(o fyne.CanvasObject) {
  355. obj := o.(*canvas.Text)
  356. obj.Text = t.Text
  357. obj.Color = t.color()
  358. obj.Alignment = t.Style.Alignment
  359. obj.TextStyle = t.Style.TextStyle
  360. obj.TextSize = t.size()
  361. obj.Refresh()
  362. }
  363. // Select tells the segment that the user is selecting the content between the two positions.
  364. func (t *TextSegment) Select(begin, end fyne.Position) {
  365. // no-op: this will be added when we progress to editor
  366. }
  367. // SelectedText should return the text representation of any content currently selected through the Select call.
  368. func (t *TextSegment) SelectedText() string {
  369. // no-op: this will be added when we progress to editor
  370. return ""
  371. }
  372. // Unselect tells the segment that the user is has cancelled the previous selection.
  373. func (t *TextSegment) Unselect() {
  374. // no-op: this will be added when we progress to editor
  375. }
  376. func (t *TextSegment) color() color.Color {
  377. if t.Style.ColorName != "" {
  378. return fyne.CurrentApp().Settings().Theme().Color(t.Style.ColorName, fyne.CurrentApp().Settings().ThemeVariant())
  379. }
  380. return theme.ForegroundColor()
  381. }
  382. func (t *TextSegment) size() float32 {
  383. if t.Style.SizeName != "" {
  384. return fyne.CurrentApp().Settings().Theme().Size(t.Style.SizeName)
  385. }
  386. return theme.TextSize()
  387. }
  388. type richImage struct {
  389. BaseWidget
  390. align fyne.TextAlign
  391. img *canvas.Image
  392. oldMin fyne.Size
  393. layout *fyne.Container
  394. min fyne.Size
  395. }
  396. func newRichImage(u fyne.URI, align fyne.TextAlign) *richImage {
  397. img := canvas.NewImageFromURI(u)
  398. img.FillMode = canvas.ImageFillOriginal
  399. i := &richImage{img: img, align: align}
  400. i.ExtendBaseWidget(i)
  401. return i
  402. }
  403. func (r *richImage) CreateRenderer() fyne.WidgetRenderer {
  404. r.layout = &fyne.Container{Layout: &richImageLayout{r}, Objects: []fyne.CanvasObject{r.img}}
  405. return NewSimpleRenderer(r.layout)
  406. }
  407. func (r *richImage) MinSize() fyne.Size {
  408. orig := r.img.MinSize()
  409. c := fyne.CurrentApp().Driver().CanvasForObject(r)
  410. if c == nil {
  411. return r.oldMin // not yet rendered
  412. }
  413. // unscale the image so it is not varying based on canvas
  414. w := scale.ToScreenCoordinate(c, orig.Width)
  415. h := scale.ToScreenCoordinate(c, orig.Height)
  416. // we return size / 2 as this assumes a HiDPI / 2x image scaling
  417. r.min = fyne.NewSize(float32(w)/2, float32(h)/2)
  418. return r.min
  419. }
  420. func (r *richImage) setAlign(a fyne.TextAlign) {
  421. if r.layout != nil {
  422. r.layout.Refresh()
  423. }
  424. r.align = a
  425. }
  426. type richImageLayout struct {
  427. r *richImage
  428. }
  429. func (r *richImageLayout) Layout(_ []fyne.CanvasObject, s fyne.Size) {
  430. r.r.img.Resize(r.r.min)
  431. gap := float32(0)
  432. switch r.r.align {
  433. case fyne.TextAlignCenter:
  434. gap = (s.Width - r.r.min.Width) / 2
  435. case fyne.TextAlignTrailing:
  436. gap = s.Width - r.r.min.Width
  437. }
  438. r.r.img.Move(fyne.NewPos(gap, 0))
  439. }
  440. func (r *richImageLayout) MinSize(_ []fyne.CanvasObject) fyne.Size {
  441. return r.r.min
  442. }
  443. type unpadTextWidgetLayout struct {
  444. }
  445. func (u *unpadTextWidgetLayout) Layout(o []fyne.CanvasObject, s fyne.Size) {
  446. pad := theme.InnerPadding() * -1
  447. pad2 := pad * -2
  448. o[0].Move(fyne.NewPos(pad, pad))
  449. o[0].Resize(s.Add(fyne.NewSize(pad2, pad2)))
  450. }
  451. func (u *unpadTextWidgetLayout) MinSize(o []fyne.CanvasObject) fyne.Size {
  452. pad := theme.InnerPadding() * 2
  453. return o[0].MinSize().Subtract(fyne.NewSize(pad, pad))
  454. }