From 293c5fd47e7b7b8316c5fbaffbe05693829dfda7 Mon Sep 17 00:00:00 2001 From: Anatolii Hrynchuk Date: Mon, 16 Nov 2026 10:56:47 +0200 Subject: [PATCH] Cycle Files editor and viewer windows with Ctrl+Tab Refs #38 --- FILES-UI.md | 2 ++ resources/terminal/markdown.js | 5 ++++- src/files_main.cpp | 4 ++++ src/files_text_window.cpp | 36 ++++++++++++++++++++++++++++++++ src/files_text_window.h | 1 + tests/files_markdown.ps1 | 23 ++++++++++++++++++++ tests/files_markdown_browser.mjs | 2 ++ 7 files changed, 72 insertions(+), 1 deletion(-) diff --git a/FILES-UI.md b/FILES-UI.md index a2a04b8..9df7f40 100644 --- a/FILES-UI.md +++ b/FILES-UI.md @@ -141,6 +141,8 @@ Warning і початковий фокус на No. - У цьому вікні F6 перемикає перегляд і редагування для поточного файла. За наявності змін перехід у перегляд пропонує Save, Discard або Cancel. Помилка збереження залишає відкритим редактор зі змінами. +- Ctrl+Tab циклічно перемикає фокус між Files, редакторами й переглядачами + у порядку їх відкриття. - F3 відкриває Markdown як форматований документ із таблицями, блоками коду, інтерактивними чекбоксами списків і діаграмами Mermaid. Відмітки в перегляді діють до повторного рендерингу або виходу з цього режиму. F4 відкриває текст diff --git a/resources/terminal/markdown.js b/resources/terminal/markdown.js index 5778e7c..2329320 100644 --- a/resources/terminal/markdown.js +++ b/resources/terminal/markdown.js @@ -116,7 +116,10 @@ document.addEventListener('pointerup', event => { }); document.addEventListener('keydown', event => { - if ((event.altKey && ['ArrowLeft', 'ArrowRight'].includes(event.key)) || + if (event.ctrlKey && !event.altKey && event.key === 'Tab') { + event.preventDefault(); + send('cycle'); + } else if ((event.altKey && ['ArrowLeft', 'ArrowRight'].includes(event.key)) || ['BrowserBack', 'BrowserForward'].includes(event.key)) { event.preventDefault(); navigate(['ArrowLeft', 'BrowserBack'].includes(event.key) ? 'back' : 'forward'); diff --git a/src/files_main.cpp b/src/files_main.cpp index d91dd19..31f2e0e 100644 --- a/src/files_main.cpp +++ b/src/files_main.cpp @@ -5414,6 +5414,10 @@ int WINAPI wWinMain(HINSTANCE instance, HINSTANCE, PWSTR, int show) { if(GetForegroundWindow()==window || IsChild(window,GetFocus())) restoreCommandFocus(); if(GetMessageW(&message,nullptr,0,0)<=0) break; + if(message.message==WM_KEYDOWN && message.wParam==VK_TAB && + (GetKeyState(VK_CONTROL)&0x8000) && !(GetKeyState(VK_MENU)&0x8000) && + ks::files::cycleTextWindows(GetAncestor(message.hwnd,GA_ROOT))) + continue; if(!IsWindow(window) || (message.hwnd && GetAncestor(message.hwnd,GA_ROOTOWNER)!=window)) { TranslateMessage(&message); diff --git a/src/files_text_window.cpp b/src/files_text_window.cpp index 2449386..61beb66 100644 --- a/src/files_text_window.cpp +++ b/src/files_text_window.cpp @@ -636,6 +636,8 @@ struct RemoteTextFile class TextWindow; std::map> textWindows; +std::vector textWindowOrder; +HWND filesOwner{}; bool textOwnerClosed=false; constexpr ULONG_PTR openDocumentQuery=0x4B534644; @@ -812,6 +814,7 @@ private: static constexpr UINT previewFailed = WM_APP + 2; static constexpr UINT openPreviewLink = WM_APP + 3; static constexpr UINT previewRendered = WM_APP + 4; + static constexpr UINT cycleWindow = WM_APP + 5; LRESULT send(UINT message, WPARAM wparam = 0, LPARAM lparam = 0) const { @@ -1471,6 +1474,11 @@ void TextWindow::updatePreview() { const bool ctrl=(GetKeyState(VK_CONTROL)&0x8000)!=0; const bool alt=(GetKeyState(VK_MENU)&0x8000)!=0; + if(ctrl && !alt && key==VK_TAB) + { + PostMessageW(window,cycleWindow,0,0); + return true; + } if(key==VK_BROWSER_BACK || key==VK_BROWSER_FORWARD || (alt && (key==VK_LEFT || key==VK_RIGHT))) { PostMessageW(window,WM_COMMAND,key==VK_LEFT || key==VK_BROWSER_BACK?historyBack:historyForward,0); @@ -1590,6 +1598,11 @@ void TextWindow::previewMessage(const std::wstring &value) } const auto message=nlohmann::json::parse(utf8(value)); const auto action=message.at("action").get(); + if(action=="cycle") + { + PostMessageW(window,cycleWindow,0,0); + return; + } if(action=="failed") { PostMessageW(window,previewFailed,0,0); @@ -2030,6 +2043,7 @@ LRESULT CALLBACK TextWindow::procedure(HWND window, UINT message, WPARAM wparam, const auto &state=*static_cast *>(create->lpCreateParams); state->window=window; textWindows.emplace(window,state); + textWindowOrder.push_back(window); } const auto found=textWindows.find(window); if(found==textWindows.end()) return DefWindowProcW(window,message,wparam,lparam); @@ -2105,6 +2119,7 @@ LRESULT CALLBACK TextWindow::procedure(HWND window, UINT message, WPARAM wparam, return 0; } case WM_SETFOCUS: self->focusContent(); return 0; + case cycleWindow: cycleTextWindows(window); return 0; case WM_KEYDOWN: if(self->previewWanted && !self->previewVisible) { @@ -2181,6 +2196,7 @@ LRESULT CALLBACK TextWindow::procedure(HWND window, UINT message, WPARAM wparam, case WM_NCDESTROY: self->window=nullptr; textWindows.erase(window); + std::erase(textWindowOrder,window); if(textOwnerClosed && textWindows.empty()) PostQuitMessage(0); break; } @@ -2296,10 +2312,29 @@ void showFarHistory(HWND dialog,int fieldId,const std::vector & void textWindowOwnerClosed() { + filesOwner=nullptr; textOwnerClosed=true; if(textWindows.empty()) PostQuitMessage(0); } +bool cycleTextWindows(HWND current) +{ + if(!current || (current!=filesOwner && !textWindows.contains(current))) return false; + std::vector windows; + if(filesOwner && IsWindowVisible(filesOwner)) windows.push_back(filesOwner); + for(const auto window:textWindowOrder) + if(IsWindowVisible(window)) windows.push_back(window); + if(windows.size()<2) return false; + const auto from=std::find(windows.begin(),windows.end(),current); + if(from==windows.end()) return false; + auto to=std::next(from); + if(to==windows.end()) to=windows.begin(); + if(IsIconic(*to)) ShowWindow(*to,SW_RESTORE); + SetForegroundWindow(*to); + if(*to!=filesOwner) SendMessageW(*to,WM_SETFOCUS,0,0); + return true; +} + HWND findOpenDocument(const std::filesystem::path &path,const lan::FilesPeer *peer) { const auto identity=documentIdentity(path,peer?peer->id:""); @@ -2331,6 +2366,7 @@ void openTextWindow(HWND owner,const std::filesystem::path &path,bool edit,TextD { try { + if(!textWindows.contains(owner)) filesOwner=owner; std::error_code error; const auto identity=peer?path.lexically_normal():std::filesystem::weakly_canonical(path,error); const auto &original=error?path:identity; diff --git a/src/files_text_window.h b/src/files_text_window.h index f720fcb..63834b7 100644 --- a/src/files_text_window.h +++ b/src/files_text_window.h @@ -54,4 +54,5 @@ struct TextDialogs void openTextWindow(HWND owner,const std::filesystem::path &path,bool edit,TextDialogs dialogs, const lan::FilesPeer *peer=nullptr,std::string_view fragment={},bool newWindow=false); void textWindowOwnerClosed(); +bool cycleTextWindows(HWND current); } diff --git a/tests/files_markdown.ps1 b/tests/files_markdown.ps1 index a1d5b81..2354fe7 100644 --- a/tests/files_markdown.ps1 +++ b/tests/files_markdown.ps1 @@ -13,6 +13,14 @@ public static class MarkdownUi { [DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr after, string type, string title); [DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr window, out uint pid); [DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr window); + [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); + [DllImport("user32.dll")] static extern void keybd_event(byte key, byte scan, uint flags, UIntPtr extra); + public static void CycleWindow() { + keybd_event(0x11,0,0,UIntPtr.Zero); + keybd_event(0x09,0,0,UIntPtr.Zero); + keybd_event(0x09,0,2,UIntPtr.Zero); + keybd_event(0x11,0,2,UIntPtr.Zero); + } [DllImport("user32.dll")] public static extern IntPtr GetDlgItem(IntPtr window, int id); [DllImport("user32.dll", CharSet=CharSet.Unicode)] public static extern int GetWindowText(IntPtr window, StringBuilder title, int capacity); [DllImport("user32.dll", EntryPoint="SendMessageW")] public static extern IntPtr Send(IntPtr window, uint msg, IntPtr w, IntPtr l); @@ -58,6 +66,10 @@ function Browser($Scenario, $Image = '', $Title = '') { if ($LASTEXITCODE) { throw "WebView2 verification failed for $Scenario" } } +function Wait-Foreground($Window) { + [void](Wait-For 'Ctrl+Tab to change the foreground window' { [MarkdownUi]::GetForegroundWindow() -eq $Window }) +} + function Scintilla($Window) { [MarkdownUi]::FindWindowEx($Window,0,'Scintilla',[NullString]::Value) } @@ -168,6 +180,11 @@ const value = 42; Browser 'edit' $editor = Wait-For 'F6 in WebView2 to open the editor' { Find-Window 'KSwitcher.Files.Text' 'Editor' } if ($editor -ne $window) { throw 'F6 changed windows.' } + Wait-Foreground $window + [MarkdownUi]::CycleWindow() + Wait-Foreground $main + [MarkdownUi]::CycleWindow() + Wait-Foreground $window $control = Scintilla $window if ([MarkdownUi]::Send($control,2140,0,0).ToInt64()) { throw 'The editor is read-only.' } Append-Character $control 'X' @@ -187,6 +204,12 @@ const value = 42; Browser 'shift-follow' $child = Wait-For 'the Shift+click Markdown window' { Find-Window 'KSwitcher.Files.Text' 'Other.md' $window } Browser 'Linked Markdown' '' 'Other.md' + Browser 'cycle' '' 'Other.md' + Wait-Foreground $main + [MarkdownUi]::CycleWindow() + Wait-Foreground $window + Browser 'cycle' '' 'Preview.md' + Wait-Foreground $child Browser 'close' '' 'Other.md' [void](Wait-For 'Ctrl+W to close the Shift+click window' { ![MarkdownUi]::IsWindowVisible($child) }) Browser 'follow' diff --git a/tests/files_markdown_browser.mjs b/tests/files_markdown_browser.mjs index 439d5f5..eb5a772 100644 --- a/tests/files_markdown_browser.mjs +++ b/tests/files_markdown_browser.mjs @@ -99,6 +99,8 @@ try { } else if (scenario === 'back' || scenario === 'forward') { await waitFor(() => evaluate(`!document.getElementById('${scenario}').disabled`), scenario); await evaluate(`document.getElementById('${scenario}').click()`); + } else if (scenario === 'cycle') { + await command('Input.dispatchKeyEvent', {type:'keyDown', key:'Tab', code:'Tab', modifiers:2, windowsVirtualKeyCode:9}); } else if (scenario === 'close') { await command('Input.dispatchKeyEvent', {type:'keyDown', key:'w', code:'KeyW', modifiers:2, windowsVirtualKeyCode:87}); } else {