Add Files shortcuts and unified Options

Refs #12
This commit is contained in:
agrynco committed 2026-11-14 10:44:54 +02:00
1 parent 4b144dad2f
commit ff2ebb0117
11 files changed
+752 -179

No files matched your search

+192 -142
View File
@@ -280,13 +280,17 @@ bool dragging{};
bool rememberElevationApproval{};
HWND picker{}, pickerFrame{}, menuBar{};
HWND menuItems[5]{};
constexpr std::wstring_view menuAccessKeys=L"LFCOR";
std::vector<size_t> commandAccessPositions;
int menuIndex{};
bool commandPicker{};
int optionsPage{};
int sizeWidth=6;
bool alignExtensions=true, showDetails=true;
bool alignFolders=false;
DWORD statusFields=15;
constexpr int defaultSizeWidth=6, defaultFontPoints=110;
constexpr bool defaultWide=false, defaultAlignExtensions=true, defaultShowDetails=true, defaultAlignFolders=false;
constexpr DWORD defaultStatusFields=15;
int sizeWidth=defaultSizeWidth;
bool alignExtensions=defaultAlignExtensions, showDetails=defaultShowDetails;
bool alignFolders=defaultAlignFolders;
DWORD statusFields=defaultStatusFields;
std::vector<fs::path> drivePaths;
int driveHover=-1;
std::array<LONG,3> driveColumnStarts{};
@@ -309,8 +313,9 @@ int drivePickerWidth{};
int pickerPanel{};
bool altAlone{};
enum Color { PanelBlue, Text, Frame, Dialog, Selection, Footer, PanelCursor, ColorCount };
COLORREF colors[]{RGB(0,0,128), RGB(230,255,255), RGB(0,255,255),
constexpr std::array<COLORREF,ColorCount> defaultColors{RGB(0,0,128), RGB(230,255,255), RGB(0,255,255),
RGB(0,128,128), RGB(0,0,0), RGB(0,128,128), RGB(0,128,128)};
auto colors=defaultColors;
constexpr auto schemeKey = L"Software\\KSwitcher\\Files";
constexpr auto placementPreference = L"WindowPlacement";
@@ -345,9 +350,9 @@ void savePlacement(HWND window)
&placement,sizeof(placement));
}
void chooseDrive(int index, bool fromMenu = false);
void openMenu(HWND window, int index, int page = 0);
void openMenu(HWND window, int index);
void showFileDetails(const Panel &panel);
void editWideMode(HWND owner);
void editOptions(HWND owner);
void layout(HWND window);
DWORD preference(const wchar_t *name, DWORD fallback)
@@ -401,6 +406,16 @@ BOOL CALLBACK updateDialogFont(HWND window,LPARAM parameter)
HBRUSH blue{}, teal{}, terminalBackground{};
void layout(HWND window);
LOGFONTW defaultFilesFont()
{
LOGFONTW font{};
font.lfWeight=FW_NORMAL;
font.lfCharSet=DEFAULT_CHARSET;
font.lfQuality=CLEARTYPE_QUALITY;
wcscpy_s(font.lfFaceName,L"Consolas");
return font;
}
bool applyFont(HWND owner, LOGFONTW font, int points)
{
const auto dpi=GetDpiForWindow(owner);
@@ -443,22 +458,19 @@ bool applyFont(HWND owner, LOGFONTW font, int points)
return true;
}
void chooseDisplayFont(HWND owner)
bool chooseDisplayFont(HWND owner,LOGFONTW &font,int &points)
{
auto font=displayFont;
CHOOSEFONTW dialog{};
dialog.lStructSize=sizeof(dialog);
dialog.hwndOwner=owner;
dialog.lpLogFont=&font;
dialog.iPointSize=points;
dialog.Flags=CF_SCREENFONTS|CF_INITTOLOGFONTSTRUCT|CF_LIMITSIZE;
dialog.nSizeMin=8;
dialog.nSizeMax=24;
if(!ChooseFontW(&dialog)) return;
if(!applyFont(owner,font,dialog.iPointSize)) return;
savePreference(L"FontPoints",fontPoints);
RegSetKeyValueW(HKEY_CURRENT_USER,schemeKey,L"DisplayFont",REG_BINARY,&displayFont,sizeof(displayFont));
layout(owner);
RedrawWindow(owner,nullptr,nullptr,RDW_INVALIDATE|RDW_ALLCHILDREN);
if(!ChooseFontW(&dialog)) return false;
points=dialog.iPointSize;
return true;
}
void refreshColors()
@@ -482,24 +494,15 @@ void loadColors()
refreshColors();
}
void changeColor(HWND owner, int index)
void choosePanelColor(HWND owner,COLORREF &color,COLORREF *custom)
{
static COLORREF custom[16]{};
CHOOSECOLORW dialog{};
dialog.lStructSize=sizeof(dialog);
dialog.hwndOwner=owner;
dialog.rgbResult=colors[index];
dialog.rgbResult=color;
dialog.lpCustColors=custom;
dialog.Flags=CC_RGBINIT | CC_FULLOPEN;
if (!ChooseColorW(&dialog)) return;
colors[index]=dialog.rgbResult;
auto name=std::to_wstring(index);
const auto result=RegSetKeyValueW(HKEY_CURRENT_USER, schemeKey, name.c_str(), REG_DWORD,
&colors[index], sizeof(COLORREF));
refreshColors();
RedrawWindow(owner,nullptr,nullptr,RDW_INVALIDATE|RDW_ERASE|RDW_ALLCHILDREN);
if(result!=ERROR_SUCCESS)
MessageBoxW(owner,L"Could not save the color.",L"KSwitcher Files",MB_OK|MB_ICONWARNING);
if(ChooseColorW(&dialog)) color=dialog.rgbResult;
}
void paintText(HDC dc, RECT rect, const wchar_t *text, COLORREF foreground, COLORREF background, bool right=false)
@@ -512,6 +515,18 @@ void paintText(HDC dc, RECT rect, const wchar_t *text, COLORREF foreground, COLO
SetTextAlign(dc,alignment);
}
void paintMenuLabel(HDC dc,RECT rect,const std::wstring &text,size_t access,COLORREF foreground,COLORREF background)
{
paintText(dc,rect,text.c_str(),foreground,background);
if(access>=text.size()) return;
SIZE prefix{};
GetTextExtentPoint32W(dc,text.c_str(),static_cast<int>(access),&prefix);
const auto mode=SetBkMode(dc,TRANSPARENT);
SetTextColor(dc,RGB(255,255,0));
ExtTextOutW(dc,rect.left+5+prefix.cx,rect.top+3,ETO_CLIPPED,&rect,text.c_str()+access,1,nullptr);
SetBkMode(dc,mode);
}
void paintDriveCaption(HDC dc,RECT row,const wchar_t *caption)
{
const int saved=SaveDC(dc);
@@ -1020,16 +1035,6 @@ void selectDrive()
if(row==2) chooseDrive(index);
else setMode(index,row==1);
}
else if(menuIndex==3)
{
if(optionsPage==0)
{
if(row==2) chooseDisplayFont(owner);
else openMenu(owner,3,row==0?1:2);
}
else if(optionsPage==1) editWideMode(owner);
else if(optionsPage==2) changeColor(owner,static_cast<int>(row));
}
else
{
const int command=menuIndex==1?113+static_cast<int>(row):110+static_cast<int>(row);
@@ -1080,9 +1085,7 @@ LRESULT CALLBACK pickerKeys(HWND window, UINT msg, WPARAM wp, LPARAM lp, UINT_PT
}
if(msg==WM_KEYDOWN && wp==VK_ESCAPE)
{
if(commandPicker && menuIndex==3 && optionsPage)
openMenu(GetParent(window),3);
else hidePicker();
hidePicker();
return 0;
}
if(msg==WM_KEYDOWN && wp==VK_RETURN){selectDrive();return 0;}
@@ -2922,69 +2925,18 @@ LRESULT CALLBACK panelKeys(HWND window, UINT message, WPARAM wp, LPARAM lp, UINT
return result;
}
INT_PTR CALLBACK wideModeDialog(HWND dialog, UINT message, WPARAM wp, LPARAM)
{
constexpr int widthField=201, extensionsField=202, detailsField=203;
if(message==WM_INITDIALOG)
{
installDialogButtonKeys(dialog);
SetDlgItemInt(dialog,widthField,sizeWidth,FALSE);
SendDlgItemMessageW(dialog,widthField,EM_SETLIMITTEXT,2,0);
CheckDlgButton(dialog,extensionsField,alignExtensions?BST_CHECKED:BST_UNCHECKED);
CheckDlgButton(dialog,detailsField,showDetails?BST_CHECKED:BST_UNCHECKED);
CheckDlgButton(dialog,204,alignFolders?BST_CHECKED:BST_UNCHECKED);
for(int i=0;i<4;++i) CheckDlgButton(dialog,205+i,(statusFields&(1u<<i))?BST_CHECKED:BST_UNCHECKED);
return TRUE;
}
if(message==WM_CLOSE || (message==WM_COMMAND && LOWORD(wp)==IDCANCEL))
{
EndDialog(dialog,IDCANCEL);
return TRUE;
}
if(message!=WM_COMMAND || LOWORD(wp)!=IDOK) return FALSE;
BOOL valid{};
auto width=GetDlgItemInt(dialog,widthField,&valid,FALSE);
if(!valid || width<6 || width>16)
{
MessageBoxW(dialog,L"Enter a width from 6 to 16 characters.",L"Wide panel mode",MB_OK|MB_ICONWARNING);
SetFocus(GetDlgItem(dialog,widthField));
SendDlgItemMessageW(dialog,widthField,EM_SETSEL,0,-1);
return TRUE;
}
sizeWidth=static_cast<int>(width);
alignExtensions=IsDlgButtonChecked(dialog,extensionsField)==BST_CHECKED;
showDetails=IsDlgButtonChecked(dialog,detailsField)==BST_CHECKED;
alignFolders=IsDlgButtonChecked(dialog,204)==BST_CHECKED;
statusFields=0;
for(int i=0;i<4;++i) if(IsDlgButtonChecked(dialog,205+i)==BST_CHECKED) statusFields|=1u<<i;
savePreference(L"SizeWidth",sizeWidth);
savePreference(L"AlignExtensions",alignExtensions);
savePreference(L"ShowDetails",showDetails);
savePreference(L"AlignFolders",alignFolders);
savePreference(L"StatusFields",statusFields);
EndDialog(dialog,IDOK);
return TRUE;
}
#include "files_options.inl"
void editWideMode(HWND owner)
void openMenu(HWND window, int index)
{
const auto result=DialogBoxParamW(GetModuleHandleW(nullptr),MAKEINTRESOURCEW(101),owner,wideModeDialog,0);
if(result==IDOK)
if(index==3)
{
for(auto &panel:panels)
{
InvalidateRect(panel.list,nullptr,TRUE);
InvalidateRect(panel.heading,nullptr,TRUE);
}
showFileDetails(panels[active]);
hidePicker();
showMenu(false);
editOptions(window);
return;
}
else if(result==-1) setStatus(L"Could not open Wide panel settings.");
}
void openMenu(HWND window, int index, int page)
{
menuIndex=index;
optionsPage=page;
commandPicker=true;
SendMessageW(picker,LB_RESETCONTENT,0,0);
std::vector<std::wstring> rows;
@@ -2997,12 +2949,34 @@ void openMenu(HWND window, int index, int page)
}
else if(index==1) rows={L"Copy\tF5",L"Move\tF6",L"Exit\tF10"};
else if(index==2) rows={L"Refresh\tF2",L"View\tF3",L"Edit\tF4"};
else if(page==0) rows={L"File panel modes",L"Colors",L"Appearance…"};
else if(page==1) rows={L"Wide"};
else if(page==2) rows={L"Panel background",L"Text",L"Frame",L"Popup background",
L"Menu selection",L"Command bar",L"File cursor"};
for(const auto &row:rows)
SendMessageW(picker,LB_ADDSTRING,0,reinterpret_cast<LPARAM>(row.c_str()));
std::set<wchar_t> used;
commandAccessPositions.assign(rows.size(),std::wstring::npos);
const auto reserve=[&](size_t row,wchar_t key)
{
const auto &text=rows[row];
for(size_t pos=0;pos<text.size() && text[pos]!=L'\t';++pos)
if(towupper(text[pos])==key && used.insert(key).second)
{
commandAccessPositions[row]=pos;
return true;
}
return false;
};
if(index==0 || index==4) {reserve(0,L'B');reserve(1,L'W');reserve(2,L'C');}
if(index==1) {reserve(0,L'C');reserve(2,L'X');}
if(index==2) {reserve(1,L'V');reserve(2,L'E');}
for(size_t i=0;i<rows.size();++i)
{
const auto &text=rows[i];
if(commandAccessPositions[i]==std::wstring::npos)
for(const auto ch:text)
{
if(ch==L'\t') break;
const auto key=towupper(ch);
if(key>=L'A' && key<=L'Z' && reserve(i,static_cast<wchar_t>(key))) break;
}
SendMessageW(picker,LB_ADDSTRING,0,reinterpret_cast<LPARAM>(text.c_str()));
}
RECT anchor{}, client{};
GetWindowRect(menuItems[index],&anchor);
MapWindowPoints(nullptr,window,reinterpret_cast<POINT*>(&anchor),2);
@@ -3018,10 +2992,59 @@ void openMenu(HWND window, int index, int page)
showMenu(true);
}
void toggleFilesMenu()
{
const bool visible=IsWindowVisible(menuBar);
hidePicker();
if(!visible) menuIndex=active==0?0:4;
showMenu(!visible);
if(visible) SetFocus(panels[active].list);
}
bool handleFilesMenuKey(HWND window,UINT message,WPARAM key,LPARAM flags,bool modified)
{
const bool repeated=(flags & (1LL << 30))!=0;
if(message==WM_SYSKEYDOWN && key==VK_MENU)
{
if(!repeated) altAlone=!modified;
return !modified;
}
if((message==WM_SYSKEYUP || message==WM_KEYUP) && key==VK_MENU)
{
if(std::exchange(altAlone,false))
{
toggleFilesMenu();
return true;
}
return false;
}
if(message!=WM_KEYDOWN && message!=WM_SYSKEYDOWN) return false;
altAlone=false;
if(modified) return false;
if(message==WM_KEYDOWN && key==VK_F9)
{
if(!repeated) toggleFilesMenu();
return true;
}
if(message==WM_SYSKEYDOWN && (flags & (1LL << 29)) && key>=L'A' && key<=L'Z')
{
const auto category=menuAccessKeys.find(static_cast<wchar_t>(key));
if(category!=menuAccessKeys.npos)
{
if(!repeated) openMenu(window,static_cast<int>(category));
return true;
}
}
return false;
}
LRESULT CALLBACK filesWindow(HWND window, UINT message, WPARAM wp, LPARAM lp)
{
switch (message)
{
case WM_ACTIVATEAPP:
if(!wp) altAlone=false;
break;
case WM_COPYDATA:
{
const auto *reply=reinterpret_cast<const COPYDATASTRUCT*>(lp);
@@ -3058,22 +3081,18 @@ LRESULT CALLBACK filesWindow(HWND window, UINT message, WPARAM wp, LPARAM lp)
if(RegGetValueW(HKEY_CURRENT_USER,schemeKey,L"DisplayFont",RRF_RT_REG_BINARY,nullptr,&font,&bytes)
!=ERROR_SUCCESS || bytes!=sizeof(font))
{
font={};
font.lfWeight=FW_NORMAL;
font.lfCharSet=DEFAULT_CHARSET;
font.lfQuality=CLEARTYPE_QUALITY;
wcscpy_s(font.lfFaceName,L"Consolas");
font=defaultFilesFont();
}
font.lfFaceName[LF_FACESIZE-1]=0;
if(!applyFont(window,font,static_cast<int>(std::clamp(preference(L"FontPoints",110),80UL,240UL)))) return -1;
if(!applyFont(window,font,static_cast<int>(std::clamp(preference(L"FontPoints",defaultFontPoints),80UL,240UL)))) return -1;
loadColors();
panels[0].wide=preference(L"LeftWide",0)!=0;
panels[1].wide=preference(L"RightWide",0)!=0;
sizeWidth=static_cast<int>(std::clamp(preference(L"SizeWidth",6),6UL,16UL));
alignExtensions=preference(L"AlignExtensions",1)!=0;
showDetails=preference(L"ShowDetails",1)!=0;
alignFolders=preference(L"AlignFolders",0)!=0;
statusFields=preference(L"StatusFields",15)&15;
panels[0].wide=preference(L"LeftWide",defaultWide)!=0;
panels[1].wide=preference(L"RightWide",defaultWide)!=0;
sizeWidth=static_cast<int>(std::clamp(preference(L"SizeWidth",defaultSizeWidth),6UL,16UL));
alignExtensions=preference(L"AlignExtensions",defaultAlignExtensions)!=0;
showDetails=preference(L"ShowDetails",defaultShowDetails)!=0;
alignFolders=preference(L"AlignFolders",defaultAlignFolders)!=0;
statusFields=preference(L"StatusFields",defaultStatusFields)&15;
menuBar=CreateWindowW(L"BUTTON",L"",WS_CHILD|WS_CLIPSIBLINGS|BS_OWNERDRAW,
0,0,0,0,window,reinterpret_cast<HMENU>(120),instance,nullptr);
const wchar_t *menus[]{L"Left",L"Files",L"Commands",L"Options",L"Right"};
@@ -3424,17 +3443,21 @@ LRESULT CALLBACK filesWindow(HWND window, UINT message, WPARAM wp, LPARAM lp)
}
}
auto shortcut=text.find(L'\t');
if(item.hwndItem==picker && commandPicker && shortcut!=text.npos)
if(item.hwndItem==picker && commandPicker && item.itemID<commandAccessPositions.size())
{
auto labelRect=item.rcItem, keyRect=item.rcItem;
SelectObject(item.hDC,mono);
SIZE extent{};
const auto key=text.c_str()+shortcut+1;
GetTextExtentPoint32W(item.hDC,key,lstrlenW(key),&extent);
keyRect.left=std::max(keyRect.left,keyRect.right-extent.cx-16);
labelRect.right=keyRect.left;
paintText(item.hDC,labelRect,text.substr(0,shortcut).c_str(),colors[Text],colors[background]);
paintText(item.hDC,keyRect,key,colors[Text],colors[background],true);
if(shortcut!=text.npos)
{
const auto key=text.c_str()+shortcut+1;
GetTextExtentPoint32W(item.hDC,key,lstrlenW(key),&extent);
keyRect.left=std::max(keyRect.left,keyRect.right-extent.cx-16);
labelRect.right=keyRect.left;
paintText(item.hDC,keyRect,key,colors[Text],colors[background],true);
}
paintMenuLabel(item.hDC,labelRect,text.substr(0,shortcut),commandAccessPositions[item.itemID],
colors[Text],colors[background]);
}
else if(item.hwndItem==picker && !commandPicker && item.itemID<drivePaths.size())
{
@@ -3487,6 +3510,8 @@ LRESULT CALLBACK filesWindow(HWND window, UINT message, WPARAM wp, LPARAM lp)
ETO_OPAQUE|ETO_CLIPPED,&bounds,text.c_str(),lstrlenW(text.c_str()),nullptr);
}
else if(item.CtlID>=130 && item.CtlID<=134)
paintMenuLabel(item.hDC,item.rcItem,text,0,foreground,colors[background]);
else paintText(item.hDC,item.rcItem,text.c_str(),foreground,colors[background]);
if(item.CtlID==102 || item.CtlID==103)
{
@@ -3621,7 +3646,7 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show)
}
LocalFree(args);
}
INITCOMMONCONTROLSEX controls{sizeof(controls), ICC_STANDARD_CLASSES|ICC_PROGRESS_CLASS};
INITCOMMONCONTROLSEX controls{sizeof(controls), ICC_STANDARD_CLASSES|ICC_PROGRESS_CLASS|ICC_TREEVIEW_CLASSES};
InitCommonControlsEx(&controls);
WNDCLASSW type{};
type.hInstance = instance;
@@ -3662,23 +3687,12 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show)
if(terminalVisible || startTerminal(window)) showTerminal(window,!terminalVisible);
continue;
}
if (message.message == WM_SYSKEYDOWN && message.wParam == VK_MENU)
{
altAlone=true;
const bool filesMessage=message.hwnd==window || IsChild(window,message.hwnd);
const bool modified=(GetKeyState(VK_CONTROL)&0x8000) || (GetKeyState(VK_SHIFT)&0x8000) ||
(GetKeyState(VK_LWIN)&0x8000) || (GetKeyState(VK_RWIN)&0x8000);
if(filesMessage && !copyInProgress &&
handleFilesMenuKey(window,message.message,message.wParam,message.lParam,modified))
continue;
}
if ((message.message == WM_SYSKEYUP || message.message == WM_KEYUP) && message.wParam == VK_MENU)
{
if(altAlone)
{
hidePicker();
menuIndex=active==0?0:4;
showMenu(true);
}
altAlone=false;
continue;
}
if(message.message==WM_SYSKEYDOWN || message.message==WM_KEYDOWN) altAlone=false;
if((message.message==WM_LBUTTONDOWN || message.message==WM_RBUTTONDOWN ||
message.message==WM_NCLBUTTONDOWN) && message.hwnd!=menuBar && message.hwnd!=picker &&
std::find(std::begin(menuItems),std::end(menuItems),message.hwnd)==std::end(menuItems))
@@ -3686,6 +3700,42 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show)
showMenu(false);
if(message.hwnd!=picker) hidePicker();
}
if((message.message==WM_CHAR || message.message==WM_SYSCHAR) && IsWindowVisible(menuBar))
{
const auto letter=towupper(static_cast<wchar_t>(message.wParam));
if(letter>=L'A' && letter<=L'Z')
{
if(!IsWindowVisible(picker))
{
const auto category=menuAccessKeys.find(static_cast<wchar_t>(letter));
if(category!=menuAccessKeys.npos) openMenu(window,static_cast<int>(category));
}
else if(commandPicker)
{
for(size_t row=0;row<commandAccessPositions.size();++row)
{
const auto pos=commandAccessPositions[row];
if(pos==std::wstring::npos) continue;
const auto length=SendMessageW(picker,LB_GETTEXTLEN,row,0);
if(length<=0 || pos>=static_cast<size_t>(length)) continue;
std::wstring label(static_cast<size_t>(length)+1,L'\0');
SendMessageW(picker,LB_GETTEXT,row,reinterpret_cast<LPARAM>(label.data()));
if(towupper(label[pos])==letter)
{
SendMessageW(picker,LB_SETCURSEL,row,0);
selectDrive();
break;
}
}
}
else
{
TranslateMessage(&message);
DispatchMessageW(&message);
}
continue;
}
}
if(message.message==WM_KEYDOWN && IsWindowVisible(menuBar) && !IsWindowVisible(picker))
{
if(message.wParam==VK_ESCAPE){showMenu(false);SetFocus(panels[active].list);continue;}