283 Рефакторинг

This commit is contained in:
prospero78su 2019-05-07 10:58:24 +03:00
parent 605b0a69fc
commit 54e360d5f2
7 changed files with 104 additions and 103 deletions

View File

@ -191,8 +191,8 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ мК
dlg.View.SetModal(true)
dlg.View.SetPack(мКнст.Vertical)
if typ == SelectDialogList {
fList := CreateFrame(dlg.View, 1, 1, BorderNone, 1)
if typ == мКнст.SelectDialogList {
fList := CreateFrame(dlg.View, 1, 1, мКнст.BorderNone, 1)
fList.SetPaddings(1, 1)
fList.SetGaps(0, 0)
dlg.list = CreateListBox(fList, 15, 5, 1)
@ -202,14 +202,14 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ мК
if selectedItem >= 0 && selectedItem < len(items) {
dlg.list.SelectItem(selectedItem)
}
} else if typ == SelectDialogEdit {
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
} else if typ == мКнст.SelectDialogEdit {
CreateFrame(dlg.View, 1, 1, мКнст.BorderNone, мКнст.Fixed)
lb := CreateLabel(dlg.View, 10, 3, items[0], 1)
lb.SetMultiline(true)
fWidth, _ := dlg.View.Size()
dlg.edit = CreateEditField(dlg.View, fWidth-2, items[1], AutoSize)
CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
dlg.edit = CreateEditField(dlg.View, fWidth-2, items[1], мКнст.AutoSize)
CreateFrame(dlg.View, 1, 1, мКнст.BorderNone, мКнст.Fixed)
dlg.edit.OnKeyPress(func(key term.Key, r rune) bool {
var input string
@ -217,7 +217,7 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ мК
input = dlg.edit.Title()
dlg.edtResult = input
dlg.value = -1
dlg.result = DialogButton1
dlg.result = мКнст.DialogButton1
WindowManager().DestroyWindow(dlg.View)
if dlg.onClose != nil {
@ -228,13 +228,13 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ мК
return false
})
} else {
fRadio := CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
fRadio := CreateFrame(dlg.View, 1, 1, мКнст.BorderNone, мКнст.Fixed)
fRadio.SetPaddings(1, 1)
fRadio.SetGaps(0, 0)
fRadio.SetPack(Vertical)
fRadio.SetPack(мКнст.Vertical)
dlg.rg = CreateRadioGroup()
for _, item := range items {
r := CreateRadio(fRadio, AutoSize, item, Fixed)
r := CreateRadio(fRadio, мКнст.AutoSize, item, мКнст.Fixed)
dlg.rg.AddItem(r)
}
if selectedItem >= 0 && selectedItem < len(items) {
@ -242,14 +242,14 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ мК
}
}
frm1 := CreateFrame(dlg.View, 16, 4, BorderNone, Fixed)
CreateFrame(frm1, 1, 1, BorderNone, 1)
btn1 := CreateButton(frm1, AutoSize, AutoSize, "OK", Fixed)
btn1.OnClick(func(ev Event) {
dlg.result = DialogButton1
if dlg.typ == SelectDialogList {
frm1 := CreateFrame(dlg.View, 16, 4, мКнст.BorderNone, мКнст.Fixed)
CreateFrame(frm1, 1, 1, мКнст.BorderNone, 1)
btn1 := CreateButton(frm1, мКнст.AutoSize, мКнст.AutoSize, "OK", мКнст.Fixed)
btn1.OnClick(func(ev мКнст.Event) {
dlg.result = мКнст.DialogButton1
if dlg.typ == мКнст.SelectDialogList {
dlg.value = dlg.list.SelectedItem()
} else if dlg.typ == SelectDialogEdit {
} else if dlg.typ == мКнст.SelectDialogEdit {
dlg.edtResult = dlg.edit.Title()
dlg.value = -1
} else {
@ -261,10 +261,10 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ мК
}
})
CreateFrame(frm1, 1, 1, BorderNone, 1)
btn2 := CreateButton(frm1, AutoSize, AutoSize, "Cancel", Fixed)
btn2.OnClick(func(ev Event) {
dlg.result = DialogButton2
CreateFrame(frm1, 1, 1, мКнст.BorderNone, 1)
btn2 := CreateButton(frm1, мКнст.AutoSize, мКнст.AutoSize, "Cancel", мКнст.Fixed)
btn2.OnClick(func(ev мКнст.Event) {
dlg.result = мКнст.DialogButton2
dlg.edtResult = ""
dlg.value = -1
WindowManager().DestroyWindow(dlg.View)
@ -273,16 +273,16 @@ func CreateSelectDialog(title string, items []string, selectedItem int, typ мК
}
})
if typ == SelectDialogEdit {
if typ == мКнст.SelectDialogEdit {
ActivateControl(dlg.View, dlg.edit)
} else {
ActivateControl(dlg.View, btn2)
}
CreateFrame(frm1, 1, 1, BorderNone, 1)
CreateFrame(frm1, 1, 1, мКнст.BorderNone, 1)
dlg.View.OnClose(func(ev Event) bool {
if dlg.result == DialogAlive {
dlg.result = DialogClosed
dlg.View.OnClose(func(ev мКнст.Event) bool {
if dlg.result == мКнст.DialogAlive {
dlg.result = мКнст.DialogClosed
if ev.X != 1 {
WindowManager().DestroyWindow(dlg.View)
}

16
edit.go
View File

@ -32,7 +32,7 @@ func (e *EditField) setTitleInternal(title string) {
e.title = title
if e.onChange != nil {
ev := Event{Msg: title}
ev := мКнст.Event{Msg: title}
e.onChange(ev)
}
}
@ -54,7 +54,7 @@ func (e *EditField) Draw() {
x, y := e.Pos()
w, _ := e.Size()
parts := []rune(SysObject(ObjEdit))
parts := []rune(SysObject(мКнст.ObjEdit))
chLeft, chRight := string(parts[0]), string(parts[1])
chStar := "*"
if len(parts) > 3 {
@ -100,11 +100,11 @@ func (e *EditField) Draw() {
}
}
fg, bg := RealColor(e.fg, e.Style(), ColorEditText), RealColor(e.bg, e.Style(), ColorEditBack)
fg, bg := RealColor(e.fg, e.Style(), мКнст.ColorEditText), RealColor(e.bg, e.Style(), мКнст.ColorEditBack)
if !e.Enabled() {
fg, bg = RealColor(e.fg, e.Style(), ColorDisabledText), RealColor(e.fg, e.Style(), ColorDisabledBack)
fg, bg = RealColor(e.fg, e.Style(), мКнст.ColorDisabledText), RealColor(e.fg, e.Style(), мКнст.ColorDisabledBack)
} else if e.Active() {
fg, bg = RealColor(e.fg, e.Style(), ColorEditActiveText), RealColor(e.bg, e.Style(), ColorEditActiveBack)
fg, bg = RealColor(e.fg, e.Style(), мКнст.ColorEditActiveText), RealColor(e.bg, e.Style(), мКнст.ColorEditActiveBack)
}
SetTextColor(fg)
@ -253,14 +253,14 @@ func (e *EditField) MaxWidth() int {
// Method does nothing if new size is less than minimal size
// EditField height cannot be changed - it equals 1 always
func (e *EditField) SetSize(width, height int) {
if width != KeepValue && (width > 1000 || width < e.minW) {
if width != мКнст.KeepValue && (width > 1000 || width < e.minW) {
return
}
if height != KeepValue && (height > 200 || height < e.minH) {
if height != мКнст.KeepValue && (height > 200 || height < e.minH) {
return
}
if width != KeepValue {
if width != мКнст.KeepValue {
e.width = width
}

View File

@ -46,7 +46,7 @@ func CreateEditField(parent Control, width int, text string, scale int) *EditFie
e.SetTitle(text)
e.SetEnabled(true)
if width == AutoSize {
if width == мКнст.AutoSize {
width = xs.Len(text) + 1
}
@ -79,11 +79,11 @@ func (e *EditField) ProcessEvent(event мКнст.Event) bool {
return false
}
if event.Type == EventActivate && event.X == 0 {
if event.Type == мКнст.EventActivate && event.X == 0 {
term.HideCursor()
}
if event.Type == EventKey && event.Key != term.KeyTab {
if event.Type == мКнст.EventKey && event.Key != term.KeyTab {
if e.onKeyPress != nil {
res := e.onKeyPress(event.Key, event.Ch)
if res {

View File

@ -8,6 +8,7 @@ import (
"strings"
term "github.com/nsf/termbox-go"
мКнст "./пакКонстанты"
)
// FileSelectDialog is a dialog to select a file or directory.
@ -215,34 +216,34 @@ func CreateFileSelectDialog(title, fileMasks, initPath string, selectDir, mustEx
defer WindowManager().EndUpdate()
dlg.View.SetModal(true)
dlg.View.SetPack(Vertical)
dlg.View.SetPack(мКнст.Vertical)
dlg.currPath = initPath
dlg.detectPath()
dlg.curDir = CreateLabel(dlg.View, AutoSize, AutoSize, "", Fixed)
dlg.curDir.SetTextDisplay(AlignRight)
dlg.curDir = CreateLabel(dlg.View, мКнст.AutoSize, мКнст.AutoSize, "", мКнст.Fixed)
dlg.curDir.SetTextDisplay(мКнст.AlignRight)
flist := CreateFrame(dlg.View, 1, 1, BorderNone, 1)
flist := CreateFrame(dlg.View, 1, 1, мКнст.BorderNone, 1)
flist.SetPaddings(1, 1)
flist.SetPack(Horizontal)
flist.SetPack(мКнст.Horizontal)
dlg.listBox = CreateListBox(flist, 16, ch-20, 1)
fselected := CreateFrame(dlg.View, 1, 1, BorderNone, Fixed)
fselected := CreateFrame(dlg.View, 1, 1, мКнст.BorderNone, мКнст.Fixed)
// text + edit field to enter name manually
fselected.SetPack(Vertical)
fselected.SetPack(мКнст.Vertical)
fselected.SetPaddings(1, 0)
CreateLabel(fselected, AutoSize, AutoSize, "Selected object:", 1)
CreateLabel(fselected, мКнст.AutoSize, мКнст.AutoSize, "Selected object:", 1)
dlg.edFile = CreateEditField(fselected, cw-22, "", 1)
// buttons at the right
blist := CreateFrame(flist, 1, 1, BorderNone, Fixed)
blist.SetPack(Vertical)
blist := CreateFrame(flist, 1, 1, мКнст.BorderNone, мКнст.Fixed)
blist.SetPack(мКнст.Vertical)
blist.SetPaddings(1, 1)
btnOpen := CreateButton(blist, AutoSize, AutoSize, "Open", Fixed)
btnSelect := CreateButton(blist, AutoSize, AutoSize, "Select", Fixed)
btnCancel := CreateButton(blist, AutoSize, AutoSize, "Cancel", Fixed)
btnOpen := CreateButton(blist, мКнст.AutoSize, мКнст.AutoSize, "Open", мКнст.Fixed)
btnSelect := CreateButton(blist, мКнст.AutoSize, мКнст.AutoSize, "Select", мКнст.Fixed)
btnCancel := CreateButton(blist, мКнст.AutoSize, мКнст.AutoSize, "Cancel", мКнст.Fixed)
btnCancel.OnClick(func(ev Event) {
btnCancel.OnClick(func(ev мКнст.Event) {
WindowManager().DestroyWindow(dlg.View)
WindowManager().BeginUpdate()
dlg.Selected = false
@ -253,7 +254,7 @@ func CreateFileSelectDialog(title, fileMasks, initPath string, selectDir, mustEx
}
})
btnSelect.OnClick(func(ev Event) {
btnSelect.OnClick(func(ev мКнст.Event) {
WindowManager().DestroyWindow(dlg.View)
WindowManager().BeginUpdate()
dlg.Selected = true
@ -272,9 +273,9 @@ func CreateFileSelectDialog(title, fileMasks, initPath string, selectDir, mustEx
}
})
dlg.View.OnClose(func(ev Event) bool {
if dlg.result == DialogAlive {
dlg.result = DialogClosed
dlg.View.OnClose(func(ev мКнст.Event) bool {
if dlg.result == мКнст.DialogAlive {
dlg.result = мКнст.DialogClosed
if ev.X != 1 {
WindowManager().DestroyWindow(dlg.View)
}
@ -285,7 +286,7 @@ func CreateFileSelectDialog(title, fileMasks, initPath string, selectDir, mustEx
return true
})
dlg.listBox.OnSelectItem(func(ev Event) {
dlg.listBox.OnSelectItem(func(ev мКнст.Event) {
item := ev.Msg
if item == ".." {
btnSelect.SetEnabled(false)
@ -312,7 +313,7 @@ func CreateFileSelectDialog(title, fileMasks, initPath string, selectDir, mustEx
}
})
btnOpen.OnClick(func(ev Event) {
btnOpen.OnClick(func(ev мКнст.Event) {
s := dlg.listBox.SelectedItemText()
if s != ".." && (s == "" || !strings.HasSuffix(s, string(os.PathSeparator))) {
return
@ -325,7 +326,7 @@ func CreateFileSelectDialog(title, fileMasks, initPath string, selectDir, mustEx
}
})
dlg.edFile.OnChange(func(ev Event) {
dlg.edFile.OnChange(func(ev мКнст.Event) {
s := ""
lowCurrText := strings.ToLower(dlg.listBox.SelectedItemText())
lowEditText := strings.ToLower(dlg.edFile.Title())

View File

@ -22,7 +22,7 @@ type Frame struct {
}
/*
NewFrame creates a new frame.
CreateFrame creates a new frame.
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.
width and heigth - are minimal size of the control.
@ -34,15 +34,15 @@ func CreateFrame(parent Control, width, height int, bs мКнст.BorderStyle, s
f := new(Frame)
f.BaseControl = NewBaseControl()
if width == AutoSize {
if width == мКнст.AutoSize {
width = 5
}
if height == AutoSize {
if height == мКнст.AutoSize {
height = 3
}
if bs == BorderAuto {
bs = BorderNone
if bs == мКнст.BorderAuto {
bs = мКнст.BorderNone
}
f.SetSize(width, height)
@ -53,7 +53,7 @@ func CreateFrame(parent Control, width, height int, bs мКнст.BorderStyle, s
f.scale = scale
f.gapX, f.gapY = 0, 0
if bs == BorderNone {
if bs == мКнст.BorderNone {
f.padX, f.padY = 0, 0
} else {
f.padX, f.padY = 1, 1
@ -65,19 +65,19 @@ func CreateFrame(parent Control, width, height int, bs мКнст.BorderStyle, s
return f
}
//SetScrollable --
func (f *Frame) SetScrollable(scrollable bool) {
f.scrollable = scrollable
if scrollable {
px, py := f.Paddings()
if f.Pack() == Vertical {
px += 1
if f.Pack() == мКнст.Vertical {
px++
}
if f.Pack() == Horizontal {
py += 1
if f.Pack() == мКнст.Horizontal {
py++
}
f.SetPaddings(px, py)
@ -85,12 +85,12 @@ func (f *Frame) SetScrollable(scrollable bool) {
f.SetClipped(scrollable)
}
//Scrollable --
func (f *Frame) Scrollable() bool {
return f.scrollable
}
// Repaint draws the control on its View surface
//Draw Repaint draws the control on its View surface
func (f *Frame) Draw() {
if f.hidden {
return
@ -132,10 +132,10 @@ func (f *Frame) Draw() {
DrawScrollBar(x+w, y, 1, h, f.lastScrollProp)
}
fg, bg := RealColor(f.fg, f.Style(), ColorViewText), RealColor(f.bg, f.Style(), ColorViewBack)
fg, bg := RealColor(f.fg, f.Style(), мКнст.ColorViewText), RealColor(f.bg, f.Style(), мКнст.ColorViewBack)
if f.border == BorderNone {
if bg != ColorDefault {
if f.border == мКнст.BorderNone {
if bg != мКнст.ColorDefault {
SetBackColor(bg)
FillRect(x, y, w, h, ' ')
}
@ -173,9 +173,9 @@ func (f *Frame) ScrollTo(x int, y int) {
f.ResizeChildren()
f.PlaceChildren()
}
//ProcessEvent --
func (f *Frame) ProcessEvent(ev мКнст.Event) bool {
if ev.Type != EventActivateChild || (!f.scrollable || ev.Target == nil) {
if ev.Type != мКнст.EventActivateChild || (!f.scrollable || ev.Target == nil) {
return false
}

View File

@ -33,10 +33,10 @@ func CreateLabel(parent Control, w, h int, title string, scale int) *Label {
c := new(Label)
c.BaseControl = NewBaseControl()
if w == AutoSize {
if w == мКнст.AutoSize {
w = xs.Len(title)
}
if h == AutoSize {
if h == мКнст.AutoSize {
h = 1
}
@ -47,7 +47,7 @@ func CreateLabel(parent Control, w, h int, title string, scale int) *Label {
c.SetConstraints(w, h)
c.SetScale(scale)
c.tabSkip = true
c.textDisplay = AlignLeft
c.textDisplay = мКнст.AlignLeft
if parent != nil {
parent.AddChild(c)
@ -74,9 +74,9 @@ func (l *Label) Draw() {
PushAttributes()
defer PopAttributes()
fg, bg := RealColor(l.fg, l.Style(), ColorText), RealColor(l.bg, l.Style(), ColorBack)
fg, bg := RealColor(l.fg, l.Style(), мКнст.ColorText), RealColor(l.bg, l.Style(), мКнст.ColorBack)
if !l.Enabled() {
fg = RealColor(l.fg, l.Style(), ColorDisabledText)
fg = RealColor(l.fg, l.Style(), мКнст.ColorDisabledText)
}
SetTextColor(fg)
@ -104,17 +104,17 @@ func (l *Label) Draw() {
SetBackColor(elem.Bg)
putCharUnsafe(xx, yy, elem.Ch)
if l.direction == Horizontal {
xx += 1
if l.direction == мКнст.Horizontal {
xx++
if xx >= l.x+l.width {
xx = l.x
yy += 1
yy++
}
} else {
yy += 1
yy++
if yy >= l.y+l.height {
yy = l.y
xx += 1
xx++
}
}
}
@ -122,7 +122,7 @@ func (l *Label) Draw() {
elem = parser.NextElement()
}
} else {
if l.direction == Horizontal {
if l.direction == мКнст.Horizontal {
shift, str := AlignColorizedText(l.title, l.width, l.align)
if str != l.title && l.align != l.textDisplay {
shift, str = AlignColorizedText(l.title, l.width, l.textDisplay)
@ -165,7 +165,7 @@ func (l *Label) TextDisplay() мКнст.Align {
// for the property. Any other value does is skipped and does not affect
// displaying the title
func (l *Label) SetTextDisplay(align мКнст.Align) {
if align != AlignLeft && align != AlignRight {
if align != мКнст.AlignLeft && align != мКнст.AlignRight {
return
}

View File

@ -41,10 +41,10 @@ func CreateListBox(parent Control, width, height int, scale int) *ListBox {
l := new(ListBox)
l.BaseControl = NewBaseControl()
if height == AutoSize {
if height == мКнст.AutoSize {
height = 3
}
if width == AutoSize {
if width == мКнст.AutoSize {
width = 5
}
@ -88,11 +88,11 @@ func (l *ListBox) drawItems() {
maxDy := l.height - 1
maxWidth := l.width - 1
fg, bg := RealColor(l.fg, l.Style(), ColorEditText), RealColor(l.bg, l.Style(), ColorEditBack)
fg, bg := RealColor(l.fg, l.Style(), мКнст.ColorEditText), RealColor(l.bg, l.Style(), мКнст.ColorEditBack)
if l.Active() {
fg, bg = RealColor(l.fg, l.Style(), ColorEditActiveText), RealColor(l.bg, l.Style(), ColorEditActiveBack)
fg, bg = RealColor(l.fg, l.Style(), мКнст.ColorEditActiveText), RealColor(l.bg, l.Style(), мКнст.ColorEditActiveBack)
}
fgSel, bgSel := RealColor(l.fgActive, l.Style(), ColorSelectionText), RealColor(l.bgActive, l.Style(), ColorSelectionBack)
fgSel, bgSel := RealColor(l.fgActive, l.Style(), мКнст.ColorSelectionText), RealColor(l.bgActive, l.Style(), мКнст.ColorSelectionBack)
for curr <= maxCurr && dy <= maxDy {
f, b := fg, bg
@ -123,9 +123,9 @@ func (l *ListBox) Draw() {
x, y := l.Pos()
w, h := l.Size()
fg, bg := RealColor(l.fg, l.Style(), ColorEditText), RealColor(l.bg, l.Style(), ColorEditBack)
fg, bg := RealColor(l.fg, l.Style(), мКнст.ColorEditText), RealColor(l.bg, l.Style(), мКнст.ColorEditBack)
if l.Active() {
fg, bg = RealColor(l.fg, l.Style(), ColorEditActiveText), RealColor(l.bg, l.Style(), ColorEditActiveBack)
fg, bg = RealColor(l.fg, l.Style(), мКнст.ColorEditActiveText), RealColor(l.bg, l.Style(), мКнст.ColorEditActiveBack)
}
SetTextColor(fg)
SetBackColor(bg)
@ -145,7 +145,7 @@ func (l *ListBox) home() {
l.topLine = 0
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
ev := мКнст.Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
@ -163,7 +163,7 @@ func (l *ListBox) end() {
}
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
ev := мКнст.Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
@ -189,7 +189,7 @@ func (l *ListBox) moveUp(dy int) {
l.EnsureVisible()
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
ev :=мКнст.Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
@ -210,7 +210,7 @@ func (l *ListBox) moveDown(dy int) {
l.EnsureVisible()
if l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
ev := мКнст.Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
}
@ -287,7 +287,7 @@ func (l *ListBox) processMouseClick(ev мКнст.Event) bool {
onSelFunc := l.onSelectItem
WindowManager().EndUpdate()
if onSelFunc != nil {
ev := Event{Y: l.topLine + dy, Msg: l.SelectedItemText()}
ev := мКнст.Event{Y: l.topLine + dy, Msg: l.SelectedItemText()}
onSelFunc(ev)
}
@ -316,7 +316,7 @@ func (l *ListBox) ProcessEvent(event мКнст.Event) bool {
}
switch event.Type {
case EventKey:
case мКнст.EventKey:
if l.onKeyPress != nil {
res := l.onKeyPress(event.Key)
if res {
@ -345,13 +345,13 @@ func (l *ListBox) ProcessEvent(event мКнст.Event) bool {
return true
case term.KeyCtrlM:
if l.currSelection != -1 && l.onSelectItem != nil {
ev := Event{Y: l.currSelection, Msg: l.SelectedItemText()}
ev := мКнст.Event{Y: l.currSelection, Msg: l.SelectedItemText()}
l.onSelectItem(ev)
}
default:
return false
}
case EventMouse:
case мКнст.EventMouse:
return l.processMouseClick(event)
}