From 646fd4f67d06ca4f3447e07321e11fa1ca03b261 Mon Sep 17 00:00:00 2001 From: sqshq Date: Sat, 13 Apr 2019 20:24:24 -0400 Subject: [PATCH] component selection using mouse click --- component/layout/layout.go | 48 +++++++++++++++++++++++++++++++++++++- component/menu.go | 2 +- console/signal.go | 1 + event/handler.go | 5 +++- 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/component/layout/layout.go b/component/layout/layout.go index b4c1065..92c4af7 100644 --- a/component/layout/layout.go +++ b/component/layout/layout.go @@ -66,7 +66,20 @@ func (l *Layout) changeMode(m Mode) { l.ChangeModeEvents <- m } -func (l *Layout) HandleConsoleEvent(e string) { +func (l *Layout) HandleMouseClick(x int, y int) { + l.getSelection().CommandChannel <- &data.Command{Type: runchart.CommandMoveSelection, Value: 0} + l.menu.Idle() + selected, i := l.findComponentAtPoint(image.Point{X: x, Y: y}) + if selected == nil { + l.changeMode(ModeDefault) + } else { + l.selection = i + l.menu.Highlight(selected) + l.changeMode(ModeComponentSelect) + } +} + +func (l *Layout) HandleKeyboardEvent(e string) { selected := l.getSelection() @@ -289,6 +302,39 @@ func (l *Layout) Draw(buffer *ui.Buffer) { l.menu.Draw(buffer) } +func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component, int) { + + columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount) + rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount) + + for i, c := range l.Components { + + x1 := math.Floor(float64(c.Location.X) * columnWidth) + y1 := math.Floor(float64(c.Location.Y) * rowHeight) + x2 := x1 + math.Floor(float64(c.Size.X))*columnWidth + y2 := y1 + math.Floor(float64(c.Size.Y))*rowHeight + + if x2-x1 < minDimension { + x2 = x1 + minDimension + } + + if y2-y1 < minDimension { + y2 = y1 + minDimension + } + + rectangle := image.Rectangle{Min: image.Point{ + X: int(x1), Y: int(y1)}, + Max: image.Point{X: int(x2), Y: int(y2)}, + } + + if point.In(rectangle) { + return c, i + } + } + + return nil, -1 +} + func (l *Layout) resetAlerts() { for _, c := range l.Components { c.AlertChannel <- nil diff --git a/component/menu.go b/component/menu.go index 4db4ff6..a581783 100644 --- a/component/menu.go +++ b/component/menu.go @@ -122,7 +122,7 @@ func (m *Menu) Draw(buffer *ui.Buffer) { func (m *Menu) renderHighlight(buffer *ui.Buffer) { - arrowsText := "Use arrows for selection" + arrowsText := "Use mouse or arrows for selection" optionsText := " to view options" resumeText := " to resume" diff --git a/console/signal.go b/console/signal.go index b6fd8a5..b146df6 100644 --- a/console/signal.go +++ b/console/signal.go @@ -2,6 +2,7 @@ package console const ( SignalResize = "" + SignalClick = "" ) const ( diff --git a/event/handler.go b/event/handler.go index 6f672b7..e122efd 100644 --- a/event/handler.go +++ b/event/handler.go @@ -44,6 +44,9 @@ func (h *Handler) HandleEvents() { ui.Render(h.layout) case e := <-h.consoleEvents: switch e.ID { + case console.SignalClick: + payload := e.Payload.(ui.Mouse) + h.layout.HandleMouseClick(payload.X, payload.Y) case console.KeyQuit, console.KeyExit: h.handleExit() return @@ -51,7 +54,7 @@ func (h *Handler) HandleEvents() { payload := e.Payload.(ui.Resize) h.layout.ChangeDimensions(payload.Width, payload.Height) default: - h.layout.HandleConsoleEvent(e.ID) + h.layout.HandleKeyboardEvent(e.ID) } } }