OnMouseEvent implemented for FlowLayout

This commit is contained in:
Timon Ringwald 2022-05-04 14:47:55 +02:00
parent d29f48896e
commit 542d5badbf
5 changed files with 26 additions and 30 deletions

View File

@ -7,7 +7,6 @@ import (
"math/rand" "math/rand"
"os" "os"
"strconv" "strconv"
"strings"
"testing" "testing"
"git.tordarus.net/Tordarus/tui" "git.tordarus.net/Tordarus/tui"
@ -285,23 +284,6 @@ func TestBorderLayout(t *testing.T) {
return return
} }
screen.MouseEvent = func(event *tui.MouseEvent) (consumed bool) {
if event.Button == tui.MouseButtonLeft {
b := new(strings.Builder)
b.WriteString(textView.Text)
for slot, dim := range borderLayout.ViewDimensions {
if event.Position.In(dim) {
b.WriteString(fmt.Sprintf("%s: %s\n", slot, dim))
}
}
textView.Text = b.String()
}
return false
}
err = screen.Start() err = screen.Start()
fmt.Println(err) fmt.Println(err)
} }

View File

@ -11,15 +11,15 @@ type BorderLayout struct {
horizontalLayout *LayoutResult horizontalLayout *LayoutResult
verticalLayout *LayoutResult verticalLayout *LayoutResult
ViewDimensions map[Slot]tui.Dimension viewDims map[Slot]tui.Dimension
} }
var _ tui.Layout = &BorderLayout{} var _ tui.Layout = &BorderLayout{}
func NewBorderLayout() *BorderLayout { func NewBorderLayout() *BorderLayout {
return &BorderLayout{ return &BorderLayout{
views: map[Slot]tui.View{}, views: map[Slot]tui.View{},
ViewDimensions: map[Slot]tui.Dimension{}, viewDims: map[Slot]tui.Dimension{},
} }
} }
@ -79,7 +79,7 @@ func (g *BorderLayout) Draw(buf *tui.ViewBuffer) {
topHeight = int(float64(buf.Height()) * float64(topHeight) / float64(verticalLayout.Sum.Height)) topHeight = int(float64(buf.Height()) * float64(topHeight) / float64(verticalLayout.Sum.Height))
} }
g.ViewDimensions[Top] = tui.D(0, 0, buf.Width(), topHeight) g.viewDims[Top] = tui.D(0, 0, buf.Width(), topHeight)
view.Draw(buf.Sub(0, 0, buf.Width(), topHeight)) view.Draw(buf.Sub(0, 0, buf.Width(), topHeight))
} }
@ -92,7 +92,7 @@ func (g *BorderLayout) Draw(buf *tui.ViewBuffer) {
bottomHeight = int(float64(buf.Height()) * float64(bottomHeight) / float64(verticalLayout.Sum.Height)) bottomHeight = int(float64(buf.Height()) * float64(bottomHeight) / float64(verticalLayout.Sum.Height))
} }
g.ViewDimensions[Bottom] = tui.D(0, buf.Height()-bottomHeight, buf.Width(), bottomHeight) g.viewDims[Bottom] = tui.D(0, buf.Height()-bottomHeight, buf.Width(), bottomHeight)
view.Draw(buf.Sub(0, buf.Height()-bottomHeight, buf.Width(), bottomHeight)) view.Draw(buf.Sub(0, buf.Height()-bottomHeight, buf.Width(), bottomHeight))
} }
@ -105,7 +105,7 @@ func (g *BorderLayout) Draw(buf *tui.ViewBuffer) {
leftWidth = int(float64(buf.Width()) * float64(leftWidth) / float64(horizontalLayout.Sum.Width)) leftWidth = int(float64(buf.Width()) * float64(leftWidth) / float64(horizontalLayout.Sum.Width))
} }
g.ViewDimensions[Left] = tui.D(0, topHeight, leftWidth, buf.Height()-topHeight-bottomHeight) g.viewDims[Left] = tui.D(0, topHeight, leftWidth, buf.Height()-topHeight-bottomHeight)
view.Draw(buf.Sub(0, topHeight, leftWidth, buf.Height()-topHeight-bottomHeight)) view.Draw(buf.Sub(0, topHeight, leftWidth, buf.Height()-topHeight-bottomHeight))
} }
@ -118,12 +118,12 @@ func (g *BorderLayout) Draw(buf *tui.ViewBuffer) {
rightWidth = int(float64(buf.Width()) * float64(rightWidth) / float64(horizontalLayout.Sum.Width)) rightWidth = int(float64(buf.Width()) * float64(rightWidth) / float64(horizontalLayout.Sum.Width))
} }
g.ViewDimensions[Right] = tui.D(buf.Width()-rightWidth, topHeight, rightWidth, buf.Height()-topHeight-bottomHeight) g.viewDims[Right] = tui.D(buf.Width()-rightWidth, topHeight, rightWidth, buf.Height()-topHeight-bottomHeight)
view.Draw(buf.Sub(buf.Width()-rightWidth, topHeight, rightWidth, buf.Height()-topHeight-bottomHeight)) view.Draw(buf.Sub(buf.Width()-rightWidth, topHeight, rightWidth, buf.Height()-topHeight-bottomHeight))
} }
if view, ok := g.views[Center]; ok { if view, ok := g.views[Center]; ok {
g.ViewDimensions[Center] = tui.D(leftWidth, topHeight, buf.Width()-leftWidth-rightWidth, buf.Height()-topHeight-bottomHeight) g.viewDims[Center] = tui.D(leftWidth, topHeight, buf.Width()-leftWidth-rightWidth, buf.Height()-topHeight-bottomHeight)
view.Draw(buf.Sub(leftWidth, topHeight, buf.Width()-leftWidth-rightWidth, buf.Height()-topHeight-bottomHeight)) view.Draw(buf.Sub(leftWidth, topHeight, buf.Width()-leftWidth-rightWidth, buf.Height()-topHeight-bottomHeight))
} }
@ -147,7 +147,7 @@ func (g *BorderLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
} }
func (g *BorderLayout) OnMouseEvent(event *tui.MouseEvent) (consumed bool) { func (g *BorderLayout) OnMouseEvent(event *tui.MouseEvent) (consumed bool) {
for slot, dim := range g.ViewDimensions { for slot, dim := range g.viewDims {
if event.Position.In(dim) { if event.Position.In(dim) {
g.views[slot].OnMouseEvent(event) g.views[slot].OnMouseEvent(event)
return true return true

View File

@ -49,4 +49,4 @@ func (g *CoordLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
return false return false
} }
// TODO OnMouseClicked // TODO OnMouseEvent

View File

@ -10,6 +10,8 @@ type FlowLayout struct {
views []tui.View views []tui.View
lastLayoutPhase *LayoutResult lastLayoutPhase *LayoutResult
viewDims map[tui.View]tui.Dimension
// Orientation defines in which direction the children will be placed // Orientation defines in which direction the children will be placed
Orientation tui.Orientation Orientation tui.Orientation
} }
@ -19,6 +21,7 @@ var _ tui.Layout = &FlowLayout{}
func NewFlowLayout(orientation tui.Orientation) *FlowLayout { func NewFlowLayout(orientation tui.Orientation) *FlowLayout {
return &FlowLayout{ return &FlowLayout{
views: make([]tui.View, 0), views: make([]tui.View, 0),
viewDims: map[tui.View]tui.Dimension{},
Orientation: orientation, Orientation: orientation,
} }
} }
@ -42,6 +45,7 @@ func (g *FlowLayout) InsertView(v tui.View, index int) {
func (g *FlowLayout) removeView(v tui.View) { func (g *FlowLayout) removeView(v tui.View) {
for index, view := range g.Views() { for index, view := range g.Views() {
if v == view { if v == view {
delete(g.viewDims, view)
g.views = append(g.views[:index], g.views[index+1:]...) g.views = append(g.views[:index], g.views[index+1:]...)
return return
} }
@ -81,6 +85,7 @@ func (g *FlowLayout) Draw(buf *tui.ViewBuffer) {
size.Width = iff(layout.Sum.Width > buf.Width(), 0, remainingSpacePerView) size.Width = iff(layout.Sum.Width > buf.Width(), 0, remainingSpacePerView)
} }
g.viewDims[view] = tui.D(x, 0, size.Width, size.Height)
view.Draw(buf.Sub(x, 0, size.Width, size.Height)) view.Draw(buf.Sub(x, 0, size.Width, size.Height))
x += size.Width x += size.Width
@ -106,6 +111,7 @@ func (g *FlowLayout) Draw(buf *tui.ViewBuffer) {
size.Height = iff(layout.Sum.Height > buf.Height(), 0, remainingSpacePerView) size.Height = iff(layout.Sum.Height > buf.Height(), 0, remainingSpacePerView)
} }
g.viewDims[view] = tui.D(0, y, size.Width, size.Height)
view.Draw(buf.Sub(0, y, size.Width, size.Height)) view.Draw(buf.Sub(0, y, size.Width, size.Height))
y += size.Height y += size.Height
@ -143,4 +149,12 @@ func (g *FlowLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
return false return false
} }
// TODO OnMouseClicked func (g *FlowLayout) OnMouseEvent(event *tui.MouseEvent) (consumed bool) {
for view, dim := range g.viewDims {
if event.Position.In(dim) {
view.OnMouseEvent(event)
return true
}
}
return false
}

View File

@ -110,4 +110,4 @@ func (g *SeperatorLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
return false return false
} }
// TODO OnMouseClicked // TODO OnMouseEvent