mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-26 13:49:01 +08:00
281 Рефакторинг
This commit is contained in:
parent
2ef0bcd2be
commit
fa72f88ed7
@ -69,8 +69,7 @@ type BarChart struct {
|
||||
onDrawCell func(*BarDataCell)
|
||||
}
|
||||
|
||||
/*
|
||||
NewBarChart creates a new bar chart.
|
||||
/*CreateBarChart creates a new bar chart.
|
||||
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.
|
||||
w and h - are minimal size of the control.
|
||||
@ -105,7 +104,7 @@ func CreateBarChart(parent Control, w, h int, scale int) *BarChart {
|
||||
return c
|
||||
}
|
||||
|
||||
// Repaint draws the control on its View surface
|
||||
//Draw Repaint draws the control on its View surface
|
||||
func (b *BarChart) Draw() {
|
||||
if b.hidden {
|
||||
return
|
||||
|
@ -273,7 +273,7 @@ func NextControl(parent Control, curr Control, next bool) Control {
|
||||
// makes it active, and then sends the event to it.
|
||||
// If it is not mouse click event then it looks for the first active child and
|
||||
// sends the event to it if it is not nil
|
||||
func SendEventToChild(parent Control, ev Event) bool {
|
||||
func SendEventToChild(parent Control, ev мКнст.Event) bool {
|
||||
var child Control
|
||||
if IsMouseClickEvent(ev) {
|
||||
child = ChildAt(parent, ev.X, ev.Y)
|
||||
|
@ -2,6 +2,7 @@ package clui
|
||||
|
||||
import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
// ConfirmationDialog is a simple dialog to get a user
|
||||
@ -29,7 +30,7 @@ type SelectDialog struct {
|
||||
rg *RadioGroup
|
||||
list *ListBox
|
||||
edit *EditField
|
||||
typ SelectDialogType
|
||||
typ мКнст.SelectDialogType
|
||||
onClose func()
|
||||
}
|
||||
|
||||
@ -160,11 +161,12 @@ func (d *ConfirmationDialog) Result() int {
|
||||
|
||||
// ------------------------ Selection Dialog ---------------------
|
||||
|
||||
//CreateEditDialog --
|
||||
func CreateEditDialog(title, message, initialText string) *SelectDialog {
|
||||
return CreateSelectDialog(title, []string{message, initialText}, 0, SelectDialogEdit)
|
||||
}
|
||||
|
||||
// NewSelectDialog creates new dialog to select an item from list.
|
||||
// CreateSelectDialog creates new dialog to select an item from list.
|
||||
// c is a composer that manages the dialog
|
||||
// title is a dialog title
|
||||
// items is a list of items to select from
|
||||
@ -172,7 +174,7 @@ func CreateEditDialog(title, message, initialText string) *SelectDialog {
|
||||
// the dialog is created
|
||||
// typ is a selection type: ListBox or RadioGroup
|
||||
// Returns nil in case of creation process fails, e.g, if item list is empty
|
||||
func CreateSelectDialog(title string, items []string, selectedItem int, typ SelectDialogType) *SelectDialog {
|
||||
func CreateSelectDialog(title string, items []string, selectedItem int, typ мКнст.SelectDialogType) *SelectDialog {
|
||||
dlg := new(SelectDialog)
|
||||
|
||||
if len(items) == 0 {
|
||||
|
3
edit.go
3
edit.go
@ -4,10 +4,11 @@ import (
|
||||
xs "github.com/huandu/xstrings"
|
||||
term "github.com/nsf/termbox-go"
|
||||
"strings"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
// OnChange sets the callback that is called when EditField content is changed
|
||||
func (e *EditField) OnChange(fn func(Event)) {
|
||||
func (e *EditField) OnChange(fn func(мКнст.Event)) {
|
||||
e.onChange = fn
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"github.com/atotto/clipboard"
|
||||
xs "github.com/huandu/xstrings"
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -27,7 +28,7 @@ type EditField struct {
|
||||
maxWidth int
|
||||
showStars bool
|
||||
|
||||
onChange func(Event)
|
||||
onChange func(мКнст.Event)
|
||||
onKeyPress func(term.Key, rune) bool
|
||||
}
|
||||
|
||||
@ -73,7 +74,7 @@ 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
|
||||
*/
|
||||
func (e *EditField) ProcessEvent(event Event) bool {
|
||||
func (e *EditField) ProcessEvent(event мКнст.Event) bool {
|
||||
if !e.Active() || !e.Enabled() {
|
||||
return false
|
||||
}
|
||||
|
9
frame.go
9
frame.go
@ -3,6 +3,7 @@ package clui
|
||||
import (
|
||||
xs "github.com/huandu/xstrings"
|
||||
"math"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -13,9 +14,9 @@ is required
|
||||
*/
|
||||
type Frame struct {
|
||||
BaseControl
|
||||
border BorderStyle
|
||||
border мКнст.BorderStyle
|
||||
children []Control
|
||||
pack PackType
|
||||
pack мКнст.PackType
|
||||
scrollable bool
|
||||
lastScrollProp int
|
||||
}
|
||||
@ -29,7 +30,7 @@ bs - type of border: no border, single or double.
|
||||
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
|
||||
control should keep its original size.
|
||||
*/
|
||||
func CreateFrame(parent Control, width, height int, bs BorderStyle, scale int) *Frame {
|
||||
func CreateFrame(parent Control, width, height int, bs мКнст.BorderStyle, scale int) *Frame {
|
||||
f := new(Frame)
|
||||
f.BaseControl = NewBaseControl()
|
||||
|
||||
@ -173,7 +174,7 @@ func (f *Frame) ScrollTo(x int, y int) {
|
||||
f.PlaceChildren()
|
||||
}
|
||||
|
||||
func (f *Frame) ProcessEvent(ev Event) bool {
|
||||
func (f *Frame) ProcessEvent(ev мКнст.Event) bool {
|
||||
if ev.Type != EventActivateChild || (!f.scrollable || ev.Target == nil) {
|
||||
return false
|
||||
}
|
||||
|
13
label.go
13
label.go
@ -2,6 +2,7 @@ package clui
|
||||
|
||||
import (
|
||||
xs "github.com/huandu/xstrings"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -14,9 +15,9 @@ is always left aligned
|
||||
*/
|
||||
type Label struct {
|
||||
BaseControl
|
||||
direction Direction
|
||||
direction мКнст.Direction
|
||||
multiline bool
|
||||
textDisplay Align
|
||||
textDisplay мКнст.Align
|
||||
}
|
||||
|
||||
/*
|
||||
@ -56,12 +57,12 @@ func CreateLabel(parent Control, w, h int, title string, scale int) *Label {
|
||||
}
|
||||
|
||||
// Direction returns direction of text output: vertical or horizontal
|
||||
func (l *Label) Direction() Direction {
|
||||
func (l *Label) Direction() мКнст.Direction {
|
||||
return l.direction
|
||||
}
|
||||
|
||||
// SetDirection sets the text output direction
|
||||
func (l *Label) SetDirection(dir Direction) {
|
||||
func (l *Label) SetDirection(dir мКнст.Direction) {
|
||||
l.direction = dir
|
||||
}
|
||||
|
||||
@ -155,7 +156,7 @@ func (l *Label) SetMultiline(multi bool) {
|
||||
// - AlignLeft - the head of the title is shown
|
||||
// - AlignRight - the tail of the title is shown
|
||||
// The property is used only by single line Label
|
||||
func (l *Label) TextDisplay() Align {
|
||||
func (l *Label) TextDisplay() мКнст.Align {
|
||||
return l.textDisplay
|
||||
}
|
||||
|
||||
@ -163,7 +164,7 @@ func (l *Label) TextDisplay() Align {
|
||||
// is longer than the lable. Only AlignLeft and AlignRigth are valid values
|
||||
// for the property. Any other value does is skipped and does not affect
|
||||
// displaying the title
|
||||
func (l *Label) SetTextDisplay(align Align) {
|
||||
func (l *Label) SetTextDisplay(align мКнст.Align) {
|
||||
if align != AlignLeft && align != AlignRight {
|
||||
return
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package clui
|
||||
import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
"strings"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -24,7 +25,7 @@ type ListBox struct {
|
||||
topLine int
|
||||
buttonPos int
|
||||
|
||||
onSelectItem func(Event)
|
||||
onSelectItem func(мКнст.Event)
|
||||
onKeyPress func(term.Key) bool
|
||||
}
|
||||
|
||||
@ -246,7 +247,7 @@ func (l *ListBox) Clear() {
|
||||
l.topLine = 0
|
||||
}
|
||||
|
||||
func (l *ListBox) processMouseClick(ev Event) bool {
|
||||
func (l *ListBox) processMouseClick(ev мКнст.Event) bool {
|
||||
if ev.Key != term.MouseLeft {
|
||||
return false
|
||||
}
|
||||
@ -309,7 +310,7 @@ 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
|
||||
*/
|
||||
func (l *ListBox) ProcessEvent(event Event) bool {
|
||||
func (l *ListBox) ProcessEvent(event мКнст.Event) bool {
|
||||
if !l.Active() || !l.Enabled() {
|
||||
return false
|
||||
}
|
||||
@ -455,7 +456,7 @@ func (l *ListBox) RemoveItem(id int) bool {
|
||||
|
||||
// OnSelectItem sets a callback that is called every time
|
||||
// the selected item is changed
|
||||
func (l *ListBox) OnSelectItem(fn func(Event)) {
|
||||
func (l *ListBox) OnSelectItem(fn func(мКнст.Event)) {
|
||||
l.onSelectItem = fn
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package clui
|
||||
|
||||
import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
// Composer is a service object that manages Views and console, processes
|
||||
@ -9,7 +10,7 @@ import (
|
||||
// one object of this type
|
||||
type mainLoop struct {
|
||||
// a channel to communicate with View(e.g, Views send redraw event to this channel)
|
||||
channel chan Event
|
||||
channel chan мКнст.Event
|
||||
}
|
||||
|
||||
var (
|
||||
@ -52,12 +53,12 @@ func MainLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
func _putEvent(ev Event) {
|
||||
func _putEvent(ev мКнст.Event) {
|
||||
loop.channel <- ev
|
||||
}
|
||||
|
||||
// PutEvent send event to a Composer directly.
|
||||
// Used by Views to ask for repainting or for quitting the application
|
||||
func PutEvent(ev Event) {
|
||||
func PutEvent(ev мКнст.Event) {
|
||||
go _putEvent(ev)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
"strconv"
|
||||
"strings"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -16,7 +17,7 @@ empty one. By default colors are the same.
|
||||
*/
|
||||
type ProgressBar struct {
|
||||
BaseControl
|
||||
direction Direction
|
||||
direction мКнст.Direction
|
||||
min, max int
|
||||
value int
|
||||
emptyFg, emptyBg term.Attribute
|
||||
|
3
radio.go
3
radio.go
@ -3,6 +3,7 @@ package clui
|
||||
import (
|
||||
xs "github.com/huandu/xstrings"
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -103,7 +104,7 @@ that the control do not want or cannot process the event and the caller sends
|
||||
the event to the control parent.
|
||||
The control processes only space button and mouse clicks to make control selected. Deselecting control is not possible: one has to click another radio of the radio group to deselect this button
|
||||
*/
|
||||
func (c *Radio) ProcessEvent(event Event) bool {
|
||||
func (c *Radio) ProcessEvent(event мКнст.Event) bool {
|
||||
if (!c.Active() && event.Type == EventKey) || !c.Enabled() {
|
||||
return false
|
||||
}
|
||||
|
15
tableview.go
15
tableview.go
@ -3,6 +3,7 @@ package clui
|
||||
import (
|
||||
"fmt"
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -77,9 +78,9 @@ type TableView struct {
|
||||
type Column struct {
|
||||
Title string
|
||||
Width int
|
||||
Alignment Align
|
||||
Alignment мКнст.Align
|
||||
Fg, Bg term.Attribute
|
||||
Sort SortOrder
|
||||
Sort мКнст.SortOrder
|
||||
}
|
||||
|
||||
// ColumnDrawInfo is a structure used in OnDrawCell event.
|
||||
@ -99,7 +100,7 @@ type ColumnDrawInfo struct {
|
||||
// cell displayed text
|
||||
Text string
|
||||
// text alignment
|
||||
Alignment Align
|
||||
Alignment мКнст.Align
|
||||
// is the row that contains the cell selected(active)
|
||||
RowSelected bool
|
||||
// is the column that contains the cell selected(active)
|
||||
@ -114,13 +115,13 @@ type ColumnDrawInfo struct {
|
||||
// TableView ask for while a user is interacting with the table
|
||||
type TableEvent struct {
|
||||
// requested action: Add, Edit, Delete, Sort data
|
||||
Action TableAction
|
||||
Action мКнст.TableAction
|
||||
// Currently selected column
|
||||
Col int
|
||||
// Currently selected row (it is not used for TableActionSort)
|
||||
Row int
|
||||
// Sort order (it is used only in TableActionSort event)
|
||||
Sort SortOrder
|
||||
Sort мКнст.SortOrder
|
||||
}
|
||||
|
||||
/*
|
||||
@ -670,7 +671,7 @@ func (l *TableView) verticalScrollClick(dy int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *TableView) processMouseClick(ev Event) bool {
|
||||
func (l *TableView) processMouseClick(ev мКнст.Event) bool {
|
||||
if ev.Key != term.MouseLeft {
|
||||
return false
|
||||
}
|
||||
@ -757,7 +758,7 @@ 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
|
||||
*/
|
||||
func (l *TableView) ProcessEvent(event Event) bool {
|
||||
func (l *TableView) ProcessEvent(event мКнст.Event) bool {
|
||||
if !l.Active() || !l.Enabled() {
|
||||
return false
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package clui
|
||||
import (
|
||||
xs "github.com/huandu/xstrings"
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
type TextDisplay struct {
|
||||
@ -156,7 +157,7 @@ func (l *TextDisplay) moveDown(count int) {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *TextDisplay) processMouseClick(ev Event) bool {
|
||||
func (l *TextDisplay) processMouseClick(ev мКнст.Event) bool {
|
||||
if ev.Key != term.MouseLeft {
|
||||
return false
|
||||
}
|
||||
@ -179,7 +180,7 @@ 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
|
||||
*/
|
||||
func (l *TextDisplay) ProcessEvent(event Event) bool {
|
||||
func (l *TextDisplay) ProcessEvent(event мКнст.Event) bool {
|
||||
if !l.Active() || !l.Enabled() {
|
||||
return false
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
"regexp"
|
||||
"strings"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -65,7 +66,7 @@ func CutText(str string, maxWidth int) string {
|
||||
// to draw a label aligned but with transparent beginning
|
||||
// and ending. If you do not need transparency you can
|
||||
// add spaces manually using the returned shift value
|
||||
func AlignText(str string, width int, align Align) (shift int, out string) {
|
||||
func AlignText(str string, width int, align мКнст.Align) (shift int, out string) {
|
||||
length := xs.Len(str)
|
||||
|
||||
if length >= width {
|
||||
|
Loading…
x
Reference in New Issue
Block a user