Browse Source

Use SetRect to position Windows

Resolves #44.
tablepad
Trevor Slocum 11 months ago
parent
commit
0e9e75f3f8
  1. 6
      demos/presentation/window.go
  2. 32
      window.go
  3. 34
      windowmanager.go

6
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")

32
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.

34
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)
}
}

Loading…
Cancel
Save