Refine dialog initialization and noisy repaint logs

This commit is contained in:
Codex
2026-03-23 15:21:48 +08:00
parent dde570ac3c
commit 4a6e153da5
9 changed files with 117 additions and 58 deletions
+2 -2
View File
@@ -314,7 +314,7 @@ bool Button::handleEvent(const ExMessage& msg)
} }
if (hover != oldHover) if (hover != oldHover)
{ {
SX_LOGD("Button") << SX_T("悬停变化: ","hover change: ") << "id=" << id SX_LOG_TRACE("Button") << SX_T("悬停变化: ","hover change: ") << "id=" << id
<< " text= " << text << " text= " << text
<< " " << (oldHover ? 1 : 0) << "->" << (hover ? 1 : 0); << " " << (oldHover ? 1 : 0) << "->" << (hover ? 1 : 0);
} }
@@ -396,7 +396,7 @@ bool Button::handleEvent(const ExMessage& msg)
// 到点就显示 // 到点就显示
if (GetTickCount64() - tipHoverTick >= (ULONGLONG)tipDelayMs) if (GetTickCount64() - tipHoverTick >= (ULONGLONG)tipDelayMs)
{ {
SX_LOGD("Button") << SX_T("提示信息显示: ","tooltip show:")<<" id = " << id <<SX_T("延时时间: ", " delayMs = ") << tipDelayMs; SX_LOG_TRACE("Button") << SX_T("提示信息显示: ","tooltip show:")<<" id = " << id <<SX_T("延时时间: ", " delayMs = ") << tipDelayMs;
tipVisible = true; tipVisible = true;
+3 -3
View File
@@ -424,7 +424,7 @@ void Canvas::requestRepaint(Control* parent)
// => 禁止局部重绘,直接升级为一次完整 draw(先把 dirty 置真,避免 draw() 早退) // => 禁止局部重绘,直接升级为一次完整 draw(先把 dirty 置真,避免 draw() 早退)
if (dirty || !hasSnap || !saveBkImage) if (dirty || !hasSnap || !saveBkImage)
{ {
SX_LOGD("Dirty") SX_LOG_TRACE("Dirty")
<< SX_T("Canvas 局部重绘降级为全量重绘: id=", "Canvas partial->full draw: id=") << SX_T("Canvas 局部重绘降级为全量重绘: id=", "Canvas partial->full draw: id=")
<< id << id
<< " dirty=" << (dirty ? 1 : 0) << " dirty=" << (dirty ? 1 : 0)
@@ -435,7 +435,7 @@ void Canvas::requestRepaint(Control* parent)
return; return;
} }
SX_LOGD("Dirty") << SX_T("Canvas 请求局部重绘:id=", "Canvas::requestRepaint(partial): id=") << id; SX_LOG_TRACE("Dirty") << SX_T("Canvas 请求局部重绘:id=", "Canvas::requestRepaint(partial): id=") << id;
for (auto& control : controls) for (auto& control : controls)
if (control->isDirty() && control->IsVisible()) if (control->isDirty() && control->IsVisible())
@@ -444,7 +444,7 @@ void Canvas::requestRepaint(Control* parent)
return; return;
} }
SX_LOGD("Dirty") << SX_T("Canvas 请求根级重绘:id=", "Canvas::requestRepaint(root): id=") << id; SX_LOG_TRACE("Dirty") << SX_T("Canvas 请求根级重绘:id=", "Canvas::requestRepaint(root): id=") << id;
onRequestRepaintAsRoot(); onRequestRepaintAsRoot();
} }
+2 -2
View File
@@ -139,7 +139,7 @@ void Control::requestRepaint(Control* parent)
return; return;
} }
SX_LOGD("Dirty") << SX_T("请求重绘:id=","requestRepaint: id=") << id << " parent=" << (parent ? parent->getId() : "null"); SX_LOG_TRACE("Dirty") << SX_T("请求重绘:id=","requestRepaint: id=") << id << " parent=" << (parent ? parent->getId() : "null");
if (parent) parent->requestRepaint(parent); // 交给容器处理(容器可局部重绘) if (parent) parent->requestRepaint(parent); // 交给容器处理(容器可局部重绘)
else onRequestRepaintAsRoot(); // 根兜底 else onRequestRepaintAsRoot(); // 根兜底
@@ -147,7 +147,7 @@ void Control::requestRepaint(Control* parent)
void Control::onRequestRepaintAsRoot() void Control::onRequestRepaintAsRoot()
{ {
SX_LOGI("Dirty") SX_LOG_TRACE("Dirty")
<< SX_T("触发根重绘:id=", "onRequestRepaintAsRoot: id=") << id << SX_T("触发根重绘:id=", "onRequestRepaintAsRoot: id=") << id
<< SX_T("(从根节点开始重画)", " (root repaint)"); << SX_T("(从根节点开始重画)", " (root repaint)");
+33 -20
View File
@@ -102,27 +102,19 @@ bool Dialog::handleEvent(const ExMessage& msg)
void Dialog::SetTitle(const std::string& title) void Dialog::SetTitle(const std::string& title)
{ {
this->titleText = title; this->titleText = title;
if (this->title) invalidateLayout(true);
{
this->title->setText(title);
}
dirty = true;
} }
void Dialog::SetMessage(const std::string& message) void Dialog::SetMessage(const std::string& message)
{ {
this->message = message; this->message = message;
splitMessageLines(); invalidateLayout(true);
getTextSize();
dirty = true;
} }
void Dialog::SetType(StellarX::MessageBoxType type) void Dialog::SetType(StellarX::MessageBoxType type)
{ {
this->type = type; this->type = type;
// 重新初始化按钮 invalidateLayout(true);
initButtons();
dirty = true;
} }
void Dialog::SetModal(bool modal) void Dialog::SetModal(bool modal)
@@ -255,9 +247,11 @@ void Dialog::setInitialization(bool init)
{ {
if (init) if (init)
{ {
initDialogSize(); invalidateLayout(true);
saveBackground((x - BorderWidth), (y - BorderWidth), (width + 2 * BorderWidth), (height + 2 * BorderWidth)); }
this->dirty = true; else
{
needsInitialization = false;
} }
} }
@@ -547,8 +541,8 @@ void Dialog::getTextSize()
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace, settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight, textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut); textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut);
int tempHeight = 0; this->textHeight = 0;
int tempWidth = 0; this->textWidth = 0;
for (auto& text : lines) for (auto& text : lines)
{ {
int w = textwidth(LPCTSTR(text.c_str())); int w = textwidth(LPCTSTR(text.c_str()));
@@ -561,10 +555,24 @@ void Dialog::getTextSize()
restoreStyle(); restoreStyle();
} }
void Dialog::invalidateLayout(bool clearChildren)
{
if (clearChildren)
clearControls();
this->textWidth = 0;
this->textHeight = 0;
this->buttonNum = 0;
this->needsInitialization = true;
this->dirty = true;
}
// 计算逻辑:对话框宽度取【文本区域最大宽度】和【按钮区域总宽度】中的较大值。 // 计算逻辑:对话框宽度取【文本区域最大宽度】和【按钮区域总宽度】中的较大值。
// 对话框高度 = 标题栏 + 文本区 + 按钮区 + 各种间距。 // 对话框高度 = 标题栏 + 文本区 + 按钮区 + 各种间距。
void Dialog::initDialogSize() void Dialog::initDialogSize()
{ {
this->textStyle.nHeight = 20;
splitMessageLines(); // 分割消息行 splitMessageLines(); // 分割消息行
getTextSize(); // 获取文本最大尺寸 getTextSize(); // 获取文本最大尺寸
@@ -591,9 +599,17 @@ void Dialog::initDialogSize()
// 计算文本区域宽度(包括边距) // 计算文本区域宽度(包括边距)
int textAreaWidth = textWidth + textToBorderMargin * 2; int textAreaWidth = textWidth + textToBorderMargin * 2;
saveStyle();
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut);
int titleAreaWidth = textwidth(LPCTSTR(titleText.c_str())) + textToBorderMargin * 2 + closeButtonWidth + buttonMargin;
restoreStyle();
// 对话框宽度取两者中的较大值,并确保最小宽度 // 对话框宽度取文本、标题和按钮区域中的较大值,并确保最小宽度
this->width = buttonAreaWidth > textAreaWidth ? buttonAreaWidth : textAreaWidth; this->width = buttonAreaWidth > textAreaWidth ? buttonAreaWidth : textAreaWidth;
if (titleAreaWidth > this->width)
this->width = titleAreaWidth;
this->width = this->width > 200 ? this->width : 200; this->width = this->width > 200 ? this->width : 200;
// 计算对话框高度 // 计算对话框高度
@@ -608,9 +624,6 @@ void Dialog::initDialogSize()
this->x = (hWnd.getWidth() - this->width) / 2; this->x = (hWnd.getWidth() - this->width) / 2;
this->y = (hWnd.getHeight() - this->height) / 2; this->y = (hWnd.getHeight() - this->height) / 2;
//this->textStyle.nWidth = 10;
this->textStyle.nHeight = 20;
initButtons(); // 初始化按钮 initButtons(); // 初始化按钮
initTitle(); // 初始化标题标签 initTitle(); // 初始化标题标签
initCloseButton(); // 初始化关闭按钮 initCloseButton(); // 初始化关闭按钮
+2
View File
@@ -117,6 +117,8 @@ private:
void splitMessageLines(); void splitMessageLines();
// 获取文本大小 // 获取文本大小
void getTextSize(); void getTextSize();
// 标记需要重新布局并按需清空旧控件
void invalidateLayout(bool clearChildren);
//初始化对话框尺寸 //初始化对话框尺寸
void initDialogSize(); void initDialogSize();
void addControl(std::unique_ptr<Control> control); void addControl(std::unique_ptr<Control> control);
-2
View File
@@ -9,7 +9,6 @@ namespace StellarX
SX_LOGI("MessageBox") << "show: Message=" << dlg.GetText() SX_LOGI("MessageBox") << "show: Message=" << dlg.GetText()
<< " modal=" << (dlg.model() ? 1 : 0); << " modal=" << (dlg.model() ? 1 : 0);
dlg.setInitialization(true);
dlg.Show(); dlg.Show();
return dlg.GetResult(); return dlg.GetResult();
} }
@@ -28,7 +27,6 @@ namespace StellarX
SX_LOGI("MessageBox") << "show: Message=" << dlg->GetText() SX_LOGI("MessageBox") << "show: Message=" << dlg->GetText()
<< " modal=" << (dlg->model() ? 1 : 0); << " modal=" << (dlg->model() ? 1 : 0);
Dialog* dlgPtr = dlg.get(); Dialog* dlgPtr = dlg.get();
dlgPtr->setInitialization(true);
// 设置回调 // 设置回调
if (onResult) if (onResult)
dlgPtr->SetResultCallback(std::move(onResult)); dlgPtr->SetResultCallback(std::move(onResult));
+58 -21
View File
@@ -1,5 +1,31 @@
#include "Table.h" #include "Table.h"
#include "SxLog.h" #include "SxLog.h"
namespace
{
std::vector<std::string> NormalizeTableRow(std::vector<std::string> row, size_t headerCount)
{
if (headerCount == 0)
return row;
if (row.size() > headerCount)
row.resize(headerCount);
else if (row.size() < headerCount)
row.resize(headerCount, "");
return row;
}
int CalculateTotalPages(size_t rowCount, int rowsPerPage)
{
if (rowsPerPage < 1)
rowsPerPage = 1;
const int total = static_cast<int>((rowCount + static_cast<size_t>(rowsPerPage) - 1) / static_cast<size_t>(rowsPerPage));
return total > 0 ? total : 1;
}
}
// 绘制表格的当前页 // 绘制表格的当前页
// 使用双循环绘制行和列,考虑分页偏移 // 使用双循环绘制行和列,考虑分页偏移
void Table::drawTable() void Table::drawTable()
@@ -449,25 +475,37 @@ void Table::setHeaders(std::initializer_list<std::string> headers)
this->headers.clear(); this->headers.clear();
for (auto& lis : headers) for (auto& lis : headers)
this->headers.push_back(lis); this->headers.push_back(lis);
if (!this->headers.empty())
{
for (auto& row : this->data)
row = NormalizeTableRow(std::move(row), this->headers.size());
}
totalPages = CalculateTotalPages(this->data.size(), rowsPerPage);
if (currentPage > totalPages)
currentPage = totalPages;
SX_LOGI("Table") << SX_T("设置表头:id=","setHeaders: id=") << id << SX_T("总数="," count=") << (int)this->headers.size(); SX_LOGI("Table") << SX_T("设置表头:id=","setHeaders: id=") << id << SX_T("总数="," count=") << (int)this->headers.size();
isNeedCellSize = true; // 标记需要重新计算单元格尺寸 isNeedCellSize = true; // 标记需要重新计算单元格尺寸
isNeedDrawHeaders = true; // 标记需要重新绘制表头 isNeedDrawHeaders = true; // 标记需要重新绘制表头
isNeedButtonAndPageNum = true;
dirty = true; dirty = true;
} }
void Table::setData(std::vector<std::string> data) void Table::setData(std::vector<std::string> data)
{ {
while (data.size() < headers.size()) data = NormalizeTableRow(std::move(data), headers.size());
data.push_back("");
this->data.push_back(data); this->data.push_back(data);
totalPages = ((int)this->data.size() + rowsPerPage - 1) / rowsPerPage; totalPages = CalculateTotalPages(this->data.size(), rowsPerPage);
if (totalPages < 1) if (currentPage > totalPages)
totalPages = 1; currentPage = totalPages;
isNeedCellSize = true; isNeedCellSize = true;
isNeedButtonAndPageNum = true;
dirty = true; dirty = true;
SX_LOGI("Table") SX_LOGI("Table")
@@ -480,24 +518,22 @@ void Table::setData(std::vector<std::string> data)
void Table::setData(std::initializer_list<std::vector<std::string>> data) void Table::setData(std::initializer_list<std::vector<std::string>> data)
{ {
for (auto lis : data) int addedRows = 0;
if (lis.size() < headers.size()) for (auto row : data)
{ {
for (size_t i = lis.size(); i < headers.size(); i++) this->data.push_back(NormalizeTableRow(std::move(row), headers.size()));
lis.push_back(""); ++addedRows;
this->data.push_back(lis);
} }
else
this->data.push_back(lis);
totalPages = ((int)this->data.size() + rowsPerPage - 1) / rowsPerPage; totalPages = CalculateTotalPages(this->data.size(), rowsPerPage);
if (totalPages < 1) if (currentPage > totalPages)
totalPages = 1; currentPage = totalPages;
isNeedCellSize = true; // 标记需要重新计算单元格尺寸 isNeedCellSize = true; // 标记需要重新计算单元格尺寸
isNeedButtonAndPageNum = true;
dirty = true; dirty = true;
SX_LOGI("Table") SX_LOGI("Table")
<< SX_T("新增Dataid=", "appendRow: id=") << id << SX_T("新增多行Dataid=", "appendRows: id=") << id
<< SX_T(" 本行列数=", " cols=") << (int)data.size() << SX_T(" 新增行数=", " addedRows=") << addedRows
<< SX_T(" 数据总行数=", " totalRows=") << (int)this->data.size() << SX_T(" 数据总行数=", " totalRows=") << (int)this->data.size()
<< SX_T(" 总页数=", " totalPages=") << totalPages; << SX_T(" 总页数=", " totalPages=") << totalPages;
@@ -509,10 +545,11 @@ void Table::setRowsPerPage(int rows)
this->rowsPerPage = rows; this->rowsPerPage = rows;
if (this->rowsPerPage < 1) if (this->rowsPerPage < 1)
this->rowsPerPage = 1; this->rowsPerPage = 1;
totalPages = ((int)data.size() + rowsPerPage - 1) / rowsPerPage; totalPages = CalculateTotalPages(data.size(), rowsPerPage);
if (totalPages < 1) if (currentPage > totalPages)
totalPages = 1; currentPage = totalPages;
isNeedCellSize = true; // 标记需要重新计算单元格尺寸 isNeedCellSize = true; // 标记需要重新计算单元格尺寸
isNeedButtonAndPageNum = true;
dirty = true; dirty = true;
} }
@@ -581,7 +618,7 @@ void Table::clearData()
{ {
this->data.clear(); this->data.clear();
this->currentPage = 1; this->currentPage = 1;
this->totalPages = 1; this->totalPages = CalculateTotalPages(this->data.size(), rowsPerPage);
isNeedCellSize = true; // 标记需要重新计算单元格尺寸 isNeedCellSize = true; // 标记需要重新计算单元格尺寸
isNeedButtonAndPageNum = true;// 标记需要重新计算翻页按钮和页码信息 isNeedButtonAndPageNum = true;// 标记需要重新计算翻页按钮和页码信息
dirty = true; dirty = true;
+11 -4
View File
@@ -698,14 +698,21 @@ HWND Window::getHwnd() const
int Window::getWidth() const int Window::getWidth() const
{ {
// 注意:这里返回 pendingW return width;
// 表示“最近一次收到的尺寸”(可能尚未应用到画布,最终以收口时的 width 为准)
return pendingW;
} }
int Window::getHeight() const int Window::getHeight() const
{ {
// 同上,返回 pendingH(与 getWidth 对应) return height;
}
int Window::getPendingWidth() const
{
return pendingW;
}
int Window::getPendingHeight() const
{
return pendingH; return pendingH;
} }
+2
View File
@@ -84,6 +84,8 @@ public:
HWND getHwnd() const; HWND getHwnd() const;
int getWidth() const; int getWidth() const;
int getHeight() const; int getHeight() const;
int getPendingWidth() const;
int getPendingHeight() const;
std::string getHeadline() const; std::string getHeadline() const;
COLORREF getBkcolor() const; COLORREF getBkcolor() const;
IMAGE* getBkImage() const; IMAGE* getBkImage() const;