// StellarX 星垣GUI框架 - 测试用例
#include"StellarX.h"
#define KEY 4

#if 1 == KEY
int main()
{
	//StellarX::SxLogger::setGBK();
	//StellarX::SxLogger::Get().enableFile("stellarx.log", false, 1024);
	StellarX::SxLogger::Get().enableConsole(true);
	StellarX::SxLogger::Get().setMinLevel(StellarX::SxLogLevel::Debug);   // Info/Debug/Trace 自己切
	StellarX::SxLogger::Get().setLanguage(StellarX::SxLogLanguage::ZhCN); // ZhCN / EnUS
	
	Window mainWindow(1200, 400, 1, RGB(255, 0, 0), "StellarX Hello");
	auto table = std::make_unique<Table>(200, 50);
	Table* table_ptr = table.get();
	table->setHeaders({ "name","age","seorc","home" });
	table->setData({
		{"zhangsan","20","99.99","wadsacafadsawd"},
		{"lisi","20","99.99","wadsacafadsawd"},
		{"wangwu","20","99.99","wadsacafadsawd"},
		{"zhaoliu","20","99.99","wadsacafadsawd"},
		{"1","20","99.99","wadsacafadsawd" },
		{"2","20","99.99","wadsacafadsawd" } ,
		{"3","20","99.99","wadsacafadsawd" } ,
		{"4","20","99.99","wadsacafadsawd" },
		{"5","20","99.99","wadsacafadsawd" } ,
		{"6","20","99.99","wadsacafadsawd" } ,
		{"7","20","99.99","wadsacvvvvvvv我afads" } ,
		{"8","2000000000000000000","99.999999999999","wadsacafa0000000d0000000sawd" } ,
		{"9555555555","2000","99.9999999","wadsacafadsawd" } });
	table->setRowsPerPage(4);
	table->setTableBorderWidth(1);
	table->textStyle.color = RGB(255, 0, 0);
	table->setTableBorder(RGB(255, 0, 0));
	table->setTableFillMode(StellarX::FillMode::Null);
	
	auto but = std::make_unique<Button>(0, 0, 100, 15, "按钮1");
	auto ta = std::make_unique<TabControl>(10, 10, 1100, 300);
	ta->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
	ta->setActiveIndex(2);

	std::pair<std::unique_ptr<Button>, std::unique_ptr<Canvas>> p;
	std::pair<std::unique_ptr<Button>, std::unique_ptr<Canvas>> p1;
	std::pair<std::unique_ptr<Button>, std::unique_ptr<Canvas>> p2;
	p2.first = std::make_unique<Button>(0, 0, 100, 15, "按钮3");
	p2.second = std::make_unique<Canvas>(200, 200, 1100, 280);
	p2.second->setShape(StellarX::ControlShape::ROUND_RECTANGLE);

	p1.first = std::make_unique<Button>(0, 0, 100, 15, "按钮2");
	p1.second = std::make_unique<Canvas>(200, 200, 1100, 280);
	p1.second->setCanvasBkColor(RGB(255, 200, 0));
	p.first = std::move(but);
	p.second = std::make_unique<Canvas>(100, 100, 1100, 280);
	p.second->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
	p.second->setCanvasBkColor(RGB(0, 255, 0));
	p.second->setCanvasfillMode(StellarX::FillMode::Null);
	auto table_get = table.get();
	p.second->addControl(std::move(table));
	ta->setTabPlacement(StellarX::TabPlacement::Bottom);

	auto b = std::make_unique<Button>(0, 0, 100, 35, "按钮");
	b->setOnClickListener([&]()
		{
			/*	StellarX::MessageBox::showAsync(mainWindow, "韦世豪大傻逼", "笑话", StellarX::MessageBoxType::AbortRetryIgnore,
			[](StellarX::MessageBoxResult result) { if (StellarX::MessageBoxResult::Retry == result)std::cout << "\a"; });
			*/
			table_ptr->clearHeaders();
			table_ptr->clearData();
			table_ptr->setHeaders({ "new_name","new_age","new_seorc","new""_home","new_test"});
			table_ptr->setData({
			{"new_zhangsan","30","88.88","new_wadsacafadsawd","123"},
			{"new_lisi","30","88.88","new_wadsacafadsawd","456"},
			{"new_wangwu","30","88.88","new_wadsacafadsawd","789"},
			{"new_zhaoliu","30","88.88","new_wadsacafadsawd","101"} });
			table_ptr->setData({"test_zhaoliu","test_30","88.88","test_wadsacafadsawd","test_101"});
				
				
		});
	p.second->addControl(std::move(b));
	ta->add(std::move(p));
	ta->add(std::move(p1));
	ta->add(std::move(p2));
	ta->setTabBarHeight(20);
	ta->setCanvasfillMode(StellarX::FillMode::Null);
	auto ta_ptr = ta.get();
	auto test = std::make_unique<Button>(1100, 350, 40, 40, "test");
	auto test_ptr = test.get();
	test->setOnClickListener([&]() 
		{
			ta_ptr->setActiveIndex(0);
		});
	//auto testTextBox = std::make_unique<TextBox>(100, 350, 80, 30);
	auto testTextBox = std::make_unique<TextBox>(0, 0, 80, 30);
	testTextBox->setText("hello");
	auto _123 = std::make_unique<Canvas>(100, 340, 180, 130);
	_123->addControl(std::move(testTextBox));
	_123->setCanvasfillMode(StellarX::FillMode::Null);
	
	mainWindow.addControl(std::move(ta));
	mainWindow.addControl(std::move(test));
	mainWindow.addControl(std::move(_123));

	mainWindow.draw("D:\\编程相关\\素材\\星垣logo.png");
	mainWindow.runEventLoop();
}
#endif

#if 4 == KEY

#include"StellarX.h"
#include <vector>

int main()
{
	StellarX::SxLogger::setGBK();
	StellarX::SxLogger::Get().enableConsole(true);
	StellarX::SxLogger::Get().setMinLevel(StellarX::SxLogLevel::Debug);
	StellarX::SxLogger::Get().setLanguage(StellarX::SxLogLanguage::ZhCN);

	Window win(1280, 780, 1, RGB(242, 246, 250), "StellarX 综合回归测试用例 4");

	auto infoPanel = std::make_unique<Canvas>(20, 20, 1240, 90);
	auto infoPanelPtr = infoPanel.get();
	infoPanelPtr->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	infoPanelPtr->setCanvasBkColor(RGB(225, 234, 244));
	infoPanelPtr->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
	infoPanelPtr->setAnchor(StellarX::Anchor::Left, StellarX::Anchor::Right);

	auto infoTitle = std::make_unique<Label>(20, 10, "综合回归测试 4：对话框遮挡、快速 Hover、分页按钮、模态 Resize");
	infoTitle->textStyle.nHeight = 26;
	infoTitle->setTextdisap(true);
	auto infoLine1 = std::make_unique<Label>(20, 42, "1. 点击右侧按钮打开非模态/模态对话框；2. 在顶部按钮、页签、分页按钮附近快速划动光标。");
	infoLine1->setTextdisap(true);
	auto infoLine2 = std::make_unique<Label>(20, 62, "3. 模态对话框打开后拖动窗口大小；4. 关闭对话框后观察底层按钮 Hover 是否立即恢复。");
	infoLine2->setTextdisap(true);

	auto openAsyncButton = std::make_unique<Button>(930, 22, 130, 42, "打开非模态");
	auto openAsyncButtonPtr = openAsyncButton.get();
	openAsyncButtonPtr->enableTooltip(true);
	openAsyncButtonPtr->setTooltipText("打开长文本非模态对话框，覆盖顶部按钮 / 页签 / 分页区");

	auto openModalButton = std::make_unique<Button>(1080, 22, 130, 42, "打开模态");
	auto openModalButtonPtr = openModalButton.get();
	openModalButtonPtr->enableTooltip(true);
	openModalButtonPtr->setTooltipText("打开模态对话框并测试拖拽窗口后的重绘与关闭");

	infoPanelPtr->addControl(std::move(infoTitle));
	infoPanelPtr->addControl(std::move(infoLine1));
	infoPanelPtr->addControl(std::move(infoLine2));
	infoPanelPtr->addControl(std::move(openAsyncButton));
	infoPanelPtr->addControl(std::move(openModalButton));

	auto hoverCanvas = std::make_unique<Canvas>(20, 140, 1240, 100);
	auto hoverCanvasPtr = hoverCanvas.get();
	hoverCanvasPtr->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	hoverCanvasPtr->setCanvasBkColor(RGB(230, 238, 248));
	hoverCanvasPtr->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
	hoverCanvasPtr->setAnchor(StellarX::Anchor::Left, StellarX::Anchor::Right);

	auto hoverTitle = std::make_unique<Label>(20, 8, "区域 A：打开非模态对话框后，请在下面 8 个按钮之间快速来回划动鼠标。");
	hoverTitle->setTextdisap(true);
	hoverCanvasPtr->addControl(std::move(hoverTitle));

	for (int i = 0; i < 8; ++i)
	{
		auto hoverButton = std::make_unique<Button>(250 + i * 92, 38, 72, 38, "B" + std::to_string(i + 1));
		hoverButton->enableTooltip(true);
		hoverButton->setTooltipText("顶部 Hover 按钮 " + std::to_string(i + 1));
		hoverButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
		hoverCanvasPtr->addControl(std::move(hoverButton));
	}

	auto tabControl = std::make_unique<TabControl>(120, 260, 1040, 180);
	auto tabControlPtr = tabControl.get();
	tabControlPtr->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	tabControlPtr->setCanvasfillMode(StellarX::FillMode::Null);
	tabControlPtr->setTabPlacement(StellarX::TabPlacement::Top);
	tabControlPtr->setTabBarHeight(28);
	tabControlPtr->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
	tabControlPtr->setAnchor(StellarX::Anchor::Left, StellarX::Anchor::Right);

	auto page1 = std::make_unique<Canvas>(0, 0, 1040, 152);
	page1->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	page1->setCanvasBkColor(RGB(246, 250, 255));
	auto page2 = std::make_unique<Canvas>(0, 0, 1040, 152);
	page2->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	page2->setCanvasBkColor(RGB(250, 248, 242));
	auto page3 = std::make_unique<Canvas>(0, 0, 1040, 152);
	page3->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	page3->setCanvasBkColor(RGB(243, 250, 244));

	auto tabPage1Ptr = page1.get();
	auto tabPage2Ptr = page2.get();
	auto tabPage3Ptr = page3.get();

	tabControlPtr->add(std::make_pair(std::make_unique<Button>(0, 0, 120, 28, "页签-事件区"), std::move(page1)));
	tabControlPtr->add(std::make_pair(std::make_unique<Button>(0, 0, 120, 28, "页签-空白区"), std::move(page2)));
	tabControlPtr->add(std::make_pair(std::make_unique<Button>(0, 0, 120, 28, "页签-辅助区"), std::move(page3)));
	tabControlPtr->setActiveIndex(0);

	auto page1Label = std::make_unique<Label>(20, 10, "区域 B：打开非模态对话框后，切换页签并在页内按钮上快速 Hover。");
	page1Label->setTextdisap(true);
	tabPage1Ptr->addControl(std::move(page1Label));
	for (int i = 0; i < 4; ++i)
	{
		auto pageButton = std::make_unique<Button>(190 + i * 150, 55, 110, 38, "页内按钮" + std::to_string(i + 1));
		pageButton->enableTooltip(true);
		pageButton->setTooltipText("页签区按钮 " + std::to_string(i + 1));
		tabPage1Ptr->addControl(std::move(pageButton));
	}

	auto page2Label = std::make_unique<Label>(30, 55, "区域 B-2：此页保留较大空白区，用来观察 Hover 离开空白区域时的恢复。");
	page2Label->setTextdisap(true);
	tabPage2Ptr->addControl(std::move(page2Label));

	auto page3Label = std::make_unique<Label>(20, 18, "区域 B-3：辅助页，用来测试页签切换本身的重绘是否稳定。");
	page3Label->setTextdisap(true);
	auto page3Button = std::make_unique<Button>(420, 65, 180, 40, "辅助按钮");
	page3Button->enableTooltip(true);
	page3Button->setTooltipText("辅助页按钮 Tooltip");
	tabPage3Ptr->addControl(std::move(page3Label));
	tabPage3Ptr->addControl(std::move(page3Button));

	auto table = std::make_unique<Table>(240, 470);
	auto tablePtr = table.get();
	tablePtr->setHeaders({ "编号", "名称", "状态", "备注" });
	tablePtr->setData({
		{"01", "顶部按钮覆盖测试", "待验证", "快速 Hover 后不能残留高亮"},
		{"02", "页签切换测试", "待验证", "页签按钮与页内容不应穿透"},
		{"03", "分页按钮测试", "待验证", "被非模态部分覆盖时仍应稳定"},
		{"04", "模态 Resize 测试", "待验证", "拖窗后标题、关闭按钮不应残影"},
		{"05", "关闭后 Hover 恢复", "待验证", "模态关闭后底层按钮 Hover 应恢复"}
		});
	tablePtr->setRowsPerPage(2);
	tablePtr->setTableBorderWidth(1);
	tablePtr->setTableBorder(RGB(60, 90, 120));
	tablePtr->setTableFillMode(StellarX::FillMode::Null);
	tablePtr->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
	tablePtr->setAnchor(StellarX::Anchor::Left, StellarX::Anchor::Right);

	const std::string asyncText =
		"KEY4 非模态回归测试正在进行。\n"
		"请依次检查以下场景：\n"
		"1. 在顶部按钮之间快速来回移动鼠标。\n"
		"2. 切换页签，并在页签按钮与页内按钮上快速 Hover。\n"
		"3. 将鼠标移动到表格分页按钮附近，观察是否闪烁或穿透。\n"
		"4. 将鼠标移出窗口后再移回窗口。\n"
		"5. 保持本对话框打开，确认它始终位于最上层。\n"
		"6. 若需要重复测试，可直接再次点击右上角按钮。\n"
		"7. 本对话框故意较高，以覆盖顶部按钮、页签区与部分分页区。\n"
		"8. 顶部按钮区域用于检测快速 Hover 残留。\n"
		"9. 页签区域用于检测被遮挡时的切换与 Hover。\n"
		"10. 表格分页区用于检测分页按钮被部分覆盖时的重绘。\n"
		"11. 若关闭本对话框，请观察底层 Hover 是否立即恢复。\n"
		"12. 本消息框保留较多行，是为了增大对遮挡场景的覆盖范围。";

	const std::string modalText =
		"KEY4 模态回归测试。\n"
		"请在本对话框保持打开时执行：\n"
		"1. 拖拽窗口大小，观察标题、关闭按钮与功能按钮是否残影。\n"
		"2. 拖拽后点击关闭按钮。\n"
		"3. 拖拽后点击功能按钮。\n"
		"4. 对话框关闭后，将鼠标停在底层顶部按钮上，观察 Hover 是否恢复。";

	openAsyncButtonPtr->setOnClickListener([&win, asyncText]()
		{
			StellarX::MessageBox::showAsync(
				win,
				asyncText,
				"KEY4 非模态综合回归",
				StellarX::MessageBoxType::YesNoCancel,
				[](StellarX::MessageBoxResult result)
				{
					SX_LOGI("KEY4") << "非模态对话框结果=" << (int)result;
				});
		});

	openModalButtonPtr->setOnClickListener([&win, modalText]()
		{
			auto result = StellarX::MessageBox::showModal(
				win,
				modalText,
				"KEY4 模态综合回归",
				StellarX::MessageBoxType::OKCancel);
			SX_LOGI("KEY4") << "模态对话框结果=" << (int)result;
		});

	win.addControl(std::move(infoPanel));
	win.addControl(std::move(hoverCanvas));
	win.addControl(std::move(tabControl));
	win.addControl(std::move(table));

	win.draw();
	return win.runEventLoop();
}

#endif

#if 2 == KEY
// 本工具基于 StellarX 构建，轻量级的 Windows GUI 框架。
#include"StellarX.h"
#include <sstream>
#include<iomanip>
#include<array>

auto   blackColor = RGB(202, 255, 255);
char initData[33] = "00000000000000000000000000000000";//初始数据
bool gSigned = false;      //是否为有符号数
int main()
{
	StellarX::SxLogger::Get().enableConsole(true);
	StellarX::SxLogger::Get().setMinLevel(StellarX::SxLogLevel::Debug);   // Info/Debug/Trace 自己切
	StellarX::SxLogger::Get().setLanguage(StellarX::SxLogLanguage::ZhCN); // ZhCN / EnUS

	Window mainWindow(700, 510, 1, RGB(255, 255, 255), "寄存器查看工具 V1.0——我在人间做废物                         (同类工具定制：3150131407(Q / V))");

	//选择区控件
	auto selectionAreaLabel = std::make_unique<Label>(18, 0, "32位选择区");
	selectionAreaLabel->setTextdisap(true);
	std::vector<std::unique_ptr<Label>>selectionAreaButtonLabel;
	std::vector<std::unique_ptr<Button>>selectionAreaButton;
	std::vector<Button*>selectionAreaButton_ptr;
	auto  selectionArea = std::make_unique <Canvas>(10, 10, 680, 150);

	selectionArea->setCanvasBkColor(blackColor);
	selectionArea->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);

	for (int y = 0; y < 2; y++)
	{
		std::ostringstream os;
		for (int x = 0; x < 16; x++)
		{
			if (0 == y)
			{
				selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 25 + 28 * (x / 4), 26, "", RGB(208, 208, 208)));
				os << std::setw(2) << std::setfill('0') << 31 - x;
				selectionAreaButtonLabel.back()->setText(os.str());
				selectionAreaButtonLabel.back()->setTextdisap(true);

				selectionAreaButton.push_back(
					std::make_unique<Button>(x * 35 + 27 + 28 * (x / 4), 58, 25, 30, "0",
						blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE));
				selectionAreaButton.back()->textStyle.color = RGB(226, 116, 152);
				selectionAreaButton.back()->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
				selectionAreaButton_ptr.push_back(selectionAreaButton.back().get());
				int k = 32 - x - 1;
				//选择区按钮被点击后在二进制0和1之间切换，并更新initData
				selectionAreaButton_ptr.back()->setOnToggleOnListener([k, btn = selectionAreaButton_ptr.back()]()
					{
						btn->setButtonText("1");
						initData[k] = '1';
					});
				selectionAreaButton_ptr.back()->setOnToggleOffListener([k, btn = selectionAreaButton_ptr.back()]()
					{
						btn->setButtonText("0");
						initData[k] = '0';
					});
			}
			else
			{
				selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 25 + 28 * (x / 4), 90, "", RGB(208, 208, 208)));
				os << std::setw(2) << std::setfill('0') << 15 - x;
				selectionAreaButtonLabel.back()->setText(os.str());
				selectionAreaButtonLabel.back()->setTextdisap(true);

				selectionAreaButton.push_back(
					std::make_unique<Button>(x * 35 + 27 + 28 * (x / 4), 120, 25, 30, "0",
						blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE));
				selectionAreaButton.back()->textStyle.color = RGB(226, 116, 152);
				selectionAreaButton.back()->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
				selectionAreaButton_ptr.push_back(selectionAreaButton.back().get());
				int k = 15 - x;
				selectionAreaButton.back()->setOnToggleOnListener([k, btn = selectionAreaButton_ptr.back()]()
					{
						btn->setButtonText("1");
						initData[k] = '1';
					});
				selectionAreaButton.back()->setOnToggleOffListener([k, btn = selectionAreaButton_ptr.back()]()
					{
						btn->setButtonText("0");
						initData[k] = '0';
					});
			}
			os.str("");
			os.clear();
		}
	}
	selectionAreaLabel->setAnchor(StellarX::Anchor::Right, StellarX::Anchor::Left);
	selectionAreaLabel->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
	
	selectionArea->addControl(std::move(selectionAreaLabel));
	for (auto& s : selectionAreaButton)
	{
		s->setAnchor(StellarX::Anchor::Right, StellarX::Anchor::Left);
		s->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
		selectionArea->addControl(std::move(s));
	}
	for (auto& s : selectionAreaButtonLabel)
	{
		s->setAnchor(StellarX::Anchor::Right, StellarX::Anchor::Left);
		s->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
		selectionArea->addControl(std::move(s));
	}
	//功能区控件
	//功能区总容器
	auto function = std::make_unique<Canvas>(10, 170, 680, 70);
	function->setCanvasfillMode(StellarX::FillMode::Null);
	function->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
	function->setCanvasBkColor(blackColor);
	auto bitInvert_que = std::make_unique<Canvas>(0, 0, 220, 70);
	auto leftShift_que = std::make_unique<Canvas>(230, 0, 220, 70);
	auto rightShift_que = std::make_unique<Canvas>(460, 0, 220, 70);

	auto bitInvert = bitInvert_que.get();
	auto leftShift = leftShift_que.get();
	auto rightShift = rightShift_que.get();

	bitInvert->setCanvasBkColor(blackColor);
	bitInvert->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
	leftShift->setCanvasBkColor(blackColor);
	leftShift->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
	rightShift->setCanvasBkColor(blackColor);
	rightShift->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);

	function->addControl(std::move(bitInvert_que));
	function->addControl(std::move(leftShift_que));
	function->addControl(std::move(rightShift_que));

	auto bitInvertLabel = std::make_unique<Label>(13, -10, "位取反");
	bitInvertLabel->setTextdisap(true);
	auto leftShiftLabel = std::make_unique<Label>(13, -10, "左移位");
	leftShiftLabel->setTextdisap(true);
	auto rightShiftLabel = std::make_unique<Label>(13, -10, "右移位");
	rightShiftLabel->setTextdisap(true);

	// ====== 公用小工具======
	auto clamp = [](int v, int lo, int hi) { return v < lo ? lo : (v > hi ? hi : v); };
	auto toInt = [](const std::string& s, int def = 0) {
		try { return std::stoi(s); }
		catch (...) { return def; }
		};
	// bit号(31..0) -> selectionAreaButton下标(0..31)
	auto vecIndexFromBit = [](int bit) { return 31 - bit; };

	// 读取当前32位点击态
	auto snapshotBits = [&]() {
		std::array<bool, 32> a{};
		for (int b = 0; b < 32; ++b)
			a[b] = selectionAreaButton_ptr[vecIndexFromBit(b)]->isClicked();
		return a;
		};

	// 应用目标态：仅当不同才 setButtonClick
	auto applyBits = [&](const std::array<bool, 32>& a) {
		for (int b = 0; b < 32; ++b) {
			auto btn = selectionAreaButton_ptr[vecIndexFromBit(b)];
			if (btn->isClicked() != a[b]) btn->setButtonClick(a[b]);
		}
		};

	//取反区控件
	std::array<std::unique_ptr<Label>, 4> bitInvertFunctionLabel;
	bitInvertFunctionLabel[0] = std::make_unique<Label>(30, 10, "低位");
	bitInvertFunctionLabel[1] = std::make_unique<Label>(90, 10, "高位");
	bitInvertFunctionLabel[2] = std::make_unique<Label>(15, 38, "从");
	bitInvertFunctionLabel[3] = std::make_unique<Label>(75, 38, "到");

	std::array<std::unique_ptr<TextBox>, 2> bitInvertFunctionTextBox;
	bitInvertFunctionTextBox[0] = std::make_unique<TextBox>(35, 35, 35, 30, "0");
	bitInvertFunctionTextBox[1] = std::make_unique<TextBox>(95, 35, 35, 30, "0");
	auto invL = bitInvertFunctionTextBox[0].get();
	auto invH = bitInvertFunctionTextBox[1].get();
	auto bitInvertFunctionButton = std::make_unique<Button>(135, 35, 80, 30, "位取反",
		blackColor, RGB(171, 196, 220));
	bitInvertFunctionButton->textStyle.color = RGB(226, 116, 152);
	bitInvertFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
	auto bitInvertFunctionButton_ptr = bitInvertFunctionButton.get();

	bitInvert->addControl(std::move(bitInvertFunctionButton));
	bitInvert->addControl(std::move(bitInvertLabel));
	for (auto& b : bitInvertFunctionTextBox)
	{
		b->setMaxCharLen(3);
		b->textStyle.color = RGB(226, 116, 152);
		b->setTextBoxBk(RGB(244, 234, 142));
		b->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
		bitInvert->addControl(std::move(b));
	}
	for (auto& b : bitInvertFunctionLabel)
	{
		b->setTextdisap(true);
		bitInvert->addControl(std::move(b));
	}
	//左移控件
	auto leftShiftFunctionLabel = std::make_unique<Label>(198, 30, "位");
	leftShiftFunctionLabel->setTextdisap(true);

	auto leftShiftFunctionTextBox = std::make_unique<TextBox>(90, 30, 100, 30, "0");
	leftShiftFunctionTextBox->setMaxCharLen(3);
	leftShiftFunctionTextBox->textStyle.color = RGB(226, 116, 152);
	leftShiftFunctionTextBox->setTextBoxBk(RGB(244, 234, 142));
	leftShiftFunctionTextBox->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
	auto shlBox = leftShiftFunctionTextBox.get();
	auto leftShiftFunctionButton = std::make_unique<Button>(15, 30, 60, 30, "左移",
		blackColor, RGB(171, 196, 220));
	leftShiftFunctionButton->textStyle.color = RGB(226, 116, 152);
	leftShiftFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
	auto leftShiftFunctionButton_ptr = leftShiftFunctionButton.get();

	leftShift->addControl(std::move(leftShiftFunctionButton));
	leftShift->addControl(std::move(leftShiftFunctionTextBox));

	leftShift->addControl(std::move(leftShiftLabel));
	leftShift->addControl(std::move(leftShiftFunctionLabel));

	//右移控件
	auto rightShiftFunctionLabel = std::make_unique<Label>(198, 30, "位");
	rightShiftFunctionLabel->setTextdisap(true);
	auto rightShiftFunctionTextBox = std::make_unique<TextBox>(90, 30, 100, 30, "0");
	rightShiftFunctionTextBox->setMaxCharLen(3);
	rightShiftFunctionTextBox->textStyle.color = RGB(226, 116, 152);
	rightShiftFunctionTextBox->setTextBoxBk(RGB(244, 234, 142));
	rightShiftFunctionTextBox->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
	auto shrBox = rightShiftFunctionTextBox.get();

	auto rightShiftFunctionButton = std::make_unique<Button>(15, 30, 60, 30, "右移",
		blackColor, RGB(171, 196, 220));
	rightShiftFunctionButton->textStyle.color = RGB(226, 116, 152);
	rightShiftFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
	auto rightShiftFunctionButton_ptr = rightShiftFunctionButton.get();

	rightShift->addControl(std::move(rightShiftFunctionButton));

	rightShift->addControl(std::move(rightShiftFunctionTextBox));
	rightShift->addControl(std::move(rightShiftLabel));
	rightShift->addControl(std::move(rightShiftFunctionLabel));

	//显示区控件
	//数值显示
	auto NumericalDisplayArea = std::make_unique<Canvas>(10, 255, 680, 70);
	NumericalDisplayArea->setCanvasBkColor(blackColor);
	NumericalDisplayArea->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);

	std::array<std::unique_ptr<Label>, 3> NumericalDisplayAreaLabel;
	NumericalDisplayAreaLabel[0] = std::make_unique<Label>(18, -10, "数值显示区");
	NumericalDisplayAreaLabel[1] = std::make_unique<Label>(20, 25, "十六进制");
	NumericalDisplayAreaLabel[2] = std::make_unique<Label>(330, 25, "十进制");

	std::array<std::unique_ptr<TextBox>, 2> NumericalDisplayAreaTextBox;
	NumericalDisplayAreaTextBox[0] = std::make_unique<TextBox>(110, 25, 200, 30, "0");
	NumericalDisplayAreaTextBox[1] = std::make_unique<TextBox>(400, 25, 200, 30, "0");
	auto hex = NumericalDisplayAreaTextBox[0].get();
	auto dec = NumericalDisplayAreaTextBox[1].get();

	for (auto& b : NumericalDisplayAreaLabel)
	{
		b->setTextdisap(true);
		NumericalDisplayArea->addControl(std::move(b));
	}
	for (auto& b : NumericalDisplayAreaTextBox)
	{
		b->setMaxCharLen(11);
		b->textStyle.color = RGB(255, 69, 0);
		b->setTextBoxBk(RGB(141, 141, 141));
		b->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
		b->setMode(StellarX::TextBoxmode::READONLY_MODE);
		NumericalDisplayArea->addControl(std::move(b));
	}
	//二进制显示
	auto BinaryDisplayArea = std::make_unique<Canvas>(10, 335, 680, 110);
	BinaryDisplayArea->setCanvasBkColor(blackColor);
	BinaryDisplayArea->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);

	std::array<std::unique_ptr<Label>, 3> BinaryDisplayAreaLabel;
	BinaryDisplayAreaLabel[0] = std::make_unique<Label>(18, -10, "二进制显示区");
	BinaryDisplayAreaLabel[1] = std::make_unique<Label>(35, 20, "上次值");
	BinaryDisplayAreaLabel[2] = std::make_unique<Label>(35, 67, "本次值");

	std::array<std::unique_ptr<TextBox>, 2> BinaryDisplayAreaTextBox;
	BinaryDisplayAreaTextBox[0] = std::make_unique<TextBox>(110, 20, 520, 30, "0000_0000_0000_0000_0000_0000_0000_0000");
	BinaryDisplayAreaTextBox[1] = std::make_unique<TextBox>(110, 67, 520, 30, "0000_0000_0000_0000_0000_0000_0000_0000");
	auto Last = BinaryDisplayAreaTextBox[0].get();
	auto This = BinaryDisplayAreaTextBox[1].get();

	for (auto& b : BinaryDisplayAreaLabel)
	{
		b->setTextdisap(true);
		BinaryDisplayArea->addControl(std::move(b));
	}
	for (auto& b : BinaryDisplayAreaTextBox)
	{
		b->setMaxCharLen(40);
		b->textStyle.color = RGB(255, 69, 0);
		b->setTextBoxBk(RGB(141, 141, 141));
		b->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
		b->setMode(StellarX::TextBoxmode::READONLY_MODE);
		BinaryDisplayArea->addControl(std::move(b));
	}

	// 由位图 bits 生成数值
	auto valueFromBits = [](const std::array<bool, 32>& bits) -> uint32_t {
		uint32_t v = 0;
		for (int b = 0; b < 32; ++b) if (bits[b]) v |= (1u << b);
		return v;
		};

	// 由位图 bits 生成 "0000_0000_..._0000"（MSB→LSB：31..0）
	auto binaryGroupedFromBits = [](const std::array<bool, 32>& bits) -> std::string {
		std::string s; s.reserve(39);
		for (int b = 31; b >= 0; --b) {
			s.push_back(bits[b] ? '1' : '0');
			if (b % 4 == 0 && b != 0) s.push_back('_');
		}
		return s;
		};

	// 用“目标位图 bits”刷新显示区
	auto refreshDisplaysWithBits = [&](const std::string& prevThis,
		const std::array<bool, 32>& bits,
		TextBox* hex, TextBox* dec, TextBox* Last, TextBox* This)
		{
			const uint32_t val = valueFromBits(bits);
			const int32_t  s = static_cast<int32_t>(val);
			char hexbuf[16];
			std::snprintf(hexbuf, sizeof(hexbuf), "%08X", val);

			hex->setText(hexbuf);                   // HEX（大写8位）
			dec->setText(gSigned ? std::to_string(s) : std::to_string(val));      // DEC
			Last->setText(prevThis);                // 上次值 ← 刷新前的本次值
			This->setText(binaryGroupedFromBits(bits)); // 本次值 ← 由“目标位图”生成
		};

	bitInvertFunctionButton_ptr->setOnClickListener([=, &snapshotBits, &applyBits, &refreshDisplaysWithBits]() {
		const std::string prevThis = This->getText();

		int L = clamp(toInt(invL->getText(), 0), 0, 31);
		int H = clamp(toInt(invH->getText(), 0), 0, 31);
		if (L > H) std::swap(L, H);

		auto cur = snapshotBits();
		for (int b = L; b <= H; ++b) cur[b] = !cur[b];

		applyBits(cur);                                   // 只改按钮点击态（触发位按钮自回调）
		refreshDisplaysWithBits(prevThis, cur, hex, dec, Last, This);
		});

	leftShiftFunctionButton_ptr->setOnClickListener([=, &snapshotBits, &applyBits, &refreshDisplaysWithBits]() {
		const std::string prevThis = This->getText();

		int n = clamp(toInt(shlBox->getText(), 0), 0, 31);
		auto cur = snapshotBits();
		std::array<bool, 32> nxt{};                         // 默认全 0

		// 逻辑左移：高位丢弃、低位补 0
		for (int b = 31; b >= 0; --b) nxt[b] = (b >= n) ? cur[b - n] : false;

		applyBits(nxt);
		refreshDisplaysWithBits(prevThis, nxt, hex, dec, Last, This);
		});

	rightShiftFunctionButton_ptr->setOnClickListener([=, &snapshotBits, &applyBits, &refreshDisplaysWithBits]() {
		const std::string prevThis = This->getText();

		int n = clamp(toInt(shrBox->getText(), 0), 0, 31);
		auto cur = snapshotBits();
		std::array<bool, 32> nxt{};

		// 逻辑右移：低位丢弃、高位补 0
		for (int b = 0; b < 32; ++b) nxt[b] = (b + n <= 31) ? cur[b + n] : false;

		applyBits(nxt);
		refreshDisplaysWithBits(prevThis, nxt, hex, dec, Last, This);
		});

	//配置区控件clearrectangle(10, 440, 690, 490);
	auto configuration = std::make_unique<Canvas>(10, 455, 680, 40);
	configuration->setCanvasBkColor(blackColor);
	configuration->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);

	auto configurationLabel = std::make_unique<Label>(20, -10, "配置区");
	configurationLabel->setTextdisap(true);

	std::array<std::unique_ptr<Button>, 2> configurationButton;
	configurationButton[0] = std::make_unique<Button>(420, 10, 90, 20, "一键置0",
		blackColor, RGB(171, 196, 220));
	configurationButton[0]->textStyle.color = RGB(226, 116, 152);
	configurationButton[0]->setButtonShape(StellarX::ControlShape::B_RECTANGLE);

	configurationButton[1] = std::make_unique<Button>(530, 10, 90, 20, "一键置1",
		blackColor, RGB(171, 196, 220));
	configurationButton[1]->textStyle.color = RGB(226, 116, 152);
	configurationButton[1]->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
	configurationButton[0]->setOnClickListener(
		[&]() {
			for (auto& s : selectionAreaButton_ptr)
				if (s->isClicked()) s->setButtonClick(false);

			// 刷新显示：prevThis 用当前 This 文本
			const std::string prevThis = This->getText();
			auto cur = snapshotBits();
			{
				char hexbuf[16];
				uint32_t u = 0; for (int b = 0; b < 32; ++b) if (cur[b]) u |= (1u << b);
				int32_t  s = static_cast<int32_t>(u);
				std::snprintf(hexbuf, sizeof(hexbuf), "%08X", u);
				hex->setText(hexbuf);
				dec->setText(gSigned ? std::to_string(s) : std::to_string(u));
				Last->setText(prevThis);
				This->setText(binaryGroupedFromBits(cur));
			}
		});

	configurationButton[1]->setOnClickListener(
		[&]() {
			for (auto& s : selectionAreaButton_ptr)
				if (!s->isClicked()) s->setButtonClick(true);

			const std::string prevThis = This->getText();
			auto cur = snapshotBits();
			char hexbuf[16];
			uint32_t u = 0; for (int b = 0; b < 32; ++b) if (cur[b]) u |= (1u << b);
			int32_t  s = static_cast<int32_t>(u);
			std::snprintf(hexbuf, sizeof(hexbuf), "%08X", u);
			hex->setText(hexbuf);
			dec->setText(gSigned ? std::to_string(s) : std::to_string(u));
			Last->setText(prevThis);
			This->setText(binaryGroupedFromBits(cur));
		});

	auto signedToggle = std::make_unique<Button>(
		330, 10, 80, 20, "无符号",
		blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE);
	signedToggle->textStyle.color = RGB(226, 116, 152);
	signedToggle->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
	auto* signedTogglePtr = signedToggle.get();

	signedTogglePtr->setOnToggleOnListener([&]() {
		gSigned = true;
		signedTogglePtr->setButtonText("有符号");
		StellarX::MessageBox::showModal(mainWindow, "有符号模式下，\n最高位为符号位，\n其余位为数值位。", "有符号模式");
		// 立即刷新十进制显示：用当前位图算出新值，仅改 dec
		auto cur = snapshotBits();
		const uint32_t u = [&] { uint32_t v = 0; for (int b = 0; b < 32; ++b) if (cur[b]) v |= (1u << b); return v; }();
		const int32_t  s = static_cast<int32_t>(u);
		dec->setText(std::to_string(s));
		});
	signedTogglePtr->setOnToggleOffListener([&]() {
		gSigned = false;
		signedTogglePtr->setButtonText("无符号");
		StellarX::MessageBox::showAsync(mainWindow, "无符号模式下，\n所有位均为数值位。", "无符号模式");
		auto cur = snapshotBits();
		const uint32_t u = [&] { uint32_t v = 0; for (int b = 0; b < 32; ++b) if (cur[b]) v |= (1u << b); return v; }();
		dec->setText(std::to_string(u));
		});

	signedTogglePtr->enableTooltip(true);
	signedTogglePtr->setTooltipTextsForToggle("切换无符号模式", "切换有符号模式");

	configuration->addControl(std::move(configurationButton[0]));
	configuration->addControl(std::move(configurationButton[1]));
	configuration->addControl(std::move(signedToggle));
	configuration->addControl(std::move(configurationLabel));

	selectionArea->setAnchor(StellarX::Anchor::Right, StellarX::Anchor::Left);
	selectionArea->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);

	mainWindow.addControl(std::move(selectionArea));
	mainWindow.addControl(std::move(function));
	mainWindow.addControl(std::move(NumericalDisplayArea));
	mainWindow.addControl(std::move(BinaryDisplayArea));
	mainWindow.addControl(std::move(configuration));

	mainWindow.draw();
	return mainWindow.runEventLoop();
}
#endif

#if 3 == KEY

// 本Demo基于 StellarX 构建，轻量级的 Windows GUI 框架。
#include"StellarX.h"

int main()
{
	StellarX::SxLogger::setGBK();
	StellarX::SxLogger::Get().enableConsole(true);
	StellarX::SxLogger::Get().setMinLevel(StellarX::SxLogLevel::Debug);   // Info/Debug/Trace 自己切
	StellarX::SxLogger::Get().setLanguage(StellarX::SxLogLanguage::ZhCN); // ZhCN / EnUS

	Window win(1300, 800, 1, RGB(255, 255, 0), "记账管理系统");

	/*********登录界面***********/
	//标签
	std::unique_ptr<Label>  logIn_label[3];
	logIn_label[0] = std::make_unique<Label>(90, 150, "欢迎使用餐馆记账管理系统");
	logIn_label[1] = std::make_unique<Label>(400, 300, "账号");
	logIn_label[2] = std::make_unique<Label>(400, 400, "密码");
	for (auto& log : logIn_label)
	{
		log->setTextdisap(true);
		log->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
		log->textStyle.lpszFace = "华文行楷";
	}
	logIn_label[0]->textStyle.nHeight = 100;
	logIn_label[0]->setAnchor(StellarX::Anchor::Top, StellarX::Anchor::NoAnchor);
	logIn_label[1]->textStyle.nHeight = 50;
	logIn_label[1]->setAnchor(StellarX::Anchor::Bottom, StellarX::Anchor::NoAnchor);
	logIn_label[2]->textStyle.nHeight = 50;
	logIn_label[2]->setAnchor(StellarX::Anchor::Bottom, StellarX::Anchor::NoAnchor);

	//输入框
	std::unique_ptr<TextBox>  logIn_textBox[2];
	TextBox* logIn_textBox_ptr[2];
	logIn_textBox[0] = std::make_unique<TextBox>(500, 295, 450, 50);
	logIn_textBox[1] = std::make_unique<TextBox>(500, 395, 450, 50);
	logIn_textBox[1]->setMode(StellarX::TextBoxmode::PASSWORD_MODE);
	logIn_textBox_ptr[0] = logIn_textBox[0].get();
	logIn_textBox_ptr[1] = logIn_textBox[1].get();
	for (auto& tb : logIn_textBox)
	{
		tb->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
		tb->setAnchor(StellarX::Anchor::Bottom, StellarX::Anchor::NoAnchor);
		tb->setMaxCharLen(15);
	}
	//按钮
	std::unique_ptr<Button>  logIn_Button[2];
	Button* logIn_Button_ptr[2];
	logIn_Button[0] = std::make_unique<Button>(350, 500, 300, 50, "管理员登录");
	logIn_Button[1] = std::make_unique<Button>(750, 500, 300, 50, "操作员登录");
	logIn_Button_ptr[0] = logIn_Button[0].get();
	logIn_Button_ptr[1] = logIn_Button[1].get();
	for (auto& b : logIn_Button)
	{
		b->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
		b->setAnchor(StellarX::Anchor::Bottom, StellarX::Anchor::NoAnchor);
	}
	//log画布
	auto log_Canvas = std::make_unique<Canvas>(0, 0, 1300, 800);
	Canvas* log_Canvas_ptr = log_Canvas.get();
	log_Canvas_ptr->setCanvasfillMode(StellarX::FillMode::Null);
	log_Canvas_ptr->setShape(StellarX::ControlShape::B_RECTANGLE);
	log_Canvas_ptr->setLayoutMode(StellarX::LayoutMode::AnchorToEdges);
	log_Canvas_ptr->setAnchor(StellarX::Anchor::Bottom, StellarX::Anchor::Top);

	//将log界面控价加入logCanvas统一管理
	for (auto& b : logIn_Button)
		log_Canvas_ptr->addControl(std::move(b));
	for (auto& tb : logIn_textBox)
		log_Canvas_ptr->addControl(std::move(tb));
	for (auto& la : logIn_label)
		log_Canvas_ptr->addControl(std::move(la));

	/**************业务UI****************/

	auto tabControl = std::make_unique<TabControl>(10, 10, 1280, 780);
	auto tabControl_ptr = tabControl.get();
	tabControl_ptr->setIsVisible(false);
	tabControl_ptr->setCanvasfillMode(StellarX::FillMode::Null);
	tabControl_ptr->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	tabControl_ptr->setTabPlacement(StellarX::TabPlacement::Left);
	tabControl_ptr->setTabBarHeight(60);
	//添加页签及页
	auto tabP = std::make_pair(std::make_unique<Button>(0, 0, 100, 100, "点餐"), std::make_unique<Canvas>(0, 0, 1290, 790));
	tabP.second->setShape(StellarX::ControlShape::ROUND_RECTANGLE);
	tabControl_ptr->add(std::move(tabP));

	/*------------login事件-------------*/
	//管理员登录按钮事件
	logIn_Button_ptr[0]->setOnClickListener([&tabControl_ptr, &log_Canvas_ptr, &logIn_textBox_ptr, &win]()
		{
			if ("\0" == logIn_textBox_ptr[0]->getText() || "\0" == logIn_textBox_ptr[1]->getText())
			{
				if ("\0" == logIn_textBox_ptr[0]->getText())logIn_textBox_ptr[0]->setTextBoxBk(RGB(255, 0, 0));
				if ("\0" == logIn_textBox_ptr[1]->getText())logIn_textBox_ptr[1]->setTextBoxBk(RGB(255, 0, 0));
				std::cout << "\a";
				StellarX::MessageBox::showAsync(win, "账号或密码不能为空！", "提示");
			}
			else if("admin" == logIn_textBox_ptr[0]->getText() && "123" == logIn_textBox_ptr[1]->getText())
			{
				log_Canvas_ptr->setIsVisible(false);
				tabControl_ptr->setIsVisible(true);
				win.draw("image\\bk1.jpg");
			}
			else
			{
				std::cout << "\a";
				StellarX::MessageBox::showModal(win, "账号或密码错误！", "提示");
			}
		});
	//操作员登录按钮事件
	logIn_Button_ptr[1]->setOnClickListener([&tabControl_ptr, &log_Canvas_ptr, &logIn_textBox_ptr, &win]()
		{
			if ("\0" == logIn_textBox_ptr[0]->getText() || "\0" == logIn_textBox_ptr[1]->getText())
			{
				if ("\0" == logIn_textBox_ptr[0]->getText())logIn_textBox_ptr[0]->setTextBoxBk(RGB(255, 0, 0));
				if ("\0" == logIn_textBox_ptr[1]->getText())logIn_textBox_ptr[1]->setTextBoxBk(RGB(255, 0, 0));
				std::cout << "\a";
				StellarX::MessageBox::showModal(win, "账号或密码不能为空！", "提示");
			}
			else
			{
				log_Canvas_ptr->setIsVisible(false);
				tabControl_ptr->setIsVisible(true);
				win.draw("image\\bk1.jpg");
			}
		});

	win.addControl(std::move(log_Canvas));
	win.addControl(std::move(tabControl));
	win.draw("image\\bk1.jpg");

	return win.runEventLoop();
}

#endif
