mirror of
https://github.com/rivo/tview.git
synced 2025-04-26 13:49:06 +08:00
A Box's inner rect will now clamp to screen space. Resolves #79
This commit is contained in:
parent
761e3d72da
commit
6e3b8a41c8
34
box.go
34
box.go
@ -16,8 +16,7 @@ type Box struct {
|
|||||||
// The position of the rect.
|
// The position of the rect.
|
||||||
x, y, width, height int
|
x, y, width, height int
|
||||||
|
|
||||||
// The inner rect reserved for the box's content. This is only used if the
|
// The inner rect reserved for the box's content.
|
||||||
// "draw" callback is not nil.
|
|
||||||
innerX, innerY, innerWidth, innerHeight int
|
innerX, innerY, innerWidth, innerHeight int
|
||||||
|
|
||||||
// Border padding.
|
// Border padding.
|
||||||
@ -49,6 +48,10 @@ type Box struct {
|
|||||||
// Whether or not this box has focus.
|
// Whether or not this box has focus.
|
||||||
hasFocus bool
|
hasFocus bool
|
||||||
|
|
||||||
|
// If set to true, the inner rect of this box will be within the screen at the
|
||||||
|
// last time the box was drawn.
|
||||||
|
clampToScreen bool
|
||||||
|
|
||||||
// An optional capture function which receives a key event and returns the
|
// An optional capture function which receives a key event and returns the
|
||||||
// event to be forwarded to the primitive's default input handler (nil if
|
// event to be forwarded to the primitive's default input handler (nil if
|
||||||
// nothing should be forwarded).
|
// nothing should be forwarded).
|
||||||
@ -63,10 +66,12 @@ func NewBox() *Box {
|
|||||||
b := &Box{
|
b := &Box{
|
||||||
width: 15,
|
width: 15,
|
||||||
height: 10,
|
height: 10,
|
||||||
|
innerX: -1, // Mark as uninitialized.
|
||||||
backgroundColor: Styles.PrimitiveBackgroundColor,
|
backgroundColor: Styles.PrimitiveBackgroundColor,
|
||||||
borderColor: Styles.BorderColor,
|
borderColor: Styles.BorderColor,
|
||||||
titleColor: Styles.TitleColor,
|
titleColor: Styles.TitleColor,
|
||||||
titleAlign: AlignCenter,
|
titleAlign: AlignCenter,
|
||||||
|
clampToScreen: true,
|
||||||
}
|
}
|
||||||
b.focus = b
|
b.focus = b
|
||||||
return b
|
return b
|
||||||
@ -87,7 +92,7 @@ func (b *Box) GetRect() (int, int, int, int) {
|
|||||||
// GetInnerRect returns the position of the inner rectangle (x, y, width,
|
// GetInnerRect returns the position of the inner rectangle (x, y, width,
|
||||||
// height), without the border and without any padding.
|
// height), without the border and without any padding.
|
||||||
func (b *Box) GetInnerRect() (int, int, int, int) {
|
func (b *Box) GetInnerRect() (int, int, int, int) {
|
||||||
if b.draw != nil {
|
if b.innerX >= 0 {
|
||||||
return b.innerX, b.innerY, b.innerWidth, b.innerHeight
|
return b.innerX, b.innerY, b.innerWidth, b.innerHeight
|
||||||
}
|
}
|
||||||
x, y, width, height := b.GetRect()
|
x, y, width, height := b.GetRect()
|
||||||
@ -270,6 +275,29 @@ func (b *Box) Draw(screen tcell.Screen) {
|
|||||||
// Call custom draw function.
|
// Call custom draw function.
|
||||||
if b.draw != nil {
|
if b.draw != nil {
|
||||||
b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.draw(screen, b.x, b.y, b.width, b.height)
|
b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.draw(screen, b.x, b.y, b.width, b.height)
|
||||||
|
} else {
|
||||||
|
// Remember the inner rect.
|
||||||
|
b.innerX = -1
|
||||||
|
b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.GetInnerRect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clamp inner rect to screen.
|
||||||
|
if b.clampToScreen {
|
||||||
|
width, height := screen.Size()
|
||||||
|
if b.innerX < 0 {
|
||||||
|
b.innerWidth += b.innerX
|
||||||
|
b.innerX = 0
|
||||||
|
}
|
||||||
|
if b.innerX+b.innerWidth >= width {
|
||||||
|
b.innerWidth = width - b.innerX
|
||||||
|
}
|
||||||
|
if b.innerY+b.innerHeight >= height {
|
||||||
|
b.innerHeight = height - b.innerY
|
||||||
|
}
|
||||||
|
if b.innerY < 0 {
|
||||||
|
b.innerHeight += b.innerY
|
||||||
|
b.innerY = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user