From 542d5badbfe822eec5753039b93db305082997ca Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 4 May 2022 14:47:55 +0200 Subject: [PATCH] OnMouseEvent implemented for FlowLayout --- tests/screen_test.go | 18 ------------------ views/layout_border.go | 18 +++++++++--------- views/layout_coord.go | 2 +- views/layout_flow.go | 16 +++++++++++++++- views/layout_separator.go | 2 +- 5 files changed, 26 insertions(+), 30 deletions(-) diff --git a/tests/screen_test.go b/tests/screen_test.go index 3effa33..b28f476 100644 --- a/tests/screen_test.go +++ b/tests/screen_test.go @@ -7,7 +7,6 @@ import ( "math/rand" "os" "strconv" - "strings" "testing" "git.tordarus.net/Tordarus/tui" @@ -285,23 +284,6 @@ func TestBorderLayout(t *testing.T) { 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() fmt.Println(err) } diff --git a/views/layout_border.go b/views/layout_border.go index e2934d5..916fba6 100644 --- a/views/layout_border.go +++ b/views/layout_border.go @@ -11,15 +11,15 @@ type BorderLayout struct { horizontalLayout *LayoutResult verticalLayout *LayoutResult - ViewDimensions map[Slot]tui.Dimension + viewDims map[Slot]tui.Dimension } var _ tui.Layout = &BorderLayout{} func NewBorderLayout() *BorderLayout { return &BorderLayout{ - views: map[Slot]tui.View{}, - ViewDimensions: map[Slot]tui.Dimension{}, + views: map[Slot]tui.View{}, + 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)) } - 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)) } @@ -92,7 +92,7 @@ func (g *BorderLayout) Draw(buf *tui.ViewBuffer) { 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)) } @@ -105,7 +105,7 @@ func (g *BorderLayout) Draw(buf *tui.ViewBuffer) { 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)) } @@ -118,12 +118,12 @@ func (g *BorderLayout) Draw(buf *tui.ViewBuffer) { 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)) } 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)) } @@ -147,7 +147,7 @@ func (g *BorderLayout) OnKeyPressed(event *tui.KeyEvent) (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) { g.views[slot].OnMouseEvent(event) return true diff --git a/views/layout_coord.go b/views/layout_coord.go index 0eb5b0e..32c846a 100644 --- a/views/layout_coord.go +++ b/views/layout_coord.go @@ -49,4 +49,4 @@ func (g *CoordLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { return false } -// TODO OnMouseClicked +// TODO OnMouseEvent diff --git a/views/layout_flow.go b/views/layout_flow.go index a1b6033..0e63416 100644 --- a/views/layout_flow.go +++ b/views/layout_flow.go @@ -10,6 +10,8 @@ type FlowLayout struct { views []tui.View lastLayoutPhase *LayoutResult + viewDims map[tui.View]tui.Dimension + // Orientation defines in which direction the children will be placed Orientation tui.Orientation } @@ -19,6 +21,7 @@ var _ tui.Layout = &FlowLayout{} func NewFlowLayout(orientation tui.Orientation) *FlowLayout { return &FlowLayout{ views: make([]tui.View, 0), + viewDims: map[tui.View]tui.Dimension{}, Orientation: orientation, } } @@ -42,6 +45,7 @@ func (g *FlowLayout) InsertView(v tui.View, index int) { func (g *FlowLayout) removeView(v tui.View) { for index, view := range g.Views() { if v == view { + delete(g.viewDims, view) g.views = append(g.views[:index], g.views[index+1:]...) return } @@ -81,6 +85,7 @@ func (g *FlowLayout) Draw(buf *tui.ViewBuffer) { 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)) x += size.Width @@ -106,6 +111,7 @@ func (g *FlowLayout) Draw(buf *tui.ViewBuffer) { 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)) y += size.Height @@ -143,4 +149,12 @@ func (g *FlowLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { 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 +} diff --git a/views/layout_separator.go b/views/layout_separator.go index a644cb6..19ff7b2 100644 --- a/views/layout_separator.go +++ b/views/layout_separator.go @@ -110,4 +110,4 @@ func (g *SeperatorLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { return false } -// TODO OnMouseClicked +// TODO OnMouseEvent