fix(files): synchronize terminal navigation and polish lan picker

This commit is contained in:
agrynco committed 2026-11-14 15:52:14 +02:00
1 parent eb481edafe
commit 2ecffae995
7 files changed
+121 -14

No files matched your search

+68 -11
View File
@@ -6,6 +6,7 @@
#include <commdlg.h>
#include <algorithm>
#include <array>
#include <deque>
#include <filesystem>
#include <fstream>
#include <future>
@@ -269,6 +270,8 @@ bool terminalViewRequested{};
HWND terminalInput{},terminalOutput{},terminalPrompt{};
constexpr UINT_PTR terminalTimer=2;
bool terminalVisible{};
std::deque<int> terminalCommandPanels;
std::wstring terminalDirectory;
int terminalRows{};
struct CommandEntry { std::wstring date, command; };
std::vector<CommandEntry> terminalHistory;
@@ -797,6 +800,8 @@ void showTerminal(HWND owner,bool show)
SetFocus(terminalInput);
}
void open(Panel &panel,const fs::path &directory,bool offerElevation);
bool startTerminal(HWND owner)
{
if(terminal.alive()) return true;
@@ -819,6 +824,20 @@ bool startTerminal(HWND owner)
traceTerminal(events[phase],height,rows,viewport,base,offset);
}
}
else if(message.starts_with(L"directory:"))
{
const auto directory=message.substr(10);
if(!terminalCommandPanels.empty())
{
const int panel=terminalCommandPanels.front();
terminalCommandPanels.pop_front();
std::error_code error;
if(panel>=0 && !panels[panel].peer && directory!=terminalDirectory &&
!directory.empty() && fs::is_directory(directory,error))
open(panels[panel],directory,false);
}
terminalDirectory=directory;
}
else if(message==L"focus-input")
{
if(GetForegroundWindow()==owner) SetFocus(terminalInput);
@@ -855,7 +874,12 @@ bool startTerminal(HWND owner)
setStatus(L"Select a local panel to start the terminal.");
return false;
}
if(terminal.start(panels[active].directory.c_str(),terminalSize)) return true;
if(terminal.start(panels[active].directory.c_str(),terminalSize))
{
terminalCommandPanels={-1};
terminalDirectory.clear();
return true;
}
setStatus(L"Could not start PowerShell.");
return false;
}
@@ -907,6 +931,7 @@ LRESULT CALLBACK terminalInputKeys(HWND input,UINT message,WPARAM wp,LPARAM lp,U
std::wstring value(GetWindowTextLengthW(input)+1,L'\0');
value.resize(GetWindowTextW(input,value.data(),static_cast<int>(value.size())));
if(!terminal.write(value+L"\r")) { setStatus(L"Could not send PowerShell input."); return 0; }
terminalCommandPanels.push_back(active);
if(value.find_first_not_of(L" \t\r\n")!=std::wstring::npos) rememberCommand(value);
terminalHistoryIndex=terminalHistory.size();
SetWindowTextW(input,L"");
@@ -1215,10 +1240,19 @@ void queryDrivePeers()
{
COPYDATASTRUCT request{FilesLanPeers,0,nullptr};
DWORD_PTR result{};
SendMessageTimeoutW(drivePeerService,WM_COPYDATA,reinterpret_cast<WPARAM>(GetParent(picker)),
reinterpret_cast<LPARAM>(&request),SMTO_ABORTIFHUNG|SMTO_ERRORONEXIT,350,&result);
if(result!=1) drivePeers.clear();
SetLastError(ERROR_SUCCESS);
const auto delivered=SendMessageTimeoutW(drivePeerService,WM_COPYDATA,
reinterpret_cast<WPARAM>(GetParent(picker)),reinterpret_cast<LPARAM>(&request),
SMTO_ABORTIFHUNG|SMTO_ERRORONEXIT,350,&result);
const auto error=GetLastError();
if(!delivered || result!=1)
{
drivePeers.clear();
ks::log(L"LAN Files peer lookup failed: delivered="+std::to_wstring(delivered!=0)+
L", reply="+std::to_wstring(result)+L", Windows error="+std::to_wstring(error));
}
}
else ks::log(L"LAN Files peer lookup failed: local KSwitcher window not found");
drivePeerService=nullptr;
}
@@ -1258,8 +1292,17 @@ void layoutDrivePicker(int selected,bool focus)
static_cast<int>(SendMessageW(picker,LB_GETCOUNT,0,0)))*textHeight;
const int x=panel.left+(panel.right-panel.left-width)/2;
const int y=panel.top+(panel.bottom-panel.top-height)/2;
SetWindowPos(pickerFrame,HWND_TOP,x-inset,y-vertical,width+2*inset,height+2*vertical,SWP_SHOWWINDOW|SWP_NOACTIVATE);
SetWindowPos(picker,HWND_TOP,x,y,width,height,SWP_SHOWWINDOW|SWP_NOACTIVATE);
constexpr UINT flags=SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOCOPYBITS;
auto layout=BeginDeferWindowPos(2);
if(layout) layout=DeferWindowPos(layout,pickerFrame,HWND_TOP,
x-inset,y-vertical,width+2*inset,height+2*vertical,flags);
if(layout) layout=DeferWindowPos(layout,picker,HWND_TOP,x,y,width,height,flags);
if(!layout || !EndDeferWindowPos(layout))
{
SetWindowPos(pickerFrame,HWND_TOP,x-inset,y-vertical,width+2*inset,height+2*vertical,flags);
SetWindowPos(picker,HWND_TOP,x,y,width,height,flags);
}
RedrawWindow(pickerFrame,nullptr,nullptr,RDW_INVALIDATE|RDW_UPDATENOW);
SendMessageW(picker,LB_SETCURSEL,selected,0);
if(focus) SetFocus(picker);
}
@@ -1347,10 +1390,15 @@ void pollDriveQueries(HWND owner)
try {query.drives=query.pending.get();}
catch(const std::system_error &e)
{
query.error=query.peer.filesBrowse?fileError(e):L"Check build / TCP 45885";
ks::log(L"LAN Files drive query failed: Windows error "+std::to_wstring(e.code().value()));
if(e.code()==std::error_code(WSAETIMEDOUT,std::system_category()))
query.error=L"Connection timed out";
else
query.error=query.peer.filesBrowse?fileError(e):L"Check build / TCP 45885";
}
catch(const std::exception &)
{
ks::log(L"LAN Files drive query failed: unexpected error");
query.error=query.peer.filesBrowse?L"Cannot reach TCP 45885":L"Check build / TCP 45885";
}
changed=true;
@@ -3805,15 +3853,14 @@ LRESULT CALLBACK filesWindow(HWND window, UINT message, WPARAM wp, LPARAM lp)
FillRect(item.hDC,&line,static_cast<HBRUSH>(GetStockObject(DC_BRUSH)));
}
}
if (item.CtlType==ODT_LISTBOX)
if (item.CtlType==ODT_LISTBOX && item.hwndItem!=picker)
{
RECT track{};
GetWindowRect(item.hwndItem,&track);
auto owner=item.hwndItem==picker?pickerFrame:window;
MapWindowPoints(nullptr,owner,reinterpret_cast<POINT*>(&track),2);
MapWindowPoints(nullptr,window,reinterpret_cast<POINT*>(&track),2);
track.left=track.right;
track.right+=5;
InvalidateRect(owner,&track,FALSE);
InvalidateRect(window,&track,FALSE);
}
return TRUE;
}
@@ -3885,6 +3932,16 @@ LRESULT CALLBACK filesWindow(HWND window, UINT message, WPARAM wp, LPARAM lp)
int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show)
{
bool writeLog=true;
try
{
const auto settings=ks::dataDirectory()/L"settings.json";
if(std::filesystem::exists(settings))
writeLog=Json::parse(ks::readFile(settings,1024*1024)).value("writeLog",true);
}
catch(const std::exception &) {}
ks::setLogFilename(L"kswitcher-files.log");
ks::setLoggingEnabled(writeLog);
int count{};
if(auto args=CommandLineToArgvW(GetCommandLineW(),&count))
{