Merge branch 'main' into feature/mcp

This commit is contained in:
SoftFever committed 2026-11-05 18:07:30 +08:00
commit 56b03d7bdf
58 files changed
+2331 -302

No files matched your search

+110 -6
View File
@@ -30,6 +30,7 @@
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/string.h>
#include <wx/timer.h>
#include <wx/utils.h>
#include <wx/webview.h>
@@ -47,6 +48,10 @@ namespace {
constexpr int kPopupWidth = 540;
constexpr int kPopupMinHeight = 60; // just above the bare search-bar height, so the dialog hugs content
constexpr int kPopupMaxHeight = 282;
// Delay between a deactivation and the auto-hide decision, so that a focus bounce shorter than this (the
// keyboard leaving and coming back within the interval) does not dismiss the dial. A window that really
// takes the focus still hides it when the timer fires.
constexpr int kHideSettleMs = 150;
int json_int_or(const nlohmann::json& j, const char* key, int fallback)
{
@@ -136,6 +141,17 @@ nlohmann::json speed_dial_ui_strings()
} // namespace
wxPoint speed_dial_origin(const wxRect& parent_screen_rect, const wxRect& work_area, const wxSize& box_size)
{
const int x = parent_screen_rect.x + (parent_screen_rect.width - box_size.GetWidth()) / 2;
const int y = parent_screen_rect.y + (parent_screen_rect.height - box_size.GetHeight()) / 2;
// std::max(0, ...) keeps the clamp bounds ordered when the work area is smaller than the box.
return {std::clamp(x, work_area.x, work_area.x + std::max(0, work_area.width - box_size.GetWidth())),
std::clamp(y, work_area.y, work_area.y + std::max(0, work_area.height - box_size.GetHeight()))};
}
bool speed_dial_should_hide(bool shown, bool active) { return shown && !active; }
SpeedDialWebDialog::SpeedDialWebDialog(wxWindow* parent)
: WebViewHostDialog(parent,
wxID_ANY,
@@ -145,14 +161,31 @@ SpeedDialWebDialog::SpeedDialWebDialog(wxWindow* parent)
wxBORDER_NONE | wxFRAME_NO_TASKBAR | wxFRAME_FLOAT_ON_PARENT | wxFRAME_SHAPED)
{
SetBackgroundColour(bg_color());
#ifdef __WXGTK__
// wxGTK dialogs default to CENTER_ON_PARENT, which places the window at its first map and then
// keeps that position for every later Show(). position_over_parent() supplies the origin instead.
gtk_window_set_position(GTK_WINDOW(GetHandle()), GTK_WIN_POS_NONE);
#endif
// The dial dismisses itself when the focus leaves it. The hide is deferred by kHideSettleMs and
// re-checked, so a focus bounce shorter than that is not a dismissal. It does not protect against
// another window being activated: the first-open close on Linux was the main frame's tab handler
// calling SetFocus() while the Calibration page was built in the background, which on wxGTK presents
// the main window (fixed in MainFrame::init_tabpanel).
m_hide_timer.SetOwner(this);
Bind(wxEVT_TIMER, &SpeedDialWebDialog::on_hide_timer, this);
Bind(wxEVT_ACTIVATE, [this](wxActivateEvent& event) {
// Focus the WebKit widget exactly when the WM makes the popup the active window
// (modeless focus is granted asynchronously, so a focus request made right after
// Show() is dropped). Also re-corrects focus on every re-open.
if (event.GetActive() && IsShown())
if (!IsShown()) {
event.Skip();
return;
}
if (event.GetActive()) {
m_hide_timer.Stop();
focus_webview(browser(), m_page_ready);
else if (!event.GetActive() && IsShown())
Hide();
} else
schedule_hide();
event.Skip();
});
if (!create_webview("web/dialog/SpeedDial/index.html", wxEmptyString, wxSize(kPopupWidth, kPopupMaxHeight),
@@ -173,14 +206,74 @@ SpeedDialWebDialog::SpeedDialWebDialog(wxWindow* parent)
apply_rounded_shape();
});
apply_rounded_shape();
// Place the window before the first map; request_show() re-derives it on every later open.
position_over_parent();
}
SpeedDialWebDialog::~SpeedDialWebDialog() { m_alive->store(false, std::memory_order_release); }
SpeedDialWebDialog::~SpeedDialWebDialog()
{
m_hide_timer.Stop();
m_alive->store(false, std::memory_order_release);
}
nlohmann::json SpeedDialWebDialog::localized_strings() const { return speed_dial_ui_strings(); }
void SpeedDialWebDialog::position_over_parent()
{
wxWindow* parent = GetParent();
if (!parent)
return;
int display_index = wxDisplay::GetFromWindow(parent);
if (display_index == wxNOT_FOUND)
display_index = 0;
const wxRect work_area = wxDisplay(static_cast<unsigned int>(display_index)).GetClientArea();
const wxPoint origin = speed_dial_origin(parent->GetScreenRect(), work_area, FromDIP(wxSize(kPopupWidth, kPopupMaxHeight)));
if (origin != GetPosition())
Move(origin);
#ifdef __WXGTK__
// wxGTK caches requested coordinates: Move() can skip the native request even when the window
// manager ignored the previous one. Reapply at native level (same as ColorPickerDialog).
gtk_window_move(GTK_WINDOW(GetHandle()), origin.x, origin.y);
#endif
}
void SpeedDialWebDialog::schedule_hide()
{
if (IsShown())
m_hide_timer.StartOnce(kHideSettleMs);
}
bool SpeedDialWebDialog::is_active()
{
#ifdef __WXGTK__
GtkWindow* const window = GTK_WINDOW(GetHandle());
return window && gtk_window_is_active(window);
#else
return IsActive();
#endif
}
void SpeedDialWebDialog::on_hide_timer(wxTimerEvent&)
{
if (!m_alive->load(std::memory_order_acquire))
return;
if (!speed_dial_should_hide(IsShown(), is_active()))
return;
Hide();
}
void SpeedDialWebDialog::request_show()
{
// A pending auto-hide belongs to the previous open.
m_hide_timer.Stop();
// Re-derived on every open: the dialog is a long-lived hidden window, so without this the
// coordinates stay at the frame's position from the first open and the dial stops following the
// frame when it is moved, resized, or dragged to another display.
position_over_parent();
if (IsShown()) {
Raise();
focus_webview(browser(), m_page_ready);
@@ -194,9 +287,17 @@ void SpeedDialWebDialog::request_show()
if (m_page_ready)
send_actions();
// Grab focus now and again on wxEVT_ACTIVATE; grabbing directly on the WebKit widget is
// what makes typing reach the search field immediately on open.
focus_webview(browser(), m_page_ready);
// what makes typing reach the search field immediately on open. Skipped while the page is still
// loading: on the first open the window is not mapped yet, so the grab would land on a window the
// WM has not activated - the ACTIVATE handler grabs it once it has.
if (m_page_ready)
focus_webview(browser(), true);
repaint_webview();
// A position requested before the window is mapped can be dropped, so reapply once it is up.
wxGetApp().CallAfter([this, alive = m_alive]() {
if (alive->load(std::memory_order_acquire))
position_over_parent();
});
}
void SpeedDialWebDialog::on_script_message(const nlohmann::json& payload)
@@ -350,6 +451,9 @@ void SpeedDialWebDialog::apply_rounded_shape()
void SpeedDialWebDialog::on_dpi_changed(const wxRect&)
{
apply_rounded_shape();
// A monitor with a different scale factor re-anchors the window: MSW applies the rect the OS
// suggests (wxNonOwnedWindow::HandleDPIChange), so re-derive the origin from the parent instead.
position_over_parent();
Refresh();
}