mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-05-01 22:18:35 +08:00
285 Рефакторинг
This commit is contained in:
parent
d3f3c56951
commit
e844aaf4ac
@ -606,7 +606,7 @@ func (c *BaseControl) setClipper() {
|
|||||||
x, y, w, h := CalcClipper(c)
|
x, y, w, h := CalcClipper(c)
|
||||||
c.clipper = &rect{x: x, y: y, w: w, h: h}
|
c.clipper = &rect{x: x, y: y, w: w, h: h}
|
||||||
}
|
}
|
||||||
|
//HitTest --
|
||||||
func (c *BaseControl) HitTest(x, y int) мКнст.HitResult {
|
func (c *BaseControl) HitTest(x, y int) мКнст.HitResult {
|
||||||
if x > c.x && x < c.x+c.width-1 &&
|
if x > c.x && x < c.x+c.width-1 &&
|
||||||
y > c.y && y < c.y+c.height-1 {
|
y > c.y && y < c.y+c.height-1 {
|
||||||
|
@ -22,7 +22,7 @@ type Button struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
NewButton creates a new Button.
|
CreateButton creates a new Button.
|
||||||
view - is a View that manages the control
|
view - is a View that manages the control
|
||||||
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
|
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
|
||||||
width and heigth - are minimal size of the control.
|
width and heigth - are minimal size of the control.
|
||||||
@ -63,7 +63,7 @@ func CreateButton(parent Control, width, height int, title string, scale int) *B
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repaint draws the control on its View surface
|
//Draw Repaint draws the control on its View surface
|
||||||
func (b *Button) Draw() {
|
func (b *Button) Draw() {
|
||||||
if b.hidden {
|
if b.hidden {
|
||||||
return
|
return
|
||||||
|
12
canvas.go
12
canvas.go
@ -137,7 +137,7 @@ func Flush() {
|
|||||||
term.Flush()
|
term.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSize sets the new Canvas size. If new size does not
|
// SetScreenSize sets the new Canvas size. If new size does not
|
||||||
// equal old size then Canvas is recreated and cleared
|
// equal old size then Canvas is recreated and cleared
|
||||||
// with default colors. Both Canvas width and height must
|
// with default colors. Both Canvas width and height must
|
||||||
// be greater than 2
|
// be greater than 2
|
||||||
@ -153,7 +153,7 @@ func SetScreenSize(width int, height int) {
|
|||||||
SetClipRect(0, 0, width, height)
|
SetClipRect(0, 0, width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns current Canvas size
|
// ScreenSize returns current Canvas size
|
||||||
func ScreenSize() (width int, height int) {
|
func ScreenSize() (width int, height int) {
|
||||||
return canvas.width, canvas.height
|
return canvas.width, canvas.height
|
||||||
}
|
}
|
||||||
@ -197,11 +197,11 @@ func SetTextColor(clr term.Attribute) {
|
|||||||
func SetBackColor(clr term.Attribute) {
|
func SetBackColor(clr term.Attribute) {
|
||||||
canvas.backColor = clr
|
canvas.backColor = clr
|
||||||
}
|
}
|
||||||
|
//TextColor --
|
||||||
func TextColor() term.Attribute {
|
func TextColor() term.Attribute {
|
||||||
return canvas.textColor
|
return canvas.textColor
|
||||||
}
|
}
|
||||||
|
//BackColor --
|
||||||
func BackColor() term.Attribute {
|
func BackColor() term.Attribute {
|
||||||
return canvas.backColor
|
return canvas.backColor
|
||||||
}
|
}
|
||||||
@ -280,7 +280,7 @@ func DrawText(x, y int, text string) {
|
|||||||
if unicode.Is(unicode.Scripts["Han"], elem.Ch) {
|
if unicode.Is(unicode.Scripts["Han"], elem.Ch) {
|
||||||
x += 2
|
x += 2
|
||||||
} else {
|
} else {
|
||||||
x += 1
|
x++
|
||||||
}
|
}
|
||||||
|
|
||||||
if firstdrawn && !drawn {
|
if firstdrawn && !drawn {
|
||||||
@ -338,7 +338,7 @@ func DrawTextVertical(x, y int, text string) {
|
|||||||
SetTextColor(elem.Fg)
|
SetTextColor(elem.Fg)
|
||||||
SetBackColor(elem.Bg)
|
SetBackColor(elem.Bg)
|
||||||
drawn := PutChar(x, y, elem.Ch)
|
drawn := PutChar(x, y, elem.Ch)
|
||||||
y += 1
|
y++
|
||||||
if firstdrawn && !drawn {
|
if firstdrawn && !drawn {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -52,7 +52,7 @@ func CreateCheckBox(parent Control, width int, title string, scale int) *CheckBo
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repaint draws the control on its View surface
|
//Draw Repaint draws the control on its View surface
|
||||||
func (c *CheckBox) Draw() {
|
func (c *CheckBox) Draw() {
|
||||||
if c.hidden {
|
if c.hidden {
|
||||||
return
|
return
|
||||||
|
@ -3,7 +3,7 @@ package clui
|
|||||||
import (
|
import (
|
||||||
term "github.com/nsf/termbox-go"
|
term "github.com/nsf/termbox-go"
|
||||||
)
|
)
|
||||||
|
//InitLibrary --
|
||||||
func InitLibrary() bool {
|
func InitLibrary() bool {
|
||||||
initThemeManager()
|
initThemeManager()
|
||||||
initComposer()
|
initComposer()
|
||||||
@ -11,7 +11,7 @@ func InitLibrary() bool {
|
|||||||
return initCanvas()
|
return initCanvas()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes console management and makes a console cursor visible
|
//DeinitLibrary Close closes console management and makes a console cursor visible
|
||||||
func DeinitLibrary() {
|
func DeinitLibrary() {
|
||||||
term.SetCursor(3, 3)
|
term.SetCursor(3, 3)
|
||||||
term.Close()
|
term.Close()
|
||||||
|
@ -2,10 +2,11 @@ package clui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
мКнст "./пакКонстанты"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParserEmpty(t *testing.T) {
|
func TestParserEmpty(t *testing.T) {
|
||||||
prs := NewColorParser("", ColorBlack, ColorWhite)
|
prs := NewColorParser("", мКнст.ColorBlack, мКнст.ColorWhite)
|
||||||
|
|
||||||
elem := prs.NextElement()
|
elem := prs.NextElement()
|
||||||
|
|
||||||
@ -17,20 +18,20 @@ func TestParserEmpty(t *testing.T) {
|
|||||||
|
|
||||||
func TestParserColors(t *testing.T) {
|
func TestParserColors(t *testing.T) {
|
||||||
prs := NewColorParser("a<b:green>c<t:red>d<b:>e<t:>fg\nf",
|
prs := NewColorParser("a<b:green>c<t:red>d<b:>e<t:>fg\nf",
|
||||||
ColorBlack, ColorWhite)
|
мКнст.ColorBlack, мКнст.ColorWhite)
|
||||||
elems := []TextElement{
|
elems := []TextElement{
|
||||||
{ElemPrintable, 'a', ColorBlack, ColorWhite},
|
{ElemPrintable, 'a', мКнст.ColorBlack, мКнст.ColorWhite},
|
||||||
{ElemBackColor, ' ', ColorBlack, ColorGreen},
|
{ElemBackColor, ' ', мКнст.ColorBlack, мКнст.ColorGreen},
|
||||||
{ElemPrintable, 'c', ColorBlack, ColorGreen},
|
{ElemPrintable, 'c', мКнст.ColorBlack, мКнст.ColorGreen},
|
||||||
{ElemTextColor, 'c', ColorRed, ColorGreen},
|
{ElemTextColor, 'c', мКнст.ColorRed, мКнст.ColorGreen},
|
||||||
{ElemPrintable, 'd', ColorRed, ColorGreen},
|
{ElemPrintable, 'd', мКнст.ColorRed, мКнст.ColorGreen},
|
||||||
{ElemBackColor, 'd', ColorRed, ColorWhite},
|
{ElemBackColor, 'd', мКнст.ColorRed, мКнст.ColorWhite},
|
||||||
{ElemPrintable, 'e', ColorRed, ColorWhite},
|
{ElemPrintable, 'e', мКнст.ColorRed, мКнст.ColorWhite},
|
||||||
{ElemTextColor, 'e', ColorBlack, ColorWhite},
|
{ElemTextColor, 'e', мКнст.ColorBlack, мКнст.ColorWhite},
|
||||||
{ElemPrintable, 'f', ColorBlack, ColorWhite},
|
{ElemPrintable, 'f', мКнст.ColorBlack, мКнст.ColorWhite},
|
||||||
{ElemPrintable, 'g', ColorBlack, ColorWhite},
|
{ElemPrintable, 'g', мКнст.ColorBlack, мКнст.ColorWhite},
|
||||||
{ElemLineBreak, 'g', ColorBlack, ColorWhite},
|
{ElemLineBreak, 'g', мКнст.ColorBlack, мКнст.ColorWhite},
|
||||||
{ElemPrintable, 'f', ColorBlack, ColorWhite},
|
{ElemPrintable, 'f', мКнст.ColorBlack, мКнст.ColorWhite},
|
||||||
}
|
}
|
||||||
|
|
||||||
idx := 0
|
idx := 0
|
||||||
@ -53,6 +54,6 @@ func TestParserColors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
el = prs.NextElement()
|
el = prs.NextElement()
|
||||||
idx += 1
|
idx++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ func termboxEventToLocal(ev term.Event) мКнст.Event {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repaints everything on the screen
|
//RefreshScreen Repaints everything on the screen
|
||||||
func RefreshScreen() {
|
func RefreshScreen() {
|
||||||
comp.BeginUpdate()
|
comp.BeginUpdate()
|
||||||
term.Clear(мКнст.ColorWhite, мКнст.ColorBlack)
|
term.Clear(мКнст.ColorWhite, мКнст.ColorBlack)
|
||||||
@ -118,7 +118,7 @@ func AddWindow(posX, posY, width, height int, title string) *Window {
|
|||||||
return window
|
return window
|
||||||
}
|
}
|
||||||
|
|
||||||
// Border returns the default window border
|
//BorderStyle returns the default window border
|
||||||
func (c *Composer) BorderStyle() мКнст.BorderStyle {
|
func (c *Composer) BorderStyle() мКнст.BorderStyle {
|
||||||
return c.windowBorder
|
return c.windowBorder
|
||||||
}
|
}
|
||||||
@ -697,7 +697,7 @@ func (c *Composer) processKey(ev мКнст.Event) {
|
|||||||
c.lastKey = term.KeyEsc
|
c.lastKey = term.KeyEsc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//ProcessEvent --
|
||||||
func ProcessEvent(ev мКнст.Event) {
|
func ProcessEvent(ev мКнст.Event) {
|
||||||
switch ev.Type {
|
switch ev.Type {
|
||||||
case мКнст.EventCloseWindow:
|
case мКнст.EventCloseWindow:
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package clui
|
package clui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
term "github.com/nsf/termbox-go"
|
|
||||||
мКнст "./пакКонстанты"
|
мКнст "./пакКонстанты"
|
||||||
|
term "github.com/nsf/termbox-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ThumbPosition returns a scrollbar thumb position depending
|
// ThumbPosition returns a scrollbar thumb position depending
|
||||||
@ -263,9 +263,9 @@ func NextControl(parent Control, curr Control, next bool) Control {
|
|||||||
|
|
||||||
if next {
|
if next {
|
||||||
return linear[nIndex]
|
return linear[nIndex]
|
||||||
} else {
|
|
||||||
return linear[pIndex]
|
|
||||||
}
|
}
|
||||||
|
return linear[pIndex]
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendEventToChild tries to find a child control that should recieve the evetn
|
// SendEventToChild tries to find a child control that should recieve the evetn
|
||||||
|
@ -32,7 +32,7 @@ type EditField struct {
|
|||||||
onKeyPress func(term.Key, rune) bool
|
onKeyPress func(term.Key, rune) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEditField creates a new EditField control
|
// CreateEditField creates a new EditField control
|
||||||
// view - is a View that manages the control
|
// view - is a View that manages the control
|
||||||
// parent - is container that keeps the control. The same View can be a view and a parent at the same time.
|
// parent - is container that keeps the control. The same View can be a view and a parent at the same time.
|
||||||
// width - is minimal width of the control.
|
// width - is minimal width of the control.
|
||||||
|
2
label.go
2
label.go
@ -21,7 +21,7 @@ type Label struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
NewLabel creates a new label.
|
CreateLabel creates a new label.
|
||||||
view - is a View that manages the control
|
view - is a View that manages the control
|
||||||
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
|
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
|
||||||
w and h - are minimal size of the control.
|
w and h - are minimal size of the control.
|
||||||
|
@ -2,11 +2,12 @@ package clui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
мКнст "./пакКонстанты"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestListBox(t *testing.T) {
|
func TestListBox(t *testing.T) {
|
||||||
width, height := 10, 5
|
width, height := 10, 5
|
||||||
lbox := CreateListBox(nil, width, height, Fixed)
|
lbox := CreateListBox(nil, width, height, мКнст.Fixed)
|
||||||
|
|
||||||
w, h := lbox.Size()
|
w, h := lbox.Size()
|
||||||
if w != width {
|
if w != width {
|
||||||
|
@ -2,6 +2,7 @@ package clui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
мКнст "./пакКонстанты"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEllipsize(t *testing.T) {
|
func TestEllipsize(t *testing.T) {
|
||||||
@ -46,16 +47,16 @@ func TestCutText(t *testing.T) {
|
|||||||
func TestAlignText(t *testing.T) {
|
func TestAlignText(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
in, want string
|
in, want string
|
||||||
align Align
|
align мКнст.Align
|
||||||
max, shift int
|
max, shift int
|
||||||
}{
|
}{
|
||||||
{"abcdefgh", "abcde", AlignLeft, 5, 0},
|
{"abcdefgh", "abcde", мКнст.AlignLeft, 5, 0},
|
||||||
{"abcdefgh", "defgh", AlignRight, 5, 0},
|
{"abcdefgh", "defgh", мКнст.AlignRight, 5, 0},
|
||||||
{"abcdefgh", "bcdef", AlignCenter, 5, 0},
|
{"abcdefgh", "bcdef", мКнст.AlignCenter, 5, 0},
|
||||||
{"abcdefgh", "abcdefgh", AlignLeft, 10, 0},
|
{"abcdefgh", "abcdefgh", мКнст.AlignLeft, 10, 0},
|
||||||
{"abcdefgh", "abcdefgh", AlignRight, 10, 2},
|
{"abcdefgh", "abcdefgh", мКнст.AlignRight, 10, 2},
|
||||||
{"abcdefgh", "abcdefgh", AlignCenter, 10, 1},
|
{"abcdefgh", "abcdefgh", мКнст.AlignCenter, 10, 1},
|
||||||
{"abcdefg", "abcdefg", AlignCenter, 10, 2},
|
{"abcdefg", "abcdefg", мКнст.AlignCenter, 10, 2},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
@ -69,25 +70,25 @@ func TestAlignText(t *testing.T) {
|
|||||||
func TestAlignColorizedText(t *testing.T) {
|
func TestAlignColorizedText(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
in, want string
|
in, want string
|
||||||
align Align
|
align мКнст.Align
|
||||||
max, shift int
|
max, shift int
|
||||||
}{
|
}{
|
||||||
// uncolored cases
|
// uncolored cases
|
||||||
{"abcdefgh", "abcde", AlignLeft, 5, 0},
|
{"abcdefgh", "abcde", мКнст.AlignLeft, 5, 0},
|
||||||
{"abcdefgh", "defgh", AlignRight, 5, 0},
|
{"abcdefgh", "defgh", мКнст.AlignRight, 5, 0},
|
||||||
{"abcdefgh", "bcdef", AlignCenter, 5, 0},
|
{"abcdefgh", "bcdef", мКнст.AlignCenter, 5, 0},
|
||||||
{"abcdefgh", "abcdefgh", AlignLeft, 10, 0},
|
{"abcdefgh", "abcdefgh", мКнст.AlignLeft, 10, 0},
|
||||||
{"abcdefgh", "abcdefgh", AlignRight, 10, 2},
|
{"abcdefgh", "abcdefgh", мКнст.AlignRight, 10, 2},
|
||||||
{"abcdefgh", "abcdefgh", AlignCenter, 10, 1},
|
{"abcdefgh", "abcdefgh", мКнст.AlignCenter, 10, 1},
|
||||||
{"abcdefg", "abcdefg", AlignCenter, 10, 2},
|
{"abcdefg", "abcdefg", мКнст.AlignCenter, 10, 2},
|
||||||
// colored cases
|
// colored cases
|
||||||
{"abc<t:green>defg", "abc<t:green>defg", AlignCenter, 10, 2},
|
{"abc<t:green>defg", "abc<t:green>defg", мКнст.AlignCenter, 10, 2},
|
||||||
{"abc<t:green>defgh", "abc<t:green>defgh", AlignRight, 10, 2},
|
{"abc<t:green>defgh", "abc<t:green>defgh", мКнст.AlignRight, 10, 2},
|
||||||
{"abc<t:green>defgh", "abc<t:green>defgh", AlignCenter, 10, 1},
|
{"abc<t:green>defgh", "abc<t:green>defgh", мКнст.AlignCenter, 10, 1},
|
||||||
{"<b:blue>ab<b:cyan>cdefgh", "<b:blue>ab<b:cyan>cde", AlignLeft, 5, 0},
|
{"<b:blue>ab<b:cyan>cdefgh", "<b:blue>ab<b:cyan>cde", мКнст.AlignLeft, 5, 0},
|
||||||
{"<b:blue>abcdefgh", "<b:cyan>defgh", AlignRight, 5, 0},
|
{"<b:blue>abcdefgh", "<b:cyan>defgh", мКнст.AlignRight, 5, 0},
|
||||||
{"<b:blue>abcdefgh", "<b:blue>b<b:cyan>cdef", AlignCenter, 5, 0},
|
{"<b:blue>abcdefgh", "<b:blue>b<b:cyan>cdef", мКнст.AlignCenter, 5, 0},
|
||||||
{"abc<t:green>defg", "ab", AlignLeft, 2, 0},
|
{"abc<t:green>defg", "ab", мКнст.AlignLeft, 2, 0},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user