diff --git a/FILES-UI.md b/FILES-UI.md index 3d781d0..6995cf3 100644 --- a/FILES-UI.md +++ b/FILES-UI.md @@ -204,6 +204,13 @@ Warning і початковий фокус на No. Незбережені зміни у примусово закритих вікнах втрачаються. Закриття процесів і результат записуються до `kswitcher.log`. +- У файловій панелі Ctrl+C копіює позначені або поточний локальний + елемент у файловий буфер Windows. Ctrl+V вставляє файли й папки в + поточний локальний каталог через звичайний діалог Copy і показує + конфлікти та прогрес. Клік або навігація у файловій панелі вибирають + файловий контекст. Клік у командному рядку або введення тексту + повертають текстовий контекст Ctrl+C / Ctrl+V. + - **Commands → Copy full path** або **Ctrl+Shift+C** копіює повний шлях файла чи папки під курсором як текст. У LAN- і FTP-панелях текст включає адресу віддаленого джерела. Для рядка `..` команда не змінює буфер. diff --git a/src/files_main.cpp b/src/files_main.cpp index 9c2b306..490cd3c 100644 --- a/src/files_main.cpp +++ b/src/files_main.cpp @@ -508,6 +508,7 @@ std::wstring panelCaption(const Panel &panel) Panel panels[2]; HWND quickViewWindow{}; int quickViewSource{}; +bool panelClipboardContext=true; void refreshQuickView(const Panel &panel); void toggleQuickView(HWND owner); FilesTerminal terminal; @@ -1712,6 +1713,23 @@ void copyPanelNames(HWND owner) copyFilesText(owner,names,L"Could not copy file names."); } +void copyPanelFiles(HWND owner,const Panel &panel) +{ + if(panel.peer || panel.ftp) + { + setStatus(L"File clipboard copy requires local files."); + return; + } + const auto paths=selectedPanelPaths(panel); + if(paths.empty()) return; + try + { + ks::writeClipboard(owner,ks::fileDropClip(paths)); + setStatus(std::to_wstring(paths.size())+(paths.size()==1?L" item copied":L" items copied")+L" to the file clipboard."); + } + catch(const std::exception &) {setStatus(L"Could not copy files to the clipboard.");} +} + void copyFullPanelPath(HWND owner,const Panel &panel) { const auto row=currentPanelRow(panel); @@ -2023,6 +2041,7 @@ LRESULT CALLBACK terminalInputKeys(HWND input,UINT message,WPARAM wp,LPARAM lp,U if(message==WM_KILLFOCUS) resetCompletion(); if(message==WM_LBUTTONDOWN || message==WM_RBUTTONDOWN) { + panelClipboardContext=false; clearTerminalSelection(); hideCompletion(); } @@ -2067,7 +2086,11 @@ LRESULT CALLBACK terminalInputKeys(HWND input,UINT message,WPARAM wp,LPARAM lp,U } if(message==WM_KEYDOWN && !terminalVisible && wp==VK_TAB) { - if(plainPanelTab()) activatePanel(1-active); + if(plainPanelTab()) + { + panelClipboardContext=true; + activatePanel(1-active); + } return 0; } if(message==WM_KEYDOWN && !terminalVisible && @@ -3599,7 +3622,7 @@ struct CopyRequest std::future validation; std::wstring ftpDestination; bool upload{}; - bool ftpRename{}; + bool ftpRename{}, externalSources{}; }; bool directoryExists(const FilesPeer *peer,const fs::path &path) @@ -4213,9 +4236,10 @@ CopyDestination validateCopyDestination(CopyRequest request,const std::wstring & else request.newName.clear(); } const auto sourceDirectory=request.sources.front().parent_path(); - const auto destination=request.move || request.newName.empty()?request.destination:sourceDirectory; + const bool renameInSource=!request.externalSources && !request.move && !request.newName.empty(); + const auto destination=renameInSource?sourceDirectory:request.destination; std::error_code error; - const auto peer=request.move || request.newName.empty()?request.destinationPeer:request.sourcePeer; + const auto peer=renameInSource?request.sourcePeer:request.destinationPeer; if(!directoryExists(peer,destination)) throw std::runtime_error(ks::utf8(L"The destination folder is unavailable.")); const bool sameHost=(!peer && !request.sourcePeer) || @@ -4373,6 +4397,7 @@ INT_PTR CALLBACK copyDialog(HWND dialog, UINT message, WPARAM wp, LPARAM lp) snapshot.move=request.move; snapshot.ftpDestination=request.ftpDestination; snapshot.upload=request.upload; + snapshot.externalSources=request.externalSources; std::packaged_task validation( [snapshot=std::move(snapshot),value=std::move(value), source=peer(request.sourcePeer),other=peer(request.otherPeer)]() mutable { @@ -5429,7 +5454,7 @@ void pruneSearchResults(Panel &panel,const std::set &removed) updateSelectionSummary(panel); } -void copySelection(HWND owner,Panel &source,Panel &destination,bool move) +void copySelection(HWND owner,Panel &source,Panel &destination,bool move,std::vector external={}) { const auto sourceFtp=source.ftp; const bool ftpRename=move && sourceFtp && !ftpProfileList(source); @@ -5465,6 +5490,7 @@ void copySelection(HWND owner,Panel &source,Panel &destination,bool move) CopyRequest request{{},ftpRename?source.directory:destination.directory}; request.move=move; request.ftpRename=ftpRename; + request.externalSources=!external.empty(); if(ftp) { request.ftpDestination=panelLocation(destination); @@ -5473,7 +5499,7 @@ void copySelection(HWND owner,Panel &source,Panel &destination,bool move) request.otherDirectory=destination.directory; request.sourcePeer=source.peer?&*source.peer:nullptr; request.otherPeer=request.destinationPeer=destination.peer?&*destination.peer:nullptr; - request.sources=selectedPanelPaths(source); + request.sources=request.externalSources?std::move(external):selectedPanelPaths(source); if(request.sources.empty()) return; if(ftpRename && request.sources.size()!=1) { @@ -5486,7 +5512,7 @@ void copySelection(HWND owner,Panel &source,Panel &destination,bool move) if(result!=IDOK) return; const bool renamed=!request.newName.empty(); - const bool renameInSource=renamed && !move && !ftp; + const bool renameInSource=renamed && !move && !ftp && !request.externalSources; const auto targetDirectory=renameInSource?request.sources.front().parent_path():request.destination; const auto *targetPeer=renameInSource?request.sourcePeer:request.destinationPeer; const auto peerId=[](const FilesPeer *peer){return peer?peer->id:std::string{};}; @@ -5578,7 +5604,7 @@ void copySelection(HWND owner,Panel &source,Panel &destination,bool move) ++copied; if(move && fromSearch) removed.insert(path); results.insert(progress.renamedTarget.empty()?to:progress.renamedTarget); - progress.ui.invoke([&] { + if(!request.externalSources) progress.ui.invoke([&] { const auto row=std::find(source.rows.begin(),source.rows.end(),path); if(row!=source.rows.end()) source.marked[row-source.rows.begin()]=false; InvalidateRect(source.list,nullptr,FALSE); @@ -6042,8 +6068,33 @@ void deleteSelection(HWND owner,Panel &panel,bool permanent) warningBrush=nullptr; } +bool pastePanelFiles(HWND owner,Panel &panel) +{ + if(!IsClipboardFormatAvailable(CF_HDROP)) return false; + if(panel.peer || panel.ftp || panel.findOrigin) + { + setStatus(L"Open a local folder before pasting files."); + return true; + } + std::vector paths; + try {paths=ks::readClipboardFilePaths(owner);} + catch(const std::exception &) + { + setStatus(L"Could not read file paths from the clipboard."); + return true; + } + if(paths.empty()) + { + setStatus(L"The clipboard has no usable file paths."); + return true; + } + copySelection(owner,panel,panel,false,std::move(paths)); + return true; +} + void selectPanelEdge(Panel &panel,bool first) { + panelClipboardContext=true; activatePanel(static_cast(&panel-panels)); const auto count=SendMessageW(panel.list,LB_GETCOUNT,0,0); if(count<=0) return; @@ -6459,7 +6510,11 @@ LRESULT CALLBACK panelKeys(HWND window, UINT message, WPARAM wp, LPARAM lp, UINT if(message==WM_LBUTTONDOWN || message==WM_LBUTTONDBLCLK || (message==WM_MOUSEMOVE && (wp&MK_LBUTTON) && GetCapture()==window)) { - if(message==WM_LBUTTONDOWN) clearTerminalSelection(); + if(message==WM_LBUTTONDOWN) + { + panelClipboardContext=true; + clearTerminalSelection(); + } auto &panel=panels[index]; const auto count=SendMessageW(window,LB_GETCOUNT,0,0); if(count<=0) @@ -6512,6 +6567,7 @@ LRESULT CALLBACK panelKeys(HWND window, UINT message, WPARAM wp, LPARAM lp, UINT const bool rangeKey=wp==VK_UP || wp==VK_DOWN || wp==VK_LEFT || wp==VK_RIGHT; if(message==WM_KEYDOWN && (wp==VK_INSERT || (rangeKey && (GetKeyState(VK_SHIFT)&0x8000)))) { + panelClipboardContext=true; auto &panel=panels[index]; const auto row=SendMessageW(window,LB_GETCURSEL,0,0); if(row<0 || static_cast(row)>=panel.marked.size()) return 0; @@ -6553,6 +6609,7 @@ LRESULT CALLBACK panelKeys(HWND window, UINT message, WPARAM wp, LPARAM lp, UINT { if(plainPanelTab()) { + panelClipboardContext=true; activatePanel(1-index); SetFocus(terminalInput); } @@ -6598,7 +6655,11 @@ LRESULT CALLBACK panelKeys(HWND window, UINT message, WPARAM wp, LPARAM lp, UINT (wp==VK_UP || wp==VK_DOWN || wp==VK_PRIOR || wp==VK_NEXT || wp==VK_HOME || wp==VK_END); const auto previous=movesCursor?SendMessageW(window,LB_GETCURSEL,0,0):LB_ERR; auto result=DefSubclassProc(window,message,wp,lp); - if(movesCursor) invalidatePanelCursorChange(window,previous); + if(movesCursor) + { + panelClipboardContext=true; + invalidatePanelCursorChange(window,previous); + } if(message==WM_LBUTTONUP && GetFocus()==window) restoreCommandFocus(); if(message==WM_PAINT && hasPanelSizeColumn(panels[index])) { @@ -7545,6 +7606,7 @@ LRESULT CALLBACK filesWindow(HWND window, UINT message, WPARAM wp, LPARAM lp) case WM_COMMAND: if(reinterpret_cast(lp)==terminalInput && HIWORD(wp)==EN_CHANGE) { + panelClipboardContext=false; clearTerminalSelection(); resetCompletion(); } @@ -7858,6 +7920,17 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show) { const bool ctrl=(GetKeyState(VK_CONTROL)&0x8000)!=0; const bool shift=(GetKeyState(VK_SHIFT)&0x8000)!=0; + const bool fileClipboard=!terminalVisible && (panelFocused || panelClipboardContext) && + commandLineAvailable(window); + if(fileClipboard && ctrl && !shift && (message.wParam=='C' || message.wParam=='V')) + { + if(!(message.lParam&(1LL<<30))) + { + if(message.wParam=='C') copyPanelFiles(window,panels[active]); + else if(!pastePanelFiles(window,panels[active])) SendMessageW(terminalInput,WM_PASTE,0,0); + } + continue; + } if(ctrl && shift && message.wParam=='C' && !terminalVisible && commandLineAvailable(window)) { if(!(message.lParam & (1LL<<30))) copyFullPanelPath(window,panels[active]); diff --git a/src/files_path_navigation.inl b/src/files_path_navigation.inl index b2ab625..3415f20 100644 --- a/src/files_path_navigation.inl +++ b/src/files_path_navigation.inl @@ -331,6 +331,7 @@ LRESULT CALLBACK pathBarInput(HWND window,UINT message,WPARAM wp,LPARAM lp,UINT_ ReleaseCapture(); const int index=GetDlgCtrlID(window)-142; if(index<0 || index>=2) return 0; + panelClipboardContext=true; auto &panel=panels[index]; if(!localNavigationAvailable(panel)) { diff --git a/src/platform.cpp b/src/platform.cpp index 978f588..fd9f044 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -486,6 +486,77 @@ Clip textClip(const std::wstring &text) memcpy(f.data.data(), text.c_str(), f.data.size()); return Clip{{std::move(f)}}; } + +Clip fileDropClip(std::span paths) +{ + std::wstring names; + for(const auto &path:paths) + { + if(!path.is_absolute() || path.native().find(L'\0')!=std::wstring::npos) + throw std::invalid_argument("A file clipboard path must be absolute and contain no nulls."); + names+=path.native(); + names+=L'\0'; + } + if(paths.empty()) names+=L'\0'; + names+=L'\0'; + DROPFILES header{}; + header.pFiles=sizeof(header); + header.fWide=TRUE; + Format files{CF_HDROP,{},Bytes(sizeof(header)+names.size()*sizeof(wchar_t))}; + memcpy(files.data.data(),&header,sizeof(header)); + memcpy(files.data.data()+sizeof(header),names.data(),names.size()*sizeof(wchar_t)); + const DWORD effect=DROPEFFECT_COPY; + Format preferred{0,CFSTR_PREFERREDDROPEFFECT,Bytes(sizeof(effect))}; + memcpy(preferred.data.data(),&effect,sizeof(effect)); + return Clip{{std::move(files),std::move(preferred)}}; +} + +std::vector fileDropPaths(HANDLE medium) +{ + const auto size=medium?GlobalSize(medium):0; + if(size(GlobalLock(medium)); + if(!bytes) return {}; + DROPFILES header{}; + memcpy(&header,bytes,sizeof(header)); + bool terminated=false; + if(header.pFiles>=sizeof(header) && header.pFiles(0))!=bytes+size; + else if(header.pFiles%alignof(wchar_t)==0) + { + const auto names=reinterpret_cast(start); + const auto end=names+(size-header.pFiles)/sizeof(wchar_t); + terminated=std::search_n(names,end,2,L'\0')!=end; + } + } + GlobalUnlock(medium); + if(!terminated) return {}; + const auto drop=reinterpret_cast(medium); + const UINT count=DragQueryFileW(drop,UINT(-1),nullptr,0); + std::vector paths; + std::set seen; + for(UINT index=0;index32767) continue; + std::wstring name(length+1,L'\0'); + if(DragQueryFileW(drop,index,name.data(),length+1)!=length) continue; + name.resize(length); + auto path=std::filesystem::path(std::move(name)).lexically_normal(); + if(path.is_absolute() && seen.insert(path).second) paths.push_back(std::move(path)); + } + return paths; +} + +std::vector readClipboardFilePaths(HWND window) +{ + ClipboardLock lock(window); + return fileDropPaths(GetClipboardData(CF_HDROP)); +} + std::wstring Clip::text() const { for (const auto &f : formats) diff --git a/src/platform.h b/src/platform.h index 20d2429..7e0b1e4 100644 --- a/src/platform.h +++ b/src/platform.h @@ -59,6 +59,9 @@ Clip readClipboard(HWND, size_t limit = 32 * 1024 * 1024, DWORD writeClipboard(HWND, const Clip &); DWORD writeClipboardGuarded(HWND, const Clip &, std::optional expected); Clip textClip(const std::wstring &); +Clip fileDropClip(std::span paths); +std::vector fileDropPaths(HANDLE medium); +std::vector readClipboardFilePaths(HWND window); bool chord(WORD key, UINT modifiers = 0); bool chord(std::span keys); struct SystemVolume