richtext_objects.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. _ bool // Without this a pointer to SeparatorSegment will always be the same.
  284. }
  285. // Inline returns false as a separator should be full width.
  286. func (s *SeparatorSegment) Inline() bool {
  287. return false
  288. }
  289. // Textual returns no content for a separator element.
  290. func (s *SeparatorSegment) Textual() string {
  291. return ""
  292. }
  293. // Visual returns the separator element for this segment.
  294. func (s *SeparatorSegment) Visual() fyne.CanvasObject {
  295. return NewSeparator()
  296. }
  297. // Update doesnt need to change a separator visual.
  298. func (s *SeparatorSegment) Update(fyne.CanvasObject) {
  299. }
  300. // Select does nothing for a separator.
  301. func (s *SeparatorSegment) Select(_, _ fyne.Position) {
  302. }
  303. // SelectedText returns the empty string for this separator.
  304. func (s *SeparatorSegment) SelectedText() string {
  305. return "" // TODO maybe return "---\n"?
  306. }
  307. // Unselect does nothing for a separator.
  308. func (s *SeparatorSegment) Unselect() {
  309. }
  310. // RichTextStyle describes the details of a text object inside a RichText widget.
  311. //
  312. // Since: 2.1
  313. type RichTextStyle struct {
  314. Alignment fyne.TextAlign
  315. ColorName fyne.ThemeColorName
  316. Inline bool
  317. SizeName fyne.ThemeSizeName
  318. TextStyle fyne.TextStyle
  319. // an internal detail where we obscure password fields
  320. concealed bool
  321. }
  322. // RichTextSegment describes any element that can be rendered in a RichText widget.
  323. //
  324. // Since: 2.1
  325. type RichTextSegment interface {
  326. Inline() bool
  327. Textual() string
  328. Update(fyne.CanvasObject)
  329. Visual() fyne.CanvasObject
  330. Select(pos1, pos2 fyne.Position)
  331. SelectedText() string
  332. Unselect()
  333. }
  334. // TextSegment represents the styling for a segment of rich text.
  335. //
  336. // Since: 2.1
  337. type TextSegment struct {
  338. Style RichTextStyle
  339. Text string
  340. }
  341. // Inline should return true if this text can be included within other elements, or false if it creates a new block.
  342. func (t *TextSegment) Inline() bool {
  343. return t.Style.Inline
  344. }
  345. // Textual returns the content of this segment rendered to plain text.
  346. func (t *TextSegment) Textual() string {
  347. return t.Text
  348. }
  349. // Visual returns the graphical elements required to render this segment.
  350. func (t *TextSegment) Visual() fyne.CanvasObject {
  351. obj := canvas.NewText(t.Text, t.color())
  352. t.Update(obj)
  353. return obj
  354. }
  355. // Update applies the current state of this text segment to an existing visual.
  356. func (t *TextSegment) Update(o fyne.CanvasObject) {
  357. obj := o.(*canvas.Text)
  358. obj.Text = t.Text
  359. obj.Color = t.color()
  360. obj.Alignment = t.Style.Alignment
  361. obj.TextStyle = t.Style.TextStyle
  362. obj.TextSize = t.size()
  363. obj.Refresh()
  364. }
  365. // Select tells the segment that the user is selecting the content between the two positions.
  366. func (t *TextSegment) Select(begin, end fyne.Position) {
  367. // no-op: this will be added when we progress to editor
  368. }
  369. // SelectedText should return the text representation of any content currently selected through the Select call.
  370. func (t *TextSegment) SelectedText() string {
  371. // no-op: this will be added when we progress to editor
  372. return ""
  373. }
  374. // Unselect tells the segment that the user is has cancelled the previous selection.
  375. func (t *TextSegment) Unselect() {
  376. // no-op: this will be added when we progress to editor
  377. }
  378. func (t *TextSegment) color() color.Color {
  379. if t.Style.ColorName != "" {
  380. return fyne.CurrentApp().Settings().Theme().Color(t.Style.ColorName, fyne.CurrentApp().Settings().ThemeVariant())
  381. }
  382. return theme.ForegroundColor()
  383. }
  384. func (t *TextSegment) size() float32 {
  385. if t.Style.SizeName != "" {
  386. return fyne.CurrentApp().Settings().Theme().Size(t.Style.SizeName)
  387. }
  388. return theme.TextSize()
  389. }
  390. type richImage struct {
  391. BaseWidget
  392. align fyne.TextAlign
  393. img *canvas.Image
  394. oldMin fyne.Size
  395. layout *fyne.Container
  396. min fyne.Size
  397. }
  398. func newRichImage(u fyne.URI, align fyne.TextAlign) *richImage {
  399. img := canvas.NewImageFromURI(u)
  400. img.FillMode = canvas.ImageFillOriginal
  401. i := &richImage{img: img, align: align}
  402. i.ExtendBaseWidget(i)
  403. return i
  404. }
  405. func (r *richImage) CreateRenderer() fyne.WidgetRenderer {
  406. r.layout = &fyne.Container{Layout: &richImageLayout{r}, Objects: []fyne.CanvasObject{r.img}}
  407. return NewSimpleRenderer(r.layout)
  408. }
  409. func (r *richImage) MinSize() fyne.Size {
  410. orig := r.img.MinSize()
  411. c := fyne.CurrentApp().Driver().CanvasForObject(r)
  412. if c == nil {
  413. return r.oldMin // not yet rendered
  414. }
  415. // unscale the image so it is not varying based on canvas
  416. w := scale.ToScreenCoordinate(c, orig.Width)
  417. h := scale.ToScreenCoordinate(c, orig.Height)
  418. // we return size / 2 as this assumes a HiDPI / 2x image scaling
  419. r.min = fyne.NewSize(float32(w)/2, float32(h)/2)
  420. return r.min
  421. }
  422. func (r *richImage) setAlign(a fyne.TextAlign) {
  423. if r.layout != nil {
  424. r.layout.Refresh()
  425. }
  426. r.align = a
  427. }
  428. type richImageLayout struct {
  429. r *richImage
  430. }
  431. func (r *richImageLayout) Layout(_ []fyne.CanvasObject, s fyne.Size) {
  432. r.r.img.Resize(r.r.min)
  433. gap := float32(0)
  434. switch r.r.align {
  435. case fyne.TextAlignCenter:
  436. gap = (s.Width - r.r.min.Width) / 2
  437. case fyne.TextAlignTrailing:
  438. gap = s.Width - r.r.min.Width
  439. }
  440. r.r.img.Move(fyne.NewPos(gap, 0))
  441. }
  442. func (r *richImageLayout) MinSize(_ []fyne.CanvasObject) fyne.Size {
  443. return r.r.min
  444. }
  445. type unpadTextWidgetLayout struct {
  446. }
  447. func (u *unpadTextWidgetLayout) Layout(o []fyne.CanvasObject, s fyne.Size) {
  448. pad := theme.InnerPadding() * -1
  449. pad2 := pad * -2
  450. o[0].Move(fyne.NewPos(pad, pad))
  451. o[0].Resize(s.Add(fyne.NewSize(pad2, pad2)))
  452. }
  453. func (u *unpadTextWidgetLayout) MinSize(o []fyne.CanvasObject) fyne.Size {
  454. pad := theme.InnerPadding() * 2
  455. return o[0].MinSize().Subtract(fyne.NewSize(pad, pad))
  456. }