Terminal.Gui.NET 跨平台终端 UI 工具包

联合创作 · 2023-09-28

Terminal.Gui 是适用于 .NET 的跨平台终端 UI 工具包。

特性

  • 跨平台:支持 Windows、Mac 和 Linux。Curses、Windows 控制台和 .NET 控制台的终端驱动程序意味着应用程序在彩色和单色终端上都能正常运行。
  • 键盘和鼠标输入:支持键盘和鼠标输入,包括拖放支持。
  • 灵活布局:支持绝对布局和创新的计算布局系统 (Computed Layout)。Computed Layout 使控件之间的相对布局变得容易,并支持动态终端 UI。
  • 支持剪贴板:剪切、复制和粘贴通过Clipboard类提供的文本。
  • 任意视图:所有可见的 UI 元素都是View类的子类,而这些子类又可以包含任意数量的 sub-views。
  • 高级应用程序功能:主循环支持处理事件、空闲处理程序、计时器和监控文件描述符。大多数类对于 threading 是安全的。
  • 响应式扩展 (Reactive Extensions):使用响应式扩展并受益于提高的代码可读性,以及应用 MVVM 模式和 ReactiveUI 数据绑定的能力。

示例代码

using Terminal.Gui;
using NStack;

Application.Init();
var top = Application.Top;

// Creates the top-level window to show
var win = new Window("MyApp")
{
	X = 0,
	Y = 1, // Leave one row for the toplevel menu

	// By using Dim.Fill(), it will automatically resize without manual intervention
	Width = Dim.Fill(),
	Height = Dim.Fill()
};

top.Add(win);

// Creates a menubar, the item "New" has a help menu.
var menu = new MenuBar(new MenuBarItem[] {
			new MenuBarItem ("_File", new MenuItem [] {
				new MenuItem ("_New", "Creates new file", null),
				new MenuItem ("_Close", "",null),
				new MenuItem ("_Quit", "", () => { if (Quit ()) top.Running = false; })
			}),
			new MenuBarItem ("_Edit", new MenuItem [] {
				new MenuItem ("_Copy", "", null),
				new MenuItem ("C_ut", "", null),
				new MenuItem ("_Paste", "", null)
			})
		});
top.Add(menu);

static bool Quit()
{
	var n = MessageBox.Query(50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
	return n == 0;
}

var login = new Label("Login: ") { X = 3, Y = 2 };
var password = new Label("Password: ")
{
	X = Pos.Left(login),
	Y = Pos.Top(login) + 1
};
var loginText = new TextField("")
{
	X = Pos.Right(password),
	Y = Pos.Top(login),
	Width = 40
};
var passText = new TextField("")
{
	Secret = true,
	X = Pos.Left(loginText),
	Y = Pos.Top(password),
	Width = Dim.Width(loginText)
};

// Add some controls, 
win.Add(
	// The ones with my favorite layout system, Computed
	login, password, loginText, passText,

	// The ones laid out like an australopithecus, with Absolute positions:
	new CheckBox(3, 6, "Remember me"),
	new RadioGroup(3, 8, new ustring[] { "_Personal", "_Company" }, 0),
	new Button(3, 14, "Ok"),
	new Button(10, 14, "Cancel"),
	new Label(3, 18, "Press F9 or ESC plus 9 to activate the menubar")
);

Application.Run();
Application.Shutdown();

上面的示例显示了使用 Terminal.Gui 支持的两种布局样式添加视图:绝对布局和计算布局。

浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报