refactoring: extract common logic into a separate method

This commit is contained in:
sqshq 2019-04-15 22:12:10 -04:00
parent 3c88c56fa2
commit 6f8f5f080c
1 changed files with 24 additions and 33 deletions

View File

@ -275,21 +275,8 @@ func (l *Layout) Draw(buffer *ui.Buffer) {
rowHeight := float64(l.GetRect().Dy()-statusbarHeight) / float64(rowsCount)
for _, 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
}
c.SetRect(int(x1), int(y1), int(x2), int(y2))
rectangle := calculateComponentCoordinates(c, columnWidth, rowHeight)
c.SetRect(rectangle.Min.X, rectangle.Min.Y, rectangle.Max.X, rectangle.Max.Y)
c.Draw(buffer)
}
@ -301,7 +288,6 @@ func (l *Layout) Draw(buffer *ui.Buffer) {
l.menu.Draw(buffer)
}
// TODO extract x/y calculation to a separate method
func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component, int) {
columnWidth := float64(l.GetRect().Dx()) / float64(columnsCount)
@ -309,6 +295,18 @@ func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component,
for i, c := range l.Components {
rectangle := calculateComponentCoordinates(c, columnWidth, rowHeight)
if point.In(rectangle) {
return c, i
}
}
return nil, -1
}
func calculateComponentCoordinates(c *component.Component, columnWidth float64, rowHeight float64) image.Rectangle {
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
@ -322,17 +320,10 @@ func (l *Layout) findComponentAtPoint(point image.Point) (*component.Component,
y2 = y1 + minDimension
}
rectangle := image.Rectangle{Min: image.Point{
return 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() {