Files
msime-windows/ui/src/ControlsInternal.h
T
fanlusky c3bfa69a29 refactor(ui): 按控件拆分 Controls.cpp
Controls.cpp 有 4200 行,Image、Button、CheckBox、ProgressBar、Slider、Separator、
Popup 及其宿主、菜单行、ComboBox、ListView、CandidateList、TreeView、TabControl、
Accordion 的实现,连同它们共用的文字测量与绘制工具全挤在一个文件里。沿用
TextBox.cpp 的做法,按控件拆成以控件命名的翻译单元:

- Image.cpp:Image(含只它用到的 WIC 读图尺寸)
- Button.cpp:Button 与 CheckBox(含只 Button 用到的 DrawTextBlock)
- RangeControls.cpp:ProgressBar 与 Slider
- Separator.cpp:Separator
- Popup.cpp:Popup(含 LayoutOverlay)、PopupHost、ContextMenuHost,以及只有
  Popup 用到的 DeflateSizeLocal / DeflateRectLocal 和 DrawPopupShadow
- Menu.cpp:MenuFlyoutItem 与 MenuSeparator(含托盘菜单字号常量和
  EstimateTrayLabelWidth)
- ComboBox.cpp:ComboBox 与下拉列表 ComboBoxPopupContent
- ListView.cpp:ListView
- CandidateList.cpp:CandidateList 及其文字度量缓存(含 FillRoundedRectCorners
  和彩色字体裁剪选项)
- TreeView.cpp、TabControl.cpp、Accordion.cpp:各自的控件
- ControlsInternal.h / ControlsInternal.cpp:多个控件共用的 GetSharedDWriteFactory、
  PointInRect、PointInRoundedRect、MeasureText、CreateCachedTextLayout、
  FillRoundedRect、DrawLabel 和两个共用圆角常量,只给 src/ 下的实现文件包含

纯搬移,不改行为:函数体、注释和格式都没动。多个文件共用的工具放进
msimeui::controls_detail 命名空间,声明在 ControlsInternal.h、定义只在
ControlsInternal.cpp 里有一份——CreateCachedTextLayout 的函数内静态
formatCache 和 GetSharedDWriteFactory 的静态工厂因此仍是全库唯一的一份,
不会按翻译单元各复制一份。只被一个控件用到的工具和常量仍留在该文件的匿名
命名空间。原来没人用的 MakeInsetRect 和 kCandidateItemGap 原样保留,分别放在
ControlsInternal.cpp 和 CandidateList.cpp 的匿名命名空间里。
using Microsoft::WRL::ComPtr 挪进内部头文件。Controls.cpp 已经没有剩余内容,
删除;ui/CMakeLists.txt 的源文件列表相应更新,Server 通过 add_subdirectory
引入 msimeui,不需要改。

非搬移改动只有两处:CreateCachedTextLayout 和 DrawLabel 的默认实参从定义挪到了
头文件里的声明上,DrawLabel 定义的形参因此由 clang-format 重新折行。

ui/scripts/check-boundary.py 仍然通过,没有新增对产品的依赖。用脚本比对了原文件
与拆分后全部文件的非空、非 #include 行多重集:除上述两个函数签名外,差异只有
新增的声明、命名空间包装、using 指令、#pragma once 和文件头注释。可用
git diff --color-moved 复核。
2026-09-27 17:28:16 +08:00

43 lines
2.1 KiB
C++

#pragma once
// Declarations shared by the control implementation files (Button.cpp, ListView.cpp, ...). Internal to msimeui:
// only src/*.cpp include this, never the public headers or product code.
#include "msimeui/Controls.h"
#include <d2d1.h>
#include <dwrite.h>
#include <string>
#include <vector>
#include <wrl/client.h>
namespace msimeui
{
using Microsoft::WRL::ComPtr;
namespace controls_detail
{
constexpr float kControlCornerRadius = 12.0f;
constexpr float kListItemCornerRadius = 14.0f;
IDWriteFactory *GetSharedDWriteFactory();
bool PointInRect(const RectF &rect, const PointF &point);
bool PointInRoundedRect(const RectF &rect, float radius, const PointF &point);
SizeF MeasureText(IDWriteFactory *factory, const std::wstring &text, float fontSize, bool bold, float maxWidth);
// Defined once in ControlsInternal.cpp so every control shares the one text-format cache.
ComPtr<IDWriteTextLayout> CreateCachedTextLayout(IDWriteFactory *factory, const std::wstring &fontFamily,
const std::wstring &text, float fontSize,
DWRITE_FONT_WEIGHT fontWeight, float width, float height,
DWRITE_TEXT_ALIGNMENT textAlignment,
DWRITE_PARAGRAPH_ALIGNMENT paragraphAlignment,
DWRITE_WORD_WRAPPING wordWrapping,
const std::vector<std::wstring> &fallbackFamilies = {});
void FillRoundedRect(DeviceResources &deviceResources, const RectF &bounds, float radius, D2D1_COLOR_F fill,
D2D1_COLOR_F stroke, float strokeWidth);
void DrawLabel(DeviceResources &deviceResources, const std::wstring &text, float fontSize, bool bold,
D2D1_COLOR_F color, const RectF &rect, DWRITE_TEXT_ALIGNMENT alignment,
DWRITE_PARAGRAPH_ALIGNMENT paragraphAlignment,
DWRITE_WORD_WRAPPING wrapping = DWRITE_WORD_WRAPPING_NO_WRAP);
} // namespace controls_detail
} // namespace msimeui