Skip to content

Commit

Permalink
Put dropdown window on active screen under Wayland (#1196)
Browse files Browse the repository at this point in the history
All complex problems in implementing this feature were caused by resizing the window programmatically on Wayland, as was done on X11. This patch sets 4 anchors instead, and then sets the left, right and bottom margins properly on showing the window.

Fixes #1142
  • Loading branch information
tsujan authored Nov 28, 2024
1 parent 9319490 commit 9ebf250
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ MainWindow::MainWindow(TerminalConfig &cfg,
presetsMenu(nullptr),
m_config(cfg),
m_dropLockButton(nullptr),
m_dropMode(dropMode)
m_dropMode(dropMode),
m_layerWindow(nullptr)
{
#ifdef HAVE_QDBUS
registerAdapter<WindowAdaptor, MainWindow>(this);
Expand Down Expand Up @@ -176,12 +177,17 @@ void MainWindow::enableDropMode()
winId();
if (QWindow *win = windowHandle())
{
if (LayerShellQt::Window* layershell = LayerShellQt::Window::get(win))
m_layerWindow = LayerShellQt::Window::get(win);
if (m_layerWindow)
{
layershell->setLayer(LayerShellQt::Window::Layer::LayerOverlay);
layershell->setKeyboardInteractivity(LayerShellQt::Window::KeyboardInteractivityOnDemand);
LayerShellQt::Window::Anchors anchors = {LayerShellQt::Window::AnchorTop};
layershell->setAnchors(anchors);
m_layerWindow->setLayer(LayerShellQt::Window::Layer::LayerOverlay);
m_layerWindow->setKeyboardInteractivity(LayerShellQt::Window::KeyboardInteractivityOnDemand);
LayerShellQt::Window::Anchors anchors = {LayerShellQt::Window::AnchorTop
| LayerShellQt::Window::AnchorBottom
| LayerShellQt::Window::AnchorLeft
| LayerShellQt::Window::AnchorRight};
m_layerWindow->setAnchors(anchors);
m_layerWindow->setScreenConfiguration(LayerShellQt::Window::ScreenConfiguration::ScreenFromCompositor);
}
}
}
Expand Down Expand Up @@ -757,9 +763,15 @@ void MainWindow::realign()
{
if (m_dropMode)
{
if (m_layerWindow)
{
return; // done in showEvent
}
QScreen *appScreen = QGuiApplication::screenAt(QCursor::pos());
if(appScreen == nullptr)
{
appScreen = QGuiApplication::primaryScreen();
}
const QRect desktop = appScreen->availableGeometry();
QRect g = QRect(desktop.x(),
desktop.y(),
Expand All @@ -768,7 +780,8 @@ void MainWindow::realign()
g.moveCenter(desktop.center());
// do not use 0 here - we need to calculate with potential panel on top
g.moveTop(desktop.top());
if (g != geometry()) {
if (g != geometry())
{
setGeometry(g);
}
}
Expand Down Expand Up @@ -871,6 +884,18 @@ bool MainWindow::event(QEvent *event)
return QMainWindow::event(event);
}

void MainWindow::showEvent(QShowEvent* event)
{
if (m_dropMode && m_layerWindow)
{
const QRect desktop = windowHandle()->screen()->availableGeometry();
int hMargin = desktop.width() * (100 - Properties::Instance()->dropWidth) / 200;
int vMargin = desktop.height() * (100 - Properties::Instance()->dropHeight) / 100;
m_layerWindow->setMargins(QMargins(hMargin, 0, hMargin, vMargin));
}
QMainWindow::showEvent(event);
}

void MainWindow::newTerminalWindow()
{
TerminalConfig cfg;
Expand Down
5 changes: 5 additions & 0 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "terminalconfig.h"
#include "dbusaddressable.h"

namespace LayerShellQt {
class Window;
}

class QToolButton;

Expand Down Expand Up @@ -55,6 +58,7 @@ class MainWindow : public QMainWindow, private Ui::mainWindow, public DBusAddres

protected:
bool event(QEvent* event) override;
void showEvent(QShowEvent* event) override;

private:
QActionGroup *tabPosition, *scrollBarPosition, *keyboardCursorShape;
Expand Down Expand Up @@ -86,6 +90,7 @@ class MainWindow : public QMainWindow, private Ui::mainWindow, public DBusAddres
void enableDropMode();
QToolButton *m_dropLockButton;
bool m_dropMode;
LayerShellQt::Window *m_layerWindow;
QxtGlobalShortcut m_dropShortcut;
void realign();
void setDropShortcut(const QKeySequence& dropShortCut);
Expand Down

0 comments on commit 9ebf250

Please sign in to comment.