替换 NIM 为独立 TTUI 引擎并完善窗口交互
移除 NIM 源码及主题依赖,加入 Win32、Direct2D、DirectWrite UI 静态库。迁移主页面、添加任务及外观设置,完善虚拟列表、统一窗口透明度和首次显示初始化。增加 Windows 7 API 回退与回归测试,在 AGENTS.md 记录后续控件体系和布局系统计划。
This commit is contained in:
1 parent
7c0ee5d964
commit
db38ec5c03
408 files changed
+1575
-55735
No files matched your search
@@ -0,0 +1,215 @@
|
||||
#include "ttui/application.h"
|
||||
#include "ttui/window.h"
|
||||
#include "ttui/virtual_list.h"
|
||||
#include "platform.h"
|
||||
#include "task_store.h"
|
||||
#include "settings_form.h"
|
||||
#include "add_task_form.h"
|
||||
#include <future>
|
||||
#include <commctrl.h>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
static void Check(bool ok,const char* message) { if (!ok) throw std::runtime_error(message); }
|
||||
static void CheckAlpha(ttui::Window& window,int percent) {
|
||||
if (percent==100 && !(GetWindowLongPtrW(window.GetHWND(),GWL_EXSTYLE)&WS_EX_LAYERED)) return;
|
||||
BYTE alpha=0; DWORD flags=0;
|
||||
Check(GetLayeredWindowAttributes(window.GetHWND(),nullptr,&alpha,&flags)!=FALSE,"read actual window alpha");
|
||||
Check((flags&LWA_ALPHA) && alpha==(percent*255+50)/100,"native window alpha matches percentage");
|
||||
}
|
||||
class SmokeWindow final : public ttui::Window {
|
||||
public:
|
||||
HWND edit=nullptr;
|
||||
int commands=0,paints=0;
|
||||
bool painted=false;
|
||||
HWND renderer_window=nullptr;
|
||||
BYTE initial_alpha=255;
|
||||
bool initially_visible=false;
|
||||
~SmokeWindow() override { if (IsOpen()) DestroyWindow(GetHWND()); }
|
||||
private:
|
||||
void OnCreate() override {
|
||||
initially_visible=IsWindowVisible(GetHWND())!=FALSE;
|
||||
DWORD flags=0;
|
||||
if (GetWindowLongPtrW(GetHWND(),GWL_EXSTYLE)&WS_EX_LAYERED)
|
||||
GetLayeredWindowAttributes(GetHWND(),nullptr,&initial_alpha,&flags);
|
||||
edit=CreateEdit(100,L"输入测试");
|
||||
}
|
||||
void OnLayout() override { Place(edit,{10,10,300,44}); ClearButtons(); Button(200,L"按钮",{10,60,130,100}); }
|
||||
void OnPaint(ttui::Canvas& c) override {
|
||||
++paints; renderer_window=c.Target()->GetHwnd(); DrawBackground(c,1); c.Text(L"TTUI 中文绘制",{10,110,300,150}); c.Progress({10,160,300,166},.5); painted=true;
|
||||
}
|
||||
void OnCommand(int) override { ++commands; }
|
||||
};
|
||||
int main() {
|
||||
try {
|
||||
ttui::VirtualList list;
|
||||
list.Configure(100000,10); list.ScrollTo(999999);
|
||||
auto [first,last]=list.Visible(); Check(first==99990 && last==100000,"viewport end clamp");
|
||||
list.Select(123); list.RevealSelection(); Check(list.First()==123,"selection above viewport");
|
||||
list.Select(900); list.RevealSelection(); Check(list.First()==891,"selection below viewport");
|
||||
list.Configure(3,10); Check(list.First()==0 && list.Selected()==2,"list shrink clamps state");
|
||||
list.Configure(0,0); Check(list.Selected()==-1 && list.Visible().first==list.Visible().second,"empty viewport");
|
||||
list.Configure(100000,10); Check(list.Visible().second-list.Visible().first<=11,"bounded row rendering");
|
||||
const std::wstring sample=L"中文路径/任务🚀";
|
||||
Check(platform::FromUtf8(platform::ToUtf8(sample))==sample,"UTF-8 roundtrip");
|
||||
Check(platform::FromUtf8(std::string("\xff",1)).empty(),"invalid UTF-8 rejected");
|
||||
Check(platform::ToUtf8(std::wstring(1,static_cast<wchar_t>(0xD800))).empty(),"unpaired surrogate rejected");
|
||||
std::wstring embedded(L"a\0b",3); Check(platform::FromUtf8(platform::ToUtf8(embedded))==embedded,"embedded NUL conversion");
|
||||
|
||||
// All database access stays on a worker, using an isolated test file.
|
||||
auto database_test=std::async(std::launch::async,[] {
|
||||
auto folder=std::filesystem::current_path()/L"ui"/L"ttui"/L"obj";
|
||||
std::filesystem::create_directories(folder);
|
||||
auto path=folder/(L"test_tasks_"+std::to_wstring(GetCurrentProcessId())+L".db");
|
||||
TaskStore store; Check(store.Open(path.wstring()),"open isolated SQLite store");
|
||||
TaskInfo task; task.url=L"https://example.test/中文🚀.bin";
|
||||
task.file_name=L"中文🚀.bin"; task.save_path=L"D:\\下载\\中文🚀.bin";
|
||||
Check(store.Insert(task) && task.id>0,"insert task with Unicode");
|
||||
Check(store.UpdateProgress(task.id,123,456),"persist progress");
|
||||
Check(store.UpdateState(task.id,TaskState::kPaused,123,456,L"暂停测试"),"persist state");
|
||||
store.Close(); Check(store.Open(path.wstring()),"reopen store");
|
||||
auto restored=store.LoadTasks();
|
||||
Check(restored.size()==1 && restored[0].url==task.url && restored[0].save_path==task.save_path
|
||||
&& restored[0].file_name==task.file_name && restored[0].state==TaskState::kPaused
|
||||
&& restored[0].downloaded_bytes==123 && restored[0].error==L"暂停测试","task recovery and UTF-8 persistence");
|
||||
Check(store.DeleteTask(task.id) && store.LoadTasks().empty(),"remove isolated task");
|
||||
store.Close();
|
||||
std::filesystem::remove(path);
|
||||
std::filesystem::remove(path.wstring()+L"-wal"); std::filesystem::remove(path.wstring()+L"-shm");
|
||||
});
|
||||
database_test.get();
|
||||
|
||||
SmokeWindow window;
|
||||
int code=ttui::RunApplication([&] {
|
||||
Check(window.Create(nullptr,L"TTUISmokeWindow",L"TTUI smoke",640,400,true),"create independent window");
|
||||
|
||||
Check((GetWindowLongPtrW(window.GetHWND(),GWL_STYLE)&(WS_THICKFRAME|WS_MAXIMIZEBOX))==0,"windows are fixed size by default");
|
||||
SmokeWindow reopened;
|
||||
for (int generation=0;generation<3;++generation) {
|
||||
Check(reopened.Create(window.GetHWND(),L"TTUIReopenSmoke",L"Reopen smoke",640,240),"recreate the same window object");
|
||||
SendMessageW(reopened.GetHWND(),WM_PAINT,0,0);
|
||||
Check(reopened.paints==generation+1 && reopened.renderer_window==reopened.GetHWND(),"renderer must bind the new HWND on every reopen");
|
||||
SendMessageW(reopened.GetHWND(),WM_LBUTTONDOWN,MK_LBUTTON,MAKELPARAM(static_cast<int>(30*reopened.Scale()),static_cast<int>(75*reopened.Scale())));
|
||||
SendMessageW(reopened.GetHWND(),WM_LBUTTONUP,0,MAKELPARAM(static_cast<int>(30*reopened.Scale()),static_cast<int>(75*reopened.Scale())));
|
||||
Check(reopened.commands==generation+1,"buttons remain interactive after reopen");
|
||||
reopened.Close();
|
||||
}
|
||||
SkinSettings appearance;
|
||||
Check(appearance.window_alpha_percent==100,"default window opacity is 100 percent");
|
||||
SmokeWindow preconfigured;
|
||||
preconfigured.SetOpacity(67);
|
||||
for (int generation=0;generation<3;++generation) {
|
||||
Check(preconfigured.Create(window.GetHWND(),L"TTUIInitialAlphaSmoke",L"Initial alpha smoke",640,240),"create preconfigured translucent window");
|
||||
Check(preconfigured.initial_alpha==(67*255+50)/100 && !preconfigured.initially_visible,"native alpha must be ready before OnCreate and first show");
|
||||
CheckAlpha(preconfigured,67);
|
||||
preconfigured.Close();
|
||||
}
|
||||
SmokeWindow opacity_peer;
|
||||
Check(opacity_peer.Create(window.GetHWND(),L"TTUIOpacityPeer",L"Opacity peer",640,240),"create second window for shared opacity");
|
||||
int previews=0,closed=0;
|
||||
SettingsForm settings(&appearance,[&] { ++previews; window.SetOpacity(appearance.window_alpha_percent); opacity_peer.SetOpacity(appearance.window_alpha_percent); window.Preview(); },[&] { ++closed; });
|
||||
Check(settings.Create(window.GetHWND(),SettingsForm::kClassName.c_str(),L"Settings smoke",600,440),"create appearance settings");
|
||||
HWND slider=GetDlgItem(settings.GetHWND(),100);
|
||||
Check(slider!=nullptr && GetDlgItem(settings.GetHWND(),101)!=nullptr && GetDlgItem(settings.GetHWND(),102)==nullptr,"background coverage and unified window opacity sliders only");
|
||||
SendMessageW(slider,TBM_SETPOS,TRUE,100);
|
||||
int before=previews;
|
||||
SendMessageW(settings.GetHWND(),WM_HSCROLL,MAKEWPARAM(TB_THUMBTRACK,37),reinterpret_cast<LPARAM>(slider));
|
||||
Check(appearance.window_opacity==37 && previews==before+1,"live background preview before release");
|
||||
SendMessageW(slider,TBM_SETPOS,TRUE,52);
|
||||
SendMessageW(settings.GetHWND(),WM_HSCROLL,MAKEWPARAM(TB_LINEUP,0),reinterpret_cast<LPARAM>(slider));
|
||||
Check(appearance.window_opacity==52,"keyboard background slider change");
|
||||
HWND alpha_slider=GetDlgItem(settings.GetHWND(),101);
|
||||
Check(SendMessageW(alpha_slider,TBM_GETRANGEMIN,0,0)==30 && SendMessageW(alpha_slider,TBM_GETRANGEMAX,0,0)==100
|
||||
&& SendMessageW(alpha_slider,TBM_GETPOS,0,0)==100,"window opacity slider bounds and default");
|
||||
SendMessageW(settings.GetHWND(),WM_HSCROLL,MAKEWPARAM(TB_THUMBTRACK,67),reinterpret_cast<LPARAM>(alpha_slider));
|
||||
Check(appearance.window_alpha_percent==67 && appearance.window_opacity==52,"window transparency independent of background coverage");
|
||||
CheckAlpha(window,67); CheckAlpha(opacity_peer,67); CheckAlpha(settings,67);
|
||||
SendMessageW(settings.GetHWND(),WM_HSCROLL,MAKEWPARAM(TB_THUMBTRACK,0),reinterpret_cast<LPARAM>(alpha_slider));
|
||||
Check(appearance.window_alpha_percent==30,"opacity is clamped to 30 percent minimum");
|
||||
CheckAlpha(window,30); CheckAlpha(opacity_peer,30); CheckAlpha(settings,30);
|
||||
SendMessageW(settings.GetHWND(),WM_HSCROLL,MAKEWPARAM(TB_THUMBPOSITION,1000),reinterpret_cast<LPARAM>(alpha_slider));
|
||||
Check(appearance.window_alpha_percent==100,"opacity is clamped to 100 percent maximum");
|
||||
CheckAlpha(window,100); CheckAlpha(opacity_peer,100); CheckAlpha(settings,100);
|
||||
SendMessageW(settings.GetHWND(),WM_HSCROLL,MAKEWPARAM(TB_THUMBTRACK,67),reinterpret_cast<LPARAM>(alpha_slider));
|
||||
opacity_peer.Close();
|
||||
settings.Close(); Check(closed==1,"appearance close callback once");
|
||||
|
||||
|
||||
int submissions=0;
|
||||
std::wstring submitted_url,submitted_directory;
|
||||
AddTaskForm add(&appearance,[&](const std::wstring& url,const std::wstring& directory) {
|
||||
++submissions; submitted_url=url; submitted_directory=directory; return true;
|
||||
});
|
||||
Check(add.Create(window.GetHWND(),AddTaskForm::kClassName.c_str(),L"Add task smoke",640,240),"create independent add window");
|
||||
CheckAlpha(add,67); // Windows created after a slider change inherit shared opacity.
|
||||
HWND url_edit=GetDlgItem(add.GetHWND(),100),path_edit=GetDlgItem(add.GetHWND(),101);
|
||||
Check(url_edit && path_edit,"download fields in add window");
|
||||
SendMessageW(add.GetHWND(),WM_COMMAND,200,0);
|
||||
Check(submissions==0 && !add.Pending() && add.IsOpen(),"blank URL must keep add window open");
|
||||
SetWindowTextW(url_edit,L" https://example.test/中文.bin "); SetWindowTextW(path_edit,L"D:\\下载");
|
||||
SendMessageW(url_edit,WM_KEYDOWN,VK_RETURN,0);
|
||||
Check(submissions==1 && add.Pending() && add.IsOpen() && submitted_url==L"https://example.test/中文.bin"
|
||||
&& submitted_directory==L"D:\\下载","submit entered URL and directory, wait for acknowledgement");
|
||||
SendMessageW(add.GetHWND(),WM_COMMAND,200,0);
|
||||
Check(submissions==1,"prevent duplicate submissions while pending");
|
||||
add.Complete(false,L"链接已存在");
|
||||
Check(add.IsOpen() && !add.Pending() && IsWindowEnabled(url_edit)
|
||||
&& ttui::Window::EditText(url_edit).find(L"example.test")!=std::wstring::npos,"rejection preserves editable input");
|
||||
SendMessageW(add.GetHWND(),WM_COMMAND,200,0); Check(submissions==2 && add.Pending(),"retry after rejection");
|
||||
add.Close();
|
||||
Check(add.Pending() && !add.IsOpen(),"closing pending window retains acknowledgement state");
|
||||
Check(add.Create(window.GetHWND(),AddTaskForm::kClassName.c_str(),L"Add task smoke",640,240),"reopen pending window");
|
||||
Check(!IsWindowEnabled(GetDlgItem(add.GetHWND(),100)),"reopen must not duplicate pending request");
|
||||
add.Complete(true); Check(!add.IsOpen() && !add.Pending(),"acceptance closes add window");
|
||||
Check(add.Create(window.GetHWND(),AddTaskForm::kClassName.c_str(),L"Add task smoke",640,240),"open next add window");
|
||||
Check(ttui::Window::EditText(GetDlgItem(add.GetHWND(),100)).empty()
|
||||
&& ttui::Window::EditText(GetDlgItem(add.GetHWND(),101))==L"D:\\下载","accepted URL cleared and save directory retained");
|
||||
add.Close();
|
||||
|
||||
SetWindowTextW(window.edit,L"中文输入内容");
|
||||
Check(ttui::Window::EditText(window.edit)==L"中文输入内容","native edit roundtrip");
|
||||
Check(window.commands==0,"editing must not submit command");
|
||||
SendMessageW(window.GetHWND(),WM_PAINT,0,0);
|
||||
Check(window.painted,"Direct2D/DirectWrite paint callback");
|
||||
float scale=window.Scale();
|
||||
auto point=[&](int x,int y) { return MAKELPARAM(static_cast<int>(x*scale),static_cast<int>(y*scale)); };
|
||||
SendMessageW(window.GetHWND(),WM_LBUTTONDOWN,MK_LBUTTON,point(30,75));
|
||||
SendMessageW(window.GetHWND(),WM_LBUTTONUP,0,point(30,75));
|
||||
Check(window.commands==1,"button click dispatch");
|
||||
SendMessageW(window.GetHWND(),WM_LBUTTONDOWN,MK_LBUTTON,point(30,75));
|
||||
SendMessageW(window.GetHWND(),WM_LBUTTONUP,0,point(350,200));
|
||||
Check(window.commands==1,"release outside cancels command");
|
||||
SendMessageW(window.GetHWND(),WM_KEYDOWN,VK_RETURN,0); Check(window.commands==2,"keyboard button activation");
|
||||
window.FocusContent();
|
||||
SendMessageW(window.GetHWND(),WM_KEYDOWN,VK_RETURN,0); Check(window.commands==2,"content focus must not activate old button");
|
||||
SendMessageW(window.edit,WM_KEYDOWN,VK_RETURN,0); Check(window.commands==3,"edit Return submits once");
|
||||
SetWindowPos(window.GetHWND(),nullptr,0,0,800,600,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
|
||||
SendMessageW(window.GetHWND(),WM_PAINT,0,0); Check(window.paints>=2,"resize repaint");
|
||||
|
||||
auto image_path=std::filesystem::current_path()/L"ui"/L"ttui"/L"obj"/(L"test_image_"+std::to_wstring(GetCurrentProcessId())+L".bmp");
|
||||
const unsigned char bmp[]={
|
||||
0x42,0x4d,58,0,0,0,0,0,0,0,54,0,0,0,40,0,0,0,
|
||||
1,0,0,0,1,0,0,0,1,0,24,0,0,0,0,0,4,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0x20,0x80,0xe0,0
|
||||
};
|
||||
{ std::ofstream file(image_path,std::ios::binary); file.write(reinterpret_cast<const char*>(bmp),sizeof(bmp)); }
|
||||
window.SetBackgroundImage(image_path.wstring());
|
||||
auto start=std::chrono::steady_clock::now();
|
||||
while (!window.HasBackgroundImage() && std::chrono::steady_clock::now()-start<std::chrono::seconds(5)) {
|
||||
MSG message{};
|
||||
while (PeekMessageW(&message,nullptr,0,0,PM_REMOVE)) { TranslateMessage(&message); DispatchMessageW(&message); }
|
||||
Sleep(5);
|
||||
}
|
||||
Check(window.HasBackgroundImage(),"asynchronous WIC background decode");
|
||||
SendMessageW(window.GetHWND(),WM_PAINT,0,0);
|
||||
window.SetBackgroundImage(L""); Check(!window.HasBackgroundImage(),"clear background cache");
|
||||
std::filesystem::remove(image_path);
|
||||
window.Close(); return true;
|
||||
});
|
||||
Check(code==0 && !window.IsOpen(),"message loop and native window cleanup");
|
||||
std::cout<<"PASS: virtual viewport, UTF conversion, isolated SQLite recovery, edit dispatch, Direct2D paint, buttons, focus, resize, WIC background, background coverage tracking, unified live window alpha, pre-show alpha initialization, repeated HWND rendering, add-window submission lifecycle, shutdown\n";
|
||||
return 0;
|
||||
} catch (const std::exception& error) { std::cerr<<"FAIL: "<<error.what()<<"\n"; return 1; }
|
||||
}
|
||||
Reference in new issue
Block a user