richtext_objects.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. out[i] = &ParagraphSegment{Texts: []RichTextSegment{
  206. bullet,
  207. in,
  208. }}
  209. }
  210. return out
  211. }
  212. // Textual returns no content for a list as the content is in sub-segments.
  213. func (l *ListSegment) Textual() string {
  214. return ""
  215. }
  216. // Visual returns no additional elements for this segment.
  217. func (l *ListSegment) Visual() fyne.CanvasObject {
  218. return nil
  219. }
  220. // Update doesnt need to change a list visual.
  221. func (l *ListSegment) Update(fyne.CanvasObject) {
  222. }
  223. // Select does nothing for a list container.
  224. func (l *ListSegment) Select(_, _ fyne.Position) {
  225. }
  226. // SelectedText returns the empty string for this list.
  227. func (l *ListSegment) SelectedText() string {
  228. return ""
  229. }
  230. // Unselect does nothing for a list container.
  231. func (l *ListSegment) Unselect() {
  232. }
  233. // ParagraphSegment wraps a number of text elements in a paragraph.
  234. // It is similar to using a list of text elements when the final style is RichTextStyleParagraph.
  235. //
  236. // Since: 2.1
  237. type ParagraphSegment struct {
  238. Texts []RichTextSegment
  239. }
  240. // Inline returns false as a paragraph should be in a block.
  241. func (p *ParagraphSegment) Inline() bool {
  242. return false
  243. }
  244. // Segments returns the list of text elements in this paragraph.
  245. func (p *ParagraphSegment) Segments() []RichTextSegment {
  246. return p.Texts
  247. }
  248. // Textual returns no content for a paragraph container.
  249. func (p *ParagraphSegment) Textual() string {
  250. return ""
  251. }
  252. // Visual returns the no extra elements.
  253. func (p *ParagraphSegment) Visual() fyne.CanvasObject {
  254. return nil
  255. }
  256. // Update doesnt need to change a paragraph container.
  257. func (p *ParagraphSegment) Update(fyne.CanvasObject) {
  258. }
  259. // Select does nothing for a paragraph container.
  260. func (p *ParagraphSegment) Select(_, _ fyne.Position) {
  261. }
  262. // SelectedText returns the empty string for this paragraph container.
  263. func (p *ParagraphSegment) SelectedText() string {
  264. return ""
  265. }
  266. // Unselect does nothing for a paragraph container.
  267. func (p *ParagraphSegment) Unselect() {
  268. }
  269. // SeparatorSegment includes a horizontal separator in a rich text widget.
  270. //
  271. // Since: 2.1
  272. type SeparatorSegment struct {
  273. //lint:ignore U1000 This is required due to language design.
  274. dummy uint8 // without this a pointer to SeparatorSegment will always be the same
  275. }
  276. // Inline returns false as a separator should be full width.
  277. func (s *SeparatorSegment) Inline() bool {
  278. return false
  279. }
  280. // Textual returns no content for a separator element.
  281. func (s *SeparatorSegment) Textual() string {
  282. return ""
  283. }
  284. // Visual returns the separator element for this segment.
  285. func (s *SeparatorSegment) Visual() fyne.CanvasObject {
  286. return NewSeparator()
  287. }
  288. // Update doesnt need to change a separator visual.
  289. func (s *SeparatorSegment) Update(fyne.CanvasObject) {
  290. }
  291. // Select does nothing for a separator.
  292. func (s *SeparatorSegment) Select(_, _ fyne.Position) {
  293. }
  294. // SelectedText returns the empty string for this separator.
  295. func (s *SeparatorSegment) SelectedText() string {
  296. return "" // TODO maybe return "---\n"?
  297. }
  298. // Unselect does nothing for a separator.
  299. func (s *SeparatorSegment) Unselect() {
  300. }
  301. // RichTextStyle describes the details of a text object inside a RichText widget.
  302. //
  303. // Since: 2.1
  304. type RichTextStyle struct {
  305. Alignment fyne.TextAlign
  306. ColorName fyne.ThemeColorName
  307. Inline bool
  308. SizeName fyne.ThemeSizeName
  309. TextStyle fyne.TextStyle
  310. // an internal detail where we obscure password fields
  311. concealed bool
  312. }
  313. // RichTextSegment describes any element that can be rendered in a RichText widget.
  314. //
  315. // Since: 2.1
  316. type RichTextSegment interface {
  317. Inline() bool
  318. Textual() string
  319. Update(fyne.CanvasObject)
  320. Visual() fyne.CanvasObject
  321. Select(pos1, pos2 fyne.Position)
  322. SelectedText() string
  323. Unselect()
  324. }
  325. // TextSegment represents the styling for a segment of rich text.
  326. //
  327. // Since: 2.1
  328. type TextSegment struct {
  329. Style RichTextStyle
  330. Text string
  331. }
  332. // Inline should return true if this text can be included within other elements, or false if it creates a new block.
  333. func (t *TextSegment) Inline() bool {
  334. return t.Style.Inline
  335. }
  336. // Textual returns the content of this segment rendered to plain text.
  337. func (t *TextSegment) Textual() string {
  338. return t.Text
  339. }
  340. // Visual returns the graphical elements required to render this segment.
  341. func (t *TextSegment) Visual() fyne.CanvasObject {
  342. obj := canvas.NewText(t.Text, t.color())
  343. t.Update(obj)
  344. return obj
  345. }
  346. // Update applies the current state of this text segment to an existing visual.
  347. func (t *TextSegment) Update(o fyne.CanvasObject) {
  348. obj := o.(*canvas.Text)
  349. obj.Text = t.Text
  350. obj.Color = t.color()
  351. obj.Alignment = t.Style.Alignment
  352. obj.TextStyle = t.Style.TextStyle
  353. obj.TextSize = t.size()
  354. obj.Refresh()
  355. }
  356. // Select tells the segment that the user is selecting the content between the two positions.
  357. func (t *TextSegment) Select(begin, end fyne.Position) {
  358. // no-op: this will be added when we progress to editor
  359. }
  360. // SelectedText should return the text representation of any content currently selected through the Select call.
  361. func (t *TextSegment) SelectedText() string {
  362. // no-op: this will be added when we progress to editor
  363. return ""
  364. }
  365. // Unselect tells the segment that the user is has cancelled the previous selection.
  366. func (t *TextSegment) Unselect() {
  367. // no-op: this will be added when we progress to editor
  368. }
  369. func (t *TextSegment) color() color.Color {
  370. if t.Style.ColorName != "" {
  371. return fyne.CurrentApp().Settings().Theme().Color(t.Style.ColorName, fyne.CurrentApp().Settings().ThemeVariant())
  372. }
  373. return theme.ForegroundColor()
  374. }
  375. func (t *TextSegment) size() float32 {
  376. if t.Style.SizeName != "" {
  377. return fyne.CurrentApp().Settings().Theme().Size(t.Style.SizeName)
  378. }
  379. return theme.TextSize()
  380. }
  381. type richImage struct {
  382. BaseWidget
  383. img *canvas.Image
  384. oldMin fyne.Size
  385. }
  386. func newRichImage(u fyne.URI) *richImage {
  387. img := canvas.NewImageFromURI(u)
  388. img.FillMode = canvas.ImageFillOriginal
  389. i := &richImage{img: img}
  390. i.ExtendBaseWidget(i)
  391. return i
  392. }
  393. func (r *richImage) CreateRenderer() fyne.WidgetRenderer {
  394. return NewSimpleRenderer(r.img)
  395. }
  396. func (r *richImage) MinSize() fyne.Size {
  397. orig := r.img.MinSize()
  398. c := fyne.CurrentApp().Driver().CanvasForObject(r)
  399. if c == nil {
  400. return r.oldMin // not yet rendered
  401. }
  402. // unscale the image so it is not varying based on canvas
  403. w := internal.ScaleInt(c, orig.Width)
  404. h := internal.ScaleInt(c, orig.Height)
  405. // we return size / 2 as this assumes a HiDPI / 2x image scaling
  406. return fyne.NewSize(float32(w)/2, float32(h)/2)
  407. }
  408. type unpadTextWidgetLayout struct {
  409. }
  410. func (u *unpadTextWidgetLayout) Layout(o []fyne.CanvasObject, s fyne.Size) {
  411. pad := theme.InnerPadding() * -1
  412. pad2 := pad * -2
  413. o[0].Move(fyne.NewPos(pad, pad))
  414. o[0].Resize(s.Add(fyne.NewSize(pad2, pad2)))
  415. }
  416. func (u *unpadTextWidgetLayout) MinSize(o []fyne.CanvasObject) fyne.Size {
  417. pad := theme.InnerPadding() * 2
  418. return o[0].MinSize().Subtract(fyne.NewSize(pad, pad))
  419. }