AppSwitcher: dynamically adjust to screen shape

Follow window resize and adjust accordingly
This commit is contained in:
gfgit 2024-01-02 23:02:58 +01:00
parent 2769857ffd
commit 4d1076c703
3 changed files with 34 additions and 11 deletions

View File

@ -44,7 +44,7 @@
AppSwitcher::AppSwitcher(QWidget* parent)
: QListView(parent)
{
setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint);
//setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint | Qt::CustomizeWindowHint);
setObjectName("AppSwitcher");
m_globalShortcut = GlobalKeyShortcut::Client::instance()->addAction(
@ -71,7 +71,7 @@ AppSwitcher::AppSwitcher(QWidget* parent)
m_delegate = new AppItemDelegate(this);
setItemDelegate(m_delegate);
setContentsMargins(5, 5, 5, 5);
setOrientation(Qt::Vertical);
setOrientation(Qt::Vertical, true);
m_timer = new QTimer(this);
m_timer->setInterval(100);
@ -125,11 +125,15 @@ void AppSwitcher::showSwitcher(bool forward)
m_timer->start();
}
void AppSwitcher::setOrientation(Qt::Orientation orientation)
void AppSwitcher::setOrientation(Qt::Orientation orientation, bool force)
{
m_delegate->setOrientation(orientation);
if(!force && m_orientation == orientation)
return;
if(orientation == Qt::Horizontal)
m_orientation = orientation;
m_delegate->setOrientation(m_orientation);
if(m_orientation == Qt::Horizontal)
{
setFlow(QListView::LeftToRight);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -143,6 +147,13 @@ void AppSwitcher::setOrientation(Qt::Orientation orientation)
}
}
Qt::Orientation AppSwitcher::calculateBestOrientation()
{
if(height() > width())
return Qt::Vertical;
return Qt::Horizontal;
}
void AppSwitcher::selectItem(bool forward)
{
m_timer->start();
@ -168,13 +179,13 @@ void AppSwitcher::activateWindow(WId id)
KX11Extras::forceActiveWindow(id);
}
void AppSwitcher::keyReleaseEvent(QKeyEvent* event)
void AppSwitcher::keyReleaseEvent(QKeyEvent* e)
{
// if (event->modifiers() == 0) {
// close();
// activateWindow(model()->data(model()->index(m_current, 0), AppRole::Window).value<WId>());
// }
QWidget::keyReleaseEvent(event);
QWidget::keyReleaseEvent(e);
}
void AppSwitcher::timer()
@ -191,3 +202,11 @@ void AppSwitcher::closeEvent(QCloseEvent*)
{
m_timer->stop();
}
void AppSwitcher::resizeEvent(QResizeEvent *e)
{
QListView::resizeEvent(e);
Qt::Orientation newOrient = calculateBestOrientation();
setOrientation(newOrient);
}

View File

@ -44,11 +44,14 @@ public:
void showSwitcher(bool forward = true);
void setOrientation(Qt::Orientation orientation);
void setOrientation(Qt::Orientation orientation, bool force = false);
Qt::Orientation calculateBestOrientation();
protected:
void keyReleaseEvent(QKeyEvent* event) override;
void closeEvent(QCloseEvent*) override;
void keyReleaseEvent(QKeyEvent *e) override;
void closeEvent(QCloseEvent *e) override;
void resizeEvent(QResizeEvent *e) override;
private:
void selectItem(bool forward = true);
@ -61,4 +64,5 @@ private:
QTimer* m_timer;
int m_current = 0;
AppItemDelegate* m_delegate = nullptr;
Qt::Orientation m_orientation = Qt::Vertical;
};

View File

@ -58,7 +58,7 @@ int main(int argc, char* argv[])
Settings::instance().setIconSize(512);
AppSwitcher switcher(nullptr);
switcher.setOrientation(Qt::Horizontal);
switcher.setOrientation(Qt::Vertical);
switcher.showSwitcher();
return app.exec();