474 lines
19 KiB
C++
474 lines
19 KiB
C++
|
|
bool pathNavigationAvailable(const Panel &panel)
|
|
{
|
|
return !transferProfileList(panel) && !panel.findOrigin && !panel.archive && !panel.directory.empty();
|
|
}
|
|
|
|
enum class PathAction { back, forward, up, edit, source, ancestors, previous, folder };
|
|
|
|
void navigatePanelPath(Panel &panel,const fs::path &path)
|
|
{
|
|
if(path.empty()) return;
|
|
const auto current=folderLocation(panel,panel.directory);
|
|
auto target=folderLocation(panel,path);
|
|
if(sameFolderLocation(current,target)) return;
|
|
closePanelSearch();
|
|
openFolderLocation(panel,target);
|
|
}
|
|
|
|
void navigatePanelHistory(Panel &panel,int direction)
|
|
{
|
|
if(const auto next=panel.navigation.neighbor(direction))
|
|
{
|
|
closePanelSearch();
|
|
openFolderLocation(panel,panel.navigation.history[*next],next);
|
|
}
|
|
}
|
|
|
|
struct PathPart
|
|
{
|
|
PathAction action{};
|
|
std::wstring label;
|
|
fs::path target;
|
|
std::vector<fs::path> hidden;
|
|
RECT rect{};
|
|
bool current{}, dim{}, enabled=true;
|
|
};
|
|
|
|
std::wstring pathSegmentLabel(const fs::path &path,FolderProvider provider)
|
|
{
|
|
if(isTransferFolder(provider) && path==ftpRoot) return L"/";
|
|
return panelParentPath(path,provider==FolderProvider::local).empty()?path.native():path.filename().native();
|
|
}
|
|
|
|
LONG segmentWidth(const fs::path &path,FolderProvider provider)
|
|
{
|
|
return static_cast<LONG>(pathSegmentLabel(path,provider).size()+2)*cellWidth;
|
|
}
|
|
|
|
std::vector<PathPart> panelPathParts(const Panel &panel,RECT bounds)
|
|
{
|
|
const LONG total=bounds.right-bounds.left;
|
|
if(total<=0) return {};
|
|
const LONG width=std::max(1L,std::min(2L*cellWidth,total/7));
|
|
std::vector<PathPart> parts;
|
|
const auto add=[&](PathPart part,LONG length)
|
|
{
|
|
if(length<=0 || bounds.left>=bounds.right) return;
|
|
part.rect={bounds.left,bounds.top,std::min(bounds.right,bounds.left+length),bounds.bottom};
|
|
bounds.left=part.rect.right;
|
|
parts.push_back(std::move(part));
|
|
};
|
|
const auto location=folderLocation(panel,panel.directory);
|
|
const auto provider=location.provider;
|
|
const auto local=provider==FolderProvider::local;
|
|
const auto parent=panelParentPath(panel.directory,local);
|
|
add({.action=PathAction::back,.label=L"←",.enabled=panel.navigation.neighbor(-1).has_value()},width);
|
|
add({.action=PathAction::forward,.label=L"→",.enabled=panel.navigation.neighbor(1).has_value()},width);
|
|
add({.action=PathAction::up,.label=L"↑",.target=parent,.enabled=!parent.empty()},width);
|
|
add({.action=PathAction::edit,.label=L"✎"},width);
|
|
|
|
if(!local)
|
|
{
|
|
const auto available=std::max(1L,(bounds.right-bounds.left)/2);
|
|
const auto sourceWidth=std::min(available,static_cast<LONG>(location.label.size()+2)*cellWidth);
|
|
add({.action=PathAction::source,.label=location.label},sourceWidth);
|
|
}
|
|
|
|
std::vector<fs::path> ancestors, previous;
|
|
for(auto path=parent;!path.empty();path=panelParentPath(path,local)) ancestors.push_back(path);
|
|
std::reverse(ancestors.begin(),ancestors.end());
|
|
if(inFolderBranch(panel.directory,panel.navigation.previousBranch,provider))
|
|
{
|
|
for(auto path=panel.navigation.previousBranch;!sameFolderPath(path,panel.directory,provider);
|
|
path=panelParentPath(path,local))
|
|
previous.push_back(path);
|
|
std::reverse(previous.begin(),previous.end());
|
|
}
|
|
|
|
const LONG room=bounds.right-bounds.left;
|
|
LONG required=segmentWidth(panel.directory,provider);
|
|
for(const auto &path:ancestors) required+=segmentWidth(path,provider);
|
|
for(const auto &path:previous) required+=segmentWidth(path,provider);
|
|
size_t firstAncestor=0,visiblePrevious=previous.size();
|
|
LONG currentWidth=segmentWidth(panel.directory,provider);
|
|
if(required>room)
|
|
{
|
|
const LONG reserved=(!ancestors.empty()?width:0)+(!previous.empty()?width:0);
|
|
currentWidth=std::min(currentWidth,std::max(1L,room-reserved));
|
|
LONG remaining=room-currentWidth-reserved;
|
|
firstAncestor=ancestors.size();
|
|
while(firstAncestor && remaining>=segmentWidth(ancestors[firstAncestor-1],provider))
|
|
remaining-=segmentWidth(ancestors[--firstAncestor],provider);
|
|
visiblePrevious=0;
|
|
while(visiblePrevious<previous.size() && remaining>=segmentWidth(previous[visiblePrevious],provider))
|
|
remaining-=segmentWidth(previous[visiblePrevious++],provider);
|
|
}
|
|
|
|
if(firstAncestor)
|
|
add({.action=PathAction::ancestors,.label=L"…",
|
|
.hidden={ancestors.begin(),ancestors.begin()+firstAncestor}},width);
|
|
for(size_t i=firstAncestor;i<ancestors.size();++i)
|
|
add({.action=PathAction::folder,.label=pathSegmentLabel(ancestors[i],provider),.target=ancestors[i]},
|
|
segmentWidth(ancestors[i],provider));
|
|
add({.action=PathAction::edit,.label=pathSegmentLabel(panel.directory,provider),.current=true},currentWidth);
|
|
for(size_t i=0;i<visiblePrevious;++i)
|
|
add({.action=PathAction::folder,.label=pathSegmentLabel(previous[i],provider),.target=previous[i],.dim=true},
|
|
segmentWidth(previous[i],provider));
|
|
if(visiblePrevious<previous.size())
|
|
add({.action=PathAction::previous,.label=L"…",
|
|
.hidden={previous.begin()+visiblePrevious,previous.end()},.dim=true},width);
|
|
return parts;
|
|
}
|
|
|
|
void paintPanelPath(HDC dc,RECT bounds,const Panel &panel,bool activePanel)
|
|
{
|
|
const int saved=SaveDC(dc);
|
|
SelectObject(dc,mono);
|
|
SetBkMode(dc,TRANSPARENT);
|
|
for(const auto &part:panelPathParts(panel,bounds))
|
|
{
|
|
const bool current=activePanel && part.current;
|
|
FillRect(dc,&part.rect,current?teal:blue);
|
|
SetTextColor(dc,!part.enabled || part.dim?RGB(160,170,190):colors[current?Text:Frame]);
|
|
auto text=part.rect;
|
|
DrawTextW(dc,part.label.c_str(),static_cast<int>(part.label.size()),&text,
|
|
DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_END_ELLIPSIS|DT_NOPREFIX);
|
|
}
|
|
if(saved) RestoreDC(dc,saved);
|
|
}
|
|
|
|
constexpr int addressField=485,addressLabel=486,addressFooter=487;
|
|
|
|
struct PathPrompt
|
|
{
|
|
fs::path base, result;
|
|
std::optional<FilesPeer> peer;
|
|
std::optional<TransferProfile> ftp;
|
|
std::wstring completionInput;
|
|
std::future<std::wstring> completion;
|
|
};
|
|
|
|
constexpr UINT addressComplete=WM_APP+29;
|
|
constexpr UINT_PTR addressCompletionTimer=1;
|
|
|
|
fs::path enteredPanelPath(const fs::path &base,std::wstring text,bool ftp)
|
|
{
|
|
if(text.size()>=2 && text.front()==L'"' && text.back()==L'"')
|
|
text=text.substr(1,text.size()-2);
|
|
if(ftp && text.find_first_of(L":\r\n")!=text.npos) return {};
|
|
auto path=fs::path(std::move(text));
|
|
if(ftp)
|
|
{
|
|
if(path.has_root_name()) return {};
|
|
return (path.has_root_directory()?path:base/path).lexically_normal();
|
|
}
|
|
return path.is_absolute()?path:base/path;
|
|
}
|
|
|
|
std::wstring panelPathCompletion(const fs::path &base,const std::wstring &text,
|
|
const std::optional<FilesPeer> &peer,const std::optional<TransferProfile> &ftp)
|
|
{
|
|
const auto path=enteredPanelPath(base,text,ftp.has_value());
|
|
if(path.empty()) return {};
|
|
const auto directory=path.has_filename()?path.parent_path():path;
|
|
const auto prefix=path.filename().native();
|
|
fs::path best;
|
|
const auto accept=[&](const fs::path &candidate)
|
|
{
|
|
const auto name=candidate.filename().native();
|
|
if(name.size()<prefix.size() || (!prefix.empty() && CompareStringOrdinal(name.data(),
|
|
static_cast<int>(prefix.size()),prefix.data(),static_cast<int>(prefix.size()),ftp?FALSE:TRUE)!=CSTR_EQUAL))
|
|
return;
|
|
if(best.empty() || candidate<best) best=candidate;
|
|
};
|
|
const auto visit=[&](const fs::path &candidate,const FileInfo &info)
|
|
{
|
|
if(info.attributes&FILE_ATTRIBUTE_DIRECTORY) accept(candidate);
|
|
return true;
|
|
};
|
|
if(peer)
|
|
{
|
|
FilesConnection host(&*peer);
|
|
listFiles(host,directory,visit);
|
|
}
|
|
else if(ftp)
|
|
{
|
|
TransferSession host(*ftp);
|
|
host.list(directory,visit);
|
|
}
|
|
else
|
|
{
|
|
std::error_code error;
|
|
for(fs::directory_iterator it(directory,error),end;!error && it!=end;it.increment(error))
|
|
{
|
|
std::error_code typeError;
|
|
if(it->is_directory(typeError)) accept(it->path());
|
|
}
|
|
}
|
|
if(best.empty()) return {};
|
|
return ftp?ftpPath(best)+L"/":best.native()+L"\\";
|
|
}
|
|
|
|
LRESULT CALLBACK addressFieldKeys(HWND field,UINT message,WPARAM wp,LPARAM lp,UINT_PTR,DWORD_PTR)
|
|
{
|
|
if(message==WM_GETDLGCODE)
|
|
{
|
|
const auto *key=reinterpret_cast<const MSG *>(lp);
|
|
if(key && key->wParam==VK_TAB && !(GetKeyState(VK_SHIFT)&0x8000))
|
|
return DefSubclassProc(field,message,wp,lp)|DLGC_WANTTAB;
|
|
}
|
|
if(message==WM_KEYDOWN && wp==VK_TAB && !(GetKeyState(VK_SHIFT)&0x8000))
|
|
{
|
|
SendMessageW(GetParent(field),addressComplete,0,0);
|
|
return 0;
|
|
}
|
|
if(message==WM_CHAR && wp==VK_TAB) return 0;
|
|
return DefSubclassProc(field,message,wp,lp);
|
|
}
|
|
|
|
INT_PTR CALLBACK editPathDialog(HWND dialog,UINT message,WPARAM wp,LPARAM lp)
|
|
{
|
|
if(message==WM_DRAWITEM)
|
|
{
|
|
paintDialogButton(*reinterpret_cast<const DRAWITEMSTRUCT *>(lp),RGB(192,192,192),RGB(0,0,0),true);
|
|
return TRUE;
|
|
}
|
|
if(const auto painted=paintCopyDialog(dialog,message,wp,lp,L" Go to folder ",false,{addressFooter}))
|
|
return painted;
|
|
if(message==WM_INITDIALOG)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(lp);
|
|
SetWindowLongPtrW(dialog,DWLP_USER,lp);
|
|
prepareTextDialog(dialog);
|
|
layoutDialogContent(dialog,addressLabel);
|
|
layoutDialogButtons(dialog,addressFooter,{IDOK,IDCANCEL});
|
|
const auto field=GetDlgItem(dialog,addressField);
|
|
SendMessageW(field,EM_SETLIMITTEXT,32767,0);
|
|
const auto initial=prompt.base/fs::path{};
|
|
SetWindowTextW(field,(prompt.ftp?ftpPath(initial):initial.native()).c_str());
|
|
SetWindowSubclass(field,addressFieldKeys,4,0);
|
|
SetFocus(field);
|
|
SendMessageW(field,EM_SETSEL,0,-1);
|
|
return FALSE;
|
|
}
|
|
if(message==addressComplete)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
if(prompt.completion.valid()) return TRUE;
|
|
prompt.completionInput=shortcutField(dialog,addressField);
|
|
std::packaged_task<std::wstring()> task([base=prompt.base,text=prompt.completionInput,
|
|
peer=prompt.peer,ftp=prompt.ftp]
|
|
{
|
|
return panelPathCompletion(base,text,peer,ftp);
|
|
});
|
|
if(!SetTimer(dialog,addressCompletionTimer,30,nullptr)) return TRUE;
|
|
prompt.completion=task.get_future();
|
|
std::thread(std::move(task)).detach();
|
|
return TRUE;
|
|
}
|
|
if(message==WM_TIMER && wp==addressCompletionTimer)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
if(!prompt.completion.valid() ||
|
|
prompt.completion.wait_for(std::chrono::seconds(0))!=std::future_status::ready) return TRUE;
|
|
KillTimer(dialog,addressCompletionTimer);
|
|
try
|
|
{
|
|
const auto value=prompt.completion.get();
|
|
if(!value.empty() && shortcutField(dialog,addressField)==prompt.completionInput)
|
|
{
|
|
const auto field=GetDlgItem(dialog,addressField);
|
|
SetWindowTextW(field,value.c_str());
|
|
SendMessageW(field,EM_SETSEL,value.size(),value.size());
|
|
SendMessageW(field,EM_SCROLLCARET,0,0);
|
|
}
|
|
}
|
|
catch(const std::exception &) {}
|
|
return TRUE;
|
|
}
|
|
if(message==WM_COMMAND && LOWORD(wp)==IDOK)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
auto path=enteredPanelPath(prompt.base,shortcutField(dialog,addressField),prompt.ftp.has_value()).lexically_normal();
|
|
if(prompt.ftp?!isFtpFolderPath(path):!path.is_absolute())
|
|
{
|
|
showFilesNotice(dialog,L"Enter a folder path.",L"Go to folder");
|
|
return TRUE;
|
|
}
|
|
if(!path.has_filename() && path.has_relative_path()) path=path.parent_path();
|
|
prompt.result=std::move(path);
|
|
FilesModalDialog::finish(dialog,IDOK);
|
|
return TRUE;
|
|
}
|
|
if(message==WM_CLOSE || (message==WM_COMMAND && LOWORD(wp)==IDCANCEL))
|
|
{
|
|
FilesModalDialog::finish(dialog,IDCANCEL);
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
void editPanelPath(Panel &panel)
|
|
{
|
|
PathPrompt prompt{panel.directory,{},panel.peer,panel.ftp};
|
|
if(FilesModalDialog::run(GetModuleHandleW(nullptr),MAKEINTRESOURCEW(117),GetParent(panel.list),
|
|
editPathDialog,reinterpret_cast<LPARAM>(&prompt))==IDOK)
|
|
navigatePanelPath(panel,prompt.result);
|
|
}
|
|
|
|
void showFolderPicker(HWND owner,int side,const wchar_t *caption,std::vector<FolderLocation> locations,
|
|
RECT anchor,bool recent=false)
|
|
{
|
|
hidePicker();
|
|
showMenu(false);
|
|
commandPicker=false;
|
|
pickerPanel=side;
|
|
folderPicker=FolderPicker{caption,std::move(locations),recent};
|
|
SendMessageW(picker,LB_RESETCONTENT,0,0);
|
|
size_t longest=wcslen(caption);
|
|
for(const auto &location:folderPicker->locations)
|
|
{
|
|
auto label=isTransferFolder(location.provider)?ftpPath(location.directory):location.directory.native();
|
|
if(recent && location.provider!=FolderProvider::local)
|
|
{
|
|
const auto &source=location.label;
|
|
const auto kind=isTransferFolder(location.provider)?transferProtocolName(folderTransferProtocol(location.provider)):L"LAN";
|
|
label=std::wstring(kind)+L" · "+(source.empty()?std::wstring{}:source+L" · ")+label;
|
|
}
|
|
longest=std::max(longest,label.size());
|
|
SendMessageW(picker,LB_ADDSTRING,0,reinterpret_cast<LPARAM>(label.c_str()));
|
|
}
|
|
RECT client{};
|
|
GetClientRect(owner,&client);
|
|
const int inset=dialogFrameMetrics(GetDpiForWindow(owner)).content();
|
|
const int width=std::max(1,std::min(static_cast<int>(client.right)-2*inset,static_cast<int>(longest+3)*cellWidth));
|
|
const int x=std::clamp(static_cast<int>(anchor.left),inset,std::max(inset,static_cast<int>(client.right)-width-inset));
|
|
const int y=anchor.bottom+inset;
|
|
const int row=std::max(1,static_cast<int>(SendMessageW(picker,LB_GETITEMHEIGHT,0,0)));
|
|
RECT input{};
|
|
GetWindowRect(terminalInput,&input);
|
|
MapWindowPoints(nullptr,owner,reinterpret_cast<POINT *>(&input),2);
|
|
const int bottom=std::min(client.bottom,input.top);
|
|
const int shown=std::clamp((bottom-y-inset)/row,1,static_cast<int>(folderPicker->locations.size()));
|
|
showFramedList(picker,pickerFrame,x,y,width,row*shown,inset);
|
|
SendMessageW(picker,LB_SETCURSEL,0,0);
|
|
SetFocus(picker);
|
|
}
|
|
|
|
void showRecentFolders(int side)
|
|
{
|
|
auto locations=readRecentFolders();
|
|
if(locations.empty())
|
|
{
|
|
setStatus(L"No recent folders.");
|
|
return;
|
|
}
|
|
const auto owner=GetParent(picker);
|
|
RECT anchor{};
|
|
GetWindowRect(panels[side].path,&anchor);
|
|
MapWindowPoints(nullptr,owner,reinterpret_cast<POINT *>(&anchor),2);
|
|
for(auto &location:locations)
|
|
{
|
|
if(!isTransferFolder(location.provider)) continue;
|
|
const auto protocol=folderTransferProtocol(location.provider);
|
|
if(loadTransferProfiles(protocol))
|
|
if(const auto *profile=findTransferProfile(location.id,protocol)) location.label=profile->name;
|
|
}
|
|
showFolderPicker(owner,side,L"Recent folders",std::move(locations),anchor,true);
|
|
}
|
|
|
|
void showHiddenPathParts(HWND window,int side,const PathPart &part)
|
|
{
|
|
const auto owner=GetParent(window);
|
|
auto anchor=part.rect;
|
|
MapWindowPoints(window,owner,reinterpret_cast<POINT*>(&anchor),2);
|
|
std::vector<FolderLocation> locations;
|
|
locations.reserve(part.hidden.size());
|
|
for(const auto &path:part.hidden) locations.push_back(folderLocation(panels[side],path));
|
|
showFolderPicker(owner,side,part.action==PathAction::ancestors?L"Parent folders":L"Previous path",
|
|
std::move(locations),anchor);
|
|
}
|
|
|
|
LRESULT CALLBACK pathBarInput(HWND window,UINT message,WPARAM wp,LPARAM lp,UINT_PTR,DWORD_PTR)
|
|
{
|
|
if(message==WM_LBUTTONDOWN)
|
|
{
|
|
SetCapture(window);
|
|
return 0;
|
|
}
|
|
if(message==WM_LBUTTONUP && GetCapture()==window)
|
|
{
|
|
ReleaseCapture();
|
|
const int index=GetDlgCtrlID(window)-142;
|
|
if(index<0 || index>=2) return 0;
|
|
panelClipboardContext=true;
|
|
auto &panel=panels[index];
|
|
if(!pathNavigationAvailable(panel))
|
|
{
|
|
selectPanelEdge(panel,true);
|
|
return 0;
|
|
}
|
|
activatePanel(index);
|
|
RECT bounds{};
|
|
GetClientRect(window,&bounds);
|
|
const POINT mouse{GET_X_LPARAM(lp),GET_Y_LPARAM(lp)};
|
|
const auto parts=panelPathParts(panel,bounds);
|
|
for(const auto &part:parts)
|
|
{
|
|
if(!part.enabled || !PtInRect(&part.rect,mouse)) continue;
|
|
switch(part.action)
|
|
{
|
|
case PathAction::back: navigatePanelHistory(panel,-1); break;
|
|
case PathAction::forward: navigatePanelHistory(panel,1); break;
|
|
case PathAction::up: case PathAction::folder: navigatePanelPath(panel,part.target); break;
|
|
case PathAction::edit: editPanelPath(panel); break;
|
|
case PathAction::source:
|
|
if(panel.peer) chooseDrive(index,false,true);
|
|
else
|
|
{
|
|
const auto protocol=panel.ftp->protocol;
|
|
const auto *profile=findTransferProfile(panel.ftp->id,protocol);
|
|
showTransferProfiles(panel,protocol,profile?
|
|
ftpRoot/std::to_wstring(profile-transferProfiles(protocol).rows.data()):fs::path{});
|
|
}
|
|
break;
|
|
case PathAction::ancestors: case PathAction::previous: showHiddenPathParts(window,index,part); break;
|
|
}
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
return DefSubclassProc(window,message,wp,lp);
|
|
}
|
|
|
|
bool handlePanelPathKey(HWND owner,const MSG &message)
|
|
{
|
|
if(!commandLineAvailable(owner) || terminalVisible ||
|
|
(message.hwnd!=terminalInput && message.hwnd!=panels[active].list) ||
|
|
((GetKeyState(VK_SHIFT)|GetKeyState(VK_LWIN)|GetKeyState(VK_RWIN))&0x8000)) return false;
|
|
auto &panel=panels[active];
|
|
if(panel.archive)
|
|
{
|
|
if(message.message!=WM_SYSKEYDOWN || message.wParam!=VK_UP ||
|
|
!(message.lParam&(1LL<<29)) || (GetKeyState(VK_CONTROL)&0x8000)) return false;
|
|
if(!(message.lParam&(1LL<<30))) parentArchive(panel);
|
|
return true;
|
|
}
|
|
if(!pathNavigationAvailable(panel)) return false;
|
|
if(message.message==WM_KEYDOWN && message.wParam=='L' &&
|
|
(GetKeyState(VK_CONTROL)&0x8000) && !(GetKeyState(VK_MENU)&0x8000))
|
|
{
|
|
if(!(message.lParam&(1LL<<30))) editPanelPath(panel);
|
|
return true;
|
|
}
|
|
if(message.message!=WM_SYSKEYDOWN || !(message.lParam&(1LL<<29)) ||
|
|
(GetKeyState(VK_CONTROL)&0x8000)) return false;
|
|
if(message.wParam==VK_UP)
|
|
navigatePanelPath(panel,panelParentPath(panel.directory,!panel.peer && !panel.ftp));
|
|
else if(message.wParam==VK_LEFT || message.wParam==VK_RIGHT)
|
|
navigatePanelHistory(panel,message.wParam==VK_LEFT?-1:1);
|
|
else return false;
|
|
return true;
|
|
}
|