Documentation
¶
Overview ¶
Package tcompents 提供基于 tcell 的终端 UI 组件库。
核心概念:
- Compents: 所有组件必须实现的核心接口, 涵盖矩形管理、焦点、 事件处理与绘制。
- Base: 接口的默认实现, 自定义组件内嵌 Base 即可获得默认行为。
- Application: 终端应用主入口, 负责屏幕初始化、事件循环与焦点管理。
布局容器(StackPanel、Grid、Absolute 等)实现 Container 接口, 通过 Children() 暴露子组件, 以便焦点查找与事件分发。
参考 README.md 获取使用示例与组件清单。
Index ¶
- Variables
- func ContainsPoint(cps Compents, px, py int) bool
- type Absolute
- func (a *Absolute) Add(cps Compents) *Absolute
- func (a *Absolute) AddAt(x, y int, cps Compents) *Absolute
- func (a *Absolute) Children() []Compents
- func (a *Absolute) Clear() *Absolute
- func (a *Absolute) Draw(screen tcell.Screen)
- func (a *Absolute) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (a *Absolute) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (a *Absolute) Remove(index int) *Absolute
- type Align
- type Application
- type Base
- func (b *Base) Blur()
- func (b *Base) Contains(px, py int) bool
- func (b *Base) Draw(screen tcell.Screen)
- func (b *Base) Focus(focus func(cps Compents))
- func (b *Base) Focusable() bool
- func (b *Base) GetRect() (int, int, int, int)
- func (b *Base) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (b *Base) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (b *Base) HasFocus() bool
- func (b *Base) SetRect(x, y, w, h int)
- type Border
- func (b *Border) Children() []Compents
- func (b *Border) Draw(screen tcell.Screen)
- func (b *Border) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (b *Border) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (b *Border) SetBorderWidth(width int) *Border
- func (b *Border) SetStyle(style tcell.Style) *Border
- func (b *Border) SetTitle(title string) *Border
- type Button
- func (btn *Button) Draw(screen tcell.Screen)
- func (btn *Button) Focusable() bool
- func (btn *Button) GetClick() func()
- func (btn *Button) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (btn *Button) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (btn *Button) SetClick(click func()) *Button
- type Canvas
- func (c *Canvas) Clear() *Canvas
- func (c *Canvas) Draw(screen tcell.Screen)
- func (c *Canvas) DrawCircle(cx, cy, r int, ch rune, style tcell.Style) *Canvas
- func (c *Canvas) DrawLine(x1, y1, x2, y2 int, ch rune, style tcell.Style) *Canvas
- func (c *Canvas) DrawRect(x, y, w, h int, ch rune, style tcell.Style) *Canvas
- func (c *Canvas) DrawText(x, y int, text string, style tcell.Style) *Canvas
- func (c *Canvas) FillRect(x, y, w, h int, ch rune, style tcell.Style) *Canvas
- func (c *Canvas) SetPixel(x, y int, ch rune, style tcell.Style) *Canvas
- func (c *Canvas) SetStyle(style tcell.Style) *Canvas
- type CheckButton
- func (cb *CheckButton) Draw(screen tcell.Screen)
- func (cb *CheckButton) Focusable() bool
- func (cb *CheckButton) GetChecked() bool
- func (cb *CheckButton) GetLabel() string
- func (cb *CheckButton) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (cb *CheckButton) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (cb *CheckButton) SetChecked(checked bool) *CheckButton
- func (cb *CheckButton) SetColors(fg, bg tcell.Color) *CheckButton
- func (cb *CheckButton) SetLabel(label string) *CheckButton
- func (cb *CheckButton) SetOnChange(onchange func(checked bool)) *CheckButton
- func (cb *CheckButton) Toggle()
- type Compents
- type Container
- type Dialog
- func (d *Dialog) AddButton(label string, onclick func()) *Button
- func (d *Dialog) Children() []Compents
- func (d *Dialog) Close(result int)
- func (d *Dialog) Draw(screen tcell.Screen)
- func (d *Dialog) Focusable() bool
- func (d *Dialog) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (d *Dialog) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (d *Dialog) SetColors(fg, bg tcell.Color) *Dialog
- func (d *Dialog) SetContent(cps Compents) *Dialog
- func (d *Dialog) SetOnClose(onclose func(result int)) *Dialog
- func (d *Dialog) SetTitle(title string) *Dialog
- type Direction
- type Form
- func (f *Form) AddButton(label string, onclick func()) *Button
- func (f *Form) AddCheckButton(label string) *CheckButton
- func (f *Form) AddField(label string, widget Compents, height int) *Form
- func (f *Form) AddInput(label string) *Input
- func (f *Form) AddSelect(label string, options []string) *Select
- func (f *Form) AddTextArea(label string, height int) *TextArea
- func (f *Form) Children() []Compents
- func (f *Form) Draw(screen tcell.Screen)
- func (f *Form) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (f *Form) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (f *Form) SetLabelWidth(width int) *Form
- func (f *Form) SetSubmit(onSubmit func()) *Form
- type FormField
- type Grid
- func (g *Grid) AddChild(cps Compents) *Grid
- func (g *Grid) Children() []Compents
- func (g *Grid) Draw(screen tcell.Screen)
- func (g *Grid) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (g *Grid) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (g *Grid) SetChild(index int, cps Compents) *Grid
- type Handler
- type Image
- type ImageMode
- type Input
- func (i *Input) Draw(screen tcell.Screen)
- func (i *Input) Focusable() bool
- func (i *Input) GetText() string
- func (i *Input) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (i *Input) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (i *Input) SetColors(fg, bg tcell.Color) *Input
- func (i *Input) SetMaxLen(maxLen int) *Input
- func (i *Input) SetOnChange(onchange func(text string)) *Input
- func (i *Input) SetPassword(password bool) *Input
- func (i *Input) SetPlaceholder(placeholder string) *Input
- func (i *Input) SetText(text string) *Input
- type Label
- type List
- func (l *List) AddItem(text string) *List
- func (l *List) Draw(screen tcell.Screen)
- func (l *List) Focusable() bool
- func (l *List) GetItems() []string
- func (l *List) GetSelected() int
- func (l *List) GetSelectedValue() string
- func (l *List) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (l *List) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (l *List) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *List
- func (l *List) SetItems(items []string) *List
- func (l *List) SetOnChange(onchange func(index int, value string)) *List
- func (l *List) SetSelected(index int) *List
- type Menu
- type MenuBar
- func (mb *MenuBar) AddMenu(menu *Menu) *MenuBar
- func (mb *MenuBar) Close()
- func (mb *MenuBar) Draw(screen tcell.Screen)
- func (mb *MenuBar) Focusable() bool
- func (mb *MenuBar) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (mb *MenuBar) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (mb *MenuBar) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *MenuBar
- type MenuItem
- type Padding
- func (p *Padding) Children() []Compents
- func (p *Padding) Draw(screen tcell.Screen)
- func (p *Padding) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (p *Padding) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (p *Padding) SetPadding(top, right, bottom, left int) *Padding
- type Page
- type Pages
- func (p *Pages) AddPage(name string, panel Compents) *Pages
- func (p *Pages) Children() []Compents
- func (p *Pages) Draw(screen tcell.Screen)
- func (p *Pages) GetCurrent() string
- func (p *Pages) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (p *Pages) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (p *Pages) RemovePage(name string) *Pages
- func (p *Pages) SetOnChange(onchange func(name string)) *Pages
- func (p *Pages) SwitchTo(name string) *Pages
- type ProgressBar
- func (pb *ProgressBar) Draw(screen tcell.Screen)
- func (pb *ProgressBar) GetProgress() float64
- func (pb *ProgressBar) SetColors(fg, bg tcell.Color) *ProgressBar
- func (pb *ProgressBar) SetProgress(progress float64) *ProgressBar
- func (pb *ProgressBar) SetProgressPercent(percent int) *ProgressBar
- func (pb *ProgressBar) SetShowText(show bool) *ProgressBar
- type Radio
- type RadioGroup
- func (rg *RadioGroup) Add(label string) *Radio
- func (rg *RadioGroup) Children() []Compents
- func (rg *RadioGroup) Draw(screen tcell.Screen)
- func (rg *RadioGroup) GetSelected() int
- func (rg *RadioGroup) GetSelectedLabel() string
- func (rg *RadioGroup) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (rg *RadioGroup) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (rg *RadioGroup) SetColors(fg, bg tcell.Color) *RadioGroup
- func (rg *RadioGroup) SetOnChange(onchange func(index int, label string)) *RadioGroup
- func (rg *RadioGroup) SetSelected(index int)
- type ScrollView
- func (sv *ScrollView) Children() []Compents
- func (sv *ScrollView) Draw(screen tcell.Screen)
- func (sv *ScrollView) GetContentSize() (int, int)
- func (sv *ScrollView) GetOffset() (int, int)
- func (sv *ScrollView) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (sv *ScrollView) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (sv *ScrollView) ScrollBy(dx, dy int) *ScrollView
- func (sv *ScrollView) ScrollTo(x, y int) *ScrollView
- func (sv *ScrollView) SetBarStyle(style tcell.Style) *ScrollView
- func (sv *ScrollView) SetContent(cps Compents) *ScrollView
- func (sv *ScrollView) SetContentSize(w, h int) *ScrollView
- type Select
- func (s *Select) Contains(x, y int) bool
- func (s *Select) Draw(screen tcell.Screen)
- func (s *Select) Focusable() bool
- func (s *Select) GetOptions() []string
- func (s *Select) GetSelected() int
- func (s *Select) GetSelectedValue() string
- func (s *Select) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (s *Select) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (s *Select) IsOpen() bool
- func (s *Select) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *Select
- func (s *Select) SetOnChange(onchange func(index int, value string)) *Select
- func (s *Select) SetOpenHeight(height int) *Select
- func (s *Select) SetOptions(options []string) *Select
- func (s *Select) SetSelected(index int) *Select
- type Spinner
- type StackItem
- type StackPanel
- func (sp *StackPanel) Add(size int, cps Compents)
- func (sp *StackPanel) AddItem(size int, cps Compents, index int)
- func (sp *StackPanel) Children() []Compents
- func (sp *StackPanel) Draw(screen tcell.Screen)
- func (sp *StackPanel) GetItem(index int) StackItem
- func (sp *StackPanel) GetItemCount() int
- func (sp *StackPanel) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (sp *StackPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (sp *StackPanel) Remove(index int)
- type TabPanel
- func (tp *TabPanel) AddTab(title string, panel Compents) *TabPanel
- func (tp *TabPanel) Children() []Compents
- func (tp *TabPanel) Draw(screen tcell.Screen)
- func (tp *TabPanel) GetPageContent(index int) Compents
- func (tp *TabPanel) GetSelected() int
- func (tp *TabPanel) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (tp *TabPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (tp *TabPanel) Select(index int) *TabPanel
- func (tp *TabPanel) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *TabPanel
- func (tp *TabPanel) SetOnChange(onchange func(index int)) *TabPanel
- type TextArea
- func (ta *TextArea) Draw(screen tcell.Screen)
- func (ta *TextArea) Focusable() bool
- func (ta *TextArea) GetText() string
- func (ta *TextArea) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (ta *TextArea) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (ta *TextArea) SetColors(fg, bg tcell.Color) *TextArea
- func (ta *TextArea) SetOnChange(onchange func(text string)) *TextArea
- func (ta *TextArea) SetText(text string) *TextArea
- type TreeNode
- type TreeView
- func (tv *TreeView) Draw(screen tcell.Screen)
- func (tv *TreeView) Focusable() bool
- func (tv *TreeView) GetRoot() *TreeNode
- func (tv *TreeView) GetSelected() *TreeNode
- func (tv *TreeView) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (tv *TreeView) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (tv *TreeView) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *TreeView
- func (tv *TreeView) SetOnChange(onchange func(node *TreeNode)) *TreeView
- func (tv *TreeView) SetRoot(root *TreeNode) *TreeView
- type WrapItem
- type WrapPanel
- func (wp *WrapPanel) Add(w, h int, cps Compents) *WrapPanel
- func (wp *WrapPanel) Children() []Compents
- func (wp *WrapPanel) Draw(screen tcell.Screen)
- func (wp *WrapPanel) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
- func (wp *WrapPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
- func (wp *WrapPanel) SetSpacing(spacing int) *WrapPanel
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotSetRoot 表示应用在未设置根组件的情况下被启动。 ErrNotSetRoot = errors.New("Application need SetRoot") )
var FocusBackground = tcell.ColorBlue
FocusBackground 是组件获得焦点时用于高亮显示的默认背景色。 修改该变量可全局调整所有组件的焦点高亮效果。
Functions ¶
func ContainsPoint ¶
ContainsPoint 判断点 (px, py) 是否落在组件矩形内部。 若组件实现了 Contains 方法(如内嵌 Base), 则优先使用该方法。
Types ¶
type Absolute ¶
type Absolute struct {
Base
// contains filtered or unexported fields
}
Absolute 是绝对定位容器, 子组件的矩形由使用者直接通过 SetRect 指定, 容器只负责绘制与事件分发。
func (*Absolute) HandlerInput ¶
HandlerInput 将按键事件转发给持有焦点的子组件。
func (*Absolute) HandlerMouse ¶
func (a *Absolute) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件分发给命中的子组件。
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
Application 是终端应用的主入口, 负责屏幕初始化、事件循环、 渲染与全局焦点管理。
func NewApplication ¶
func NewApplication() (*Application, error)
NewApplication 创建并初始化一个 Application。 它会分配屏幕资源, 若分配失败则返回错误。
func (*Application) GetFocus ¶
func (app *Application) GetFocus() Compents
GetFocus 返回当前持有焦点的组件, 无焦点时返回 nil。
func (*Application) Run ¶
func (app *Application) Run() error
Run 启动事件循环。在启动前必须调用 SetRoot 设置根组件, 否则返回 ErrNotSetRoot。按 Ctrl+C 或调用 Quit 可退出应用。
func (*Application) SetFocus ¶
func (app *Application) SetFocus(cps Compents)
SetFocus 将焦点转移到指定组件。可作为 focus 回调传给组件的 事件处理器, 由组件自行请求获得焦点。
func (*Application) SetRoot ¶
func (app *Application) SetRoot(root Compents)
SetRoot 设置应用的根组件。根组件的矩形会在每次绘制时 被调整为整个屏幕大小。
type Base ¶
type Base struct {
// contains filtered or unexported fields
}
Base 是 Compents 接口的默认实现, 提供矩形管理、焦点跟踪以及事件 处理器的空实现。自定义组件通常通过内嵌 Base 来快速获得默认行为, 再按需覆盖 Draw / HandlerInput / HandlerMouse 等方法。
func (*Base) Focusable ¶
Focusable 返回组件是否可以获得键盘焦点, 默认不可获得焦点。 需要接收键盘输入的组件(如 Input、Button)应覆盖此方法返回 true。
func (*Base) HandlerInput ¶
HandlerInput 默认不处理按键事件。
func (*Base) HandlerMouse ¶
func (b *Base) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 默认不处理鼠标事件。
type Border ¶
type Border struct {
Base
// contains filtered or unexported fields
}
Border 是为子组件绘制边框的容器组件。 边框占用的区域会从子组件的可用区域中扣除。
func (*Border) HandlerInput ¶
HandlerInput 将按键事件转发给内部的子组件。
func (*Border) HandlerMouse ¶
func (b *Border) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件转发给内部的子组件。
func (*Border) SetBorderWidth ¶
SetBorderWidth 设置边框线宽(字符数), 默认 1。
type Button ¶
type Button struct {
Label
// contains filtered or unexported fields
}
Button 是可点击的按钮组件, 内嵌 Label 以复用文本绘制能力。 按钮可以获得焦点, 支持鼠标点击与键盘(Enter / 空格)触发。 按钮文本默认水平居中, 可通过 SetTextAlign 修改对齐方式。
func (*Button) HandlerInput ¶
HandlerInput 处理按键事件: 按下 Enter 或空格时触发点击回调。
func (*Button) HandlerMouse ¶
func (btn *Button) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件:
- 左键按下且位于按钮内部时请求焦点;
- 左键松开且此前在按钮内部按下时触发点击回调。
type Canvas ¶
type Canvas struct {
Base
// contains filtered or unexported fields
}
Canvas 提供类似网页 Canvas 的 2D 绘图能力。使用者通过 DrawText / DrawLine / DrawRect / DrawCircle 等方法登记绘制指令, 在 Draw 阶段统一输出到屏幕。所有坐标均相对于画布左上角。
func (*Canvas) DrawCircle ¶
DrawCircle 使用中点圆算法登记一个圆绘制指令。
type CheckButton ¶
type CheckButton struct {
Base
// contains filtered or unexported fields
}
CheckButton 是复选框组件, 支持选中与取消选中, 状态变化时触发回调。
func (*CheckButton) Draw ¶
func (cb *CheckButton) Draw(screen tcell.Screen)
Draw 绘制复选框。选中时显示 [x], 未选中时显示 [ ]。 获得焦点时使用高亮背景色。
func (*CheckButton) HandlerInput ¶
func (cb *CheckButton) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
HandlerInput 处理按键事件: 空格或回车翻转选中状态。
func (*CheckButton) HandlerMouse ¶
func (cb *CheckButton) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 左键点击翻转选中状态并请求焦点。
func (*CheckButton) SetChecked ¶
func (cb *CheckButton) SetChecked(checked bool) *CheckButton
SetChecked 设置复选框的选中状态, 不触发回调。
func (*CheckButton) SetColors ¶
func (cb *CheckButton) SetColors(fg, bg tcell.Color) *CheckButton
SetColors 设置复选框的前景色与背景色。
func (*CheckButton) SetLabel ¶
func (cb *CheckButton) SetLabel(label string) *CheckButton
SetLabel 设置复选框右侧的文本。
func (*CheckButton) SetOnChange ¶
func (cb *CheckButton) SetOnChange(onchange func(checked bool)) *CheckButton
SetOnChange 设置选中状态变化时的回调。
type Compents ¶
type Compents interface {
// GetRect 返回组件矩形 (x, y, w, h)。
GetRect() (x, y, w, h int)
// SetRect 设置组件矩形。
SetRect(x, y, w, h int)
// Focus 在组件获得焦点时被调用, focus 参数用于请求将焦点转移到其他组件。
Focus(focus func(cps Compents))
// Blur 在组件失去焦点时被调用。
Blur()
// HandlerInput 返回按键事件处理器。返回 nil 表示不处理按键事件。
HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
// HandlerMouse 返回鼠标事件处理器。返回 nil 表示不处理鼠标事件。
HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
// Draw 将组件绘制到给定的屏幕上。
Draw(screen tcell.Screen)
// HasFocus 返回组件当前是否持有焦点。
HasFocus() bool
// Focusable 返回组件是否可以获得焦点。
Focusable() bool
}
Compents 是所有组件必须实现的核心接口。
组件通过 GetRect/SetRect 管理自身占用的矩形区域; 通过 Focus/Blur/ HasFocus 参与应用的焦点管理; 通过 HandlerInput/HandlerMouse 返回 按键与鼠标事件处理器; 通过 Draw 将自身绘制到屏幕上。
func FindFocused ¶
FindFocused 深度优先查找当前持有焦点的后代组件。 若组件自身持有焦点则直接返回该组件。
type Dialog ¶
type Dialog struct {
Base
// contains filtered or unexported fields
}
Dialog 是带边框的对话框组件, 包含标题、内容区域与底部按钮栏。 支持键盘与鼠标操作, 通常被放置在 Absolute 或 Grid 等容器中。
func (*Dialog) AddButton ¶
AddButton 添加一个按钮到对话框底部, 返回创建的按钮以便设置回调。 无论按钮通过键盘还是鼠标触发, 点击回调执行后都会自动调用 Close(该按钮的下标), 保证 onclose 行为一致。
func (*Dialog) HandlerInput ¶
HandlerInput 处理按键事件:
- Tab / Shift+Tab 在内容与按钮之间循环焦点;
- 内容区持有焦点时, 方向键等按键优先转发给内容区;
- 内容区无焦点时, 左右键切换按钮焦点, 回车触发按钮, Esc 关闭。
func (*Dialog) HandlerMouse ¶
func (d *Dialog) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件分发给命中的按钮或内容。
func (*Dialog) SetOnClose ¶
SetOnClose 设置对话框关闭时的回调, 参数为触发关闭的按钮下标。
type Form ¶
type Form struct {
Base
// contains filtered or unexported fields
}
Form 是表单容器, 将多个带标签的字段按行垂直排列。 支持 Tab 在字段之间切换焦点, 回车触发提交回调。
func (*Form) AddCheckButton ¶
func (f *Form) AddCheckButton(label string) *CheckButton
AddCheckButton 添加一行复选框, 返回创建的复选框以便配置。
func (*Form) AddTextArea ¶
AddTextArea 添加一行多行文本输入框, 返回创建的文本域以便配置。
func (*Form) HandlerInput ¶
HandlerInput 处理按键事件: 回车触发提交, 其余按键转交给字段。 当焦点位于按钮或已展开的选择框上时, 回车交由对应组件处理。
func (*Form) HandlerMouse ¶
func (f *Form) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件分发给命中的字段。
func (*Form) SetLabelWidth ¶
SetLabelWidth 设置每个字段标签占用的宽度。
type FormField ¶
type FormField struct {
// contains filtered or unexported fields
}
FormField 是表单中的一行: 左侧为标签, 右侧为输入组件。
type Grid ¶
type Grid struct {
Base
// contains filtered or unexported fields
}
Grid 是网格布局容器, 将可用区域划分为 cols x rows 个等大小的单元格, 子组件按行优先的顺序依次放入单元格。
func (*Grid) HandlerInput ¶
HandlerInput 处理容器的按键事件, 支持 Tab 切换子组件焦点。
func (*Grid) HandlerMouse ¶
func (g *Grid) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件分发给命中的子组件。
type Input ¶
type Input struct {
Base
// contains filtered or unexported fields
}
Input 是单行文本输入框, 支持光标移动、字符插入与删除。
func (*Input) HandlerInput ¶
HandlerInput 处理按键事件: 字符输入、退格、删除以及光标移动。
func (*Input) HandlerMouse ¶
func (i *Input) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 点击输入框时请求焦点。
func (*Input) SetOnChange ¶
SetOnChange 设置文本变化时的回调。
func (*Input) SetPassword ¶
SetPassword 设置是否以掩码字符显示内容。
func (*Input) SetPlaceholder ¶
SetPlaceholder 设置占位文本(文本为空时显示)。
type Label ¶
type Label struct {
Base
// contains filtered or unexported fields
}
Label 是最基础的文本显示组件, 支持背景填充、前景色以及对齐方式。 其他组件(如 Button)常常内嵌 Label 以获得文本绘制能力。
func (*Label) SetBackground ¶
SetBackground 设置背景色并返回自身以便链式调用。
func (*Label) SetContent ¶
SetContent 设置标签文本并返回自身以便链式调用。
func (*Label) SetTextAlign ¶
SetTextAlign 设置文本水平对齐方式并返回自身以便链式调用。
type List ¶
type List struct {
Base
// contains filtered or unexported fields
}
List 是可选择的列表组件, 支持键盘与鼠标选择, 内容超出时自动滚动。
func (*List) GetSelectedValue ¶
GetSelectedValue 返回当前选中项的文本。
func (*List) HandlerInput ¶
HandlerInput 处理按键事件: 上下键移动选择, 回车触发回调。
func (*List) HandlerMouse ¶
func (l *List) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 点击选择列表项, 滚轮滚动。
func (*List) SetOnChange ¶
SetOnChange 设置选中项变化时的回调。
func (*List) SetSelected ¶
SetSelected 选中指定下标的列表项, 下标越界时不做任何操作。
type MenuBar ¶
type MenuBar struct {
Base
// contains filtered or unexported fields
}
MenuBar 是顶部菜单栏组件, 支持鼠标与键盘操作。
func (*MenuBar) HandlerInput ¶
HandlerInput 处理按键事件: 菜单展开时用方向键浏览, Enter 执行; 未展开时用左右键定位菜单。
func (*MenuBar) HandlerMouse ¶
func (mb *MenuBar) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 点击菜单展开, 点击菜单项执行。
type MenuItem ¶
type MenuItem struct {
// contains filtered or unexported fields
}
MenuItem 是菜单中的一个条目, 支持子菜单。
func (*MenuItem) AddSubItem ¶
AddSubItem 向菜单条目添加一个子菜单条目。
type Padding ¶
type Padding struct {
Base
// contains filtered or unexported fields
}
Padding 是一个容器组件, 为内部的子组件四周增加内边距。 内边距不会绘制任何内容, 仅负责压缩子组件的可用区域。
func NewPadding ¶
NewPadding 创建一个指定内边距的容器。参数顺序为: 上、右、下、左。
func NewUniformPadding ¶
NewUniformPadding 创建一个四周内边距相等的容器。
func (*Padding) HandlerInput ¶
HandlerInput 将按键事件转发给内部的子组件。
func (*Padding) HandlerMouse ¶
func (p *Padding) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件转发给内部的子组件。
func (*Padding) SetPadding ¶
SetPadding 设置内边距, 参数顺序为: 上、右、下、左。
type Page ¶
type Page struct {
// contains filtered or unexported fields
}
Page 是 Pages 容器中的一个页面, 通过名称标识。
type Pages ¶
type Pages struct {
Base
// contains filtered or unexported fields
}
Pages 是页面容器, 同时只显示一个页面, 支持按名称切换。
func (*Pages) HandlerInput ¶
HandlerInput 将按键事件转发给当前页面。
func (*Pages) HandlerMouse ¶
func (p *Pages) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件转发给当前页面。
func (*Pages) SetOnChange ¶
SetOnChange 设置页面切换时的回调。
type ProgressBar ¶
type ProgressBar struct {
Base
// contains filtered or unexported fields
}
ProgressBar 是进度条组件, 通过 progress 表示进度(0.0 到 1.0)。
func (*ProgressBar) SetColors ¶
func (pb *ProgressBar) SetColors(fg, bg tcell.Color) *ProgressBar
SetColors 设置进度条的前景色与背景色。
func (*ProgressBar) SetProgress ¶
func (pb *ProgressBar) SetProgress(progress float64) *ProgressBar
SetProgress 设置进度, 数值会被限制在 [0, 1] 区间内。
func (*ProgressBar) SetProgressPercent ¶
func (pb *ProgressBar) SetProgressPercent(percent int) *ProgressBar
SetProgressPercent 以百分比形式设置进度(0 到 100)。
func (*ProgressBar) SetShowText ¶
func (pb *ProgressBar) SetShowText(show bool) *ProgressBar
SetShowText 设置是否在进度条中央显示百分比文本。
type Radio ¶
type Radio struct {
Base
// contains filtered or unexported fields
}
Radio 是单选按钮组中的单个选项。
func (*Radio) HandlerMouse ¶
func (r *Radio) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理单个选项的鼠标事件: 点击时选中并请求焦点。
type RadioGroup ¶
type RadioGroup struct {
Base
// contains filtered or unexported fields
}
RadioGroup 是单选按钮组容器, 同一组内同时只能选中一项。
func (*RadioGroup) Add ¶
func (rg *RadioGroup) Add(label string) *Radio
Add 向组中添加一个选项并返回该选项, 以便链式设置样式。
func (*RadioGroup) GetSelected ¶
func (rg *RadioGroup) GetSelected() int
GetSelected 返回当前选中选项的下标, 未选中任何项时返回 -1。
func (*RadioGroup) GetSelectedLabel ¶
func (rg *RadioGroup) GetSelectedLabel() string
GetSelectedLabel 返回当前选中选项的文本。
func (*RadioGroup) HandlerInput ¶
func (rg *RadioGroup) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
HandlerInput 处理按键事件: 上下方向键移动焦点, 空格选中。
func (*RadioGroup) HandlerMouse ¶
func (rg *RadioGroup) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件分发给命中的选项。
func (*RadioGroup) SetColors ¶
func (rg *RadioGroup) SetColors(fg, bg tcell.Color) *RadioGroup
SetColors 为组内所有选项设置前景色与背景色。
func (*RadioGroup) SetOnChange ¶
func (rg *RadioGroup) SetOnChange(onchange func(index int, label string)) *RadioGroup
SetOnChange 设置选中项变化时的回调。
func (*RadioGroup) SetSelected ¶
func (rg *RadioGroup) SetSelected(index int)
SetSelected 选中指定下标的选项, 下标越界时不做任何操作。
type ScrollView ¶
type ScrollView struct {
Base
// contains filtered or unexported fields
}
ScrollView 是滚动视口容器, 将一个可能大于自身区域的子组件 裁剪到可见范围, 并通过偏移量实现滚动。支持滚轮与方向键滚动。
func (*ScrollView) Draw ¶
func (sv *ScrollView) Draw(screen tcell.Screen)
Draw 将子组件绘制到滚动视口中, 并绘制滚动条。
func (*ScrollView) GetContentSize ¶
func (sv *ScrollView) GetContentSize() (int, int)
GetContentSize 返回内容区域尺寸。
func (*ScrollView) HandlerInput ¶
func (sv *ScrollView) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
HandlerInput 处理按键事件:
- 内容区持有焦点时, 方向键等按键优先转发给内容区;
- 内容区无焦点时, 方向键与翻页键控制滚动。
func (*ScrollView) HandlerMouse ¶
func (sv *ScrollView) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 滚轮滚动, 点击事件转换为内容坐标后分发给子组件。
func (*ScrollView) ScrollBy ¶
func (sv *ScrollView) ScrollBy(dx, dy int) *ScrollView
ScrollBy 按指定增量滚动。
func (*ScrollView) ScrollTo ¶
func (sv *ScrollView) ScrollTo(x, y int) *ScrollView
ScrollTo 滚动到指定偏移, 偏移会被限制在合法范围内。
func (*ScrollView) SetBarStyle ¶
func (sv *ScrollView) SetBarStyle(style tcell.Style) *ScrollView
SetBarStyle 设置滚动条样式。
func (*ScrollView) SetContent ¶
func (sv *ScrollView) SetContent(cps Compents) *ScrollView
SetContent 设置滚动视口中的子组件。
func (*ScrollView) SetContentSize ¶
func (sv *ScrollView) SetContentSize(w, h int) *ScrollView
SetContentSize 设置内容区域尺寸, 用于限制滚动范围与绘制滚动条。
type Select ¶
type Select struct {
Base
// contains filtered or unexported fields
}
Select 是下拉选择组件。点击或按回车展开选项列表, 从列表中选择一项。
func (*Select) GetSelectedValue ¶
GetSelectedValue 返回选中项的文本。
func (*Select) HandlerInput ¶
HandlerInput 处理按键事件: 回车/空格展开或收起, 上下键在选项中移动。
func (*Select) HandlerMouse ¶
func (s *Select) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件:
- 未展开时点击组件展开下拉列表并请求焦点;
- 展开时点击组件收起, 点击下拉选项完成选择。
func (*Select) SetOnChange ¶
SetOnChange 设置选中项变化时的回调。
func (*Select) SetOpenHeight ¶
SetOpenHeight 设置下拉列表展开时显示的最大行数。
func (*Select) SetOptions ¶
SetOptions 设置下拉选项列表。
type Spinner ¶
type Spinner struct {
Base
// contains filtered or unexported fields
}
Spinner 是加载指示器组件, 每隔固定间隔切换一个动画帧。
type StackItem ¶
type StackItem struct {
// contains filtered or unexported fields
}
StackItem 是 StackPanel 中的一个子项, size 表示子组件在主轴方向占用的尺寸。
type StackPanel ¶
type StackPanel struct {
Base
// contains filtered or unexported fields
}
StackPanel 是栈式布局容器, 将子组件沿水平或垂直方向依次排列。
func NewStackPanel ¶
func NewStackPanel(direct Direction) *StackPanel
NewStackPanel 按指定方向创建一个栈式布局容器。
func (*StackPanel) AddItem ¶
func (sp *StackPanel) AddItem(size int, cps Compents, index int)
AddItem 在指定下标处插入一个子项。index 越界时追加到末尾。
func (*StackPanel) Children ¶
func (sp *StackPanel) Children() []Compents
Children 返回所有子组件, 用于容器事件分发。
func (*StackPanel) GetItem ¶
func (sp *StackPanel) GetItem(index int) StackItem
GetItem 返回指定下标的子项, 越界时返回零值。
func (*StackPanel) HandlerInput ¶
func (sp *StackPanel) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
HandlerInput 处理容器的按键事件, 支持 Tab 切换子组件焦点。
func (*StackPanel) HandlerMouse ¶
func (sp *StackPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理容器的鼠标事件, 将事件分发给命中的子组件。
type TabPanel ¶
type TabPanel struct {
Base
// contains filtered or unexported fields
}
TabPanel 是选项卡容器, 顶部显示标签栏, 下方显示当前选项卡对应的内容。
func (*TabPanel) GetPageContent ¶
GetPageContent 返回指定下标的页签内容组件, 下标越界时返回 nil。
func (*TabPanel) HandlerInput ¶
HandlerInput 处理按键事件:
- Tab 在内容区组件之间循环焦点, Ctrl+Tab 切换选项卡;
- 内容区持有焦点时, 方向键等按键优先转发给内容区;
- 内容区无焦点时, 左右键切换选项卡。
func (*TabPanel) HandlerMouse ¶
func (tp *TabPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 点击标签切换选项卡, 点击内容区转发给页面。
func (*TabPanel) SetOnChange ¶
SetOnChange 设置选项卡切换时的回调。
type TextArea ¶
type TextArea struct {
Base
// contains filtered or unexported fields
}
TextArea 是多行文本输入框, 支持换行、光标移动以及行内编辑。
func (*TextArea) HandlerInput ¶
HandlerInput 处理按键事件。
func (*TextArea) HandlerMouse ¶
func (ta *TextArea) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 点击文本区域时请求焦点, 并把光标移动到点击位置。
func (*TextArea) SetOnChange ¶
SetOnChange 设置文本变化时的回调。
type TreeNode ¶
type TreeNode struct {
// contains filtered or unexported fields
}
TreeNode 是树形视图中的节点, 支持子节点与展开/折叠状态。
func NewTreeNode ¶
NewTreeNode 创建一个树节点。
func (*TreeNode) SetExpanded ¶
SetExpanded 设置节点的展开状态。
type TreeView ¶
type TreeView struct {
Base
// contains filtered or unexported fields
}
TreeView 是树形视图组件, 支持键盘与鼠标浏览和展开折叠节点。
func (*TreeView) HandlerInput ¶
HandlerInput 处理按键事件: 上下键移动选中, 左右键展开/折叠。
func (*TreeView) HandlerMouse ¶
func (tv *TreeView) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 处理鼠标事件: 点击节点选中, 双击展开/折叠。
func (*TreeView) SetOnChange ¶
SetOnChange 设置选中节点变化时的回调。
type WrapItem ¶
type WrapItem struct {
// contains filtered or unexported fields
}
WrapItem 是 WrapPanel 中的一个子项, w 与 h 表示该子项占用的尺寸。
type WrapPanel ¶
type WrapPanel struct {
Base
// contains filtered or unexported fields
}
WrapPanel 是流式布局容器。水平方向时, 子项从左到右排列, 一行放不下时自动换行; 垂直方向时依次向下排列, 列放不下时自动换列。
func (*WrapPanel) HandlerInput ¶
HandlerInput 处理容器的按键事件, 支持 Tab 切换子组件焦点。
func (*WrapPanel) HandlerMouse ¶
func (wp *WrapPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
HandlerMouse 将鼠标事件分发给命中的子组件。
func (*WrapPanel) SetSpacing ¶
SetSpacing 设置子项之间的间距。
