Skip to content

Commit

Permalink
Add alignment for RichText ImageSegment
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Jul 24, 2023
1 parent dd3155c commit bd8ca23
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/fyne_demo/tutorials/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ This styled row should also wrap as expected, but only *when required*.
> An interesting quote here, most likely sharing some very interesting wisdom.`)
rich.Scroll = container.ScrollBoth
rich.Segments[2].(*widget.ImageSegment).Alignment = fyne.TextAlignTrailing

radioAlign := widget.NewRadioGroup([]string{"Text Alignment Leading", "Text Alignment Center", "Text Alignment Trailing"}, func(s string) {
var align fyne.TextAlign
Expand Down
2 changes: 1 addition & 1 deletion widget/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func makeImage(n *ast.Image) *ImageSegment {
if err != nil {
u = storage.NewFileURI(dest)
}
return &ImageSegment{Source: u, Title: string(n.Title)}
return &ImageSegment{Source: u, Title: string(n.Title), Alignment: fyne.TextAlignCenter}
}

func makeLink(n *ast.Link) *HyperlinkSegment {
Expand Down
34 changes: 28 additions & 6 deletions widget/richtext_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/scale"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
)

Expand Down Expand Up @@ -166,6 +167,10 @@ func (h *HyperlinkSegment) Unselect() {
type ImageSegment struct {
Source fyne.URI
Title string

// Alignment specifies the horizontal alignment of this image segment
// Since: 2.4
Alignment fyne.TextAlign
}

// Inline returns false as images in rich text are blocks.
Expand All @@ -180,7 +185,7 @@ func (i *ImageSegment) Textual() string {

// Visual returns the image widget required to render this segment.
func (i *ImageSegment) Visual() fyne.CanvasObject {
return newRichImage(i.Source)
return newRichImage(i.Source, i.Alignment)
}

// Update applies the current state of this image segment to an existing visual.
Expand All @@ -191,6 +196,7 @@ func (i *ImageSegment) Update(o fyne.CanvasObject) {
// one of the following will be used
img.img.File = newer.File
img.img.Resource = newer.Resource
img.setAlign(i.Alignment)

img.Refresh()
}
Expand Down Expand Up @@ -452,20 +458,26 @@ func (t *TextSegment) size() float32 {

type richImage struct {
BaseWidget
img *canvas.Image
oldMin fyne.Size
align fyne.TextAlign
img *canvas.Image
oldMin fyne.Size
pad1, pad2 fyne.CanvasObject
}

func newRichImage(u fyne.URI) *richImage {
func newRichImage(u fyne.URI, align fyne.TextAlign) *richImage {
img := canvas.NewImageFromURI(u)
img.FillMode = canvas.ImageFillOriginal
i := &richImage{img: img}
i := &richImage{img: img, align: align}
i.ExtendBaseWidget(i)
return i
}

func (r *richImage) CreateRenderer() fyne.WidgetRenderer {
return NewSimpleRenderer(r.img)
r.pad1 = layout.NewSpacer()
r.pad2 = layout.NewSpacer()
c := &fyne.Container{Layout: layout.NewHBoxLayout(), Objects: []fyne.CanvasObject{r.pad1, r.img, r.pad2}}
r.setAlign(r.align)
return NewSimpleRenderer(c)
}

func (r *richImage) MinSize() fyne.Size {
Expand All @@ -482,6 +494,16 @@ func (r *richImage) MinSize() fyne.Size {
return fyne.NewSize(float32(w)/2, float32(h)/2)
}

func (r *richImage) setAlign(a fyne.TextAlign) {
if a == fyne.TextAlignLeading && r.pad1 != nil {
r.pad1.Hide()
}
if a == fyne.TextAlignTrailing && r.pad2 != nil {
r.pad2.Hide()
}
r.align = a
}

type unpadTextWidgetLayout struct {
}

Expand Down

0 comments on commit bd8ca23

Please sign in to comment.