ui/uiwidget/label.go

244 lines
6.7 KiB
Go
Raw Normal View History

2024-03-30 17:45:07 +01:00
package uiwidget
import (
"fmt"
"strings"
"gitea.hevanto-it.com/hevanto/ui/uilayout"
2024-03-30 17:45:07 +01:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
2024-04-03 16:07:24 +02:00
// LabelOpts defines the options for a label.
2024-03-30 17:45:07 +01:00
type LabelOpts interface {
ApplyTo(*widget.Label)
}
type allignmentOpt struct {
Alignment fyne.TextAlign
}
2024-04-03 16:07:24 +02:00
// AlignmentOpt returns a LabelOpts with the specified alignment.
2024-03-30 17:45:07 +01:00
func AlignmentOpt(alignment fyne.TextAlign) LabelOpts {
return &allignmentOpt{alignment}
}
2024-04-03 16:07:24 +02:00
// ApplyTo sets the alignment of a label to the specified alignment.
2024-03-30 17:45:07 +01:00
func (a *allignmentOpt) ApplyTo(lbl *widget.Label) {
lbl.Alignment = a.Alignment
}
type wrappingOpt struct {
Wrapping fyne.TextWrap
}
2024-04-03 16:07:24 +02:00
// WrappingOpt creates a LabelOpts with the specified text wrapping option.
2024-03-30 17:45:07 +01:00
func WrappingOpt(wrapping fyne.TextWrap) LabelOpts {
return &wrappingOpt{wrapping}
}
2024-04-03 16:07:24 +02:00
// ApplyTo applies the wrapping option to the label.
2024-03-30 17:45:07 +01:00
func (w *wrappingOpt) ApplyTo(lbl *widget.Label) {
lbl.Wrapping = w.Wrapping
}
type textStyleOpt struct {
Style fyne.TextStyle
}
2024-04-03 16:07:24 +02:00
// TextStyleOpt returns the label options for the given text style.
2024-03-30 17:45:07 +01:00
func TextStyleOpt(style fyne.TextStyle) LabelOpts {
return &textStyleOpt{style}
}
2024-04-03 16:07:24 +02:00
// ApplyTo applies the text style option to the label.
2024-03-30 17:45:07 +01:00
func (t *textStyleOpt) ApplyTo(lbl *widget.Label) {
lbl.TextStyle = t.Style
}
type truncationOpt struct {
Truncation fyne.TextTruncation
}
2024-04-03 16:07:24 +02:00
// TruncationOpt returns the label options for the given text truncation option.
2024-03-30 17:45:07 +01:00
func TruncationOpt(truncation fyne.TextTruncation) LabelOpts {
return &truncationOpt{truncation}
}
2024-04-03 16:07:24 +02:00
// ApplyTo applies the text truncation option to the label.
2024-03-30 17:45:07 +01:00
func (t *truncationOpt) ApplyTo(lbl *widget.Label) {
lbl.Truncation = t.Truncation
}
type importanceOpt struct {
Importance widget.Importance
}
2024-04-03 16:07:24 +02:00
// ImportanceOpt returns the label options for the given text importance option.
2024-03-30 17:45:07 +01:00
func ImportanceOpt(importance widget.Importance) LabelOpts {
return &importanceOpt{importance}
}
2024-04-03 16:07:24 +02:00
// ApplyTo applies the text importance option to the label.
2024-03-30 17:45:07 +01:00
func (i *importanceOpt) ApplyTo(lbl *widget.Label) {
lbl.Importance = i.Importance
}
2024-04-03 16:07:24 +02:00
// NewLabel creates a new label widget with the given text and options.
//
// Arguments
// - text: the text to be displayed on the label.
// - opts: optional configuration options for the label.
//
// Returns *widget.Label: a pointer to the newly created label widget.
2024-03-30 17:45:07 +01:00
func NewLabel(text string, opts ...LabelOpts) *widget.Label {
lbl := widget.NewLabel(text)
for _, opt := range opts {
opt.ApplyTo(lbl)
}
return lbl
}
2024-04-03 16:07:24 +02:00
// NewLabelWithData creates a new Label widget with the provided data and options.
//
// Arguments
// - data: The string data to be displayed on the label.
// - opts: Additional options to customize the label.
//
// Returns *widget.Label: a pointer to the newly created Label widget.
2024-03-30 17:45:07 +01:00
func NewLabelWithData(data binding.String, opts ...LabelOpts) *widget.Label {
lbl := widget.NewLabelWithData(data)
for _, opt := range opts {
opt.ApplyTo(lbl)
}
return lbl
}
2024-04-03 16:07:24 +02:00
// NewUpDownLabelWithData creates a new label widget with up and down buttons.
//
// Arguments
// - data: the string data to display in the label.
// - upHandler: the function to call when the up button is clicked.
// - downHandler: the function to call when the down button is clicked.
//
// Returns fyne.CanvasObject: the created widget.
2024-03-30 17:45:07 +01:00
func NewUpDownLabelWithData(
2024-04-11 14:52:32 +02:00
minSize *fyne.Size,
2024-03-30 17:45:07 +01:00
data binding.String,
upHandler func(),
downHandler func(),
) fyne.CanvasObject {
c2 := container.NewWithoutLayout()
w := NewWidgetBorder(
widget.NewLabelWithData(data),
)
bu := widget.NewButtonWithIcon("", theme.MoveUpIcon(), upHandler)
bd := widget.NewButtonWithIcon("", theme.MoveDownIcon(), downHandler)
w.Resize(fyne.NewSize(100, w.MinSize().Height))
c2.Move(fyne.NewPos(w.Size().Width, 0))
bu.Resize(fyne.NewSize(30, w.Size().Height*0.5))
bd.Resize(fyne.NewSize(30, w.Size().Height*0.5))
bd.Move(fyne.NewPos(0, w.Size().Height*0.5))
c2.Add(bu)
c2.Add(bd)
2024-04-11 14:52:32 +02:00
if minSize == nil {
minSize = &fyne.Size{Width: 100, Height: 0}
}
return container.NewHBox(
container.New(uilayout.NewMinSizeLayout(*minSize),
w), c2)
2024-03-30 17:45:07 +01:00
}
2024-04-03 16:07:24 +02:00
// NewH creates a RichText widget formatted as a header of the provided level.
//
// Parameters:
// - level: an integer representing the level of the text.
// - text: a string containing the text to display.
//
// Returns a pointer to a RichText widget.
2024-03-30 17:45:07 +01:00
func NewH(level int, text string) *widget.RichText {
return widget.NewRichTextFromMarkdown(
fmt.Sprintf("%s %s", strings.Repeat("#", level), text))
}
2024-04-03 16:07:24 +02:00
// NewHWithData creates a new RichText widget formatted as a header of the provided level.
//
// Parameters:
// - level: an integer representing the level of the text.
// - data: a string binding containing the text to display.
//
// Returns a pointer to a RichText widget.
2024-03-30 17:45:07 +01:00
func NewHWithData(level int, data binding.String) *widget.RichText {
txt, _ := data.Get()
w := NewH(level, txt)
data.AddListener(binding.NewDataListener(func() {
txt, _ := data.Get()
w.ParseMarkdown(fmt.Sprintf("%s %s", strings.Repeat("#", level), txt))
}))
return w
}
2024-04-03 16:07:24 +02:00
// NewH1 creates a new RichText widget with header level 1.
2024-03-30 17:45:07 +01:00
func NewH1(text string) *widget.RichText {
return NewH(1, text)
}
2024-04-03 16:07:24 +02:00
// NewH1WithData creates a new RichText widget with header level 1.
2024-03-30 17:45:07 +01:00
func NewH1WithData(data binding.String) *widget.RichText {
return NewHWithData(1, data)
}
2024-04-03 16:07:24 +02:00
// NewH2 creates a new RichText widget with header level 2.
2024-03-30 17:45:07 +01:00
func NewH2(text string) *widget.RichText {
return NewH(2, text)
}
2024-04-03 16:07:24 +02:00
// NewH2WithData creates a new RichText widget with header level 2.
2024-03-30 17:45:07 +01:00
func NewH2WithData(data binding.String) *widget.RichText {
return NewHWithData(2, data)
}
2024-04-03 16:07:24 +02:00
// NewH3 creates a new RichText widget with header level 3.
2024-03-30 17:45:07 +01:00
func NewH3(text string) *widget.RichText {
return NewH(3, text)
}
2024-04-03 16:07:24 +02:00
// NewH3WithData creates a new RichText widget with header level 3.
2024-03-30 17:45:07 +01:00
func NewH3WithData(data binding.String) *widget.RichText {
return NewHWithData(3, data)
}
2024-04-03 16:07:24 +02:00
// NewH4 creates a new RichText widget with header level 4.
2024-03-30 17:45:07 +01:00
func NewH4(text string) *widget.RichText {
return NewH(4, text)
}
2024-04-03 16:07:24 +02:00
// NewH4WithData creates a new RichText widget with header level 4.
2024-03-30 17:45:07 +01:00
func NewH4WithData(data binding.String) *widget.RichText {
return NewHWithData(4, data)
}
2024-04-03 16:07:24 +02:00
// NewH5 creates a new RichText widget with header level 5.
2024-03-30 17:45:07 +01:00
func NewH5(text string) *widget.RichText {
return NewH(5, text)
}
2024-04-03 16:07:24 +02:00
// NewH5WithData creates a new RichText widget with header level 5.
2024-03-30 17:45:07 +01:00
func NewH5WithData(data binding.String) *widget.RichText {
return NewHWithData(5, data)
}
2024-04-03 16:07:24 +02:00
// NewH6 creates a new RichText widget with header level 6.
2024-03-30 17:45:07 +01:00
func NewH6(text string) *widget.RichText {
return NewH(6, text)
}
2024-04-03 16:07:24 +02:00
// NewH6WithData creates a new RichText widget with header level 6.
2024-03-30 17:45:07 +01:00
func NewH6WithData(data binding.String) *widget.RichText {
return NewHWithData(6, data)
}