summary refs log tree commit diff stats
path: root/lib/ui
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2018-02-27 19:30:59 -0500
committerDrew DeVault <sir@cmpwn.com>2018-02-27 19:31:09 -0500
commit46756487fb56acf26122a7b5d46be2ff8ee3c051 (patch)
tree3be5178f68abd94f4fc1e47904eef24caef78b7a /lib/ui
parent384fe0d82691d55615655fc17a350f710dd4cf1c (diff)
downloadaerc-46756487fb56acf26122a7b5d46be2ff8ee3c051.tar.gz
Add stack UI container
Diffstat (limited to 'lib/ui')
-rw-r--r--lib/ui/grid.go14
-rw-r--r--lib/ui/stack.go73
2 files changed, 80 insertions, 7 deletions
diff --git a/lib/ui/grid.go b/lib/ui/grid.go
index ede7d0c..3c375ee 100644
--- a/lib/ui/grid.go
+++ b/lib/ui/grid.go
@@ -10,7 +10,7 @@ type Grid struct {
 	rowLayout    []gridLayout
 	columns      []GridSpec
 	columnLayout []gridLayout
-	Cells        []*GridCell
+	cells        []*GridCell
 	onInvalidate func(d Drawable)
 	invalid      bool
 }
@@ -76,7 +76,7 @@ func (grid *Grid) Draw(ctx *Context) {
 	if invalid {
 		grid.reflow(ctx)
 	}
-	for _, cell := range grid.Cells {
+	for _, cell := range grid.cells {
 		if !cell.invalid && !invalid {
 			continue
 		}
@@ -140,7 +140,7 @@ func (grid *Grid) invalidateLayout() {
 
 func (grid *Grid) Invalidate() {
 	grid.invalidateLayout()
-	for _, cell := range grid.Cells {
+	for _, cell := range grid.cells {
 		cell.Content.Invalidate()
 	}
 }
@@ -156,7 +156,7 @@ func (grid *Grid) AddChild(content Drawable) *GridCell {
 		Content: content,
 		invalid: true,
 	}
-	grid.Cells = append(grid.Cells, cell)
+	grid.cells = append(grid.cells, cell)
 	cell.Content.OnInvalidate(grid.cellInvalidated)
 	cell.invalid = true
 	grid.invalidateLayout()
@@ -164,9 +164,9 @@ func (grid *Grid) AddChild(content Drawable) *GridCell {
 }
 
 func (grid *Grid) RemoveChild(cell *GridCell) {
-	for i, _cell := range grid.Cells {
+	for i, _cell := range grid.cells {
 		if _cell == cell {
-			grid.Cells = append(grid.Cells[:i], grid.Cells[i+1:]...)
+			grid.cells = append(grid.cells[:i], grid.cells[i+1:]...)
 			break
 		}
 	}
@@ -175,7 +175,7 @@ func (grid *Grid) RemoveChild(cell *GridCell) {
 
 func (grid *Grid) cellInvalidated(drawable Drawable) {
 	var cell *GridCell
-	for _, cell = range grid.Cells {
+	for _, cell = range grid.cells {
 		if cell.Content == drawable {
 			break
 		}
diff --git a/lib/ui/stack.go b/lib/ui/stack.go
new file mode 100644
index 0000000..9f81db8
--- /dev/null
+++ b/lib/ui/stack.go
@@ -0,0 +1,73 @@
+package ui
+
+import (
+	"fmt"
+
+	tb "github.com/nsf/termbox-go"
+)
+
+type Stack struct {
+	children []Drawable
+	onInvalidate func(d Drawable)
+}
+
+func NewStack() *Stack {
+	return &Stack{}
+}
+
+func (stack *Stack) OnInvalidate(onInvalidate func (d Drawable)) {
+	stack.onInvalidate = onInvalidate
+}
+
+func (stack *Stack) Invalidate() {
+	if stack.onInvalidate != nil {
+		stack.onInvalidate(stack)
+	}
+}
+
+func (stack *Stack) Draw(ctx *Context) {
+	if len(stack.children) > 0 {
+		stack.Peek().Draw(ctx)
+	} else {
+		cell := tb.Cell{
+			Fg: tb.ColorDefault,
+			Bg: tb.ColorDefault,
+			Ch: ' ',
+		}
+		ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell)
+	}
+}
+
+func (stack *Stack) Push(d Drawable) {
+	if len(stack.children) != 0 {
+		stack.Peek().OnInvalidate(nil)
+	}
+	stack.children = append(stack.children, d)
+	d.OnInvalidate(stack.invalidateFromChild)
+	stack.Invalidate()
+}
+
+func (stack *Stack) Pop() Drawable {
+	if len(stack.children) == 0 {
+		panic(fmt.Errorf("Tried to pop from an empty UI stack"))
+	}
+	d := stack.children[len(stack.children)-1]
+	stack.children = stack.children[:len(stack.children)-1]
+	stack.Invalidate()
+	d.OnInvalidate(nil)
+	if len(stack.children) != 0 {
+		stack.Peek().OnInvalidate(stack.invalidateFromChild)
+	}
+	return d
+}
+
+func (stack *Stack) Peek() Drawable {
+	if len(stack.children) == 0 {
+		panic(fmt.Errorf("Tried to peek from an empty stack"))
+	}
+	return stack.children[len(stack.children)-1]
+}
+
+func (stack *Stack) invalidateFromChild(d Drawable) {
+	stack.Invalidate()
+}
2 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456