clui/interface.go

322 lines
15 KiB
Go
Raw Normal View History

2015-10-16 10:27:43 -07:00
package clui
import (
term "github.com/nsf/termbox-go"
"log"
)
2015-10-26 17:19:22 -07:00
/*
Screen is a core of the library. It dispatches keyboard and mouse messages, supports theming, and manages views
*/
2015-10-16 10:27:43 -07:00
type Screen interface {
2015-10-26 17:19:22 -07:00
// Theme returns the current color theme
2015-10-16 10:27:43 -07:00
Theme() Theme
2015-10-26 17:19:22 -07:00
// PutEvent sends an event directly to the the event loop. It is used by some controls to ask Screen to repaint console
PutEvent(event Event)
// DestroyView removes view from the view list and makes the next view in the view stack active. It is not possible to destroy the last view - Screen must have at least one visible view
DestroyView(view View)
2015-10-30 15:12:35 -07:00
// Size returns size of the console(visible) buffer
Size() (width int, height int)
2015-10-16 10:27:43 -07:00
Logger() *log.Logger
}
2015-10-26 17:19:22 -07:00
/*
Canvas is a 'graphical' buffer than represents a View or Screen. Its size equal size of parent object and supports a full set of painting methods
*/
2015-10-16 10:27:43 -07:00
type Canvas interface {
2015-10-26 17:19:22 -07:00
// SetSize sets the new Canvas size. If new size does not equal old size then Canvas is recreated and cleared with default colors. Both Canvas width and height must be greater than 2
SetSize(width int, height int)
// Size returns current Canvas size
Size() (width int, height int)
// PutSymbol sets value for the Canvas cell: rune and its colors. Returns result of operation: e.g, if the symbol position is outside Canvas the operation fails and the function returns false
PutSymbol(x int, y int, symbol term.Cell) bool
// PutText draws horizontal string on Canvas clipping by Canvas boundaries. x and y are starting point, text is a string to display, fg and bg are text and background attributes
PutText(x int, y int, text string, fg term.Attribute, bg term.Attribute)
// PutVerticalText draws vertical string on Canvas clipping by Canvas boundaries. x and y are starting point, text is a string to display, fg and bg are text and background attributes
PutVerticalText(x int, y int, text string, fg term.Attribute, bg term.Attribute)
/*
PutColorizedText draws multicolor string on Canvas clipping by Canvas boundaries.
Multiline is not supported. Align feature is limited: the text is aligned only if it is
shorter than maximum width, and displayed left aligned otherwise
*/
PutColorizedText(x int, y int, maxWidth int, text string, fg term.Attribute, bg term.Attribute, dir Direction, align Align)
// Symbol returns current Canvas cell value at given coordinates. If coordinates are outside Canvas ok is false
Symbol(x int, y int) (symbol term.Cell, ok bool)
// Clear fills Canvas with given background color
Clear(bg term.Attribute)
// FillRect fills area of Canvas with user-defined rune and colors
FillRect(x int, y int, width int, height int, symbol term.Cell)
// DrawFrame paints a frame inside Canvas with optional border rune set(by default, in case of border is empty string, the rune set equals "─│┌┐└┘" - single border). The inner area of frame is not filled - in other words it is transparent
DrawFrame(x int, y int, width int, height int, fg term.Attribute, bg term.Attribute, border string)
// SetCursorPos sets text caret position. Used by controls like EditField
SetCursorPos(x int, y int)
2015-10-16 10:27:43 -07:00
}
2015-10-28 17:21:55 -07:00
// Theme is a theme manager: set of colors and object looks
2015-10-16 10:27:43 -07:00
type Theme interface {
2015-10-28 17:21:55 -07:00
// SysObject returns object look by its id for the current
// theme. E.g, border lines for frame or arrows for scrollbar
2015-10-20 14:32:16 -07:00
SysObject(string) string
2015-10-28 17:21:55 -07:00
// SysColor returns attribute by its id for the current theme
2015-10-20 14:32:16 -07:00
SysColor(string) term.Attribute
2015-10-28 17:21:55 -07:00
// SetCurrentTheme changes the current theme.
// Returns false if changing failed - e.g, theme does not exist
2015-10-20 15:46:14 -07:00
SetCurrentTheme(string) bool
2015-10-28 17:21:55 -07:00
// ThemeNames returns the list of short theme names (file names)
2015-10-20 15:46:14 -07:00
ThemeNames() []string
2015-10-28 17:21:55 -07:00
// ThemeInfo returns detailed info about theme
2015-10-20 15:46:14 -07:00
ThemeInfo(string) ThemeInfo
2015-10-28 17:57:05 -07:00
// SetThemePath changes the directory that contains themes.
// If new path does not equal old one, theme list reloads
2015-10-20 15:46:14 -07:00
SetThemePath(string)
2015-10-16 10:27:43 -07:00
}
2015-10-29 16:13:31 -07:00
// View is an interface that every object that is managed by
// composer should implement
2015-10-16 10:27:43 -07:00
type View interface {
2015-10-28 13:18:53 -07:00
// Title returns the current title or text of the control
2015-10-16 10:27:43 -07:00
Title() string
2015-10-28 13:18:53 -07:00
// SetTitle changes control text or title
2015-10-16 10:27:43 -07:00
SetTitle(string)
2015-10-29 16:13:31 -07:00
// Draw paints the view screen buffer to a canvas. It does not
// repaint all view children
2015-10-16 10:27:43 -07:00
Draw(Canvas)
2015-10-27 15:21:13 -07:00
// Repaint draws the control on console surface
2015-10-16 10:27:43 -07:00
Repaint()
2015-10-28 13:18:53 -07:00
// Constraints return minimal control widht and height
2015-10-16 10:27:43 -07:00
Constraints() (int, int)
2015-10-28 13:18:53 -07:00
// Size returns current control width and height
2015-10-16 10:27:43 -07:00
Size() (int, int)
2015-10-28 13:18:53 -07:00
// SetSize changes control size. Constant DoNotChange can be
// used as placeholder to indicate that the control attrubute
// should be unchanged.
// Method panics if new size is less than minimal size
2015-10-16 10:27:43 -07:00
SetSize(int, int)
2015-10-28 13:18:53 -07:00
// Pos returns the current control position: X and Y.
// For View the position's origin is top left corner of console window,
// for other controls the origin is top left corner of View that hold
// the control
2015-10-16 10:27:43 -07:00
Pos() (int, int)
2015-10-28 13:18:53 -07:00
// SetPos changes contols position. Manual call of the method does not
// make sense for any control except View because control positions
// inside of container always recalculated after View resizes
2015-10-16 10:27:43 -07:00
SetPos(int, int)
2015-10-29 16:13:31 -07:00
// Canvas returns an internal graphic buffer to draw everything.
// Used by children controls - they paint themselves on the canvas
2015-10-16 10:27:43 -07:00
Canvas() Canvas
2015-10-28 13:18:53 -07:00
// Active returns if a control is active. Only active controls can
// process keyboard events. Parent View looks for active controls to
// make sure that there is only one active control at a time
2015-10-16 10:27:43 -07:00
Active() bool
2015-10-28 13:18:53 -07:00
// SetActive activates and deactivates control
2015-10-16 10:27:43 -07:00
SetActive(bool)
2015-10-27 15:21:13 -07:00
/*
ProcessEvent processes all events come from the control parent. If a control
processes an event it should return true. If the method returns false it means
that the control do not want or cannot process the event and the caller sends
the event to the control parent
*/
2015-10-16 10:27:43 -07:00
ProcessEvent(Event) bool
2015-10-29 16:13:31 -07:00
// ActivateControl make the control active and previously
// focused control loses the focus. As a side effect the method
// emits two events: deactivate for previously focused and
// activate for new one if it is possible (EventActivate with
// different X values)
2015-10-16 10:27:43 -07:00
ActivateControl(Control)
2015-10-29 16:13:31 -07:00
// RegisterControl adds a control to the view control list. It
// a list of all controls visible on the view - used to
// calculate the control under mouse when a user clicks, and
// to calculate the next control after a user presses TAB key
2015-10-16 10:27:43 -07:00
RegisterControl(Control)
2015-10-29 16:13:31 -07:00
// Screen returns the composer that manages the view
2015-10-16 10:27:43 -07:00
Screen() Screen
2015-10-28 13:18:53 -07:00
// Parent return control's container or nil if there is no parent container
2015-10-16 10:27:43 -07:00
Parent() Control
2015-10-29 16:13:31 -07:00
// HitTest returns the area that corresponds to the clicked
// position X, Y (absolute position in console window): title,
// internal view area, title button, border or outside the view
2015-10-16 10:27:43 -07:00
HitTest(int, int) HitResult
2015-10-29 16:13:31 -07:00
// SetModal enables or disables modal mode
2015-10-21 09:38:43 -07:00
SetModal(bool)
2015-10-29 16:13:31 -07:00
// Modal returns if the view is in modal mode.In modal mode a
// user cannot switch to any other view until the user closes
// the modal view. Used by confirmation and select dialog to be
// sure that the user has made a choice before continuing work
2015-10-21 09:38:43 -07:00
Modal() bool
2015-10-29 16:13:31 -07:00
// OnClose sets a callback that is called when view is closed.
// For dialogs after windows is closed a user can check the
// close result
2015-10-21 17:04:57 -07:00
OnClose(func(Event))
2015-10-16 10:27:43 -07:00
2015-10-28 13:18:53 -07:00
// Paddings returns a number of spaces used to auto-arrange children inside
// a container: indent from left and right sides, indent from top and bottom
// sides, horizontal space between controls, vertical space between controls.
// Horizontal space is used in case of PackType is horizontal, and vertical
// in other case
2015-10-16 10:27:43 -07:00
Paddings() (int, int, int, int)
2015-10-28 13:18:53 -07:00
// SetPaddings changes indents for the container. Use DoNotChange as a placeholder
// if you do not want to touch a parameter
SetPaddings(int, int, int, int)
2015-10-29 16:13:31 -07:00
// AddChild add control to a list of view children. Minimal size
// of the view calculated as a sum of sizes of its children.
// Method panics if the same control is added twice
2015-10-16 10:27:43 -07:00
AddChild(Control, int)
2015-10-28 13:18:53 -07:00
// SetPack changes the direction of children packing
2015-10-16 10:27:43 -07:00
SetPack(PackType)
2015-10-28 13:18:53 -07:00
// Pack returns direction in which a container packs
// its children: horizontal or vertical
2015-10-16 10:27:43 -07:00
Pack() PackType
2015-10-28 13:18:53 -07:00
// Children returns the list of container child controls
2015-10-16 10:27:43 -07:00
Children() []Control
2015-10-29 16:13:31 -07:00
// ChildExists returns true if the container already has
// the control in its children list
2015-10-16 10:27:43 -07:00
ChildExists(Control) bool
2015-10-28 13:18:53 -07:00
// Scale return scale coefficient that is used to calculate
// new control size after its parent resizes.
// DoNotScale means the controls never changes its size.
// Any positive value is a real coefficient of scaling.
// How the scaling works: after resizing, parent control
// calculates the difference between minimal and current sizes,
// then divides the difference between controls that has
// positive scale depending on a scale value. The more scale,
// the larger control after resizing. Example: if you have
// two controls with scales 1 and 2, then after every resizing
// the latter controls expands by 100% more than the first one.
2015-10-16 10:27:43 -07:00
Scale() int
2015-10-28 13:18:53 -07:00
// SetScale sets a scale coefficient for the control.
// See Scale method for details
2015-10-16 10:27:43 -07:00
SetScale(int)
2015-10-28 13:18:53 -07:00
// TabStop returns if a control can be selected by traversing
// controls using TAB key
2015-10-16 10:27:43 -07:00
TabStop() bool
2015-10-28 13:18:53 -07:00
// Colors return the basic attrubutes for the controls: text
// attribute and background one. Some controls inroduce their
// own additional controls: see ProgressBar
2015-10-16 10:27:43 -07:00
Colors() (term.Attribute, term.Attribute)
2015-10-28 13:18:53 -07:00
// ActiveColors return the attrubutes for the controls when it
// is active: text and background colors
2015-10-19 12:05:43 -07:00
ActiveColors() (term.Attribute, term.Attribute)
2015-10-28 13:18:53 -07:00
// SetBackColor changes background color of the control
2015-10-19 12:05:43 -07:00
SetBackColor(term.Attribute)
2015-10-28 13:18:53 -07:00
// SetActiveBackColor changes background color of the active control
2015-10-19 12:05:43 -07:00
SetActiveBackColor(term.Attribute)
2015-10-28 13:18:53 -07:00
// SetTextColor changes text color of the control
2015-10-19 12:05:43 -07:00
SetTextColor(term.Attribute)
2015-10-28 13:18:53 -07:00
// SetActiveTextColor changes text color of the active control
2015-10-19 12:05:43 -07:00
SetActiveTextColor(term.Attribute)
2015-10-28 13:18:53 -07:00
// RecalculateConstraints used by containers to recalculate new minimal size
// depending on its children constraints after a new child is added
2015-10-17 01:16:19 -07:00
RecalculateConstraints()
2015-10-30 15:12:35 -07:00
// SetMaximized opens the view to full screen or restores its
// previous size
SetMaximized(maximize bool)
// Maximized returns if the view is in full screen mode
Maximized() bool
2015-10-16 10:27:43 -07:00
Logger() *log.Logger
}
2015-10-28 17:57:05 -07:00
// Control is an interface that every visible control on the View must
// implement
2015-10-16 10:27:43 -07:00
type Control interface {
2015-10-27 17:47:53 -07:00
// Title returns the current title or text of the control
2015-10-16 10:27:43 -07:00
Title() string
2015-10-27 17:47:53 -07:00
// SetTitle changes control text or title
2015-10-16 10:27:43 -07:00
SetTitle(string)
2015-10-27 17:47:53 -07:00
// Pos returns the current control position: X and Y.
// For View the position's origin is top left corner of console window,
// for other controls the origin is top left corner of View that hold
// the control
2015-10-16 10:27:43 -07:00
Pos() (int, int)
2015-10-27 17:47:53 -07:00
// SetPos changes contols position. Manual call of the method does not
// make sense for any control except View because control positions
// inside of container always recalculated after View resizes
2015-10-16 10:27:43 -07:00
SetPos(int, int)
2015-10-27 17:47:53 -07:00
// Size returns current control width and height
2015-10-16 10:27:43 -07:00
Size() (int, int)
2015-10-27 17:47:53 -07:00
// SetSize changes control size. Constant DoNotChange can be
// used as placeholder to indicate that the control attrubute
// should be unchanged.
// Method panics if new size is less than minimal size
2015-10-16 10:27:43 -07:00
SetSize(int, int)
2015-10-28 13:18:53 -07:00
// Scale return scale coefficient that is used to calculate
// new control size after its parent resizes.
// DoNotScale means the controls never changes its size.
// Any positive value is a real coefficient of scaling.
// How the scaling works: after resizing, parent control
// calculates the difference between minimal and current sizes,
// then divides the difference between controls that has
// positive scale depending on a scale value. The more scale,
// the larger control after resizing. Example: if you have
// two controls with scales 1 and 2, then after every resizing
// the latter controls expands by 100% more than the first one.
2015-10-16 10:27:43 -07:00
Scale() int
2015-10-28 13:18:53 -07:00
// SetScale sets a scale coefficient for the control.
// See Scale method for details
2015-10-16 10:27:43 -07:00
SetScale(int)
2015-10-28 13:18:53 -07:00
// Constraints return minimal control widht and height
2015-10-16 10:27:43 -07:00
Constraints() (int, int)
2015-10-28 13:18:53 -07:00
// Paddings returns a number of spaces used to auto-arrange children inside
// a container: indent from left and right sides, indent from top and bottom
// sides, horizontal space between controls, vertical space between controls.
// Horizontal space is used in case of PackType is horizontal, and vertical
// in other case
2015-10-16 10:27:43 -07:00
Paddings() (int, int, int, int)
2015-10-28 13:18:53 -07:00
// SetPaddings changes indents for the container. Use DoNotChange as a placeholder
// if you do not want to touch a parameter
SetPaddings(int, int, int, int)
2015-10-27 15:21:13 -07:00
// Repaint draws the control on its View surface
2015-10-16 10:27:43 -07:00
Repaint()
2015-10-28 13:18:53 -07:00
// AddChild adds a new child to a container. For the most
// of controls the method is just a stub that panics
// because not every control can be a container
2015-10-16 10:27:43 -07:00
AddChild(Control, int)
2015-10-28 13:18:53 -07:00
// SetPack changes the direction of children packing
2015-10-16 10:27:43 -07:00
SetPack(PackType)
2015-10-28 13:18:53 -07:00
// Pack returns direction in which a container packs
// its children: horizontal or vertical
2015-10-16 10:27:43 -07:00
Pack() PackType
2015-10-28 13:18:53 -07:00
// Children returns the list of container child controls
2015-10-16 10:27:43 -07:00
Children() []Control
2015-10-28 13:18:53 -07:00
// Active returns if a control is active. Only active controls can
// process keyboard events. Parent View looks for active controls to
// make sure that there is only one active control at a time
2015-10-16 10:27:43 -07:00
Active() bool
2015-10-28 13:18:53 -07:00
// SetActive activates and deactivates control
2015-10-16 10:27:43 -07:00
SetActive(bool)
2015-10-27 15:21:13 -07:00
/*
ProcessEvent processes all events come from the control parent. If a control
processes an event it should return true. If the method returns false it means
that the control do not want or cannot process the event and the caller sends
the event to the control parent
*/
2015-10-16 10:27:43 -07:00
ProcessEvent(Event) bool
2015-10-28 13:18:53 -07:00
// TabStop returns if a control can be selected by traversing
// controls using TAB key
2015-10-16 10:27:43 -07:00
TabStop() bool
2015-10-28 13:18:53 -07:00
// Parent return control's container or nil if there is no parent container
2015-10-16 10:27:43 -07:00
Parent() Control
2015-10-28 13:18:53 -07:00
// Colors return the basic attrubutes for the controls: text
// attribute and background one. Some controls inroduce their
// own additional controls: see ProgressBar
2015-10-16 10:27:43 -07:00
Colors() (term.Attribute, term.Attribute)
2015-10-28 13:18:53 -07:00
// ActiveColors return the attrubutes for the controls when it
// is active: text and background colors
2015-10-19 12:05:43 -07:00
ActiveColors() (term.Attribute, term.Attribute)
2015-10-28 13:18:53 -07:00
// SetBackColor changes background color of the control
2015-10-19 12:05:43 -07:00
SetBackColor(term.Attribute)
2015-10-28 13:18:53 -07:00
// SetActiveBackColor changes background color of the active control
2015-10-19 12:05:43 -07:00
SetActiveBackColor(term.Attribute)
2015-10-28 13:18:53 -07:00
// SetTextColor changes text color of the control
2015-10-19 12:05:43 -07:00
SetTextColor(term.Attribute)
2015-10-28 13:18:53 -07:00
// SetActiveTextColor changes text color of the active control
2015-10-19 12:05:43 -07:00
SetActiveTextColor(term.Attribute)
2015-10-16 10:27:43 -07:00
2015-10-28 13:18:53 -07:00
// RecalculateConstraints used by containers to recalculate new minimal size
// depending on its children constraints after a new child is added
2015-10-17 01:16:19 -07:00
RecalculateConstraints()
2015-10-16 10:27:43 -07:00
Logger() *log.Logger
}