Skip to content

Truly independent web browser

UI/Qt: Place popup windows the way window.open() asks for

Andreas Kling143885c556d3
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 23ec047b3ee..137795d3b1f 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -1573,12 +1573,8 @@ static WindowConfiguration window_configuration_for(SessionWindowGeometry const&
auto* screen = QGuiApplication::screenAt(rect.center());
if (!screen)
screen = QGuiApplication::primaryScreen();
- if (screen) {
- auto available = screen->availableGeometry();
- rect.setSize(rect.size().boundedTo(available.size()));
- rect.moveLeft(clamp(rect.left(), available.left(), available.right() - rect.width() + 1));
- rect.moveTop(clamp(rect.top(), available.top(), available.bottom() - rect.height() + 1));
- }
+ if (screen)
+ rect = BrowserWindow::rect_fitted_into(rect, screen->availableGeometry());
return {
.x = Web::DevicePixels { rect.x() },
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 1f5bde2d3ed..42462650ec1 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -938,10 +938,9 @@ void BrowserWindow::initialize_tab_view(Tab* tab)
WindowConfiguration configuration {
.x = hints.screen_x.has_value() ? hints.screen_x : cascaded_configuration.x,
.y = hints.screen_y.has_value() ? hints.screen_y : cascaded_configuration.y,
- .width = hints.width,
- .height = hints.height,
};
- Application::the().new_window({}, configuration, IsPopupWindow::Yes, m_is_private, hub_view_id);
+ auto& window = Application::the().new_window({}, configuration, IsPopupWindow::Yes, m_is_private, hub_view_id);
+ window.place_popup_window(hints, *screen());
return;
}
create_new_tab(activate_tab, hub_view_id);
@@ -1652,6 +1651,49 @@ void BrowserWindow::set_window_rect(Optional<Web::DevicePixels> x, Optional<Web:
setGeometry(x.value().value(), y.value().value(), width.value().value(), height.value().value());
}
+QRect BrowserWindow::rect_fitted_into(QRect rect, QRect const& bounds)
+{
+ rect.setSize(rect.size().boundedTo(bounds.size()));
+ rect.moveLeft(clamp(rect.left(), bounds.left(), bounds.right() - rect.width() + 1));
+ rect.moveTop(clamp(rect.top(), bounds.top(), bounds.bottom() - rect.height() + 1));
+ return rect;
+}
+
+void BrowserWindow::place_popup_window(Web::HTML::WebViewHints const& hints, QScreen const& opener_screen)
+{
+ // NB: The window must be shown, as Qt does not lay out a hidden window, so the size of the tab bar and toolbar is not
+ // known before that.
+ VERIFY(isVisible());
+
+ // A page asks for the position of the window's frame, as that is the position it sees.
+ auto position = pos();
+ if (hints.screen_x.has_value())
+ position.setX(hints.screen_x->value());
+ if (hints.screen_y.has_value())
+ position.setY(hints.screen_y->value());
+
+ // NB: A Wayland compositor decides where a window goes and does not tell where that is. The window is then taken to
+ // be on the screen of the window it was opened from.
+ static bool const is_wayland = QGuiApplication::platformName().startsWith(QStringLiteral("wayland"));
+ auto const* target_screen = is_wayland ? nullptr : QGuiApplication::screenAt(position);
+ if (!target_screen)
+ target_screen = &opener_screen;
+ auto available_geometry = target_screen->availableGeometry();
+
+ // A page asks for the size of its viewport, so the window is larger than that by the size of what is around the
+ // viewport. A size of 0 leaves the size of the window as it is.
+ auto size_around_viewport = size() - view().size();
+ auto frame_size = frameGeometry().size() - size();
+ auto window_size = size();
+ if (hints.width.has_value() && hints.width->value() > 0)
+ window_size.setWidth(min(hints.width->value(), available_geometry.width()) + size_around_viewport.width());
+ if (hints.height.has_value() && hints.height->value() > 0)
+ window_size.setHeight(min(hints.height->value(), available_geometry.height()) + size_around_viewport.height());
+ resize(window_size.boundedTo(available_geometry.size() - frame_size));
+
+ move(rect_fitted_into({ position, frameGeometry().size() }, available_geometry).topLeft());
+}
+
void BrowserWindow::enter_fullscreen()
{
m_tabs_container->set_tab_bar_visible(false);
diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.h
index f3e545e4831..84c7c4d84f1 100644
--- a/UI/Qt/BrowserWindow.h
+++ b/UI/Qt/BrowserWindow.h
@@ -254,6 +254,9 @@ public:
void on_devtools_disabled();
void set_window_rect(Optional<Web::DevicePixels> x, Optional<Web::DevicePixels> y, Optional<Web::DevicePixels> width, Optional<Web::DevicePixels> height);
+ // Returns the rect made no larger than the bounds, and moved to be inside them.
+ static QRect rect_fitted_into(QRect, QRect const& bounds);
+ void place_popup_window(Web::HTML::WebViewHints const&, QScreen const& opener_screen);
public slots:
void tab_title_changed(int index, QString const&);