tcompents

package module
v0.0.0-...-cb2c650 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2026 License: MIT Imports: 11 Imported by: 0

README

tcompents

tcompents 是一个基于 tcell 的 Go 终端 UI 组件库。它提供了一套轻量、易扩展的组件模型, 用于快速构建交互式终端应用。

preview

特性

  • 统一的组件接口, 支持焦点管理、键盘与鼠标事件。
  • 丰富的布局容器: StackPanel、Grid、Absolute、WrapPanel、Padding、Border。
  • 常用控件: Label、Button、Input、TextArea、CheckButton、Radio、Select、List、 TreeView、ProgressBar、Spinner、Canvas。
  • 高级容器: ScrollView、Pages、TabPanel、MenuBar、Dialog、Form、Image。
  • 内置仿真屏幕, 组件行为可以方便地编写单元测试。

安装

go get github.com/23jdd/tcompents

快速开始

package main

import (
	"github.com/23jdd/tcompents"
	"github.com/gdamore/tcell/v2"
)

func main() {
	app, err := tcompents.NewApplication()
	if err != nil {
		panic(err)
	}

	panel := tcompents.NewStackPanel(tcompents.Vertical)

	title := tcompents.NewLabel().
		SetContent("Hello, tcompents").
		SetTextAlign(tcompents.Center).
		SetBackground(tcell.ColorBlue)
	panel.Add(1, title)

	button := tcompents.NewButton()
	button.SetContent("click me")
	button.SetClick(func() {
		title.SetContent("Button clicked!")
	})
	panel.Add(1, button)

	app.SetRoot(tcompents.NewBorder(panel).SetTitle("demo"))
	if err := app.Run(); err != nil {
		panic(err)
	}
}

按 Ctrl+C 退出应用。

组件模型

所有组件都实现 Compents 接口:

type Compents interface {
	GetRect() (x, y, w, h int)
	SetRect(x, y, w, h int)
	Focus(focus func(cps Compents))
	Blur()
	HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))
	HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))
	Draw(screen tcell.Screen)
	HasFocus() bool
	Focusable() bool
}
  • GetRect / SetRect: 组件在终端屏幕上的矩形区域。
  • Focus / Blur / HasFocus: 参与应用的焦点管理。
  • HandlerInput / HandlerMouse: 返回按键与鼠标事件处理器, 处理器中的 focus 回调用于向应用请求转移焦点。
  • Draw: 将组件绘制到屏幕。
  • Focusable: 组件是否可以获得键盘焦点。

自定义组件通常通过内嵌 Base 获得默认实现, 再按需覆盖对应方法。 容器组件额外实现 Container 接口(Children()), 以便焦点查找与事件分发。

组件列表

布局容器
组件 说明
StackPanel 栈式布局, 水平或垂直依次排列子组件。
Grid 网格布局, 将区域划分为等大小的单元格。
Absolute 绝对定位, 子组件坐标由使用者直接指定。
WrapPanel 流式布局, 主轴放不下时自动换行/换列。
Padding 为子组件四周增加内边距。
Border 为子组件绘制边框并支持标题。
基础控件
组件 说明
Label 文本显示, 支持前景色、背景色与对齐。
Button 可点击按钮, 支持鼠标与键盘触发。
Input 单行文本输入框, 支持光标与占位符。
TextArea 多行文本输入框。
CheckButton 复选框。
RadioGroup / Radio 单选按钮组, 同组内互斥选择。
List 可滚动、可选择的列表。
Select 下拉选择框。
TreeView 可展开折叠的树形视图。
ProgressBar 进度条。
Spinner 加载动画。
Canvas 2D 绘图画布, 支持文本、像素、直线、矩形与圆。
高级容器
组件 说明
ScrollView 滚动视口, 支持滚动条与滚轮。
Pages 页面容器, 同一时间只显示一个页面。
TabPanel 选项卡容器, 顶部标签栏切换内容。
MenuBar 顶部菜单栏, 支持下拉菜单。
Dialog 对话框, 带标题、内容与按钮栏。
Form 表单, 自动排列带标签的字段并支持提交。
Image 显示图片, 支持拉伸、等比与裁剪填充模式。

焦点与事件

  • 应用启动后按 Tab 在可聚焦组件之间切换焦点, 容器会循环处理。
  • 获得焦点的组件以高亮背景色(FocusBackground, 默认蓝色)显示, 输入类组件的光标位置与列表的选中项仍以反色显示。
  • 鼠标左键点击可聚焦组件会同时请求焦点。
  • 滚动类组件(如 List、TextArea、ScrollView)支持鼠标滚轮。
  • 可通过修改全局变量 tcompents.FocusBackground 调整焦点高亮颜色。

编写自定义组件

type MyWidget struct {
	Base
	text string
}

func NewMyWidget() *MyWidget {
	return &MyWidget{}
}

// Focusable 使组件可以获得焦点。
func (w *MyWidget) Focusable() bool {
	return true
}

// Draw 绘制组件。
func (w *MyWidget) Draw(screen tcell.Screen) {
	style := tcell.StyleDefault
	if w.HasFocus() {
		style = style.Reverse(true)
	}
	drawTextClipped(screen, w.x, w.y, w.w, w.h, style, w.text)
}

// HandlerInput 处理按键事件。
func (w *MyWidget) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents)) {
	return func(event *tcell.EventKey, focus func(cps Compents)) {
		if event.Key() == tcell.KeyRune {
			w.text += string(event.Rune())
		}
	}
}

用例演示

go run ./example

示例演示了组件库中绝大多数组件: 顶部菜单栏(MenuBar)、页签容器(TabPanel)、 表单(Form)、对话框(Dialog)、画布(Canvas)、树形视图(TreeView)、滚动视口 (ScrollView)、图片(Image)、流式布局(WrapPanel)以及全部基础控件。 各页签布局总高约 22 行, 适配常见 24 行终端。

测试

go test ./...

Documentation

Overview

Package tcompents 提供基于 tcell 的终端 UI 组件库。

核心概念:

  • Compents: 所有组件必须实现的核心接口, 涵盖矩形管理、焦点、 事件处理与绘制。
  • Base: 接口的默认实现, 自定义组件内嵌 Base 即可获得默认行为。
  • Application: 终端应用主入口, 负责屏幕初始化、事件循环与焦点管理。

布局容器(StackPanel、Grid、Absolute 等)实现 Container 接口, 通过 Children() 暴露子组件, 以便焦点查找与事件分发。

参考 README.md 获取使用示例与组件清单。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSetRoot 表示应用在未设置根组件的情况下被启动。
	ErrNotSetRoot = errors.New("Application need SetRoot")
)
View Source
var FocusBackground = tcell.ColorBlue

FocusBackground 是组件获得焦点时用于高亮显示的默认背景色。 修改该变量可全局调整所有组件的焦点高亮效果。

Functions

func ContainsPoint

func ContainsPoint(cps Compents, px, py int) bool

ContainsPoint 判断点 (px, py) 是否落在组件矩形内部。 若组件实现了 Contains 方法(如内嵌 Base), 则优先使用该方法。

Types

type Absolute

type Absolute struct {
	Base
	// contains filtered or unexported fields
}

Absolute 是绝对定位容器, 子组件的矩形由使用者直接通过 SetRect 指定, 容器只负责绘制与事件分发。

func NewAbsolute

func NewAbsolute() *Absolute

NewAbsolute 创建一个绝对定位容器。

func (*Absolute) Add

func (a *Absolute) Add(cps Compents) *Absolute

Add 添加一个子组件。

func (*Absolute) AddAt

func (a *Absolute) AddAt(x, y int, cps Compents) *Absolute

AddAt 以指定坐标添加一个子组件。宽度与高度沿用子组件当前矩形。

func (*Absolute) Children

func (a *Absolute) Children() []Compents

Children 返回所有子组件。

func (*Absolute) Clear

func (a *Absolute) Clear() *Absolute

Clear 清空所有子组件。

func (*Absolute) Draw

func (a *Absolute) Draw(screen tcell.Screen)

Draw 依次绘制所有子组件。子组件保持自身设置的矩形坐标。

func (*Absolute) HandlerInput

func (a *Absolute) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 将按键事件转发给持有焦点的子组件。

func (*Absolute) HandlerMouse

func (a *Absolute) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件分发给命中的子组件。

func (*Absolute) Remove

func (a *Absolute) Remove(index int) *Absolute

Remove 移除指定下标的子组件, 下标越界时不做任何操作。

type Align

type Align int

Align 表示文本的水平对齐方式。

const (
	// Left 左对齐。
	Left Align = iota
	// Center 水平居中。
	Center
	// Right 右对齐。
	Right
)

type Application

type Application struct {
	// contains filtered or unexported fields
}

Application 是终端应用的主入口, 负责屏幕初始化、事件循环、 渲染与全局焦点管理。

func NewApplication

func NewApplication() (*Application, error)

NewApplication 创建并初始化一个 Application。 它会分配屏幕资源, 若分配失败则返回错误。

func (*Application) Draw

func (app *Application) Draw()

Draw 清空屏幕并将根组件绘制到屏幕上。

func (*Application) GetFocus

func (app *Application) GetFocus() Compents

GetFocus 返回当前持有焦点的组件, 无焦点时返回 nil。

func (*Application) Queue

func (app *Application) Queue(h Handler)

Queue 将一个任务投递到主循环中异步执行。

func (*Application) Quit

func (app *Application) Quit()

Quit 请求事件循环退出。可在任意 goroutine 中安全调用。

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) Blur

func (b *Base) Blur()

Blur 在组件失去焦点时被调用。

func (*Base) Contains

func (b *Base) Contains(px, py int) bool

Contains 判断点 (px, py) 是否落在组件矩形内部(含边界)。

func (*Base) Draw

func (b *Base) Draw(screen tcell.Screen)

Draw 默认不做任何绘制。

func (*Base) Focus

func (b *Base) Focus(focus func(cps Compents))

Focus 在组件获得焦点时被调用, 通常由 Application.SetFocus 触发。

func (*Base) Focusable

func (b *Base) Focusable() bool

Focusable 返回组件是否可以获得键盘焦点, 默认不可获得焦点。 需要接收键盘输入的组件(如 Input、Button)应覆盖此方法返回 true。

func (*Base) GetRect

func (b *Base) GetRect() (int, int, int, int)

GetRect 返回组件矩形 (x, y, w, h)。

func (*Base) HandlerInput

func (b *Base) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 默认不处理按键事件。

func (*Base) HandlerMouse

func (b *Base) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 默认不处理鼠标事件。

func (*Base) HasFocus

func (b *Base) HasFocus() bool

HasFocus 返回组件当前是否持有焦点。

func (*Base) SetRect

func (b *Base) SetRect(x, y, w, h int)

SetRect 设置组件矩形。

type Border

type Border struct {
	Base
	// contains filtered or unexported fields
}

Border 是为子组件绘制边框的容器组件。 边框占用的区域会从子组件的可用区域中扣除。

func NewBorder

func NewBorder(cps Compents) *Border

NewBorder 创建一个边框容器, 包裹指定的子组件。

func (*Border) Children

func (b *Border) Children() []Compents

Children 返回被包裹的子组件。

func (*Border) Draw

func (b *Border) Draw(screen tcell.Screen)

Draw 先绘制边框, 再在内部区域绘制子组件。

func (*Border) HandlerInput

func (b *Border) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 将按键事件转发给内部的子组件。

func (*Border) HandlerMouse

func (b *Border) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件转发给内部的子组件。

func (*Border) SetBorderWidth

func (b *Border) SetBorderWidth(width int) *Border

SetBorderWidth 设置边框线宽(字符数), 默认 1。

func (*Border) SetStyle

func (b *Border) SetStyle(style tcell.Style) *Border

SetStyle 设置边框样式。

func (*Border) SetTitle

func (b *Border) SetTitle(title string) *Border

SetTitle 设置显示在顶边框的标题。

type Button

type Button struct {
	Label
	// contains filtered or unexported fields
}

Button 是可点击的按钮组件, 内嵌 Label 以复用文本绘制能力。 按钮可以获得焦点, 支持鼠标点击与键盘(Enter / 空格)触发。 按钮文本默认水平居中, 可通过 SetTextAlign 修改对齐方式。

func NewButton

func NewButton() *Button

NewButton 创建一个新的按钮组件, 文本默认水平居中。

func (*Button) Draw

func (btn *Button) Draw(screen tcell.Screen)

Draw 绘制按钮。按钮获得焦点时使用高亮背景色。

func (*Button) Focusable

func (btn *Button) Focusable() bool

Focusable 按钮可以获得焦点。

func (*Button) GetClick

func (btn *Button) GetClick() func()

GetClick 返回当前点击回调。

func (*Button) HandlerInput

func (btn *Button) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件: 按下 Enter 或空格时触发点击回调。

func (*Button) HandlerMouse

func (btn *Button) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件:

  • 左键按下且位于按钮内部时请求焦点;
  • 左键松开且此前在按钮内部按下时触发点击回调。

func (*Button) SetClick

func (btn *Button) SetClick(click func()) *Button

SetClick 设置按钮的点击回调, 返回自身以便链式调用。

type Canvas

type Canvas struct {
	Base
	// contains filtered or unexported fields
}

Canvas 提供类似网页 Canvas 的 2D 绘图能力。使用者通过 DrawText / DrawLine / DrawRect / DrawCircle 等方法登记绘制指令, 在 Draw 阶段统一输出到屏幕。所有坐标均相对于画布左上角。

func NewCanvas

func NewCanvas() *Canvas

NewCanvas 创建一个新的画布组件。

func (*Canvas) Clear

func (c *Canvas) Clear() *Canvas

Clear 清空已登记的所有绘制指令。

func (*Canvas) Draw

func (c *Canvas) Draw(screen tcell.Screen)

Draw 将登记的所有绘制指令输出到屏幕, 超出画布区域的部分被裁剪。

func (*Canvas) DrawCircle

func (c *Canvas) DrawCircle(cx, cy, r int, ch rune, style tcell.Style) *Canvas

DrawCircle 使用中点圆算法登记一个圆绘制指令。

func (*Canvas) DrawLine

func (c *Canvas) DrawLine(x1, y1, x2, y2 int, ch rune, style tcell.Style) *Canvas

DrawLine 使用 Bresenham 算法登记一条直线绘制指令。

func (*Canvas) DrawRect

func (c *Canvas) DrawRect(x, y, w, h int, ch rune, style tcell.Style) *Canvas

DrawRect 登记一个矩形边框绘制指令。

func (*Canvas) DrawText

func (c *Canvas) DrawText(x, y int, text string, style tcell.Style) *Canvas

DrawText 登记一条文本绘制指令。

func (*Canvas) FillRect

func (c *Canvas) FillRect(x, y, w, h int, ch rune, style tcell.Style) *Canvas

FillRect 登记一个实心矩形绘制指令。

func (*Canvas) SetPixel

func (c *Canvas) SetPixel(x, y int, ch rune, style tcell.Style) *Canvas

SetPixel 登记一个像素(字符)绘制指令。

func (*Canvas) SetStyle

func (c *Canvas) SetStyle(style tcell.Style) *Canvas

SetStyle 设置默认绘图样式。

type CheckButton

type CheckButton struct {
	Base
	// contains filtered or unexported fields
}

CheckButton 是复选框组件, 支持选中与取消选中, 状态变化时触发回调。

func NewCheckButton

func NewCheckButton() *CheckButton

NewCheckButton 创建一个新的复选框。

func (*CheckButton) Draw

func (cb *CheckButton) Draw(screen tcell.Screen)

Draw 绘制复选框。选中时显示 [x], 未选中时显示 [ ]。 获得焦点时使用高亮背景色。

func (*CheckButton) Focusable

func (cb *CheckButton) Focusable() bool

Focusable 复选框可以获得焦点。

func (*CheckButton) GetChecked

func (cb *CheckButton) GetChecked() bool

GetChecked 返回复选框的选中状态。

func (*CheckButton) GetLabel

func (cb *CheckButton) GetLabel() string

GetLabel 返回复选框的文本。

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 设置选中状态变化时的回调。

func (*CheckButton) Toggle

func (cb *CheckButton) Toggle()

Toggle 翻转选中状态并触发回调。

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 FindAt

func FindAt(cps Compents, px, py int) Compents

FindAt 返回包含指定点 (px, py) 的最深层组件。

func FindFocused

func FindFocused(cps Compents) Compents

FindFocused 深度优先查找当前持有焦点的后代组件。 若组件自身持有焦点则直接返回该组件。

func Focusables

func Focusables(cps Compents) []Compents

Focusables 深度优先返回所有可获得焦点的后代组件(不含自身)。

type Container

type Container interface {
	Compents
	// Children 按绘制顺序返回所有直接子组件。
	Children() []Compents
}

Container 是由可以容纳子组件的容器实现的接口。 所有容器组件都应实现 Children, 以便焦点查找与事件分发。

type Dialog

type Dialog struct {
	Base
	// contains filtered or unexported fields
}

Dialog 是带边框的对话框组件, 包含标题、内容区域与底部按钮栏。 支持键盘与鼠标操作, 通常被放置在 Absolute 或 Grid 等容器中。

func NewDialog

func NewDialog() *Dialog

NewDialog 创建一个对话框。

func (*Dialog) AddButton

func (d *Dialog) AddButton(label string, onclick func()) *Button

AddButton 添加一个按钮到对话框底部, 返回创建的按钮以便设置回调。 无论按钮通过键盘还是鼠标触发, 点击回调执行后都会自动调用 Close(该按钮的下标), 保证 onclose 行为一致。

func (*Dialog) Children

func (d *Dialog) Children() []Compents

Children 返回对话框的内容组件与按钮。

func (*Dialog) Close

func (d *Dialog) Close(result int)

Close 关闭对话框并触发 onclose 回调。

func (*Dialog) Draw

func (d *Dialog) Draw(screen tcell.Screen)

Draw 绘制对话框的边框、标题、内容与按钮栏。

func (*Dialog) Focusable

func (d *Dialog) Focusable() bool

Focusable 对话框可以获得焦点。

func (*Dialog) HandlerInput

func (d *Dialog) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件:

  • Tab / Shift+Tab 在内容与按钮之间循环焦点;
  • 内容区持有焦点时, 方向键等按键优先转发给内容区;
  • 内容区无焦点时, 左右键切换按钮焦点, 回车触发按钮, Esc 关闭。

func (*Dialog) HandlerMouse

func (d *Dialog) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件分发给命中的按钮或内容。

func (*Dialog) SetColors

func (d *Dialog) SetColors(fg, bg tcell.Color) *Dialog

SetColors 设置对话框的前景色与背景色。

func (*Dialog) SetContent

func (d *Dialog) SetContent(cps Compents) *Dialog

SetContent 设置对话框内容组件。

func (*Dialog) SetOnClose

func (d *Dialog) SetOnClose(onclose func(result int)) *Dialog

SetOnClose 设置对话框关闭时的回调, 参数为触发关闭的按钮下标。

func (*Dialog) SetTitle

func (d *Dialog) SetTitle(title string) *Dialog

SetTitle 设置对话框标题。

type Direction

type Direction = int

Direction 表示布局方向。

const (
	// Horizontal 水平方向, 子组件从左到右排列。
	Horizontal Direction = iota
	// Vertical 垂直方向, 子组件从上到下排列。
	Vertical
)

type Form

type Form struct {
	Base
	// contains filtered or unexported fields
}

Form 是表单容器, 将多个带标签的字段按行垂直排列。 支持 Tab 在字段之间切换焦点, 回车触发提交回调。

func NewForm

func NewForm() *Form

NewForm 创建一个表单容器。

func (*Form) AddButton

func (f *Form) AddButton(label string, onclick func()) *Button

AddButton 在表单底部添加一个按钮, 返回创建的按钮以便设置回调。

func (*Form) AddCheckButton

func (f *Form) AddCheckButton(label string) *CheckButton

AddCheckButton 添加一行复选框, 返回创建的复选框以便配置。

func (*Form) AddField

func (f *Form) AddField(label string, widget Compents, height int) *Form

AddField 添加一行字段, height 为该行占用的行高。

func (*Form) AddInput

func (f *Form) AddInput(label string) *Input

AddInput 添加一行单行文本输入框, 返回创建的输入框以便配置。

func (*Form) AddSelect

func (f *Form) AddSelect(label string, options []string) *Select

AddSelect 添加一行下拉选择框, 返回创建的下拉框以便配置。

func (*Form) AddTextArea

func (f *Form) AddTextArea(label string, height int) *TextArea

AddTextArea 添加一行多行文本输入框, 返回创建的文本域以便配置。

func (*Form) Children

func (f *Form) Children() []Compents

Children 返回表单中的所有字段组件。

func (*Form) Draw

func (f *Form) Draw(screen tcell.Screen)

Draw 按行绘制每个字段: 左侧标签 + 右侧输入组件。

func (*Form) HandlerInput

func (f *Form) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件: 回车触发提交, 其余按键转交给字段。 当焦点位于按钮或已展开的选择框上时, 回车交由对应组件处理。

func (*Form) HandlerMouse

func (f *Form) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件分发给命中的字段。

func (*Form) SetLabelWidth

func (f *Form) SetLabelWidth(width int) *Form

SetLabelWidth 设置每个字段标签占用的宽度。

func (*Form) SetSubmit

func (f *Form) SetSubmit(onSubmit func()) *Form

SetSubmit 设置表单提交回调, 在无按钮持有焦点时按回车触发。

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 NewGrid

func NewGrid(cols, rows int) *Grid

NewGrid 创建一个 cols 列 x rows 行的网格容器。

func (*Grid) AddChild

func (g *Grid) AddChild(cps Compents) *Grid

AddChild 依次添加子组件, 数量超过单元格总数时多余部分被忽略。

func (*Grid) Children

func (g *Grid) Children() []Compents

Children 返回所有子组件。

func (*Grid) Draw

func (g *Grid) Draw(screen tcell.Screen)

Draw 计算单元格尺寸并将子组件绘制到对应单元格中。

func (*Grid) HandlerInput

func (g *Grid) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理容器的按键事件, 支持 Tab 切换子组件焦点。

func (*Grid) HandlerMouse

func (g *Grid) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件分发给命中的子组件。

func (*Grid) SetChild

func (g *Grid) SetChild(index int, cps Compents) *Grid

SetChild 将指定单元格位置上的子组件替换为新组件。 若 index 越界则不做任何操作。

type Handler

type Handler func()

Handler 是投递到应用主循环中执行的任务。

type Image

type Image struct {
	Base
	// contains filtered or unexported fields
}

func NewImage

func NewImage(mode ImageMode) *Image

func (*Image) Draw

func (img *Image) Draw(s tcell.Screen)

func (*Image) SetContent

func (img *Image) SetContent(src image.Image) *Image

type ImageMode

type ImageMode int
const (
	Stretch ImageMode = iota
	Proportionate
	CropFill
)

type Input

type Input struct {
	Base
	// contains filtered or unexported fields
}

Input 是单行文本输入框, 支持光标移动、字符插入与删除。

func NewInput

func NewInput() *Input

NewInput 创建一个新的文本输入框。

func (*Input) Draw

func (i *Input) Draw(screen tcell.Screen)

Draw 绘制输入框, 获得焦点时使用高亮背景, 光标位置使用反色显示。

func (*Input) Focusable

func (i *Input) Focusable() bool

Focusable 输入框可以获得焦点。

func (*Input) GetText

func (i *Input) GetText() string

GetText 返回当前输入框中的文本。

func (*Input) HandlerInput

func (i *Input) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件: 字符输入、退格、删除以及光标移动。

func (*Input) HandlerMouse

func (i *Input) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件: 点击输入框时请求焦点。

func (*Input) SetColors

func (i *Input) SetColors(fg, bg tcell.Color) *Input

SetColors 设置输入框的前景色与背景色。

func (*Input) SetMaxLen

func (i *Input) SetMaxLen(maxLen int) *Input

SetMaxLen 设置允许输入的最大字符数, 0 表示不限。

func (*Input) SetOnChange

func (i *Input) SetOnChange(onchange func(text string)) *Input

SetOnChange 设置文本变化时的回调。

func (*Input) SetPassword

func (i *Input) SetPassword(password bool) *Input

SetPassword 设置是否以掩码字符显示内容。

func (*Input) SetPlaceholder

func (i *Input) SetPlaceholder(placeholder string) *Input

SetPlaceholder 设置占位文本(文本为空时显示)。

func (*Input) SetText

func (i *Input) SetText(text string) *Input

SetText 设置输入框文本, 光标移动到末尾。

type Label

type Label struct {
	Base
	// contains filtered or unexported fields
}

Label 是最基础的文本显示组件, 支持背景填充、前景色以及对齐方式。 其他组件(如 Button)常常内嵌 Label 以获得文本绘制能力。

func NewLabel

func NewLabel() *Label

NewLabel 创建一个新的标签组件。

func (*Label) Draw

func (label *Label) Draw(screen tcell.Screen)

Draw 使用默认前景色与背景色绘制标签。

func (*Label) GetContent

func (label *Label) GetContent() string

GetContent 返回当前文本内容。

func (*Label) SetBackground

func (label *Label) SetBackground(bg tcell.Color) *Label

SetBackground 设置背景色并返回自身以便链式调用。

func (*Label) SetContent

func (label *Label) SetContent(content string) *Label

SetContent 设置标签文本并返回自身以便链式调用。

func (*Label) SetForce

func (label *Label) SetForce(fg tcell.Color) *Label

SetForce 设置前景色并返回自身以便链式调用。

func (*Label) SetTextAlign

func (label *Label) SetTextAlign(ali Align) *Label

SetTextAlign 设置文本水平对齐方式并返回自身以便链式调用。

type List

type List struct {
	Base
	// contains filtered or unexported fields
}

List 是可选择的列表组件, 支持键盘与鼠标选择, 内容超出时自动滚动。

func NewList

func NewList() *List

NewList 创建一个新的列表组件。

func (*List) AddItem

func (l *List) AddItem(text string) *List

AddItem 向列表末尾追加一项。

func (*List) Draw

func (l *List) Draw(screen tcell.Screen)

Draw 绘制列表, 获得焦点时使用高亮背景, 当前选中项以反色高亮。

func (*List) Focusable

func (l *List) Focusable() bool

Focusable 列表可以获得焦点。

func (*List) GetItems

func (l *List) GetItems() []string

GetItems 返回当前全部列表项。

func (*List) GetSelected

func (l *List) GetSelected() int

GetSelected 返回当前选中项的下标, 未选中时返回 -1。

func (*List) GetSelectedValue

func (l *List) GetSelectedValue() string

GetSelectedValue 返回当前选中项的文本。

func (*List) HandlerInput

func (l *List) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件: 上下键移动选择, 回车触发回调。

func (*List) HandlerMouse

func (l *List) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件: 点击选择列表项, 滚轮滚动。

func (*List) SetColors

func (l *List) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *List

SetColors 设置列表前景色、背景色与选中项样式。

func (*List) SetItems

func (l *List) SetItems(items []string) *List

SetItems 用给定的字符串切片替换全部列表项。

func (*List) SetOnChange

func (l *List) SetOnChange(onchange func(index int, value string)) *List

SetOnChange 设置选中项变化时的回调。

func (*List) SetSelected

func (l *List) SetSelected(index int) *List

SetSelected 选中指定下标的列表项, 下标越界时不做任何操作。

type Menu struct {
	// contains filtered or unexported fields
}

Menu 是菜单栏中的一个顶层菜单。

func NewMenu

func NewMenu(title string) *Menu

NewMenu 创建一个顶层菜单。

func (m *Menu) Add(label string, onclick func()) *Menu

Add 以文本与回调的方式向菜单添加一个条目。

func (m *Menu) AddItem(item *MenuItem) *Menu

AddItem 向菜单添加一个条目。

type MenuBar struct {
	Base
	// contains filtered or unexported fields
}

MenuBar 是顶部菜单栏组件, 支持鼠标与键盘操作。

func NewMenuBar

func NewMenuBar() *MenuBar

NewMenuBar 创建一个菜单栏。

func (mb *MenuBar) AddMenu(menu *Menu) *MenuBar

AddMenu 添加一个顶层菜单。

func (mb *MenuBar) Close()

Close 关闭当前展开的菜单。

func (mb *MenuBar) Draw(screen tcell.Screen)

Draw 绘制菜单栏与展开的菜单项。

func (mb *MenuBar) Focusable() bool

Focusable 菜单栏可以获得焦点。

func (mb *MenuBar) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件: 菜单展开时用方向键浏览, Enter 执行; 未展开时用左右键定位菜单。

func (mb *MenuBar) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件: 点击菜单展开, 点击菜单项执行。

func (mb *MenuBar) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *MenuBar

SetColors 设置菜单栏的前景色、背景色与高亮项样式。

type MenuItem struct {
	// contains filtered or unexported fields
}

MenuItem 是菜单中的一个条目, 支持子菜单。

func NewMenuItem

func NewMenuItem(label string, onclick func()) *MenuItem

NewMenuItem 创建一个菜单条目。

func (mi *MenuItem) AddSubItem(label string, onclick func()) *MenuItem

AddSubItem 向菜单条目添加一个子菜单条目。

type Padding

type Padding struct {
	Base
	// contains filtered or unexported fields
}

Padding 是一个容器组件, 为内部的子组件四周增加内边距。 内边距不会绘制任何内容, 仅负责压缩子组件的可用区域。

func NewPadding

func NewPadding(top, right, bottom, left int, cps Compents) *Padding

NewPadding 创建一个指定内边距的容器。参数顺序为: 上、右、下、左。

func NewUniformPadding

func NewUniformPadding(padding int, cps Compents) *Padding

NewUniformPadding 创建一个四周内边距相等的容器。

func (*Padding) Children

func (p *Padding) Children() []Compents

Children 返回被包裹的子组件。

func (*Padding) Draw

func (p *Padding) Draw(screen tcell.Screen)

Draw 将子组件矩形向内收缩内边距后绘制。

func (*Padding) HandlerInput

func (p *Padding) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 将按键事件转发给内部的子组件。

func (*Padding) HandlerMouse

func (p *Padding) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件转发给内部的子组件。

func (*Padding) SetPadding

func (p *Padding) SetPadding(top, right, bottom, left int) *Padding

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 NewPages

func NewPages() *Pages

NewPages 创建一个页面容器。

func (*Pages) AddPage

func (p *Pages) AddPage(name string, panel Compents) *Pages

AddPage 添加一个命名页面。

func (*Pages) Children

func (p *Pages) Children() []Compents

Children 返回当前页面组件。

func (*Pages) Draw

func (p *Pages) Draw(screen tcell.Screen)

Draw 绘制当前页面。

func (*Pages) GetCurrent

func (p *Pages) GetCurrent() string

GetCurrent 返回当前页面名称。

func (*Pages) HandlerInput

func (p *Pages) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 将按键事件转发给当前页面。

func (*Pages) HandlerMouse

func (p *Pages) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件转发给当前页面。

func (*Pages) RemovePage

func (p *Pages) RemovePage(name string) *Pages

RemovePage 移除指定名称的页面。

func (*Pages) SetOnChange

func (p *Pages) SetOnChange(onchange func(name string)) *Pages

SetOnChange 设置页面切换时的回调。

func (*Pages) SwitchTo

func (p *Pages) SwitchTo(name string) *Pages

SwitchTo 切换到指定名称的页面并触发回调。

type ProgressBar

type ProgressBar struct {
	Base
	// contains filtered or unexported fields
}

ProgressBar 是进度条组件, 通过 progress 表示进度(0.0 到 1.0)。

func NewProgressBar

func NewProgressBar() *ProgressBar

NewProgressBar 创建一个新的进度条组件。

func (*ProgressBar) Draw

func (pb *ProgressBar) Draw(screen tcell.Screen)

Draw 绘制进度条。

func (*ProgressBar) GetProgress

func (pb *ProgressBar) GetProgress() float64

GetProgress 返回当前进度。

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) Draw

func (r *Radio) Draw(screen tcell.Screen)

Draw 绘制单个选项: 选中时显示 (*), 未选中时显示 ( )。 获得焦点时使用高亮背景色。

func (*Radio) Focusable

func (r *Radio) Focusable() bool

Focusable 选项可以获得焦点。

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 NewRadioGroup

func NewRadioGroup() *RadioGroup

NewRadioGroup 创建一个新的单选按钮组。

func (*RadioGroup) Add

func (rg *RadioGroup) Add(label string) *Radio

Add 向组中添加一个选项并返回该选项, 以便链式设置样式。

func (*RadioGroup) Children

func (rg *RadioGroup) Children() []Compents

Children 返回组内的所有选项。

func (*RadioGroup) Draw

func (rg *RadioGroup) Draw(screen tcell.Screen)

Draw 按垂直方向依次绘制所有选项。

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 NewScrollView

func NewScrollView(cps Compents) *ScrollView

NewScrollView 创建一个滚动视口, 包裹给定的子组件。

func (*ScrollView) Children

func (sv *ScrollView) Children() []Compents

Children 返回被包裹的子组件。

func (*ScrollView) Draw

func (sv *ScrollView) Draw(screen tcell.Screen)

Draw 将子组件绘制到滚动视口中, 并绘制滚动条。

func (*ScrollView) GetContentSize

func (sv *ScrollView) GetContentSize() (int, int)

GetContentSize 返回内容区域尺寸。

func (*ScrollView) GetOffset

func (sv *ScrollView) GetOffset() (int, int)

GetOffset 返回当前滚动偏移。

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 NewSelect

func NewSelect() *Select

NewSelect 创建一个新的下拉选择组件。

func (*Select) Contains

func (s *Select) Contains(x, y int) bool

Contains 判断点是否落在组件矩形内, 展开状态下还包括下方的下拉列表区域。

func (*Select) Draw

func (s *Select) Draw(screen tcell.Screen)

Draw 绘制下拉框。获得焦点时使用高亮背景, 未展开时显示当前选中项, 展开时在其下方显示选项列表。

func (*Select) Focusable

func (s *Select) Focusable() bool

Focusable 下拉选择组件可以获得焦点。

func (*Select) GetOptions

func (s *Select) GetOptions() []string

GetOptions 返回全部选项。

func (*Select) GetSelected

func (s *Select) GetSelected() int

GetSelected 返回选中项下标, 未选中时返回 -1。

func (*Select) GetSelectedValue

func (s *Select) GetSelectedValue() string

GetSelectedValue 返回选中项的文本。

func (*Select) HandlerInput

func (s *Select) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件: 回车/空格展开或收起, 上下键在选项中移动。

func (*Select) HandlerMouse

func (s *Select) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件:

  • 未展开时点击组件展开下拉列表并请求焦点;
  • 展开时点击组件收起, 点击下拉选项完成选择。

func (*Select) IsOpen

func (s *Select) IsOpen() bool

IsOpen 返回下拉列表是否处于展开状态。

func (*Select) SetColors

func (s *Select) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *Select

SetColors 设置组件的前景色、背景色与选中项样式。

func (*Select) SetOnChange

func (s *Select) SetOnChange(onchange func(index int, value string)) *Select

SetOnChange 设置选中项变化时的回调。

func (*Select) SetOpenHeight

func (s *Select) SetOpenHeight(height int) *Select

SetOpenHeight 设置下拉列表展开时显示的最大行数。

func (*Select) SetOptions

func (s *Select) SetOptions(options []string) *Select

SetOptions 设置下拉选项列表。

func (*Select) SetSelected

func (s *Select) SetSelected(index int) *Select

SetSelected 选中指定下标的选项。

type Spinner

type Spinner struct {
	Base
	// contains filtered or unexported fields
}

Spinner 是加载指示器组件, 每隔固定间隔切换一个动画帧。

func NewSpinner

func NewSpinner() *Spinner

NewSpinner 创建一个新的加载指示器, 默认使用 | / - \\ 帧序列。

func (*Spinner) Advance

func (s *Spinner) Advance()

Advance 手动切换到下一个动画帧。

func (*Spinner) Draw

func (s *Spinner) Draw(screen tcell.Screen)

Draw 依据时间间隔自动切换动画帧并绘制到屏幕中央。

func (*Spinner) GetFrame

func (s *Spinner) GetFrame() rune

GetFrame 返回当前动画帧对应的字符。

func (*Spinner) SetFrames

func (s *Spinner) SetFrames(frames []rune) *Spinner

SetFrames 设置自定义动画帧序列。

func (*Spinner) SetStyle

func (s *Spinner) SetStyle(style tcell.Style) *Spinner

SetStyle 设置动画字符的样式。

type StackItem

type StackItem struct {
	// contains filtered or unexported fields
}

StackItem 是 StackPanel 中的一个子项, size 表示子组件在主轴方向占用的尺寸。

func NewStackItem

func NewStackItem(size int, cps Compents) StackItem

NewStackItem 创建一个栈式布局子项。

type StackPanel

type StackPanel struct {
	Base
	// contains filtered or unexported fields
}

StackPanel 是栈式布局容器, 将子组件沿水平或垂直方向依次排列。

func NewStackPanel

func NewStackPanel(direct Direction) *StackPanel

NewStackPanel 按指定方向创建一个栈式布局容器。

func (*StackPanel) Add

func (sp *StackPanel) Add(size int, cps Compents)

Add 将子项追加到容器末尾。

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) Draw

func (sp *StackPanel) Draw(screen tcell.Screen)

Draw 依据布局方向依次绘制所有子组件。

func (*StackPanel) GetItem

func (sp *StackPanel) GetItem(index int) StackItem

GetItem 返回指定下标的子项, 越界时返回零值。

func (*StackPanel) GetItemCount

func (sp *StackPanel) GetItemCount() int

GetItemCount 返回子项数量。

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 处理容器的鼠标事件, 将事件分发给命中的子组件。

func (*StackPanel) Remove

func (sp *StackPanel) Remove(index int)

Remove 移除指定下标的子项, 下标越界时不做任何操作。

type TabPanel

type TabPanel struct {
	Base
	// contains filtered or unexported fields
}

TabPanel 是选项卡容器, 顶部显示标签栏, 下方显示当前选项卡对应的内容。

func NewTabPanel

func NewTabPanel() *TabPanel

NewTabPanel 创建一个选项卡容器。

func (*TabPanel) AddTab

func (tp *TabPanel) AddTab(title string, panel Compents) *TabPanel

AddTab 添加一个选项卡, title 为标签文本。

func (*TabPanel) Children

func (tp *TabPanel) Children() []Compents

Children 返回当前选项卡的内容组件。

func (*TabPanel) Draw

func (tp *TabPanel) Draw(screen tcell.Screen)

Draw 绘制标签栏与当前选项卡的内容。

func (*TabPanel) GetPageContent

func (tp *TabPanel) GetPageContent(index int) Compents

GetPageContent 返回指定下标的页签内容组件, 下标越界时返回 nil。

func (*TabPanel) GetSelected

func (tp *TabPanel) GetSelected() int

GetSelected 返回当前选中的选项卡下标。

func (*TabPanel) HandlerInput

func (tp *TabPanel) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件:

  • Tab 在内容区组件之间循环焦点, Ctrl+Tab 切换选项卡;
  • 内容区持有焦点时, 方向键等按键优先转发给内容区;
  • 内容区无焦点时, 左右键切换选项卡。

func (*TabPanel) HandlerMouse

func (tp *TabPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件: 点击标签切换选项卡, 点击内容区转发给页面。

func (*TabPanel) Select

func (tp *TabPanel) Select(index int) *TabPanel

Select 选中指定下标的选项卡。

func (*TabPanel) SetColors

func (tp *TabPanel) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *TabPanel

SetColors 设置标签栏的前景色、背景色与选中项样式。

func (*TabPanel) SetOnChange

func (tp *TabPanel) SetOnChange(onchange func(index int)) *TabPanel

SetOnChange 设置选项卡切换时的回调。

type TextArea

type TextArea struct {
	Base
	// contains filtered or unexported fields
}

TextArea 是多行文本输入框, 支持换行、光标移动以及行内编辑。

func NewTextArea

func NewTextArea() *TextArea

NewTextArea 创建一个新的多行文本输入框。

func (*TextArea) Draw

func (ta *TextArea) Draw(screen tcell.Screen)

Draw 绘制文本区域, 获得焦点时使用高亮背景, 光标位置使用反色显示。

func (*TextArea) Focusable

func (ta *TextArea) Focusable() bool

Focusable 文本区域可以获得焦点。

func (*TextArea) GetText

func (ta *TextArea) GetText() string

GetText 返回完整的文本内容。

func (*TextArea) HandlerInput

func (ta *TextArea) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件。

func (*TextArea) HandlerMouse

func (ta *TextArea) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件: 点击文本区域时请求焦点, 并把光标移动到点击位置。

func (*TextArea) SetColors

func (ta *TextArea) SetColors(fg, bg tcell.Color) *TextArea

SetColors 设置文本区域的前景色与背景色。

func (*TextArea) SetOnChange

func (ta *TextArea) SetOnChange(onchange func(text string)) *TextArea

SetOnChange 设置文本变化时的回调。

func (*TextArea) SetText

func (ta *TextArea) SetText(text string) *TextArea

SetText 设置文本内容, 自动按换行符切分。

type TreeNode

type TreeNode struct {
	// contains filtered or unexported fields
}

TreeNode 是树形视图中的节点, 支持子节点与展开/折叠状态。

func NewTreeNode

func NewTreeNode(label string, children ...*TreeNode) *TreeNode

NewTreeNode 创建一个树节点。

func (*TreeNode) AddChild

func (n *TreeNode) AddChild(child *TreeNode) *TreeNode

AddChild 添加一个子节点并返回自身以便链式调用。

func (*TreeNode) GetChildren

func (n *TreeNode) GetChildren() []*TreeNode

GetChildren 返回节点的直接子节点。

func (*TreeNode) GetLabel

func (n *TreeNode) GetLabel() string

GetLabel 返回节点文本。

func (*TreeNode) IsExpanded

func (n *TreeNode) IsExpanded() bool

IsExpanded 返回节点是否展开。

func (*TreeNode) SetExpanded

func (n *TreeNode) SetExpanded(expanded bool) *TreeNode

SetExpanded 设置节点的展开状态。

type TreeView

type TreeView struct {
	Base
	// contains filtered or unexported fields
}

TreeView 是树形视图组件, 支持键盘与鼠标浏览和展开折叠节点。

func NewTreeView

func NewTreeView() *TreeView

NewTreeView 创建一个树形视图。

func (*TreeView) Draw

func (tv *TreeView) Draw(screen tcell.Screen)

Draw 按缩进层级绘制可见节点, 获得焦点时使用高亮背景。

func (*TreeView) Focusable

func (tv *TreeView) Focusable() bool

Focusable 树形视图可以获得焦点。

func (*TreeView) GetRoot

func (tv *TreeView) GetRoot() *TreeNode

GetRoot 返回根节点。

func (*TreeView) GetSelected

func (tv *TreeView) GetSelected() *TreeNode

GetSelected 返回当前选中节点。

func (*TreeView) HandlerInput

func (tv *TreeView) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理按键事件: 上下键移动选中, 左右键展开/折叠。

func (*TreeView) HandlerMouse

func (tv *TreeView) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 处理鼠标事件: 点击节点选中, 双击展开/折叠。

func (*TreeView) SetColors

func (tv *TreeView) SetColors(fg, bg tcell.Color, selStyle tcell.Style) *TreeView

SetColors 设置树形视图的前景色、背景色与选中项样式。

func (*TreeView) SetOnChange

func (tv *TreeView) SetOnChange(onchange func(node *TreeNode)) *TreeView

SetOnChange 设置选中节点变化时的回调。

func (*TreeView) SetRoot

func (tv *TreeView) SetRoot(root *TreeNode) *TreeView

SetRoot 设置根节点。

type WrapItem

type WrapItem struct {
	// contains filtered or unexported fields
}

WrapItem 是 WrapPanel 中的一个子项, w 与 h 表示该子项占用的尺寸。

func NewWrapItem

func NewWrapItem(w, h int, cps Compents) WrapItem

NewWrapItem 创建一个流式子项。

type WrapPanel

type WrapPanel struct {
	Base
	// contains filtered or unexported fields
}

WrapPanel 是流式布局容器。水平方向时, 子项从左到右排列, 一行放不下时自动换行; 垂直方向时依次向下排列, 列放不下时自动换列。

func NewWrapPanel

func NewWrapPanel() *WrapPanel

NewWrapPanel 创建一个水平流式布局容器。

func NewWrapPanelV

func NewWrapPanelV() *WrapPanel

NewWrapPanelV 创建一个垂直流式布局容器。

func (*WrapPanel) Add

func (wp *WrapPanel) Add(w, h int, cps Compents) *WrapPanel

Add 添加一个指定尺寸的子项。

func (*WrapPanel) Children

func (wp *WrapPanel) Children() []Compents

Children 返回所有子组件。

func (*WrapPanel) Draw

func (wp *WrapPanel) Draw(screen tcell.Screen)

Draw 依据布局方向绘制所有子组件。

func (*WrapPanel) HandlerInput

func (wp *WrapPanel) HandlerInput() func(event *tcell.EventKey, focus func(cps Compents))

HandlerInput 处理容器的按键事件, 支持 Tab 切换子组件焦点。

func (*WrapPanel) HandlerMouse

func (wp *WrapPanel) HandlerMouse() func(event *tcell.EventMouse, focus func(cps Compents))

HandlerMouse 将鼠标事件分发给命中的子组件。

func (*WrapPanel) SetSpacing

func (wp *WrapPanel) SetSpacing(spacing int) *WrapPanel

SetSpacing 设置子项之间的间距。

Directories

Path Synopsis
Command example 是一个完整的待办事项(todo)应用示例, 演示 tcompents 组件库在真实项目中的用法: 数据模型、JSON 持久化、事件回调、模态对话框、 菜单栏与进度反馈等。
Command example 是一个完整的待办事项(todo)应用示例, 演示 tcompents 组件库在真实项目中的用法: 数据模型、JSON 持久化、事件回调、模态对话框、 菜单栏与进度反馈等。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL