Use SetRect to position Windows

Resolves #44.
This commit is contained in:
Trevor Slocum 2020-10-20 09:22:54 -07:00
parent 0b2c8f8a5d
commit 0e9e75f3f8
3 changed files with 43 additions and 29 deletions

View File

@ -24,12 +24,10 @@ func Window(nextSlide func()) (title string, content cview.Primitive) {
loremIpsum.SetText(loremIpsumText)
w1 := cview.NewWindow(list)
w1.SetPosition(2, 2)
w1.SetSize(10, 7)
w1.SetRect(2, 2, 10, 7)
w2 := cview.NewWindow(loremIpsum)
w2.SetPosition(7, 4)
w2.SetSize(12, 12)
w2.SetRect(7, 4, 12, 12)
w1.SetTitle("List")
w2.SetTitle("Lorem Ipsum")

View File

@ -13,9 +13,10 @@ type Window struct {
primitive Primitive
x, y int
width, height int
fullscreen bool
fullscreen bool
normalX, normalY int
normalW, normalH int
dragX, dragY int
dragWX, dragWY int
@ -35,29 +36,22 @@ func NewWindow(primitive Primitive) *Window {
return w
}
// SetPosition sets the position of the window.
func (w *Window) SetPosition(x, y int) {
w.Lock()
defer w.Unlock()
w.x, w.y = x, y
}
// SetSize sets the size of the window.
func (w *Window) SetSize(width, height int) {
w.Lock()
defer w.Unlock()
w.width, w.height = width, height
}
// SetFullscreen sets the flag indicating whether or not the the window should
// be drawn fullscreen.
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)
}
}
// Focus is called when this primitive receives focus.

View File

@ -27,6 +27,10 @@ func (wm *WindowManager) Add(w ...*Window) {
wm.Lock()
defer wm.Unlock()
for _, window := range w {
window.SetBorder(true)
}
wm.windows = append(wm.windows, w...)
}
@ -81,11 +85,10 @@ func (wm *WindowManager) Draw(screen tcell.Screen) {
continue
}
w.SetBorder(false)
w.SetRect(x, y+1, width, height-1)
w.Draw(screen)
hasFullScreen = true
w.SetRect(x-1, y, width+2, height+1)
w.Draw(screen)
}
if hasFullScreen {
return
@ -95,8 +98,27 @@ func (wm *WindowManager) Draw(screen tcell.Screen) {
if !w.GetVisible() {
continue
}
w.SetBorder(true)
w.SetRect(x+w.x, x+w.y, w.width, w.height)
// Reposition out of bounds windows
margin := 3
wx, wy, ww, wh := w.GetRect()
ox, oy := wx, wy
if wx > x+width-margin {
wx = x + width - margin
}
if wx+ww < x+margin {
wx = x - ww + margin
}
if wy > y+height-margin {
wy = y + height - margin
}
if wy < y {
wy = y // No top margin
}
if wx != ox || wy != oy {
w.SetRect(wx, wy, ww, wh)
}
w.Draw(screen)
}
}