Add extfyne packages

This commit is contained in:
qowevisa 2024-07-12 20:33:26 +03:00
parent f8a14f7a69
commit 028d1c25ac
6 changed files with 299 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package layouts
import "fyne.io/fyne/v2"
// Implements: fyne.Layout
type EntryBtn7030 struct{}
// Implements: fyne.Layout 1
func (p *EntryBtn7030) MinSize(objects []fyne.CanvasObject) fyne.Size {
minWidth := float32(0)
minHeight := float32(0)
for _, obj := range objects {
minSize := obj.MinSize()
minWidth += minSize.Width
if minSize.Height > minHeight {
minHeight = minSize.Height
}
}
return fyne.NewSize(minWidth, minHeight)
}
// Implements: fyne.Layout 2
func (p *EntryBtn7030) Layout(objects []fyne.CanvasObject, size fyne.Size) {
if len(objects) != 2 {
return
}
entryWidth := size.Width * 0.7
buttonWidth := size.Width * 0.3
objects[0].Resize(fyne.NewSize(entryWidth, size.Height))
objects[1].Resize(fyne.NewSize(buttonWidth, size.Height))
objects[0].Move(fyne.NewPos(0, 0))
objects[1].Move(fyne.NewPos(entryWidth, 0))
}
func NewEntryBtn7030() fyne.Layout {
return &EntryBtn7030{}
}

View File

@ -0,0 +1,41 @@
package layouts
import (
"fyne.io/fyne/v2"
)
// Implements: fyne.Layout
type FullScale struct {
size fyne.Size
}
// Implements: fyne.Layout 1
func (p *FullScale) MinSize(objects []fyne.CanvasObject) fyne.Size {
return p.size
}
// Implements: fyne.Layout 2
func (p *FullScale) Layout(objects []fyne.CanvasObject, size fyne.Size) {
if len(objects) != 1 {
return
}
if p.size.Width < size.Width {
p.size.Width = size.Width
}
if p.size.Height < size.Height {
p.size.Height = size.Height
}
objects[0].Resize(fyne.NewSize(p.size.Width, p.size.Height))
objects[0].Move(fyne.NewPos(0, 0))
}
func NewFullWidth() fyne.Layout {
return &FullScale{}
}
func NewFullWidthWithSize(s fyne.Size) fyne.Layout {
return &FullScale{
size: s,
}
}

View File

@ -0,0 +1,58 @@
package layouts
import "fyne.io/fyne/v2"
// Implements: fyne.Layout
type VarGridCols struct {
cols int
cfg []int
}
func NewVariableGridWithColumns(cols int, cfg []int) *VarGridCols {
return &VarGridCols{
cols: cols,
cfg: cfg,
}
}
// Implements: fyne.Layout : 1
func (v *VarGridCols) MinSize(objects []fyne.CanvasObject) fyne.Size {
w, h := float32(0), float32(0)
i := 0
for _, o := range objects {
if !o.Visible() {
continue
}
childSize := o.MinSize()
// NOTE: can be out-of-bounds error if not checked at creation
takenCols := v.cfg[i]
if takenCols > 1 {
w += childSize.Width * float32(takenCols)
} else {
w += childSize.Width
}
if h+childSize.Height > h {
h += childSize.Height
}
i++
}
return fyne.NewSize(w, h)
}
// Implements: fyne.Layout : 2
func (v *VarGridCols) Layout(objects []fyne.CanvasObject, contSize fyne.Size) {
// pos := fyne.NewPos(0, contSize.Height-v.MinSize(objects).Height)
pos := fyne.NewPos(0, 0)
cellWidthSingular := float64(contSize.Width) / float64(v.cols)
cellHeight := contSize.Height
for i, o := range objects {
numCols := v.cfg[i]
w := cellWidthSingular * float64(numCols)
o.Move(pos)
o.Resize(fyne.NewSize(float32(w), cellHeight))
pos = pos.Add(fyne.NewPos(float32(w), 0))
}
}

View File

@ -0,0 +1,53 @@
package layouts
import "fyne.io/fyne/v2"
// Implements: fyne.Layout
type VarGridRows struct {
rows int
cfg []int
}
func NewVariableGridWithRows(cols int, cfg []int) *VarGridRows {
return &VarGridRows{
rows: cols,
cfg: cfg,
}
}
// Implements: fyne.Layout : 1
func (v *VarGridRows) MinSize(objects []fyne.CanvasObject) fyne.Size {
w, h := float32(0), float32(0)
for i, o := range objects {
childSize := o.MinSize()
// NOTE: can be out-of-bounds error if not checked at creation
takenRows := v.cfg[i]
if takenRows > 1 {
h += childSize.Height * float32(takenRows)
} else {
h += childSize.Height
}
if h+childSize.Height > h {
w += childSize.Width
}
}
return fyne.NewSize(w, h)
}
// Implements: fyne.Layout : 2
func (v *VarGridRows) Layout(objects []fyne.CanvasObject, contSize fyne.Size) {
// pos := fyne.NewPos(0, contSize.Height-v.MinSize(objects).Height)
pos := fyne.NewPos(0, 0)
cellHeightSingular := float64(contSize.Height) / float64(v.rows)
cellWidth := contSize.Height
for i, o := range objects {
numRows := v.cfg[i]
h := cellHeightSingular * float64(numRows)
o.Move(pos)
o.Resize(fyne.NewSize(cellWidth, float32(h)))
pos = pos.Add(fyne.NewPos(0, float32(h)))
}
}

1
extfyne/main.go Normal file
View File

@ -0,0 +1 @@
package extfyne

View File

@ -0,0 +1,106 @@
package widgets
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
type MBoardMessage struct {
LeftAlign bool
Data *widget.Label
}
type MessageBoard struct {
widget.BaseWidget
Messages []MBoardMessage
}
func NewEmptyMessageBoard() *MessageBoard {
board := &MessageBoard{
Messages: make([]MBoardMessage, 0),
}
board.ExtendBaseWidget(board)
return board
}
func NewMessageBoard(msgs []MBoardMessage) *MessageBoard {
board := &MessageBoard{
Messages: msgs,
}
board.ExtendBaseWidget(board)
return board
}
func (m *MessageBoard) Add(msg MBoardMessage) {
m.Messages = append(m.Messages, msg)
m.Refresh()
}
func (m *MessageBoard) CreateRenderer() fyne.WidgetRenderer {
content := container.NewVBox()
for _, msg := range m.Messages {
hbox := container.NewHBox()
background := canvas.NewRectangle(&color.RGBA{R: 0, G: 0, B: 255, A: 128})
background.SetMinSize(msg.Data.MinSize())
if msg.LeftAlign {
hbox.Add(container.NewStack(background, msg.Data))
} else {
hbox.Add(layout.NewSpacer())
hbox.Add(container.NewStack(background, msg.Data))
}
content.Add(hbox)
}
scrollContainer := container.NewScroll(content)
return &messageBoardRenderer{
board: m,
content: scrollContainer,
}
}
type messageBoardRenderer struct {
board *MessageBoard
content *container.Scroll
}
func (r *messageBoardRenderer) Layout(size fyne.Size) {
r.content.Resize(size)
}
func (r *messageBoardRenderer) MinSize() fyne.Size {
return r.content.MinSize()
}
func (r *messageBoardRenderer) Refresh() {
r.content.Content.(*fyne.Container).Objects = nil
for _, msg := range r.board.Messages {
hbox := container.NewHBox()
if msg.LeftAlign {
background := canvas.NewRectangle(&color.RGBA{R: 0, G: 0, B: 255, A: 128})
background.SetMinSize(msg.Data.MinSize())
hbox.Add(container.NewStack(background, msg.Data))
} else {
background := canvas.NewRectangle(&color.RGBA{R: 0, G: 255, B: 0, A: 128})
background.SetMinSize(msg.Data.MinSize())
hbox.Add(layout.NewSpacer())
hbox.Add(container.NewStack(background, msg.Data))
}
r.content.Content.(*fyne.Container).Add(hbox)
}
r.content.Refresh()
}
func (r *messageBoardRenderer) Destroy() {
// Clean up resources if needed
}
func (r *messageBoardRenderer) Objects() []fyne.CanvasObject {
return []fyne.CanvasObject{r.content}
}