2017-01-14 06:07:43 +00:00
|
|
|
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
|
2015-03-20 20:21:50 +00:00
|
|
|
// Use of this source code is governed by a MIT license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
2015-02-03 19:13:51 +00:00
|
|
|
package termui
|
|
|
|
|
2019-01-24 04:12:10 +00:00
|
|
|
import (
|
|
|
|
"image"
|
2019-03-07 10:00:36 +00:00
|
|
|
"sync"
|
2019-01-24 04:12:10 +00:00
|
|
|
)
|
2015-04-21 13:56:10 +00:00
|
|
|
|
2019-01-24 12:02:18 +00:00
|
|
|
// Block is the base struct inherited by most widgets.
|
|
|
|
// Block manages size, position, border, and title.
|
|
|
|
// It implements all 3 of the methods needed for the `Drawable` interface.
|
|
|
|
// Custom widgets will override the Draw method.
|
2019-01-24 04:12:10 +00:00
|
|
|
type Block struct {
|
2019-03-01 02:47:54 +00:00
|
|
|
Border bool
|
|
|
|
BorderStyle Style
|
|
|
|
|
|
|
|
BorderLeft, BorderRight, BorderTop, BorderBottom bool
|
|
|
|
|
|
|
|
PaddingLeft, PaddingRight, PaddingTop, PaddingBottom int
|
2015-04-26 04:13:49 +00:00
|
|
|
|
2019-01-24 04:12:10 +00:00
|
|
|
image.Rectangle
|
|
|
|
Inner image.Rectangle
|
2015-04-26 04:13:49 +00:00
|
|
|
|
2019-01-24 04:12:10 +00:00
|
|
|
Title string
|
|
|
|
TitleStyle Style
|
2019-03-07 10:00:36 +00:00
|
|
|
|
|
|
|
sync.Mutex
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 04:12:10 +00:00
|
|
|
func NewBlock() *Block {
|
|
|
|
return &Block{
|
|
|
|
Border: true,
|
|
|
|
BorderStyle: Theme.Block.Border,
|
|
|
|
BorderLeft: true,
|
|
|
|
BorderRight: true,
|
|
|
|
BorderTop: true,
|
|
|
|
BorderBottom: true,
|
|
|
|
|
|
|
|
TitleStyle: Theme.Block.Title,
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 04:12:10 +00:00
|
|
|
func (self *Block) drawBorder(buf *Buffer) {
|
|
|
|
verticalCell := Cell{VERTICAL_LINE, self.BorderStyle}
|
|
|
|
horizontalCell := Cell{HORIZONTAL_LINE, self.BorderStyle}
|
2015-04-26 04:13:49 +00:00
|
|
|
|
|
|
|
// draw lines
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderTop {
|
|
|
|
buf.Fill(horizontalCell, image.Rect(self.Min.X, self.Min.Y, self.Max.X, self.Min.Y+1))
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderBottom {
|
|
|
|
buf.Fill(horizontalCell, image.Rect(self.Min.X, self.Max.Y-1, self.Max.X, self.Max.Y))
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderLeft {
|
|
|
|
buf.Fill(verticalCell, image.Rect(self.Min.X, self.Min.Y, self.Min.X+1, self.Max.Y))
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderRight {
|
|
|
|
buf.Fill(verticalCell, image.Rect(self.Max.X-1, self.Min.Y, self.Max.X, self.Max.Y))
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw corners
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderTop && self.BorderLeft {
|
|
|
|
buf.SetCell(Cell{TOP_LEFT, self.BorderStyle}, self.Min)
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderTop && self.BorderRight {
|
|
|
|
buf.SetCell(Cell{TOP_RIGHT, self.BorderStyle}, image.Pt(self.Max.X-1, self.Min.Y))
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderBottom && self.BorderLeft {
|
|
|
|
buf.SetCell(Cell{BOTTOM_LEFT, self.BorderStyle}, image.Pt(self.Min.X, self.Max.Y-1))
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
2019-01-24 04:12:10 +00:00
|
|
|
if self.BorderBottom && self.BorderRight {
|
|
|
|
buf.SetCell(Cell{BOTTOM_RIGHT, self.BorderStyle}, self.Max.Sub(image.Pt(1, 1)))
|
2015-04-26 04:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 12:02:18 +00:00
|
|
|
// Draw implements the Drawable interface.
|
2019-01-24 04:12:10 +00:00
|
|
|
func (self *Block) Draw(buf *Buffer) {
|
2019-01-24 12:02:18 +00:00
|
|
|
if self.Border {
|
2019-03-01 02:47:54 +00:00
|
|
|
self.drawBorder(buf)
|
2019-01-24 12:02:18 +00:00
|
|
|
}
|
2019-01-24 04:12:10 +00:00
|
|
|
buf.SetString(
|
|
|
|
self.Title,
|
|
|
|
self.TitleStyle,
|
|
|
|
image.Pt(self.Min.X+2, self.Min.Y),
|
|
|
|
)
|
2015-02-03 19:13:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-24 12:02:18 +00:00
|
|
|
// SetRect implements the Drawable interface.
|
2019-01-24 04:12:10 +00:00
|
|
|
func (self *Block) SetRect(x1, y1, x2, y2 int) {
|
|
|
|
self.Rectangle = image.Rect(x1, y1, x2, y2)
|
2019-03-01 02:47:54 +00:00
|
|
|
self.Inner = image.Rect(
|
|
|
|
self.Min.X+1+self.PaddingLeft,
|
|
|
|
self.Min.Y+1+self.PaddingTop,
|
2019-03-01 02:52:20 +00:00
|
|
|
self.Max.X-1-self.PaddingRight,
|
|
|
|
self.Max.Y-1-self.PaddingBottom,
|
2019-03-01 02:47:54 +00:00
|
|
|
)
|
2015-02-03 19:13:51 +00:00
|
|
|
}
|
2015-03-28 16:58:23 +00:00
|
|
|
|
2019-01-24 12:02:18 +00:00
|
|
|
// GetRect implements the Drawable interface.
|
2019-01-24 04:12:10 +00:00
|
|
|
func (self *Block) GetRect() image.Rectangle {
|
|
|
|
return self.Rectangle
|
2015-10-07 18:25:59 +00:00
|
|
|
}
|