From 189d728b3f81860ee0d5925108ae6f3a7759e0b8 Mon Sep 17 00:00:00 2001 From: David Kocik Date: Thu, 5 Dec 2024 13:39:21 +0100 Subject: [PATCH] SPE-2564: Add javascript listeners only once/ + keyboard shortcut fix --- src/slic3r/GUI/MainFrame.cpp | 6 +-- src/slic3r/GUI/WebViewPanel.cpp | 85 ++++++++++++++++++++------------- 2 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 7edf86c451..f26335dd25 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -1008,10 +1008,10 @@ void MainFrame::on_tab_change_rename_reload_item(int new_tab) || (m_connect_webview_added && new_tab == m_tabpanel->FindPage(m_connect_webview)) || (m_printer_webview_added && new_tab == m_tabpanel->FindPage(m_printer_webview))) { - m_menu_item_reload->SetItemLabel(_L("Re&load Web Content") + "\t\xA0" + "F5"); + m_menu_item_reload->SetItemLabel(_L("Re&load Web Content") + "\tF5"); m_menu_item_reload->SetHelp(_L("Reload Web Content")); } else { - m_menu_item_reload->SetItemLabel(_L("Re&load from Disk") + "\t\xA0" + "F5"); + m_menu_item_reload->SetItemLabel(_L("Re&load from Disk") + "\tF5"); m_menu_item_reload->SetHelp(_L("Reload the plater from disk")); } } @@ -1642,7 +1642,7 @@ void MainFrame::init_menubar_as_editor() _L("Reload Web Content"), [this](wxCommandEvent&) { reload_selected_webview(); }, "", nullptr, [this]() {return is_any_webview_selected(); }, this); #else - m_menu_item_reload = append_menu_item(editMenu, wxID_ANY, _L("Re&load from Disk") + sep + "F5", + m_menu_item_reload = append_menu_item(editMenu, wxID_ANY, _L("Re&load from Disk") + "\tF5", _L("Reload the plater from disk"), [this](wxCommandEvent&) { reload_item_function_cb(); }, "", nullptr, [this]() {return reload_item_condition_cb(); }, this); #endif // __APPLE__ diff --git a/src/slic3r/GUI/WebViewPanel.cpp b/src/slic3r/GUI/WebViewPanel.cpp index 81a98324e8..d7c4c099a6 100644 --- a/src/slic3r/GUI/WebViewPanel.cpp +++ b/src/slic3r/GUI/WebViewPanel.cpp @@ -636,17 +636,24 @@ void WebViewPanel::define_css() document.head.appendChild(style); )"; -#ifdef __APPLE__ +//#ifdef __APPLE__ +#if defined(__APPLE__) // WebView on Windows does read keyboard shortcuts // Thus doing f.e. Reload twice would make the oparation to fail script += R"( - document.addEventListener('keydown', function (event) { - if (event.key === 'F5' || (event.ctrlKey && event.key === 'r') || (event.metaKey && event.key === 'r')) { - window.ExternalApp.postMessage(JSON.stringify({ event: 'reloadHomePage', fromKeyboard: 1})); + (function() { + const listenerKey = 'custom-click-listener'; + if (!document[listenerKey]) { + document.addEventListener('keydown', function (event) { + if (event.key === 'F5' || (event.ctrlKey && event.key === 'r') || (event.metaKey && event.key === 'r')) { + window.ExternalApp.postMessage(JSON.stringify({ event: 'reloadHomePage', fromKeyboard: 1})); + } + }); + document[listenerKey] = true; } - }); + })(); )"; -#endif // !_WIN32 +#endif // __APPLE__ run_script(script); } @@ -1579,36 +1586,46 @@ void PrintablesWebViewPanel::define_css() // Capture click on hypertext // Rewritten from mobileApp code (function() { - document.addEventListener('click', function(event) { - const target = event.target.closest('a[href]'); - if (!target) return; // Ignore clicks that are not on links - const url = target.href; - // Allow empty iframe navigation - if (url === 'about:blank') { - return; // Let it proceed - } - // Debug log for navigation - console.log(`Printables:onNavigationRequest: ${url}`); - // Handle all non-printables.com domains in an external browser - if (!/printables\.com/.test(url)) { - window.ExternalApp.postMessage(JSON.stringify({ event: 'openExternalUrl', url })) - event.preventDefault(); - } - // Default: Allow navigation to proceed - }, true); // Capture the event during the capture phase - })(); - )"; -#ifdef __APPLE__ - // WebView on Windows does read keyboard shortcuts - // Thus doing f.e. Reload twice would make the oparation to fail - script += R"( - document.addEventListener('keydown', function (event) { - if (event.key === 'F5' || (event.ctrlKey && event.key === 'r') || (event.metaKey && event.key === 'r')) { - window.ExternalApp.postMessage(JSON.stringify({ event: 'reloadHomePage', fromKeyboard: 1})); + const listenerKey = 'custom-click-listener'; + if (!document[listenerKey]) { + document.addEventListener( 'click', function(event) { + const target = event.target.closest('a[href]'); + if (!target) return; // Ignore clicks that are not on links + const url = target.href; + // Allow empty iframe navigation + if (url === 'about:blank') { + return; // Let it proceed + } + // Debug log for navigation + console.log(`Printables:onNavigationRequest: ${url}`); + // Handle all non-printables.com domains in an external browser + if (!/printables\.com/.test(url)) { + window.ExternalApp.postMessage(JSON.stringify({ event: 'openExternalUrl', url })) + event.preventDefault(); + } + // Default: Allow navigation to proceed + },true); // Capture the event during the capture phase + document[listenerKey] = true; } - }); + })(); )"; -#endif // !_WIN32 +#if defined(__APPLE__) + // WebView on Windows does read keyboard shortcuts + // Thus doing f.e. Reload twice would make the oparation to fail + script += R"( + (function() { + const listenerKey = 'custom-click-listener'; + if (!document[listenerKey]) { + document.addEventListener('keydown', function (event) { + if (event.key === 'F5' || (event.ctrlKey && event.key === 'r') || (event.metaKey && event.key === 'r')) { + window.ExternalApp.postMessage(JSON.stringify({ event: 'reloadHomePage', fromKeyboard: 1})); + } + }); + document[listenerKey] = true; + } + })(); + )"; +#endif // defined(__APPLE__) run_script(script); }