Add independent sidebar task pages and persistent deleted history

This commit is contained in:
tongtong committed 2026-11-20 18:46:57 +08:00
1 parent 3cf197a98c
commit a787f470ed
6 files changed
+141 -80

No files matched your search

+15 -8
View File
@@ -399,7 +399,7 @@ void DownloadEngine::ThreadMain()
pause_current_ = false;
auto live = tasks_.find(id);
if (live != tasks_.end())
if (live != tasks_.end() && live->second.state!=TaskState::kDeleted)
live->second = task;
continue;
}
@@ -470,6 +470,10 @@ void DownloadEngine::HandleAdd(const Command& cmd)
for (const auto& entry : tasks_)
{
if (entry.second.state==TaskState::kDeleted) {
if (!cmd.external.request_id.empty() && entry.second.request_id==cmd.external.request_id) { reject(L"原下载任务已删除"); return; }
continue;
}
if (!cmd.external.request_id.empty() && entry.second.request_id==cmd.external.request_id && entry.second.url!=url) {
reject(L"请求编号已用于其他下载地址"); return;
}
@@ -524,7 +528,7 @@ void DownloadEngine::HandleAdd(const Command& cmd)
void DownloadEngine::HandlePause(int64_t id)
{
auto it = tasks_.find(id);
if (it == tasks_.end())
if (it == tasks_.end() || it->second.state==TaskState::kDeleted)
return;
if (id == current_id_)
@@ -549,7 +553,7 @@ void DownloadEngine::HandlePause(int64_t id)
void DownloadEngine::HandleResume(int64_t id)
{
auto it = tasks_.find(id);
if (it == tasks_.end())
if (it == tasks_.end() || it->second.state==TaskState::kDeleted)
return;
if (id == current_id_)
@@ -580,9 +584,13 @@ void DownloadEngine::HandleResume(int64_t id)
void DownloadEngine::HandleDelete(int64_t id, bool delete_file)
{
auto it = tasks_.find(id);
if (it == tasks_.end())
if (it == tasks_.end() || it->second.state==TaskState::kDeleted)
return;
auto deleted=it->second; deleted.state=TaskState::kDeleted;
if (!store_open_ || !store_.UpdateState(id,TaskState::kDeleted,deleted.downloaded_bytes,deleted.total_bytes,L"")) {
PostRejected(L"无法保存删除记录,任务未删除"); return;
}
const std::wstring part_path = it->second.save_path + L".tdpart";
const std::wstring save_path = it->second.save_path;
if (id == current_id_)
@@ -598,14 +606,13 @@ void DownloadEngine::HandleDelete(int64_t id, bool delete_file)
::DeleteFileW(save_path.c_str());
}
tasks_.erase(it);
it->second=deleted;
queue_.erase(std::remove(queue_.begin(), queue_.end(), id), queue_.end());
if (store_open_)
store_.DeleteTask(id);
EngineEvent* event = new EngineEvent;
event->type = EngineEventType::kTaskRemoved;
event->task.id = id;
event->task = deleted;
PostEvent(event);
}
+104 -70
View File
@@ -113,7 +113,9 @@ namespace
return L"已完成 · " + FormatSize(info.total_bytes > 0 ? info.total_bytes : info.downloaded_bytes);
case TaskState::kFailed:
return L"下载失败:" + (info.error.empty() ? std::wstring(L"未知错误") : info.error);
case TaskState::kPaused:
case TaskState::kDeleted: return L"已删除";
case TaskState::kScheduled: return L"计划任务";
case TaskState::kPaused:
if (info.total_bytes > 0)
{
int percent = static_cast<int>(info.downloaded_bytes * 100 / info.total_bytes);
@@ -132,7 +134,7 @@ namespace
const std::wstring DownloaderForm::kClassName=L"TTDownloaderMainWindow";
namespace { enum { kAdd=200,kStart,kPause,kDelete,kSettings,kExit }; }
namespace { enum { kAdd=200,kStart,kPause,kDelete,kSettings,kExit,kDownloading,kCompleted,kDeleted,kScheduled }; }
DownloaderForm::DownloaderForm():taskbar_created_message_(RegisterWindowMessageW(L"TaskbarCreated")) {
LoadSkinSettings();
SetOpacity(skin_.window_alpha_percent);
@@ -144,8 +146,9 @@ DownloaderForm::~DownloaderForm() {
StopEngine();
}
void DownloaderForm::OnCreate() {
scrollbar_=CreateWindowExW(0,L"SCROLLBAR",nullptr,WS_CHILD|WS_VISIBLE|SBS_VERT,
0,0,1,1,GetHWND(),nullptr,GetModuleHandleW(nullptr),nullptr);
for (auto* page:{&downloading_page_,&completed_page_,&deleted_page_,&scheduled_page_})
page->scrollbar=CreateWindowExW(0,L"SCROLLBAR",nullptr,WS_CHILD|SBS_VERT,0,0,1,1,GetHWND(),nullptr,GetModuleHandleW(nullptr),nullptr);
::ShowWindow(Page().scrollbar,SW_SHOW);
HMENU menu_bar=CreateMenu();
HMENU file_menu=CreatePopupMenu();
HMENU settings_menu=CreatePopupMenu();
@@ -161,43 +164,76 @@ void DownloaderForm::OnCreate() {
engine_=std::make_unique<DownloadEngine>(GetHWND()); engine_->Start();
browser_bridge_=StartBrowserBridge(*engine_,GetHWND());
}
void DownloaderForm::SyncPages() {
TaskPage* pages[]={&downloading_page_,&completed_page_,&deleted_page_,&scheduled_page_};
for (int category=0;category<4;++category) {
auto& page=*pages[category]; int selected=page.viewport.Selected();
int64_t id=selected>=0 && selected<static_cast<int>(page.rows.size())?page.rows[selected].info.id:0;
page.rows.clear(); int next=-1;
for (const auto& row:rows_) {
auto state=row.info.state;
int target=state==TaskState::kCompleted?1:state==TaskState::kDeleted?2:state==TaskState::kScheduled?3:0;
if (target!=category) continue;
if (row.info.id==id) next=static_cast<int>(page.rows.size());
page.rows.push_back(row);
}
page.viewport.Configure(page.rows.size(),static_cast<int>(std::max(1.f,(list_rect_.bottom-list_rect_.top)/64)));
page.viewport.Select(next);
}
UpdateViewport();
}
void DownloaderForm::SwitchPage(int page) {
if (page==active_page_) return;
::ShowWindow(Page().scrollbar,SW_HIDE); active_page_=page;
::ShowWindow(Page().scrollbar,SW_SHOW); OnLayout(); UpdateSummary(); UpdateSelectedInfo(); Invalidate();
}
void DownloaderForm::OnLayout() {
float w=Width(),h=Height();
list_rect_={16,48,w-34,std::max(48.f,h-100)};
Place(scrollbar_,{w-32,48,w-16,std::max(49.f,h-100)});
list_rect_={168,48,w-34,std::max(48.f,h-100)};
Place(Page().scrollbar,{w-32,48,w-16,std::max(49.f,h-100)});
ClearButtons();
const wchar_t* labels[]={L"正在下载",L"下载完成",L"已删除",L"计划任务"};
for (int i=0;i<4;++i) Button(kDownloading+i,labels[i],{16,24+i*48.f,148,62+i*48.f},active_page_==i);
float y=h-58;
Button(kStart,L"开始",{16,y,88,y+34});
Button(kPause,L"暂停",{96,y,168,y+34});
Button(kDelete,L"删除选中",{176,y,276,y+34});
if (active_page_==0) Button(kStart,L"开始",{168,y,240,y+34});
if (active_page_==0) Button(kPause,L"暂停",{248,y,320,y+34});
if (active_page_<2) Button(kDelete,L"删除选中",{328,y,428,y+34});
UpdateViewport();
}
void DownloaderForm::OnPaint(ttui::Canvas& c) {
float w=Width(),h=Height();
c.Text(L"任务",{28,20,w-320,46},0x64748B,13,true);
c.Text(L"进度",{w-310,20,w-132,46},0x64748B,13,true);
c.Text(L"速度",{w-124,20,w-36,46},0x64748B,13,true);
if (active_page_==3) {
c.Text(L"计划任务",{180,20,w-28,54},0x64748B,18,true);
c.Text(L"暂无计划任务",{184,82,w-44,142},0x8492A6,17);
c.Text(L"定时创建与执行功能尚未接入",{184,146,w-44,180},0x64748B,13);
return;
}
c.Text(L"任务",{180,20,w-320,46},0x64748B,13,true);
c.Text(active_page_==0?L"进度":L"文件大小",{w-310,20,w-132,46},0x64748B,13,true);
c.Text(active_page_==0?L"速度":L"状态",{w-124,20,w-36,46},0x64748B,13,true);
c.Clip(list_rect_);
auto [first,last]=list_.Visible();
auto [first,last]=Page().viewport.Visible();
for (int i=first;i<last;++i) {
float y=list_rect_.top+(i-first)*64.f;
const auto& row=rows_[i];
if (i==list_.Selected()) c.Fill({18,y+8,20,y+56},0x307EF3);
c.Text(row.info.file_name.empty()?ParseFileNameFromUrl(row.info.url):row.info.file_name,{28,y+3,w-330,y+29});
c.Text(FormatStatus(row.info),{28,y+29,w-330,y+57},0x64748B,12);
const auto& row=Rows()[i];
if (i==Page().viewport.Selected()) c.Fill({170,y+8,172,y+56},0x307EF3);
c.Text(row.info.file_name.empty()?ParseFileNameFromUrl(row.info.url):row.info.file_name,{180,y+3,w-330,y+29});
c.Text(FormatStatus(row.info),{180,y+29,w-330,y+57},0x64748B,12);
double fraction=row.info.total_bytes>0?static_cast<double>(row.info.downloaded_bytes)/row.info.total_bytes:0;
c.Progress({w-310,y+28,w-152,y+34},fraction);
if (active_page_==0) c.Progress({w-310,y+28,w-152,y+34},fraction);
else c.Text(FormatSize(row.info.total_bytes),{w-310,y,w-152,y+64},0x64748B,12);
std::wstring speed=row.info.state==TaskState::kDownloading?FormatSpeed(row.speed):
row.info.state==TaskState::kCompleted?L"完成":row.info.state==TaskState::kPaused?L"暂停":row.info.state==TaskState::kFailed?L"失败":L"等待";
row.info.state==TaskState::kDeleted?L"已删除":row.info.state==TaskState::kScheduled?L"计划":row.info.state==TaskState::kCompleted?L"完成":row.info.state==TaskState::kPaused?L"暂停":row.info.state==TaskState::kFailed?L"失败":L"等待";
c.Text(speed,{w-124,y,w-38,y+64},0x64748B,12);
c.Fill({28,y+63,w-46,y+64},0xE8EDF5);
c.Fill({180,y+63,w-46,y+64},0xE8EDF5);
}
if (rows_.empty()) c.Text(L"从“文件 → 添加”新建下载任务",{44,82,w-44,142},0x8492A6,17);
if (Rows().empty()) c.Text(active_page_==0?L"从“文件 → 添加”新建下载任务":active_page_==1?L"暂无已完成的下载":active_page_==2?L"暂无已删除的任务":L"暂无计划任务",{184,82,w-44,142},0x8492A6,17);
c.Unclip();
c.Text(summary_,{28,h-88,w-28,h-62},0x64748B,12);
c.Text(selected_text_,{292,h-58,w-28,h-24},0x64748B,12);
c.Text(selected_text_,{444,h-58,w-28,h-24},0x64748B,12);
}
void DownloaderForm::OnCommand(int id) {
if (id>=kDownloading && id<=kScheduled) { SwitchPage(id-kDownloading); return; }
switch(id) {
case kAdd: ShowAddTaskForm(); break;
case kStart: StartSelectedTask(); break;
@@ -208,10 +244,10 @@ void DownloaderForm::OnCommand(int id) {
}
}
void DownloaderForm::UpdateViewport() {
list_.Configure(rows_.size(),static_cast<int>(std::max(1.f,(list_rect_.bottom-list_rect_.top)/64)));
Page().viewport.Configure(Rows().size(),static_cast<int>(std::max(1.f,(list_rect_.bottom-list_rect_.top)/64)));
SCROLLINFO info{sizeof(info),SIF_RANGE|SIF_PAGE|SIF_POS};
info.nMin=0; info.nMax=std::max(0,list_.Count()-1); info.nPage=list_.Page(); info.nPos=list_.First();
SetScrollInfo(scrollbar_,SB_CTL,&info,TRUE); Invalidate();
info.nMin=0; info.nMax=std::max(0,Page().viewport.Count()-1); info.nPage=Page().viewport.Page(); info.nPos=Page().viewport.First();
SetScrollInfo(Page().scrollbar,SB_CTL,&info,TRUE); Invalidate();
}
std::optional<LRESULT> DownloaderForm::OnMessage(UINT msg,WPARAM wp,LPARAM lp) {
if (msg==WM_COPYDATA) {
@@ -242,7 +278,7 @@ std::optional<LRESULT> DownloaderForm::OnMessage(UINT msg,WPARAM wp,LPARAM lp) {
for (int i=0;i<255 && PeekMessageW(&next,GetHWND(),DownloadEngine::kEventMessage,DownloadEngine::kEventMessage,PM_REMOVE);++i) {
event.reset(reinterpret_cast<EngineEvent*>(next.lParam)); if (event) HandleEngineEvent(*event);
}
Invalidate(); return 0;
SyncPages(); UpdateSummary(); UpdateSelectedInfo(); Invalidate(); return 0;
}
if (msg==WM_SYSCOMMAND && (wp&0xfff0)==SC_MINIMIZE) {
ShowWindow(SW_HIDE); if (settings_form_) settings_form_->ShowWindow(SW_HIDE);
@@ -254,31 +290,31 @@ std::optional<LRESULT> DownloaderForm::OnMessage(UINT msg,WPARAM wp,LPARAM lp) {
if (add_task_form_) add_task_form_->Close();
if (settings_form_) settings_form_->Close(); StopEngine(); RemoveTrayIcon();
}
if (msg==WM_VSCROLL && reinterpret_cast<HWND>(lp)==scrollbar_) {
int first=list_.First(); SCROLLINFO info{sizeof(info),SIF_TRACKPOS}; GetScrollInfo(scrollbar_,SB_CTL,&info);
if (msg==WM_VSCROLL && reinterpret_cast<HWND>(lp)==Page().scrollbar) {
int first=Page().viewport.First(); SCROLLINFO info{sizeof(info),SIF_TRACKPOS}; GetScrollInfo(Page().scrollbar,SB_CTL,&info);
switch(LOWORD(wp)) {
case SB_LINEUP: --first; break; case SB_LINEDOWN: ++first; break;
case SB_PAGEUP: first-=list_.Page(); break; case SB_PAGEDOWN: first+=list_.Page(); break;
case SB_PAGEUP: first-=Page().viewport.Page(); break; case SB_PAGEDOWN: first+=Page().viewport.Page(); break;
case SB_THUMBTRACK: case SB_THUMBPOSITION: first=info.nTrackPos; break;
case SB_TOP: first=0; break; case SB_BOTTOM: first=list_.Count(); break;
case SB_TOP: first=0; break; case SB_BOTTOM: first=Page().viewport.Count(); break;
}
list_.ScrollTo(first); UpdateViewport(); return 0;
Page().viewport.ScrollTo(first); UpdateViewport(); return 0;
}
if (msg==WM_MOUSEWHEEL) { list_.ScrollTo(list_.First()-GET_WHEEL_DELTA_WPARAM(wp)/WHEEL_DELTA*3); UpdateViewport(); return 0; }
if (msg==WM_MOUSEWHEEL) { Page().viewport.ScrollTo(Page().viewport.First()-GET_WHEEL_DELTA_WPARAM(wp)/WHEEL_DELTA*3); UpdateViewport(); return 0; }
if (msg==WM_KEYDOWN) {
int selected=list_.Selected();
int selected=Page().viewport.Selected();
if (wp==VK_UP || wp==VK_DOWN || wp==VK_PRIOR || wp==VK_NEXT || wp==VK_HOME || wp==VK_END) {
if (wp==VK_UP) --selected; if (wp==VK_DOWN) ++selected;
if (wp==VK_PRIOR) selected-=list_.Page(); if (wp==VK_NEXT) selected+=list_.Page();
if (wp==VK_HOME) selected=0; if (wp==VK_END) selected=list_.Count()-1;
list_.Select(std::clamp(selected,0,std::max(0,list_.Count()-1))); list_.RevealSelection(); UpdateViewport(); UpdateSelectedInfo(); return 0;
if (wp==VK_PRIOR) selected-=Page().viewport.Page(); if (wp==VK_NEXT) selected+=Page().viewport.Page();
if (wp==VK_HOME) selected=0; if (wp==VK_END) selected=Page().viewport.Count()-1;
Page().viewport.Select(std::clamp(selected,0,std::max(0,Page().viewport.Count()-1))); Page().viewport.RevealSelection(); UpdateViewport(); UpdateSelectedInfo(); return 0;
}
if (wp==VK_DELETE) { DeleteSelectedTask(); return 0; }
}
if (msg==WM_LBUTTONDOWN || msg==WM_LBUTTONDBLCLK) {
POINT point{GET_X_LPARAM(lp),GET_Y_LPARAM(lp)}; int row=HitTestRow(point);
if (row>=0) {
list_.Select(row); FocusContent(); UpdateSelectedInfo(); Invalidate();
Page().viewport.Select(row); FocusContent(); UpdateSelectedInfo(); Invalidate();
if (msg==WM_LBUTTONDBLCLK) OpenSelectedFile(); else PrepareFileDrag(lp);
return 0;
}
@@ -365,6 +401,7 @@ void DownloaderForm::HandleBrowserRequest(BrowserDialogRequest& request) {
};
const auto& id=request.download.request_id;
for (const auto& row:rows_) if (row.info.request_id==id) {
if (row.info.state==TaskState::kDeleted) { reply("canceled","原下载任务已删除"); return; }
if (request.action=="add" && row.info.url!=request.download.url) {
reply("rejected","请求编号已用于其他下载地址"); return;
}
@@ -392,7 +429,7 @@ void DownloaderForm::HandleBrowserRequest(BrowserDialogRequest& request) {
if ((add_task_form_ && (add_task_form_->IsOpen() || add_task_form_->Pending())) || !browser_dialog_id_.empty()) {
reply("busy","请先处理客户端中已打开的添加任务窗口"); return;
}
for (const auto& row:rows_) if (row.info.url==request.download.url) {
for (const auto& row:rows_) if (row.info.state!=TaskState::kDeleted && row.info.url==request.download.url) {
reply("duplicate","下载链接已存在",row.info.id); return;
}
// Bound unpersisted dialog history; persisted request IDs are recovered through the task snapshot.
@@ -408,8 +445,8 @@ void DownloaderForm::HandleBrowserRequest(BrowserDialogRequest& request) {
}
int DownloaderForm::HitTestRow(const POINT& p) const {
if (!list_rect_.Contains(p.x/Scale(),p.y/Scale())) return -1;
int row=list_.First()+static_cast<int>((p.y/Scale()-list_rect_.top)/64);
return row<static_cast<int>(rows_.size())?row:-1;
int row=Page().viewport.First()+static_cast<int>((p.y/Scale()-list_rect_.top)/64);
return row<static_cast<int>(Rows().size())?row:-1;
}
int DownloaderForm::FindRowIndex(int64_t id) const {
auto it=indices_.find(id); return it==indices_.end()?-1:it->second;
@@ -418,7 +455,7 @@ void DownloaderForm::HandleEngineEvent(const EngineEvent& event) {
int index=FindRowIndex(event.task.id);
switch(event.type) {
case EngineEventType::kSnapshot:
rows_.clear(); indices_.clear(); list_.Select(-1);
rows_.clear(); indices_.clear(); Page().viewport.Select(-1);
for (const auto& task:event.tasks) { indices_[task.id]=static_cast<int>(rows_.size()); rows_.push_back({task,0}); }
UpdateViewport(); UpdateSummary();
snapshot_received_=true;
@@ -438,13 +475,8 @@ void DownloaderForm::HandleEngineEvent(const EngineEvent& event) {
case EngineEventType::kTaskUpdated:
if (index>=0) rows_[index]={event.task,event.speed}; break;
case EngineEventType::kTaskRemoved:
if (index>=0) {
int selected=list_.Selected(); rows_.erase(rows_.begin()+index); indices_.erase(event.task.id);
for (int i=index;i<static_cast<int>(rows_.size());++i) indices_[rows_[i].info.id]=i;
list_.Configure(rows_.size(),list_.Page());
list_.Select(selected==index?-1:selected>index?selected-1:selected);
}
UpdateViewport(); UpdateSummary(); break;
if (index>=0) rows_[index]={event.task,0};
break;
case EngineEventType::kTaskRejected:
if (!event.request_id.empty() && event.request_id==browser_dialog_id_ && add_task_form_) {
add_task_form_->Complete(false,event.message); add_task_form_->Close();
@@ -459,16 +491,16 @@ void DownloaderForm::HandleEngineEvent(const EngineEvent& event) {
UpdateSelectedInfo(); Invalidate();
}
void DownloaderForm::UpdateSummary(const std::wstring& tip) {
summary_=L"共 "+std::to_wstring(rows_.size())+L" 个任务";
summary_=L"共 "+std::to_wstring(Rows().size())+L" 个任务";
if (!tip.empty()) summary_+=L"("+tip+L")";
Invalidate();
}
void DownloaderForm::UpdateSelectedInfo() {
int selected=list_.Selected(); selected_text_=selected>=0 && selected<static_cast<int>(rows_.size())?rows_[selected].info.url:L""; Invalidate();
int selected=Page().viewport.Selected(); selected_text_=selected>=0 && selected<static_cast<int>(Rows().size())?Rows()[selected].info.url:L""; Invalidate();
}
void DownloaderForm::OpenSelectedFile() {
int selected=list_.Selected(); if (selected<0 || selected>=static_cast<int>(rows_.size())) return;
auto task=rows_[selected].info;
int selected=Page().viewport.Selected(); if (selected<0 || selected>=static_cast<int>(Rows().size())) return;
auto task=Rows()[selected].info;
if (task.state!=TaskState::kCompleted) { UpdateSummary(L"任务尚未完成"); return; }
if (reinterpret_cast<INT_PTR>(ShellExecuteW(GetHWND(),L"open",task.save_path.c_str(),nullptr,nullptr,SW_SHOWNORMAL))<=32) UpdateSummary(L"无法打开文件");
}
@@ -487,11 +519,11 @@ void DownloaderForm::ApplySkinSettings() {
void DownloaderForm::PauseSelectedTask()
{
int index = list_.Selected();
if (index < 0 || index >= static_cast<int>(rows_.size()))
int index = Page().viewport.Selected();
if (index < 0 || index >= static_cast<int>(Rows().size()))
return;
const TaskInfo& info = rows_[index].info;
const TaskInfo& info = Rows()[index].info;
if (info.state == TaskState::kDownloading || info.state == TaskState::kQueued)
{
engine_->PauseTask(info.id);
@@ -509,11 +541,11 @@ void DownloaderForm::PauseSelectedTask()
void DownloaderForm::StartSelectedTask()
{
int index = list_.Selected();
if (index < 0 || index >= static_cast<int>(rows_.size()))
int index = Page().viewport.Selected();
if (index < 0 || index >= static_cast<int>(Rows().size()))
return;
const TaskInfo& info = rows_[index].info;
const TaskInfo& info = Rows()[index].info;
if (info.state == TaskState::kPaused || info.state == TaskState::kFailed)
{
engine_->ResumeTask(info.id);
@@ -539,7 +571,7 @@ void DownloaderForm::PrepareFileDrag(LPARAM lParam)
if (index < 0)
return;
const TaskInfo& info = rows_[index].info;
const TaskInfo& info = Rows()[index].info;
if (info.state != TaskState::kCompleted)
return;
if (::GetFileAttributesW(info.save_path.c_str()) == INVALID_FILE_ATTRIBUTES)
@@ -557,9 +589,9 @@ void DownloaderForm::ShowTaskMenu(LPARAM lParam)
if (index < 0)
return;
list_.Select(index);
Page().viewport.Select(index);
const TaskInfo& info = rows_[index].info;
const TaskInfo& info = Rows()[index].info;
const int64_t task_id = info.id;
bool can_pause = info.state == TaskState::kDownloading || info.state == TaskState::kQueued;
bool can_start = info.state == TaskState::kPaused || info.state == TaskState::kFailed;
@@ -568,7 +600,7 @@ void DownloaderForm::ShowTaskMenu(LPARAM lParam)
::AppendMenuW(menu, MF_STRING | (can_pause ? 0 : MF_GRAYED), 1, L"暂停");
::AppendMenuW(menu, MF_STRING | (can_start ? 0 : MF_GRAYED), 2, L"开始");
::AppendMenuW(menu, MF_SEPARATOR, 0, nullptr);
::AppendMenuW(menu, MF_STRING, 3, L"删除");
::AppendMenuW(menu, MF_STRING | (active_page_>=2?MF_GRAYED:0), 3, L"删除");
::AppendMenuW(menu, MF_STRING, 4, L"打开文件位置");
POINT screen_pt;
@@ -581,10 +613,11 @@ void DownloaderForm::ShowTaskMenu(LPARAM lParam)
if (cmd == 0)
return;
int current = FindRowIndex(task_id);
int current = -1;
for (int i=0;i<static_cast<int>(Rows().size());++i) if (Rows()[i].info.id==task_id) { current=i; break; }
if (current < 0)
return;
list_.Select(current);
Page().viewport.Select(current);
if (cmd == 1)
PauseSelectedTask();
@@ -598,11 +631,11 @@ void DownloaderForm::ShowTaskMenu(LPARAM lParam)
void DownloaderForm::OpenSelectedLocation()
{
int index = list_.Selected();
if (index < 0 || index >= static_cast<int>(rows_.size()))
int index = Page().viewport.Selected();
if (index < 0 || index >= static_cast<int>(Rows().size()))
return;
const std::wstring& path = rows_[index].info.save_path;
const std::wstring& path = Rows()[index].info.save_path;
std::wstring args;
if (::GetFileAttributesW(path.c_str()) != INVALID_FILE_ATTRIBUTES)
{
@@ -624,11 +657,12 @@ void DownloaderForm::OpenSelectedLocation()
void DownloaderForm::DeleteSelectedTask()
{
int index = list_.Selected();
if (index < 0 || index >= static_cast<int>(rows_.size()))
if (active_page_>=2) return;
int index = Page().viewport.Selected();
if (index < 0 || index >= static_cast<int>(Rows().size()))
return;
const int64_t task_id = rows_[index].info.id;
const int64_t task_id = Rows()[index].info.id;
int result = ::MessageBoxW(GetHWND(),
L"确定删除选中的任务吗?\n\n"
L"是:同时删除已下载的文件\n"
+14 -2
View File
@@ -51,9 +51,21 @@ private:
void SaveSkinSettings();
NOTIFYICONDATAW tray_icon_{};
UINT taskbar_created_message_=0;
HWND scrollbar_=nullptr;
struct TaskPage {
std::vector<TaskRow> rows;
ttui::VirtualList viewport;
HWND scrollbar=nullptr;
};
TaskPage downloading_page_,completed_page_,deleted_page_,scheduled_page_;
int active_page_=0;
TaskPage& Page() { return active_page_==0?downloading_page_:active_page_==1?completed_page_:active_page_==2?deleted_page_:scheduled_page_; }
const TaskPage& Page() const { return active_page_==0?downloading_page_:active_page_==1?completed_page_:active_page_==2?deleted_page_:scheduled_page_; }
const std::vector<TaskRow>& Rows() const { return Page().rows; }
void SyncPages();
void SwitchPage(int);
ttui::Rect list_rect_{};
ttui::VirtualList list_;
SkinSettings skin_;
std::unique_ptr<SettingsForm> settings_form_;
std::unique_ptr<AddTaskForm> add_task_form_;
+2
View File
@@ -13,6 +13,8 @@ enum class TaskState : int
kCompleted = 2,
kFailed = 3,
kPaused = 4,
kDeleted = 5,
kScheduled = 6,
};
struct TaskInfo