package cview
import (
"sync"
"github.com/gdamore/tcell/v2"
)
type Window struct {
*Box
primitive Primitive
fullscreen bool
normalX, normalY int
normalW, normalH int
dragX, dragY int
dragWX, dragWY int
sync .RWMutex
}
func NewWindow (primitive Primitive ) *Window {
w := &Window {
Box : NewBox (),
primitive : primitive ,
dragWX : -1 ,
dragWY : -1 ,
}
w .Box .focus = w
return w
}
func (w *Window ) SetFullscreen (fullscreen bool ) {
w .Lock ()
defer w .Unlock ()
if w .fullscreen == fullscreen {
return
}
w .fullscreen = fullscreen
if w .fullscreen {
w .normalX , w .normalY , w .normalW , w .normalH = w .GetRect ()
} else {
w .SetRect (w .normalX , w .normalY , w .normalW , w .normalH )
}
}
func (w *Window ) Focus (delegate func (p Primitive )) {
w .Lock ()
defer w .Unlock ()
w .Box .Focus (delegate )
w .primitive .Focus (delegate )
}
func (w *Window ) Blur () {
w .Lock ()
defer w .Unlock ()
w .Box .Blur ()
w .primitive .Blur ()
}
func (w *Window ) HasFocus () bool {
w .RLock ()
defer w .RUnlock ()
focusable := w .primitive .GetFocusable ()
if focusable != nil {
return focusable .HasFocus ()
}
return w .Box .HasFocus ()
}
func (w *Window ) Draw (screen tcell .Screen ) {
if !w .GetVisible () {
return
}
w .RLock ()
defer w .RUnlock ()
w .Box .Draw (screen )
x , y , width , height := w .GetInnerRect ()
w .primitive .SetRect (x , y , width , height )
w .primitive .Draw (screen )
}
func (w *Window ) InputHandler () func (event *tcell .EventKey , setFocus func (p Primitive )) {
return w .primitive .InputHandler ()
}
func (w *Window ) MouseHandler () func (action MouseAction , event *tcell .EventMouse , setFocus func (p Primitive )) (consumed bool , capture Primitive ) {
return w .WrapMouseHandler (func (action MouseAction , event *tcell .EventMouse , setFocus func (p Primitive )) (consumed bool , capture Primitive ) {
if !w .InRect (event .Position ()) {
return false , nil
}
if action == MouseLeftDown || action == MouseMiddleDown || action == MouseRightDown {
setFocus (w )
}
if action == MouseLeftDown {
x , y , width , height := w .GetRect ()
mouseX , mouseY := event .Position ()
leftEdge := mouseX == x
rightEdge := mouseX == x +width -1
bottomEdge := mouseY == y +height -1
topEdge := mouseY == y
if mouseY >= y && mouseY <= y +height -1 {
if leftEdge {
w .dragX = -1
} else if rightEdge {
w .dragX = 1
}
}
if mouseX >= x && mouseX <= x +width -1 {
if bottomEdge {
w .dragY = -1
} else if topEdge {
if leftEdge || rightEdge {
w .dragY = 1
} else {
w .dragWX = mouseX - x
w .dragWY = mouseY - y
}
}
}
}
_, capture = w .primitive .MouseHandler ()(action , event , setFocus )
return true , capture
})
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .