Files
SimpleRemoter/server/2015Remote/SearchBarDlg.cpp
T
yuanyuanxiang 6b87a31c6f UI: explain dangerous operations, screen modes and host filtering
Four items from a user-side review of the master. The common thread: the UI
now says what it is about to do instead of leaving the user to guess.

Confirm boxes. Logout, restart, shutdown, delete host and uninstall client
all asked "are you sure" without naming the action or the hosts, so a
multi-selection could not be checked. DescribeTargetHosts() lists
"note (or computer name) (ip)" capped at five lines, and the default button
is No. The preview is dropped before the modal box and targets are
re-resolved after it, since the list can reshuffle in between.

Screen modes. High Speed / Ultra Speed became High Quality (DXGI +
differential: lossless, bandwidth hungry) and Smooth (GDI + H264: lossy,
cheap). TranslateMenu looks menus up by displayed text, so the .rc entries,
the FeatureLimitsDlg item names and the language pack keys changed together.

Host filter. The Ctrl+F bar was find-next; it now filters as you type over
IP, location, note, OS and version, and Esc clears the filter. The status
line reads "filtered: N / M total" while active. Commands still resolve by
clientID and m_FilteredIndices still maps row to m_HostList index, so no
command path changes. SearchBarDlg drops from 446 to 312 lines.

Quality changes. The adaptive level now shows its reason for twenty seconds
("Good (high latency)") instead of silently changing. The full-screen info
bar was a fixed 170px, so that reason was clipped; the width is now measured
from the text with the same font that paints it, 170 becoming the floor and
16px quantisation so changing digits do not jitter the window. Cosmetic
only: no threshold, hysteresis, cooldown or lock-out logic is touched.

Verified: debug server build, 0 errors; new strings present in .res and in
the exe. UI not exercised - the debug master would fight the running release
master for the listening port in Bin/settings.ini.
2026-11-07 15:34:45 +01:00

313 lines
8.5 KiB
C++

// SearchBarDlg.cpp - 浮动搜索工具栏实现
#include "stdafx.h"
#include "SearchBarDlg.h"
#include "2015RemoteDlg.h"
IMPLEMENT_DYNAMIC(CSearchBarDlg, CDialogEx)
CSearchBarDlg::CSearchBarDlg(CMy2015RemoteDlg* pParent)
: CDialogLangEx(IDD_SEARCH_BAR, pParent)
, m_pParent(pParent)
{
m_brushEdit.CreateSolidBrush(RGB(60, 60, 60));
m_brushStatic.CreateSolidBrush(RGB(40, 40, 40));
}
CSearchBarDlg::~CSearchBarDlg()
{
m_brushEdit.DeleteObject();
m_brushStatic.DeleteObject();
}
void CSearchBarDlg::DoDataExchange(CDataExchange* pDX)
{
__super::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SEARCH_EDIT, m_editSearch);
DDX_Control(pDX, IDC_SEARCH_COUNT, m_staticCount);
}
BEGIN_MESSAGE_MAP(CSearchBarDlg, CDialogEx)
ON_BN_CLICKED(IDC_SEARCH_PREV, &CSearchBarDlg::OnBnClickedPrev)
ON_BN_CLICKED(IDC_SEARCH_NEXT, &CSearchBarDlg::OnBnClickedNext)
ON_BN_CLICKED(IDC_SEARCH_CLOSE, &CSearchBarDlg::OnBnClickedClose)
ON_EN_CHANGE(IDC_SEARCH_EDIT, &CSearchBarDlg::OnEnChangeSearch)
ON_WM_TIMER()
ON_WM_ERASEBKGND()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
BOOL CSearchBarDlg::OnInitDialog()
{
__super::OnInitDialog();
// 设置分层窗口样式(透明 + 不抢焦点)
ModifyStyleEx(0, WS_EX_LAYERED | WS_EX_NOACTIVATE);
SetLayeredWindowAttributes(0, 200, LWA_ALPHA);
// 子类化按钮为 CIconButton
m_btnPrev.SubclassDlgItem(IDC_SEARCH_PREV, this);
m_btnNext.SubclassDlgItem(IDC_SEARCH_NEXT, this);
m_btnClose.SubclassDlgItem(IDC_SEARCH_CLOSE, this);
// 设置图标
m_btnPrev.SetIconDrawFunc(CIconButton::DrawIconArrowLeft);
m_btnNext.SetIconDrawFunc(CIconButton::DrawIconArrowRight);
m_btnClose.SetIconDrawFunc(CIconButton::DrawIconClose);
m_btnClose.SetIsCloseButton(true);
// 创建工具提示
m_tooltip.Create(this);
m_tooltip.Activate(TRUE);
m_tooltip.AddTool(&m_btnPrev, _TR("上一个 (Shift+Enter)"));
m_tooltip.AddTool(&m_btnNext, _TR("下一个 (Enter)"));
m_tooltip.AddTool(&m_btnClose, _TR("关闭 (Esc)"));
// 设置输入框提示文本
m_editSearch.SetCueBanner(L"Search...", TRUE);
// 初始化计数文本
m_staticCount.SetWindowText(_T(""));
return TRUE;
}
BOOL CSearchBarDlg::PreTranslateMessage(MSG* pMsg)
{
if (m_tooltip.GetSafeHwnd()) {
m_tooltip.RelayEvent(pMsg);
}
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam == VK_ESCAPE) {
Hide();
return TRUE;
}
if (pMsg->wParam == VK_RETURN) {
if (GetKeyState(VK_SHIFT) & 0x8000) {
GotoPrev();
} else {
GotoNext();
}
return TRUE;
}
// F3 = 下一个, Shift+F3 = 上一个
if (pMsg->wParam == VK_F3) {
if (GetKeyState(VK_SHIFT) & 0x8000) {
GotoPrev();
} else {
GotoNext();
}
return TRUE;
}
}
return __super::PreTranslateMessage(pMsg);
}
BOOL CSearchBarDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);
pDC->FillSolidRect(rect, RGB(40, 40, 40));
return TRUE;
}
HBRUSH CSearchBarDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = __super::OnCtlColor(pDC, pWnd, nCtlColor);
// 输入框深色背景
if (nCtlColor == CTLCOLOR_EDIT && pWnd->GetDlgCtrlID() == IDC_SEARCH_EDIT) {
pDC->SetTextColor(RGB(240, 240, 240));
pDC->SetBkColor(RGB(60, 60, 60));
return m_brushEdit;
}
// 静态文本(计数)- 使用实色背景避免重绘问题
if (nCtlColor == CTLCOLOR_STATIC) {
pDC->SetTextColor(RGB(180, 180, 180));
pDC->SetBkColor(RGB(40, 40, 40));
return m_brushStatic;
}
return hbr;
}
void CSearchBarDlg::Show()
{
if (!GetSafeHwnd()) {
Create(IDD_SEARCH_BAR, m_pParent);
}
UpdatePosition();
ShowWindow(SW_SHOWNOACTIVATE);
SetWindowPos(&wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
// 让输入框获得焦点并选中所有文本
m_editSearch.SetFocus();
m_editSearch.SetSel(0, -1);
}
void CSearchBarDlg::Hide()
{
KillTimer(TIMER_SEARCH); // 清理定时器
ShowWindow(SW_HIDE);
// 关掉搜索栏就撤掉筛选。否则列表停在被筛过的状态,用户看到“少了很多机器”
// 却找不到原因 —— 筛选条已经关掉了,没有任何地方提示当前处于筛选状态。
if (m_pParent && !m_strLastSearch.IsEmpty()) {
m_strLastSearch.Empty();
m_editSearch.SetWindowText(_T(""));
m_pParent->SetHostFilter(CString());
}
// 将焦点还给主窗口列表
if (m_pParent) {
m_pParent->SetFocus();
}
}
void CSearchBarDlg::InvalidateCache()
{
// 杀掉 pending 的搜索定时器,避免竞争条件
KillTimer(TIMER_SEARCH);
// 筛选本身由主窗口的 RebuildFilteredIndices 维护(切分组、主机上下线都会重建),
// 这里只需要把计数刷新一下。
if (GetSafeHwnd() && IsWindowVisible()) {
UpdateCountText();
}
}
void CSearchBarDlg::UpdatePosition()
{
if (!m_pParent || !GetSafeHwnd()) return;
CRect rcParent;
m_pParent->GetWindowRect(&rcParent);
CRect rcThis;
GetWindowRect(&rcThis);
// 居中显示
int x = rcParent.left + (rcParent.Width() - rcThis.Width()) / 2;
int y = rcParent.top + (rcParent.Height() - rcThis.Height()) / 2;
SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
void CSearchBarDlg::OnEnChangeSearch()
{
// 延迟搜索:重置定时器,避免每次输入都搜索
KillTimer(TIMER_SEARCH);
SetTimer(TIMER_SEARCH, SEARCH_DELAY_MS, NULL);
}
void CSearchBarDlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == TIMER_SEARCH) {
KillTimer(TIMER_SEARCH);
DoSearch();
}
__super::OnTimer(nIDEvent);
}
void CSearchBarDlg::DoSearch()
{
CString searchText;
m_editSearch.GetWindowText(searchText);
searchText.Trim();
if (searchText == m_strLastSearch) return;
m_strLastSearch = searchText;
if (!m_pParent) return;
// 交给主窗口按文本重建过滤索引:命中的主机才留在列表里。
// 原来的实现是“在列表里找下一个匹配项并跳过去”,机器一多(几千台)就没法用:
// 用户想的是“把带 LEVI 的都列出来”,而不是一条条按 F3 翻。
m_pParent->SetHostFilter(searchText);
// 列表已经被筛过,把选中项放到第一行,键盘可以直接接着操作
CListCtrl& list = m_pParent->m_CList_Online;
const int count = list.GetItemCount();
if (count > 0) {
int sel = -1;
while ((sel = list.GetNextItem(sel, LVNI_SELECTED)) != -1) {
list.SetItemState(sel, 0, LVIS_SELECTED | LVIS_FOCUSED);
}
list.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
list.EnsureVisible(0, FALSE);
}
UpdateCountText();
}
void CSearchBarDlg::GotoPrev()
{
MoveSelection(-1);
}
void CSearchBarDlg::GotoNext()
{
MoveSelection(+1);
}
// 在(已经筛选过的)列表里上下移动选中项。列表本身就是结果集,
// 所以这里不再需要维护 clientID → 行号的映射表。
void CSearchBarDlg::MoveSelection(int delta)
{
if (!m_pParent) return;
CListCtrl& list = m_pParent->m_CList_Online;
const int count = list.GetItemCount();
if (count <= 0) return;
const int cur = list.GetNextItem(-1, LVNI_SELECTED);
int next = (cur < 0) ? 0 : cur + delta;
if (next < 0) next = count - 1; // 循环
if (next >= count) next = 0; // 循环
if (cur >= 0) {
list.SetItemState(cur, 0, LVIS_SELECTED | LVIS_FOCUSED);
}
list.SetItemState(next, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
list.EnsureVisible(next, FALSE);
}
void CSearchBarDlg::UpdateCountText()
{
CString text;
CString searchText;
m_editSearch.GetWindowText(searchText);
searchText.Trim();
if (!searchText.IsEmpty() && m_pParent) {
const int count = m_pParent->m_CList_Online.GetItemCount();
if (count > 0) {
text.Format(_T("%d"), count); // 匹配台数(列表里就这么多行)
} else {
text = _TR("无匹配");
}
}
m_staticCount.SetWindowText(text);
m_staticCount.Invalidate();
m_staticCount.UpdateWindow();
}
void CSearchBarDlg::OnBnClickedPrev()
{
GotoPrev();
}
void CSearchBarDlg::OnBnClickedNext()
{
GotoNext();
}
void CSearchBarDlg::OnBnClickedClose()
{
Hide();
}