richtext_objects.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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"
  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. }
  109. // Inline returns true as hyperlinks are inside other elements.
  110. func (h *HyperlinkSegment) Inline() bool {
  111. return true
  112. }
  113. // Textual returns the content of this segment rendered to plain text.
  114. func (h *HyperlinkSegment) Textual() string {
  115. return h.Text
  116. }
  117. // Visual returns the hyperlink widget required to render this segment.
  118. func (h *HyperlinkSegment) Visual() fyne.CanvasObject {
  119. link := NewHyperlink(h.Text, h.URL)
  120. link.Alignment = h.Alignment
  121. return &fyne.Container{Layout: &unpadTextWidgetLayout{}, Objects: []fyne.CanvasObject{link}}
  122. }
  123. // Update applies the current state of this hyperlink segment to an existing visual.
  124. func (h *HyperlinkSegment) Update(o fyne.CanvasObject) {
  125. link := o.(*fyne.Container).Objects[0].(*Hyperlink)
  126. link.Text = h.Text
  127. link.URL = h.URL
  128. link.Alignment = h.Alignment
  129. link.Refresh()
  130. }
  131. // Select tells the segment that the user is selecting the content between the two positions.
  132. func (h *HyperlinkSegment) Select(begin, end fyne.Position) {
  133. // no-op: this will be added when we progress to editor
  134. }
  135. // SelectedText should return the text representation of any content currently selected through the Select call.
  136. func (h *HyperlinkSegment) SelectedText() string {
  137. // no-op: this will be added when we progress to editor
  138. return ""
  139. }
  140. // Unselect tells the segment that the user is has cancelled the previous selection.
  141. func (h *HyperlinkSegment) Unselect() {
  142. // no-op: this will be added when we progress to editor
  143. }
  144. // ImageSegment represents an image within a rich text widget.
  145. //
  146. // Since: 2.3
  147. type ImageSegment struct {
  148. Source fyne.URI
  149. Title string
  150. }
  151. // Inline returns false as images in rich text are blocks.
  152. func (i *ImageSegment) Inline() bool {
  153. return false
  154. }
  155. // Textual returns the content of this segment rendered to plain text.
  156. func (i *ImageSegment) Textual() string {
  157. return "Image " + i.Title
  158. }
  159. // Visual returns the image widget required to render this segment.
  160. func (i *ImageSegment) Visual() fyne.CanvasObject {
  161. return newRichImage(i.Source)
  162. }
  163. // Update applies the current state of this image segment to an existing visual.
  164. func (i *ImageSegment) Update(o fyne.CanvasObject) {
  165. newer := canvas.NewImageFromURI(i.Source)
  166. img := o.(*richImage)
  167. // one of the following will be used
  168. img.img.File = newer.File
  169. img.img.Resource = newer.Resource
  170. img.Refresh()
  171. }
  172. // Select tells the segment that the user is selecting the content between the two positions.
  173. func (i *ImageSegment) Select(begin, end fyne.Position) {
  174. // no-op: this will be added when we progress to editor
  175. }
  176. // SelectedText should return the text representation of any content currently selected through the Select call.
  177. func (i *ImageSegment) SelectedText() string {
  178. // no-op: images have no text rendering
  179. return ""
  180. }
  181. // Unselect tells the segment that the user is has cancelled the previous selection.
  182. func (i *ImageSegment) Unselect() {
  183. // no-op: this will be added when we progress to editor
  184. }
  185. // ListSegment includes an itemised list with the content set using the Items field.
  186. //
  187. // Since: 2.1
  188. type ListSegment struct {
  189. Items []RichTextSegment
  190. Ordered bool
  191. }
  192. // Inline returns false as a list should be in a block.
  193. func (l *ListSegment) Inline() bool {
  194. return false
  195. }
  196. // Segments returns the segments required to draw bullets before each item
  197. func (l *ListSegment) Segments() []RichTextSegment {
  198. out := make([]RichTextSegment, len(l.Items))
  199. for i, in := range l.Items {
  200. txt := "• "
  201. if l.Ordered {
  202. txt = strconv.Itoa(i+1) + "."
  203. }
  204. bullet := &TextSegment{Text: txt + " ", Style: RichTextStyleStrong}
  205. if para, ok := in.(*ParagraphSegment); ok {
  206. seg := &ParagraphSegment{Texts: []RichTextSegment{bullet}}
  207. seg.Texts = append(seg.Texts, para.Texts...)
  208. out[i] = seg
  209. } else {
  210. out[i] = &ParagraphSegment{Texts: []RichTextSegment{
  211. bullet,
  212. in,
  213. }}
  214. }
  215. }
  216. return out
  217. }
  218. // Textual returns no content for a list as the content is in sub-segments.
  219. func (l *ListSegment) Textual() string {
  220. return ""
  221. }
  222. // Visual returns no additional elements for this segment.
  223. func (l *ListSegment) Visual() fyne.CanvasObject {
  224. return nil
  225. }
  226. // Update doesnt need to change a list visual.
  227. func (l *ListSegment) Update(fyne.CanvasObject) {
  228. }
  229. // Select does nothing for a list container.
  230. func (l *ListSegment) Select(_, _ fyne.Position) {
  231. }
  232. // SelectedText returns the empty string for this list.
  233. func (l *ListSegment) SelectedText() string {
  234. return ""
  235. }
  236. // Unselect does nothing for a list container.
  237. func (l *ListSegment) Unselect() {
  238. }
  239. // ParagraphSegment wraps a number of text elements in a paragraph.
  240. // It is similar to using a list of text elements when the final style is RichTextStyleParagraph.
  241. //
  242. // Since: 2.1
  243. type ParagraphSegment struct {
  244. Texts []RichTextSegment
  245. }
  246. // Inline returns false as a paragraph should be in a block.
  247. func (p *ParagraphSegment) Inline() bool {
  248. return false
  249. }
  250. // Segments returns the list of text elements in this paragraph.
  251. func (p *ParagraphSegment) Segments() []RichTextSegment {
  252. return p.Texts
  253. }
  254. // Textual returns no content for a paragraph container.
  255. func (p *ParagraphSegment) Textual() string {
  256. return ""
  257. }
  258. // Visual returns the no extra elements.
  259. func (p *ParagraphSegment) Visual() fyne.CanvasObject {
  260. return nil
  261. }
  262. // Update doesnt need to change a paragraph container.
  263. func (p *ParagraphSegment) Update(fyne.CanvasObject) {
  264. }
  265. // Select does nothing for a paragraph container.
  266. func (p *ParagraphSegment) Select(_, _ fyne.Position) {
  267. }
  268. // SelectedText returns the empty string for this paragraph container.
  269. func (p *ParagraphSegment) SelectedText() string {
  270. return ""
  271. }
  272. // Unselect does nothing for a paragraph container.
  273. func (p *ParagraphSegment) Unselect() {
  274. }
  275. // SeparatorSegment includes a horizontal separator in a rich text widget.
  276. //
  277. // Since: 2.1
  278. type SeparatorSegment struct {
  279. //lint:ignore U1000 This is required due to language design.
  280. dummy uint8 // without this a pointer to SeparatorSegment will always be the same
  281. }
  282. // Inline returns false as a separator should be full width.
  283. func (s *SeparatorSegment) Inline() bool {
  284. return false
  285. }
  286. // Textual returns no content for a separator element.
  287. func (s *SeparatorSegment) Textual() string {
  288. return ""
  289. }
  290. // Visual returns the separator element for this segment.
  291. func (s *SeparatorSegment) Visual() fyne.CanvasObject {
  292. return NewSeparator()
  293. }
  294. // Update doesnt need to change a separator visual.
  295. func (s *SeparatorSegment) Update(fyne.CanvasObject) {
  296. }
  297. // Select does nothing for a separator.
  298. func (s *SeparatorSegment) Select(_, _ fyne.Position) {
  299. }
  300. // SelectedText returns the empty string for this separator.
  301. func (s *SeparatorSegment) SelectedText() string {
  302. return "" // TODO maybe return "---\n"?
  303. }
  304. // Unselect does nothing for a separator.
  305. func (s *SeparatorSegment) Unselect() {
  306. }
  307. // RichTextStyle describes the details of a text object inside a RichText widget.
  308. //
  309. // Since: 2.1
  310. type RichTextStyle struct {
  311. Alignment fyne.TextAlign
  312. ColorName fyne.ThemeColorName
  313. Inline bool
  314. SizeName fyne.ThemeSizeName
  315. TextStyle fyne.TextStyle
  316. // an internal detail where we obscure password fields
  317. concealed bool
  318. }
  319. // RichTextSegment describes any element that can be rendered in a RichText widget.
  320. //
  321. // Since: 2.1
  322. type RichTextSegment interface {
  323. Inline() bool
  324. Textual() string
  325. Update(fyne.CanvasObject)
  326. Visual() fyne.CanvasObject
  327. Select(pos1, pos2 fyne.Position)
  328. SelectedText() string
  329. Unselect()
  330. }
  331. // TextSegment represents the styling for a segment of rich text.
  332. //
  333. // Since: 2.1
  334. type TextSegment struct {
  335. Style RichTextStyle
  336. Text string
  337. }
  338. // Inline should return true if this text can be included within other elements, or false if it creates a new block.
  339. func (t *TextSegment) Inline() bool {
  340. return t.Style.Inline
  341. }
  342. // Textual returns the content of this segment rendered to plain text.
  343. func (t *TextSegment) Textual() string {
  344. return t.Text
  345. }
  346. // Visual returns the graphical elements required to render this segment.
  347. func (t *TextSegment) Visual() fyne.CanvasObject {
  348. obj := canvas.NewText(t.Text, t.color())
  349. t.Update(obj)
  350. return obj
  351. }
  352. // Update applies the current state of this text segment to an existing visual.
  353. func (t *TextSegment) Update(o fyne.CanvasObject) {
  354. obj := o.(*canvas.Text)
  355. obj.Text = t.Text
  356. obj.Color = t.color()
  357. obj.Alignment = t.Style.Alignment
  358. obj.TextStyle = t.Style.TextStyle
  359. obj.TextSize = t.size()
  360. obj.Refresh()
  361. }
  362. // Select tells the segment that the user is selecting the content between the two positions.
  363. func (t *TextSegment) Select(begin, end fyne.Position) {
  364. // no-op: this will be added when we progress to editor
  365. }
  366. // SelectedText should return the text representation of any content currently selected through the Select call.
  367. func (t *TextSegment) SelectedText() string {
  368. // no-op: this will be added when we progress to editor
  369. return ""
  370. }
  371. // Unselect tells the segment that the user is has cancelled the previous selection.
  372. func (t *TextSegment) Unselect() {
  373. // no-op: this will be added when we progress to editor
  374. }
  375. func (t *TextSegment) color() color.Color {
  376. if t.Style.ColorName != "" {
  377. return fyne.CurrentApp().Settings().Theme().Color(t.Style.ColorName, fyne.CurrentApp().Settings().ThemeVariant())
  378. }
  379. return theme.ForegroundColor()
  380. }
  381. func (t *TextSegment) size() float32 {
  382. if t.Style.SizeName != "" {
  383. return fyne.CurrentApp().Settings().Theme().Size(t.Style.SizeName)
  384. }
  385. return theme.TextSize()
  386. }
  387. type richImage struct {
  388. BaseWidget
  389. img *canvas.Image
  390. oldMin fyne.Size
  391. }
  392. func newRichImage(u fyne.URI) *richImage {
  393. img := canvas.NewImageFromURI(u)
  394. img.FillMode = canvas.ImageFillOriginal
  395. i := &richImage{img: img}
  396. i.ExtendBaseWidget(i)
  397. return i
  398. }
  399. func (r *richImage) CreateRenderer() fyne.WidgetRenderer {
  400. return NewSimpleRenderer(r.img)
  401. }
  402. func (r *richImage) MinSize() fyne.Size {
  403. orig := r.img.MinSize()
  404. c := fyne.CurrentApp().Driver().CanvasForObject(r)
  405. if c == nil {
  406. return r.oldMin // not yet rendered
  407. }
  408. // unscale the image so it is not varying based on canvas
  409. w := internal.ScaleInt(c, orig.Width)
  410. h := internal.ScaleInt(c, orig.Height)
  411. // we return size / 2 as this assumes a HiDPI / 2x image scaling
  412. return fyne.NewSize(float32(w)/2, float32(h)/2)
  413. }
  414. type unpadTextWidgetLayout struct {
  415. }
  416. func (u *unpadTextWidgetLayout) Layout(o []fyne.CanvasObject, s fyne.Size) {
  417. pad := theme.InnerPadding() * -1
  418. pad2 := pad * -2
  419. o[0].Move(fyne.NewPos(pad, pad))
  420. o[0].Resize(s.Add(fyne.NewSize(pad2, pad2)))
  421. }
  422. func (u *unpadTextWidgetLayout) MinSize(o []fyne.CanvasObject) fyne.Size {
  423. pad := theme.InnerPadding() * 2
  424. return o[0].MinSize().Subtract(fyne.NewSize(pad, pad))
  425. }