mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-05-08 19:29:42 +08:00
#24 - add docs for Composer
This commit is contained in:
parent
249adc0c10
commit
4622b011c3
36
composer.go
36
composer.go
@ -6,8 +6,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// An service object that manages Views and console, processes events, and provides service methods
|
// Composer is a service object that manages Views and console, processes
|
||||||
// One application must have only one object of this type
|
// events, and provides service methods. One application must have only
|
||||||
|
// one object of this type
|
||||||
type Composer struct {
|
type Composer struct {
|
||||||
// list of visible Views
|
// list of visible Views
|
||||||
views []View
|
views []View
|
||||||
@ -35,7 +36,7 @@ func (c *Composer) initBuffer() {
|
|||||||
c.canvas.Clear(ColorBlack)
|
c.canvas.Clear(ColorBlack)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize library and starts console management
|
// InitLibrary initializes library and starts console management
|
||||||
func InitLibrary() *Composer {
|
func InitLibrary() *Composer {
|
||||||
err := term.Init()
|
err := term.Init()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -94,6 +95,10 @@ func (c *Composer) refreshScreen(invalidate bool) {
|
|||||||
c.redrawAll()
|
c.redrawAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateView constucts a new View
|
||||||
|
// posX and posY are top left coordinates of the View
|
||||||
|
// width and height are View size
|
||||||
|
// title is a View title shown inside the top View line
|
||||||
func (c *Composer) CreateView(posX, posY, width, height int, title string) View {
|
func (c *Composer) CreateView(posX, posY, width, height int, title string) View {
|
||||||
view := NewWindow(c, posX, posY, width, height, title)
|
view := NewWindow(c, posX, posY, width, height, title)
|
||||||
|
|
||||||
@ -327,6 +332,7 @@ func (c *Composer) processKeySeq(ev term.Event) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// processKey returns false in case of the application should be terminated
|
||||||
func (c *Composer) processKey(ev term.Event) bool {
|
func (c *Composer) processKey(ev term.Event) bool {
|
||||||
if c.processKeySeq(ev) {
|
if c.processKeySeq(ev) {
|
||||||
return false
|
return false
|
||||||
@ -338,14 +344,6 @@ func (c *Composer) processKey(ev term.Event) bool {
|
|||||||
switch ev.Key {
|
switch ev.Key {
|
||||||
case term.KeyCtrlQ:
|
case term.KeyCtrlQ:
|
||||||
return true
|
return true
|
||||||
// case term.KeyArrowUp, term.KeyArrowDown, term.KeyArrowLeft, term.KeyArrowRight:
|
|
||||||
// if c.sendEventToActiveView(c.termboxEventToLocal(ev)) {
|
|
||||||
// c.refreshScreen()
|
|
||||||
// }
|
|
||||||
// case term.KeyEnd:
|
|
||||||
// if c.sendEventToActiveView(c.termboxEventToLocal(ev)) {
|
|
||||||
// c.refreshScreen()
|
|
||||||
// }
|
|
||||||
default:
|
default:
|
||||||
if c.sendEventToActiveView(c.termboxEventToLocal(ev)) {
|
if c.sendEventToActiveView(c.termboxEventToLocal(ev)) {
|
||||||
c.topView().Repaint()
|
c.topView().Repaint()
|
||||||
@ -394,13 +392,14 @@ func (c *Composer) processMouseClick(ev term.Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Asks a Composer to stops console management and quit application
|
// Stop sends termination event to Composer. Composer should stop
|
||||||
|
// console management and quit application
|
||||||
func (c *Composer) Stop() {
|
func (c *Composer) Stop() {
|
||||||
ev := Event{Type: EventQuit}
|
ev := Event{Type: EventQuit}
|
||||||
go c.PutEvent(ev)
|
go c.PutEvent(ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Main event loop
|
// MainLoop starts the main application event loop
|
||||||
func (c *Composer) MainLoop() {
|
func (c *Composer) MainLoop() {
|
||||||
c.refreshScreen(true)
|
c.refreshScreen(true)
|
||||||
|
|
||||||
@ -438,22 +437,25 @@ func (c *Composer) MainLoop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send event to a Composer. Used by Windows to ask for repainting or for quitting the application
|
// PutEvent send event to a Composer directly.
|
||||||
|
// Used by Views to ask for repainting or for quitting the application
|
||||||
func (c *Composer) PutEvent(ev Event) {
|
func (c *Composer) PutEvent(ev Event) {
|
||||||
c.channel <- ev
|
c.channel <- ev
|
||||||
}
|
}
|
||||||
|
|
||||||
// Closes console management and makes a console cursor visible
|
// Close closes console management and makes a console cursor visible
|
||||||
func (c *Composer) Close() {
|
func (c *Composer) Close() {
|
||||||
term.SetCursor(3, 3)
|
term.SetCursor(3, 3)
|
||||||
term.Close()
|
term.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shows consolse cursor at given position. Setting cursor to -1,-1 hides cursor
|
// SetCursorPos shows consolse cursor at given position.
|
||||||
|
// Setting cursor to -1,-1 hides cursor
|
||||||
func (c *Composer) SetCursorPos(x, y int) {
|
func (c *Composer) SetCursorPos(x, y int) {
|
||||||
term.SetCursor(x, y)
|
term.SetCursor(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DestroyView removes the View from the list of managed views
|
||||||
func (c *Composer) DestroyView(view View) {
|
func (c *Composer) DestroyView(view View) {
|
||||||
ev := Event{Type: EventClose}
|
ev := Event{Type: EventClose}
|
||||||
c.sendEventToActiveView(ev)
|
c.sendEventToActiveView(ev)
|
||||||
@ -468,6 +470,8 @@ func (c *Composer) DestroyView(view View) {
|
|||||||
c.activateView(c.topView())
|
c.activateView(c.topView())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Theme returns the theme manager. Theme manager implements
|
||||||
|
// Theme interface, so a caller can read the current colors
|
||||||
func (c *Composer) Theme() Theme {
|
func (c *Composer) Theme() Theme {
|
||||||
return c.themeManager
|
return c.themeManager
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user