mirror of
https://github.com/VladimirMarkelov/clui.git
synced 2025-04-26 13:49:01 +08:00
280 Рефакторинг
This commit is contained in:
parent
c9a1930170
commit
2ef0bcd2be
@ -607,7 +607,7 @@ func (c *BaseControl) setClipper() {
|
||||
c.clipper = &rect{x: x, y: y, w: w, h: h}
|
||||
}
|
||||
|
||||
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 &&
|
||||
y > c.y && y < c.y+c.height-1 {
|
||||
return HitInside
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -17,7 +18,7 @@ type Button struct {
|
||||
shadowColor term.Attribute
|
||||
bgActive term.Attribute
|
||||
pressed int32
|
||||
onClick func(Event)
|
||||
onClick func(мКнст.Event)
|
||||
}
|
||||
|
||||
/*
|
||||
@ -116,12 +117,12 @@ 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 (b *Button) ProcessEvent(event Event) bool {
|
||||
func (b *Button) ProcessEvent(event мКнст.Event) bool {
|
||||
if !b.Enabled() {
|
||||
return false
|
||||
}
|
||||
|
||||
if event.Type == EventKey {
|
||||
if event.Type == мКнст.EventKey {
|
||||
if event.Key == term.KeySpace && b.isPressed() == 0 {
|
||||
b.setPressed(1)
|
||||
ev := Event{Type: EventRedraw}
|
||||
@ -164,6 +165,6 @@ func (b *Button) ProcessEvent(event Event) bool {
|
||||
|
||||
// OnClick sets the callback that is called when one clicks button
|
||||
// with mouse or pressing space on keyboard while the button is active
|
||||
func (b *Button) OnClick(fn func(Event)) {
|
||||
func (b *Button) OnClick(fn func(мКнст.Event)) {
|
||||
b.onClick = fn
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
"strings"
|
||||
"unicode"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
type attr struct {
|
||||
@ -377,7 +378,7 @@ func DrawRawTextVertical(x, y int, text string) {
|
||||
}
|
||||
|
||||
// DrawFrame paints the frame without changing area inside it
|
||||
func DrawFrame(x, y, w, h int, border BorderStyle) {
|
||||
func DrawFrame(x, y, w, h int, border мКнст.BorderStyle) {
|
||||
var chars string
|
||||
if border == BorderThick {
|
||||
chars = SysObject(ObjDoubleBorder)
|
||||
|
@ -3,6 +3,7 @@ package clui
|
||||
import (
|
||||
xs "github.com/huandu/xstrings"
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -101,7 +102,7 @@ func (c *CheckBox) Draw() {
|
||||
// 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 (c *CheckBox) ProcessEvent(event Event) bool {
|
||||
func (c *CheckBox) ProcessEvent(event мКнст.Event) bool {
|
||||
if (!c.Active() && event.Type == EventKey) || !c.Enabled() {
|
||||
return false
|
||||
}
|
||||
|
27
composer.go
27
composer.go
@ -3,6 +3,7 @@ package clui
|
||||
import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
"sync"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
// Composer is a service object that manages Views and console, processes
|
||||
@ -11,7 +12,7 @@ import (
|
||||
type Composer struct {
|
||||
// list of visible Views
|
||||
windows []Control
|
||||
windowBorder BorderStyle
|
||||
windowBorder мКнст.BorderStyle
|
||||
consumer Control
|
||||
// last pressed key - to make repeatable actions simpler, e.g, at first
|
||||
// one presses Ctrl+S and then just repeatedly presses arrow lest to
|
||||
@ -23,7 +24,7 @@ type Composer struct {
|
||||
// last processed coordinates: e.g, for mouse move
|
||||
lastX, lastY int
|
||||
// Type of dragging
|
||||
dragType DragType
|
||||
dragType мКнст.DragType
|
||||
// For safe Window manipulations
|
||||
mtx sync.RWMutex
|
||||
}
|
||||
@ -62,7 +63,7 @@ func ReleaseEvents() {
|
||||
comp.consumer = nil
|
||||
}
|
||||
|
||||
func termboxEventToLocal(ev term.Event) Event {
|
||||
func termboxEventToLocal(ev term.Event) мКнст.Event {
|
||||
e := Event{Type: EventType(ev.Type), Ch: ev.Ch,
|
||||
Key: ev.Key, Err: ev.Err, X: ev.MouseX, Y: ev.MouseY,
|
||||
Mod: ev.Mod, Width: ev.Width, Height: ev.Height}
|
||||
@ -118,12 +119,12 @@ func AddWindow(posX, posY, width, height int, title string) *Window {
|
||||
}
|
||||
|
||||
// Border returns the default window border
|
||||
func (c *Composer) BorderStyle() BorderStyle {
|
||||
func (c *Composer) BorderStyle() мКнст.BorderStyle {
|
||||
return c.windowBorder
|
||||
}
|
||||
|
||||
// SetBorder changes the default window border
|
||||
func (c *Composer) SetBorder(border BorderStyle) {
|
||||
func (c *Composer) SetBorder(border мКнст.BorderStyle) {
|
||||
c.windowBorder = border
|
||||
}
|
||||
|
||||
@ -154,7 +155,7 @@ func (c *Composer) getWindowList() []Control {
|
||||
return arr_copy
|
||||
}
|
||||
|
||||
func (c *Composer) checkWindowUnderMouse(screenX, screenY int) (Control, HitResult) {
|
||||
func (c *Composer) checkWindowUnderMouse(screenX, screenY int) (Control, мКнст.HitResult) {
|
||||
windows := c.getWindowList()
|
||||
if len(windows) == 0 {
|
||||
return nil, HitOutside
|
||||
@ -255,7 +256,7 @@ func (c *Composer) moveActiveWindowToBottom() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Composer) sendEventToActiveWindow(ev Event) bool {
|
||||
func (c *Composer) sendEventToActiveWindow(ev мКнст.Event) bool {
|
||||
view := c.topWindow()
|
||||
if view != nil {
|
||||
return view.ProcessEvent(ev)
|
||||
@ -274,7 +275,7 @@ func (c *Composer) topWindow() Control {
|
||||
return windows[len(windows)-1]
|
||||
}
|
||||
|
||||
func (c *Composer) resizeTopWindow(ev Event) bool {
|
||||
func (c *Composer) resizeTopWindow(ev мКнст.Event) bool {
|
||||
view := c.topWindow()
|
||||
if view == nil {
|
||||
return false
|
||||
@ -308,7 +309,7 @@ func (c *Composer) resizeTopWindow(ev Event) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Composer) moveTopWindow(ev Event) bool {
|
||||
func (c *Composer) moveTopWindow(ev мКнст.Event) bool {
|
||||
view := c.topWindow()
|
||||
if view != nil {
|
||||
topwindow, ok := view.(*Window)
|
||||
@ -361,7 +362,7 @@ func (c *Composer) closeTopWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Composer) processWindowDrag(ev Event) {
|
||||
func (c *Composer) processWindowDrag(ev мКнст.Event) {
|
||||
if ev.Mod != term.ModMotion || c.dragType == DragNone {
|
||||
return
|
||||
}
|
||||
@ -490,7 +491,7 @@ func (c *Composer) processWindowDrag(ev Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Composer) processMouse(ev Event) {
|
||||
func (c *Composer) processMouse(ev мКнст.Event) {
|
||||
if c.consumer != nil {
|
||||
tmp := c.consumer
|
||||
tmp.ProcessEvent(ev)
|
||||
@ -621,7 +622,7 @@ func IsDeadKey(key term.Key) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Composer) processKey(ev Event) {
|
||||
func (c *Composer) processKey(ev мКнст.Event) {
|
||||
if ev.Key == term.KeyEsc {
|
||||
if IsDeadKey(c.lastKey) {
|
||||
c.lastKey = term.KeyEsc
|
||||
@ -697,7 +698,7 @@ func (c *Composer) processKey(ev Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func ProcessEvent(ev Event) {
|
||||
func ProcessEvent(ev мКнст.Event) {
|
||||
switch ev.Type {
|
||||
case EventCloseWindow:
|
||||
comp.closeTopWindow()
|
||||
|
@ -2,6 +2,7 @@ package clui
|
||||
|
||||
import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
// Control is an interface that every visible control should implement
|
||||
@ -65,9 +66,9 @@ type Control interface {
|
||||
SetGaps(dx, dy int)
|
||||
// Pack returns direction in which a container packs
|
||||
// its children: horizontal or vertical
|
||||
Pack() PackType
|
||||
Pack() мКнст.PackType
|
||||
// SetPack changes the direction of children packing
|
||||
SetPack(pack PackType)
|
||||
SetPack(pack мКнст.PackType)
|
||||
// Scale return scale coefficient that is used to calculate
|
||||
// new control size after its parent resizes.
|
||||
// Fixed means the controls never changes its size.
|
||||
@ -84,8 +85,8 @@ type Control interface {
|
||||
// See Scale method for details
|
||||
SetScale(scale int)
|
||||
// Align returns alignment of title in control
|
||||
Align() Align
|
||||
SetAlign(align Align)
|
||||
Align() мКнст.Align
|
||||
SetAlign(align мКнст.Align)
|
||||
|
||||
TextColor() term.Attribute
|
||||
// SetTextColor changes text color of the control.
|
||||
@ -141,12 +142,12 @@ type Control interface {
|
||||
// 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 control
|
||||
HitTest(x, y int) HitResult
|
||||
HitTest(x, y int) мКнст.HitResult
|
||||
// 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
|
||||
ProcessEvent(ev Event) bool
|
||||
ProcessEvent(ev мКнст.Event) bool
|
||||
// RefID returns the controls internal reference id
|
||||
RefID() int64
|
||||
// removeChild removes a child from a container
|
||||
|
@ -2,6 +2,7 @@ package clui
|
||||
|
||||
import (
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
// ThumbPosition returns a scrollbar thumb position depending
|
||||
@ -138,7 +139,7 @@ func FindChild(parent, control Control) Control {
|
||||
}
|
||||
|
||||
// IsMouseClickEvent returns if a user action can be treated as mouse click.
|
||||
func IsMouseClickEvent(ev Event) bool {
|
||||
func IsMouseClickEvent(ev мКнст.Event) bool {
|
||||
if ev.Type == EventClick {
|
||||
return true
|
||||
}
|
||||
|
@ -3,13 +3,14 @@ package clui
|
||||
import (
|
||||
xs "github.com/huandu/xstrings"
|
||||
term "github.com/nsf/termbox-go"
|
||||
мКнст "./пакКонстанты"
|
||||
)
|
||||
|
||||
// Window is an implemetation of View managed by Composer.
|
||||
type Window struct {
|
||||
BaseControl
|
||||
|
||||
buttons ViewButton
|
||||
buttons мКнст.ViewButton
|
||||
maximized bool
|
||||
// maximization support
|
||||
origWidth int
|
||||
@ -19,7 +20,7 @@ type Window struct {
|
||||
hidden bool
|
||||
immovable bool
|
||||
fixedSize bool
|
||||
border BorderStyle
|
||||
border мКнст.BorderStyle
|
||||
|
||||
onClose func(Event) bool
|
||||
onScreenResize func(Event)
|
||||
|
Loading…
x
Reference in New Issue
Block a user