244 lines
6.7 KiB
Go
244 lines
6.7 KiB
Go
package uiwidget
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gitea.hevanto-it.com/hevanto/ui/uilayout"
|
|
"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"
|
|
)
|
|
|
|
// LabelOpts defines the options for a label.
|
|
type LabelOpts interface {
|
|
ApplyTo(*widget.Label)
|
|
}
|
|
|
|
type allignmentOpt struct {
|
|
Alignment fyne.TextAlign
|
|
}
|
|
|
|
// AlignmentOpt returns a LabelOpts with the specified alignment.
|
|
func AlignmentOpt(alignment fyne.TextAlign) LabelOpts {
|
|
return &allignmentOpt{alignment}
|
|
}
|
|
|
|
// ApplyTo sets the alignment of a label to the specified alignment.
|
|
func (a *allignmentOpt) ApplyTo(lbl *widget.Label) {
|
|
lbl.Alignment = a.Alignment
|
|
}
|
|
|
|
type wrappingOpt struct {
|
|
Wrapping fyne.TextWrap
|
|
}
|
|
|
|
// WrappingOpt creates a LabelOpts with the specified text wrapping option.
|
|
func WrappingOpt(wrapping fyne.TextWrap) LabelOpts {
|
|
return &wrappingOpt{wrapping}
|
|
}
|
|
|
|
// ApplyTo applies the wrapping option to the label.
|
|
func (w *wrappingOpt) ApplyTo(lbl *widget.Label) {
|
|
lbl.Wrapping = w.Wrapping
|
|
}
|
|
|
|
type textStyleOpt struct {
|
|
Style fyne.TextStyle
|
|
}
|
|
|
|
// TextStyleOpt returns the label options for the given text style.
|
|
func TextStyleOpt(style fyne.TextStyle) LabelOpts {
|
|
return &textStyleOpt{style}
|
|
}
|
|
|
|
// ApplyTo applies the text style option to the label.
|
|
func (t *textStyleOpt) ApplyTo(lbl *widget.Label) {
|
|
lbl.TextStyle = t.Style
|
|
}
|
|
|
|
type truncationOpt struct {
|
|
Truncation fyne.TextTruncation
|
|
}
|
|
|
|
// TruncationOpt returns the label options for the given text truncation option.
|
|
func TruncationOpt(truncation fyne.TextTruncation) LabelOpts {
|
|
return &truncationOpt{truncation}
|
|
}
|
|
|
|
// ApplyTo applies the text truncation option to the label.
|
|
func (t *truncationOpt) ApplyTo(lbl *widget.Label) {
|
|
lbl.Truncation = t.Truncation
|
|
}
|
|
|
|
type importanceOpt struct {
|
|
Importance widget.Importance
|
|
}
|
|
|
|
// ImportanceOpt returns the label options for the given text importance option.
|
|
func ImportanceOpt(importance widget.Importance) LabelOpts {
|
|
return &importanceOpt{importance}
|
|
}
|
|
|
|
// ApplyTo applies the text importance option to the label.
|
|
func (i *importanceOpt) ApplyTo(lbl *widget.Label) {
|
|
lbl.Importance = i.Importance
|
|
}
|
|
|
|
// 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.
|
|
func NewLabel(text string, opts ...LabelOpts) *widget.Label {
|
|
lbl := widget.NewLabel(text)
|
|
for _, opt := range opts {
|
|
opt.ApplyTo(lbl)
|
|
}
|
|
return lbl
|
|
}
|
|
|
|
// 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.
|
|
func NewLabelWithData(data binding.String, opts ...LabelOpts) *widget.Label {
|
|
lbl := widget.NewLabelWithData(data)
|
|
for _, opt := range opts {
|
|
opt.ApplyTo(lbl)
|
|
}
|
|
return lbl
|
|
}
|
|
|
|
// 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.
|
|
func NewUpDownLabelWithData(
|
|
minSize *fyne.Size,
|
|
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)
|
|
|
|
if minSize == nil {
|
|
minSize = &fyne.Size{Width: 100, Height: 0}
|
|
}
|
|
return container.NewHBox(
|
|
container.New(uilayout.NewMinSizeLayout(*minSize),
|
|
w), c2)
|
|
}
|
|
|
|
// 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.
|
|
func NewH(level int, text string) *widget.RichText {
|
|
return widget.NewRichTextFromMarkdown(
|
|
fmt.Sprintf("%s %s", strings.Repeat("#", level), text))
|
|
}
|
|
|
|
// 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.
|
|
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
|
|
}
|
|
|
|
// NewH1 creates a new RichText widget with header level 1.
|
|
func NewH1(text string) *widget.RichText {
|
|
return NewH(1, text)
|
|
}
|
|
|
|
// NewH1WithData creates a new RichText widget with header level 1.
|
|
func NewH1WithData(data binding.String) *widget.RichText {
|
|
return NewHWithData(1, data)
|
|
}
|
|
|
|
// NewH2 creates a new RichText widget with header level 2.
|
|
func NewH2(text string) *widget.RichText {
|
|
return NewH(2, text)
|
|
}
|
|
|
|
// NewH2WithData creates a new RichText widget with header level 2.
|
|
func NewH2WithData(data binding.String) *widget.RichText {
|
|
return NewHWithData(2, data)
|
|
}
|
|
|
|
// NewH3 creates a new RichText widget with header level 3.
|
|
func NewH3(text string) *widget.RichText {
|
|
return NewH(3, text)
|
|
}
|
|
|
|
// NewH3WithData creates a new RichText widget with header level 3.
|
|
func NewH3WithData(data binding.String) *widget.RichText {
|
|
return NewHWithData(3, data)
|
|
}
|
|
|
|
// NewH4 creates a new RichText widget with header level 4.
|
|
func NewH4(text string) *widget.RichText {
|
|
return NewH(4, text)
|
|
}
|
|
|
|
// NewH4WithData creates a new RichText widget with header level 4.
|
|
func NewH4WithData(data binding.String) *widget.RichText {
|
|
return NewHWithData(4, data)
|
|
}
|
|
|
|
// NewH5 creates a new RichText widget with header level 5.
|
|
func NewH5(text string) *widget.RichText {
|
|
return NewH(5, text)
|
|
}
|
|
|
|
// NewH5WithData creates a new RichText widget with header level 5.
|
|
func NewH5WithData(data binding.String) *widget.RichText {
|
|
return NewHWithData(5, data)
|
|
}
|
|
|
|
// NewH6 creates a new RichText widget with header level 6.
|
|
func NewH6(text string) *widget.RichText {
|
|
return NewH(6, text)
|
|
}
|
|
|
|
// NewH6WithData creates a new RichText widget with header level 6.
|
|
func NewH6WithData(data binding.String) *widget.RichText {
|
|
return NewHWithData(6, data)
|
|
}
|