From 0e9e75f3f84f888c45385fe0f778f45a0ab9c371 Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Tue, 20 Oct 2020 09:22:54 -0700 Subject: [PATCH] Use SetRect to position Windows Resolves #44. --- demos/presentation/window.go | 6 ++---- window.go | 32 +++++++++++++------------------- windowmanager.go | 34 ++++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/demos/presentation/window.go b/demos/presentation/window.go index d8d1ed5..1d3af46 100644 --- a/demos/presentation/window.go +++ b/demos/presentation/window.go @@ -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") diff --git a/window.go b/window.go index 6625dcb..592b2cc 100644 --- a/window.go +++ b/window.go @@ -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. diff --git a/windowmanager.go b/windowmanager.go index 3b77805..ca9fffd 100644 --- a/windowmanager.go +++ b/windowmanager.go @@ -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) } }