1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
|
/*
SPDX-FileCopyrightText: 2008-2014 Eike Hein <hein@kde.org>
SPDX-FileCopyrightText: 2009 Juan Carlos Torres <carlosdgtorres@gmail.com>
SPDX-FileCopyrightText: 2020 Ryan McCoskrie <work@ryanmccoskrie.me>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "mainwindow.h"
#include "config/appearancesettings.h"
#include "config/windowsettings.h"
#include "firstrundialog.h"
#include "sessionstack.h"
#include "settings.h"
#include "skin.h"
#include "tabbar.h"
#include "terminal.h"
#include "titlebar.h"
#include "ui_behaviorsettings.h"
#include <KAboutData>
#include <KActionCollection>
#include <KConfigDialog>
#include <KGlobalAccel>
#include <KHelpMenu>
#include <KLocalizedString>
#include <KMessageBox>
#include <KNotification>
#include <KNotifyConfigWidget>
#include <KShortcutsDialog>
#include <KStandardAction>
#include <KStandardActions>
#include <KStatusNotifierItem>
#include <KToggleFullScreenAction>
#include <KWindowEffects>
#include <KWindowInfo>
#include <KWindowSystem>
#include <KX11Extras>
#include <QApplication>
#include <QDBusConnection>
#include <QDBusPendingReply>
#include <QDBusReply>
#include <QMenu>
#include <QPainter>
#include <QScreen>
#include <QWhatsThis>
#include <QWindow>
#if HAVE_X11
#include <private/qtx11extras_p.h>
#include <X11/Xlib.h>
#include <fixx11h.h>
#endif
#if HAVE_KWAYLAND
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmashell.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/surface.h>
#endif
MainWindow::MainWindow(QWidget *parent)
: KMainWindow(parent, Qt::CustomizeWindowHint | Qt::FramelessWindowHint | Qt::Tool)
{
QDBusConnection::sessionBus().registerObject(QStringLiteral("/yakuake/window"), this, QDBusConnection::ExportScriptableSlots);
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_DeleteOnClose, false);
setAttribute(Qt::WA_QuitOnClose, true);
m_skin = new Skin();
m_menu = new QMenu(this);
m_helpMenu = new KHelpMenu(this, KAboutData::applicationData());
m_sessionStack = new SessionStack(this);
m_titleBar = new TitleBar(this);
m_tabBar = new TabBar(this);
m_notifierItem = nullptr;
m_firstRunDialog = nullptr;
m_isFullscreen = false;
#if HAVE_X11
m_kwinAssistPropSet = false;
m_isX11 = KWindowSystem::isPlatformX11();
#else
m_isX11 = false;
#endif
m_isWayland = KWindowSystem::isPlatformWayland();
#if HAVE_KWAYLAND
m_plasmaShell = nullptr;
m_plasmaShellSurface = nullptr;
initWayland();
#endif
m_toggleLock = false;
setupActions();
setupMenu();
connect(m_tabBar, SIGNAL(newTabRequested()), m_sessionStack, SLOT(addSession()));
connect(m_tabBar, SIGNAL(lastTabClosed()), m_tabBar, SIGNAL(newTabRequested()));
connect(m_tabBar, SIGNAL(lastTabClosed()), this, SLOT(handleLastTabClosed()));
connect(m_tabBar, SIGNAL(tabSelected(int)), m_sessionStack, SLOT(raiseSession(int)));
connect(m_tabBar, SIGNAL(tabClosed(int)), m_sessionStack, SLOT(removeSession(int)));
connect(m_tabBar, &TabBar::tabTitleEdited, m_sessionStack, [&](int, QString) {
m_sessionStack->raiseSession(m_sessionStack->activeSessionId());
});
connect(m_tabBar, SIGNAL(requestTerminalHighlight(int)), m_sessionStack, SLOT(handleTerminalHighlightRequest(int)));
connect(m_tabBar, SIGNAL(requestRemoveTerminalHighlight()), m_sessionStack, SIGNAL(removeTerminalHighlight()));
connect(m_tabBar, SIGNAL(tabContextMenuClosed()), m_sessionStack, SIGNAL(removeTerminalHighlight()));
connect(m_sessionStack, SIGNAL(sessionAdded(int, QString)), m_tabBar, SLOT(addTab(int, QString)));
connect(m_sessionStack, SIGNAL(sessionRaised(int)), m_tabBar, SLOT(selectTab(int)));
connect(m_sessionStack, SIGNAL(sessionRemoved(int)), m_tabBar, SLOT(removeTab(int)));
connect(m_sessionStack, SIGNAL(activeTitleChanged(QString)), m_titleBar, SLOT(setTitle(QString)));
connect(m_sessionStack, SIGNAL(activeTitleChanged(QString)), this, SLOT(setWindowTitle(QString)));
connect(m_sessionStack, &SessionStack::wantsBlurChanged, this, &MainWindow::applyWindowProperties);
connect(&m_mousePoller, SIGNAL(timeout()), this, SLOT(pollMouse()));
if (KWindowSystem::isPlatformX11()) {
connect(KX11Extras::self(), &KX11Extras::workAreaChanged, this, &MainWindow::applyWindowGeometry);
}
connect(qApp, &QGuiApplication::screenAdded, this, &MainWindow::updateScreenMenu);
connect(qApp, &QGuiApplication::screenRemoved, this, &MainWindow::updateScreenMenu);
applySettings();
m_sessionStack->addSession();
if (Settings::firstRun()) {
QMetaObject::invokeMethod(this, "toggleWindowState", Qt::QueuedConnection);
QMetaObject::invokeMethod(this, "showFirstRunDialog", Qt::QueuedConnection);
} else {
if (Settings::pollMouse())
toggleMousePoll(true);
}
if (Settings::openAfterStart())
QMetaObject::invokeMethod(this, "toggleWindowState", Qt::QueuedConnection);
}
MainWindow::~MainWindow()
{
Settings::self()->save();
delete m_skin;
}
#if HAVE_KWAYLAND
void MainWindow::initWayland()
{
if (!m_isWayland) {
return;
}
using namespace KWayland::Client;
auto connection = ConnectionThread::fromApplication(this);
if (!connection) {
return;
}
Registry *registry = new Registry(this);
registry->create(connection);
QObject::connect(registry, &Registry::interfacesAnnounced, this, [registry, this] {
const auto interface = registry->interface(Registry::Interface::PlasmaShell);
if (interface.name != 0) {
m_plasmaShell = registry->createPlasmaShell(interface.name, interface.version, this);
}
});
registry->setup();
connection->roundtrip();
}
void MainWindow::initWaylandSurface()
{
if (m_plasmaShellSurface) {
m_plasmaShellSurface->setPosition(pos());
return;
}
if (!m_plasmaShell) {
return;
}
if (auto surface = KWayland::Client::Surface::fromWindow(windowHandle())) {
m_plasmaShellSurface = m_plasmaShell->createSurface(surface, this);
m_plasmaShellSurface->setPosition(pos());
m_plasmaShellSurface->setSkipTaskbar(true);
m_plasmaShellSurface->setSkipSwitcher(true);
}
}
#endif
bool MainWindow::queryClose()
{
bool confirmQuit = Settings::confirmQuit();
bool hasUnclosableSessions = m_sessionStack->hasUnclosableSessions();
QString closeQuestion = xi18nc("@info", "Are you sure you want to quit?");
QString warningMessage;
if ((confirmQuit && m_sessionStack->count() > 1) || hasUnclosableSessions) {
if (confirmQuit && m_sessionStack->count() > 1) {
if (hasUnclosableSessions)
warningMessage = xi18nc("@info",
"<warning>There are multiple open sessions, <emphasis>some of which you have locked to prevent closing them "
"accidentally.</emphasis> These will be killed if you continue.</warning>");
else
warningMessage = xi18nc("@info", "<warning>There are multiple open sessions. These will be killed if you continue.</warning>");
} else if (hasUnclosableSessions) {
warningMessage = xi18nc("@info",
"<warning>There are one or more open sessions that you have locked to prevent closing them accidentally. These will be "
"killed if you continue.</warning>");
}
int result = KMessageBox::warningContinueCancel(this,
warningMessage + QStringLiteral("<br /><br />") + closeQuestion,
xi18nc("@title:window", "Really Quit?"),
KStandardGuiItem::quit(),
KStandardGuiItem::cancel());
return result != KMessageBox::Cancel;
}
return true;
}
void MainWindow::setupActions()
{
m_actionCollection = new KActionCollection(this);
KToggleFullScreenAction *fullScreenAction = new KToggleFullScreenAction(this);
fullScreenAction->setWindow(this);
actionCollection()->setDefaultShortcut(fullScreenAction, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_F11));
m_actionCollection->addAction(QStringLiteral("view-full-screen"), fullScreenAction);
connect(fullScreenAction, SIGNAL(toggled(bool)), this, SLOT(setFullScreen(bool)));
QAction *action = KStandardAction::quit(this, SLOT(close()), actionCollection());
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Q));
action = KStandardAction::aboutApp(m_helpMenu, SLOT(aboutApplication()), actionCollection());
action = KStandardAction::reportBug(m_helpMenu, SLOT(reportBug()), actionCollection());
action = KStandardAction::aboutKDE(m_helpMenu, SLOT(aboutKDE()), actionCollection());
action = KStandardAction::keyBindings(this, SLOT(configureKeys()), actionCollection());
action = KStandardAction::configureNotifications(this, SLOT(configureNotifications()), actionCollection());
action = KStandardAction::preferences(this, SLOT(configureApp()), actionCollection());
action = KStandardAction::whatsThis(this, SLOT(whatsThis()), actionCollection());
action = actionCollection()->addAction(QStringLiteral("toggle-window-state"));
action->setText(xi18nc("@action", "Open/Retract Yakuake"));
action->setIcon(QIcon::fromTheme(QStringLiteral("yakuake")));
KGlobalAccel::self()->setGlobalShortcut(action, QList<QKeySequence>() << QKeySequence(Qt::Key_F12));
connect(action, SIGNAL(triggered()), this, SLOT(toggleWindowState()));
connect(action, SIGNAL(changed()), this, SLOT(updateTrayTooltip()));
connect(KGlobalAccel::self(), SIGNAL(globalShortcutChanged(QAction *, const QKeySequence &)), this, SLOT(updateTrayTooltip()));
updateTrayTooltip();
action = actionCollection()->addAction(QStringLiteral("keep-open"));
action->setText(xi18nc("@action", "Keep window open when it loses focus"));
action->setCheckable(true);
connect(action, SIGNAL(toggled(bool)), this, SLOT(setKeepOpen(bool)));
action = actionCollection()->addAction(QStringLiteral("manage-profiles"));
action->setText(xi18nc("@action", "Manage Profiles..."));
action->setIcon(QIcon::fromTheme(QStringLiteral("configure")));
connect(action, SIGNAL(triggered()), m_sessionStack, SIGNAL(manageProfiles()));
action = actionCollection()->addAction(QStringLiteral("edit-profile"));
action->setText(xi18nc("@action", "Edit Current Profile..."));
action->setIcon(QIcon::fromTheme(QStringLiteral("document-properties")));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("increase-window-width"));
action->setText(xi18nc("@action", "Increase Window Width"));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_Right));
connect(action, SIGNAL(triggered()), this, SLOT(increaseWindowWidth()));
action = actionCollection()->addAction(QStringLiteral("decrease-window-width"));
action->setText(xi18nc("@action", "Decrease Window Width"));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_Left));
connect(action, SIGNAL(triggered()), this, SLOT(decreaseWindowWidth()));
action = actionCollection()->addAction(QStringLiteral("increase-window-height"));
action->setText(xi18nc("@action", "Increase Window Height"));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_Down));
connect(action, SIGNAL(triggered()), this, SLOT(increaseWindowHeight()));
action = actionCollection()->addAction(QStringLiteral("decrease-window-height"));
action->setText(xi18nc("@action", "Decrease Window Height"));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::ALT | Qt::SHIFT | Qt::Key_Up));
connect(action, SIGNAL(triggered()), this, SLOT(decreaseWindowHeight()));
action = actionCollection()->addAction(QStringLiteral("new-session"));
action->setText(xi18nc("@action", "New Session"));
action->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_T));
connect(action, SIGNAL(triggered()), m_sessionStack, SLOT(addSession()));
action = actionCollection()->addAction(QStringLiteral("new-session-two-horizontal"));
action->setText(xi18nc("@action", "Two Terminals, Horizontally"));
action->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
connect(action, SIGNAL(triggered()), m_sessionStack, SLOT(addSessionTwoHorizontal()));
action = actionCollection()->addAction(QStringLiteral("new-session-two-vertical"));
action->setText(xi18nc("@action", "Two Terminals, Vertically"));
action->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
connect(action, SIGNAL(triggered()), m_sessionStack, SLOT(addSessionTwoVertical()));
action = actionCollection()->addAction(QStringLiteral("new-session-quad"));
action->setText(xi18nc("@action", "Four Terminals, Grid"));
action->setIcon(QIcon::fromTheme(QStringLiteral("tab-new")));
connect(action, SIGNAL(triggered()), m_sessionStack, SLOT(addSessionQuad()));
action = actionCollection()->addAction(QStringLiteral("close-session"));
action->setText(xi18nc("@action", "Close Session"));
action->setIcon(QIcon::fromTheme(QStringLiteral("tab-close")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_W));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("previous-session"));
action->setText(xi18nc("@action", "Previous Session"));
action->setIcon(QIcon::fromTheme(QStringLiteral("go-previous")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::SHIFT | Qt::Key_Left));
connect(action, SIGNAL(triggered()), m_tabBar, SLOT(selectPreviousTab()));
action = actionCollection()->addAction(QStringLiteral("next-session"));
action->setText(xi18nc("@action", "Next Session"));
action->setIcon(QIcon::fromTheme(QStringLiteral("go-next")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::SHIFT | Qt::Key_Right));
connect(action, SIGNAL(triggered()), m_tabBar, SLOT(selectNextTab()));
action = actionCollection()->addAction(QStringLiteral("move-session-left"));
action->setText(xi18nc("@action", "Move Session Left"));
action->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Left));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("move-session-right"));
action->setText(xi18nc("@action", "Move Session Right"));
action->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Right));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("grow-terminal-right"));
action->setText(xi18nc("@action", "Grow Terminal to the Right"));
action->setIcon(QIcon::fromTheme(QStringLiteral("arrow-right")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Right));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("grow-terminal-left"));
action->setText(xi18nc("@action", "Grow Terminal to the Left"));
action->setIcon(QIcon::fromTheme(QStringLiteral("arrow-left")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Left));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("grow-terminal-top"));
action->setText(xi18nc("@action", "Grow Terminal to the Top"));
action->setIcon(QIcon::fromTheme(QStringLiteral("arrow-up")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Up));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("grow-terminal-bottom"));
action->setText(xi18nc("@action", "Grow Terminal to the Bottom"));
action->setIcon(QIcon::fromTheme(QStringLiteral("arrow-down")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_Down));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("rename-session"));
action->setText(xi18nc("@action", "Rename Session..."));
action->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_S));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("previous-terminal"));
action->setText(xi18nc("@action", "Previous Terminal"));
action->setIcon(QIcon::fromTheme(QStringLiteral("go-previous")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_Tab));
connect(action, SIGNAL(triggered()), m_sessionStack, SIGNAL(previousTerminal()));
action = actionCollection()->addAction(QStringLiteral("next-terminal"));
action->setText(xi18nc("@action", "Next Terminal"));
action->setIcon(QIcon::fromTheme(QStringLiteral("go-next")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_Tab));
connect(action, SIGNAL(triggered()), m_sessionStack, SIGNAL(nextTerminal()));
action = actionCollection()->addAction(QStringLiteral("close-active-terminal"));
action->setText(xi18nc("@action", "Close Active Terminal"));
action->setIcon(QIcon::fromTheme(QStringLiteral("view-close")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_R));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("split-left-right"));
action->setText(xi18nc("@action", "Split Left/Right"));
action->setIcon(QIcon::fromTheme(QStringLiteral("view-split-left-right")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_ParenLeft));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("split-top-bottom"));
action->setText(xi18nc("@action", "Split Top/Bottom"));
action->setIcon(QIcon::fromTheme(QStringLiteral("view-split-top-bottom")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_ParenRight));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("split-auto"));
action->setText(xi18nc("@action", "Split Automatically"));
action->setIcon(QIcon::fromTheme(QStringLiteral("view-split-auto")));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_Asterisk));
connect(action, SIGNAL(triggered()), this, SLOT(handleContextDependentAction()));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("toggle-session-prevent-closing"));
action->setText(xi18nc("@action", "Prevent Closing"));
action->setCheckable(true);
connect(action, SIGNAL(triggered(bool)), this, SLOT(handleContextDependentToggleAction(bool)));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("toggle-session-keyboard-input"));
action->setText(xi18nc("@action", "Disable Keyboard Input"));
action->setCheckable(true);
connect(action, SIGNAL(triggered(bool)), this, SLOT(handleContextDependentToggleAction(bool)));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("toggle-session-monitor-activity"));
action->setText(xi18nc("@action", "Monitor for Activity"));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_A));
action->setCheckable(true);
connect(action, SIGNAL(triggered(bool)), this, SLOT(handleContextDependentToggleAction(bool)));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("toggle-session-monitor-silence"));
action->setText(xi18nc("@action", "Monitor for Silence"));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_I));
action->setCheckable(true);
connect(action, SIGNAL(triggered(bool)), this, SLOT(handleContextDependentToggleAction(bool)));
m_contextDependentActions << action;
action = actionCollection()->addAction(QStringLiteral("toggle-titlebar"));
action->setText(xi18nc("@action", "Toggle Titlebar"));
actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_M));
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(handleToggleTitlebar()));
for (uint i = 1; i <= 10; ++i) {
action = actionCollection()->addAction(QStringLiteral("switch-to-session-%1").arg(i));
action->setText(xi18nc("@action", "Switch to Session %1", i));
action->setData(i - 1);
connect(action, SIGNAL(triggered()), this, SLOT(handleSwitchToAction()));
if (i < 10) {
// add default shortcut bindings for the first 9 sessions
actionCollection()->setDefaultShortcut(action, QStringLiteral("Alt+%1").arg(i));
} else {
// add default shortcut bindings for the 10th session
actionCollection()->setDefaultShortcut(action, Qt::ALT | Qt::Key_0);
}
}
m_actionCollection->associateWidget(this);
m_actionCollection->readSettings();
}
void MainWindow::handleContextDependentAction(QAction *action, int sessionId)
{
if (sessionId == -1)
sessionId = m_sessionStack->activeSessionId();
if (sessionId == -1)
return;
if (!action)
action = qobject_cast<QAction *>(QObject::sender());
if (action == actionCollection()->action(QStringLiteral("edit-profile")))
m_sessionStack->editProfile(sessionId);
if (action == actionCollection()->action(QStringLiteral("close-session")))
m_sessionStack->removeSession(sessionId);
if (action == actionCollection()->action(QStringLiteral("move-session-left")))
m_tabBar->moveTabLeft(sessionId);
if (action == actionCollection()->action(QStringLiteral("move-session-right")))
m_tabBar->moveTabRight(sessionId);
if (action == actionCollection()->action(QStringLiteral("rename-session")))
m_tabBar->interactiveRename(sessionId);
if (action == actionCollection()->action(QStringLiteral("close-active-terminal")))
m_sessionStack->closeActiveTerminal(sessionId);
if (action == actionCollection()->action(QStringLiteral("split-left-right")))
m_sessionStack->splitSessionLeftRight(sessionId);
if (action == actionCollection()->action(QStringLiteral("split-top-bottom")))
m_sessionStack->splitSessionTopBottom(sessionId);
if (action == actionCollection()->action(QStringLiteral("split-auto")))
m_sessionStack->splitSessionAuto(sessionId);
if (action == actionCollection()->action(QStringLiteral("grow-terminal-right")))
m_sessionStack->tryGrowTerminalRight(m_sessionStack->activeTerminalId());
if (action == actionCollection()->action(QStringLiteral("grow-terminal-left")))
m_sessionStack->tryGrowTerminalLeft(m_sessionStack->activeTerminalId());
if (action == actionCollection()->action(QStringLiteral("grow-terminal-top")))
m_sessionStack->tryGrowTerminalTop(m_sessionStack->activeTerminalId());
if (action == actionCollection()->action(QStringLiteral("grow-terminal-bottom")))
m_sessionStack->tryGrowTerminalBottom(m_sessionStack->activeTerminalId());
}
void MainWindow::handleContextDependentToggleAction(bool checked, QAction *action, int sessionId)
{
if (sessionId == -1)
sessionId = m_sessionStack->activeSessionId();
if (sessionId == -1)
return;
if (!action)
action = qobject_cast<QAction *>(QObject::sender());
if (action == actionCollection()->action(QStringLiteral("toggle-session-prevent-closing"))) {
m_sessionStack->setSessionClosable(sessionId, !checked);
// Repaint the tab bar when the Prevent Closing action is toggled
// so the lock icon is added to or removed from the tab label.
m_tabBar->repaint();
}
if (action == actionCollection()->action(QStringLiteral("toggle-session-keyboard-input")))
m_sessionStack->setSessionKeyboardInputEnabled(sessionId, !checked);
if (action == actionCollection()->action(QStringLiteral("toggle-session-monitor-activity")))
m_sessionStack->setSessionMonitorActivityEnabled(sessionId, checked);
if (action == actionCollection()->action(QStringLiteral("toggle-session-monitor-silence")))
m_sessionStack->setSessionMonitorSilenceEnabled(sessionId, checked);
}
void MainWindow::setContextDependentActionsQuiet(bool quiet)
{
QListIterator<QAction *> i(m_contextDependentActions);
while (i.hasNext())
i.next()->blockSignals(quiet);
}
void MainWindow::handleToggleTerminalKeyboardInput(bool checked)
{
QAction *action = qobject_cast<QAction *>(QObject::sender());
if (!action || action->data().isNull())
return;
bool ok = false;
int terminalId = action->data().toInt(&ok);
if (!ok)
return;
m_sessionStack->setTerminalKeyboardInputEnabled(terminalId, !checked);
}
void MainWindow::handleToggleTerminalMonitorActivity(bool checked)
{
QAction *action = qobject_cast<QAction *>(QObject::sender());
if (!action || action->data().isNull())
return;
bool ok = false;
int terminalId = action->data().toInt(&ok);
if (!ok)
return;
m_sessionStack->setTerminalMonitorActivityEnabled(terminalId, checked);
}
void MainWindow::handleToggleTerminalMonitorSilence(bool checked)
{
QAction *action = qobject_cast<QAction *>(QObject::sender());
if (!action || action->data().isNull())
return;
bool ok = false;
int terminalId = action->data().toInt(&ok);
if (!ok)
return;
m_sessionStack->setTerminalMonitorSilenceEnabled(terminalId, checked);
}
void MainWindow::handleTerminalActivity(Terminal *terminal)
{
Session *session = qobject_cast<Session *>(sender());
if (session) {
disconnect(terminal, SIGNAL(activityDetected(Terminal *)), session, SIGNAL(activityDetected(Terminal *)));
QString message(xi18nc("@info", "Activity detected in monitored terminal in session \"%1\".", m_tabBar->tabTitle(session->id())));
KNotification *n = new KNotification(QLatin1String("activity"), KNotification::CloseWhenWindowActivated);
n->setWindow(terminal->partWidget()->window()->windowHandle());
n->setText(message);
n->sendEvent();
}
}
void MainWindow::handleTerminalSilence(Terminal *terminal)
{
Session *session = qobject_cast<Session *>(sender());
if (session) {
QString message(xi18nc("@info", "Silence detected in monitored terminal in session \"%1\".", m_tabBar->tabTitle(session->id())));
KNotification *n = new KNotification(QLatin1String("silence"), KNotification::CloseWhenWindowActivated);
n->setWindow(terminal->partWidget()->window()->windowHandle());
n->setText(message);
n->sendEvent();
}
}
void MainWindow::handleLastTabClosed()
{
if (isVisible() && !Settings::keepOpenAfterLastSessionCloses())
toggleWindowState();
}
void MainWindow::handleSwitchToAction()
{
QAction *action = qobject_cast<QAction *>(QObject::sender());
if (action && !action->data().isNull())
m_sessionStack->raiseSession(m_tabBar->sessionAtTab(action->data().toInt()));
}
void MainWindow::handleToggleTitlebar()
{
auto toggleFunc = [this]() {
bool showTitleBar = !Settings::showTitleBar();
m_titleBar->setVisible(showTitleBar);
Settings::setShowTitleBar(showTitleBar);
Settings::self()->save();
applyWindowGeometry();
};
if (Settings::showTitleBar()) { // If the title bar is hidden don't ask if toggling is ok
const char *message =
"You are about to hide the title bar. This will keep you "
"from accessing the settings menu via the mouse. To show "
"the title bar again press the keyboard shortcut (default "
"Ctrl+Shift+m) or access the settings menu via keyboard "
"shortcut (default: Ctrl+Shift+,).";
const int result = KMessageBox::warningContinueCancel(this,
xi18nc("@info", message),
xi18nc("@title:window", "Hiding Title Bar"),
KStandardGuiItem::cont(),
KStandardGuiItem::cancel(),
QStringLiteral("hinding_title_bar"));
if (result == KMessageBox::ButtonCode::Continue) {
toggleFunc();
}
} else {
toggleFunc();
}
}
void MainWindow::setupMenu()
{
m_menu->insertSection(nullptr, xi18nc("@title:menu", "Help"));
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::WhatsThis)));
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::ReportBug)));
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::AboutApp)));
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::AboutKDE)));
m_menu->insertSection(nullptr, xi18nc("@title:menu", "Quick Options"));
m_menu->addAction(actionCollection()->action(QStringLiteral("view-full-screen")));
m_menu->addAction(actionCollection()->action(QStringLiteral("keep-open")));
m_screenMenu = new QMenu(this);
connect(m_screenMenu, SIGNAL(triggered(QAction *)), this, SLOT(setScreen(QAction *)));
m_screenMenu->setTitle(xi18nc("@title:menu", "Screen"));
m_menu->addMenu(m_screenMenu);
m_windowWidthMenu = new QMenu(this);
connect(m_windowWidthMenu, SIGNAL(triggered(QAction *)), this, SLOT(setWindowWidth(QAction *)));
m_windowWidthMenu->setTitle(xi18nc("@title:menu", "Width"));
m_menu->addMenu(m_windowWidthMenu);
m_windowHeightMenu = new QMenu(this);
connect(m_windowHeightMenu, SIGNAL(triggered(QAction *)), this, SLOT(setWindowHeight(QAction *)));
m_windowHeightMenu->setTitle(xi18nc("@title:menu", "Height"));
m_menu->addMenu(m_windowHeightMenu);
m_menu->insertSection(nullptr, xi18nc("@title:menu", "Settings"));
m_menu->addAction(actionCollection()->action(QStringLiteral("manage-profiles")));
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::KeyBindings)));
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::ConfigureNotifications)));
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::Preferences)));
m_menu->addSeparator();
m_menu->addAction(actionCollection()->action(KStandardActions::name(KStandardActions::Quit)));
}
void MainWindow::updateScreenMenu()
{
QAction *action;
m_screenMenu->clear();
action = m_screenMenu->addAction(xi18nc("@item:inmenu", "At mouse location"));
action->setCheckable(true);
action->setData(0);
action->setChecked(Settings::screen() == 0);
for (int i = 1; i <= QGuiApplication::screens().count(); i++) {
action = m_screenMenu->addAction(xi18nc("@item:inmenu", "Screen %1", i));
action->setCheckable(true);
action->setData(i);
action->setChecked(i == Settings::screen());
}
action = m_screenMenu->menuAction();
action->setVisible(QGuiApplication::screens().count() > 1);
}
void MainWindow::updateWindowSizeMenus()
{
updateWindowWidthMenu();
updateWindowHeightMenu();
}
void MainWindow::updateWindowWidthMenu()
{
QAction *action = nullptr;
if (m_windowWidthMenu->isEmpty()) {
for (int i = 10; i <= 100; i += 10) {
action = m_windowWidthMenu->addAction(i18n("%1%", i));
action->setCheckable(true);
action->setData(i);
action->setChecked(i == Settings::width());
}
} else {
QListIterator<QAction *> i(m_windowWidthMenu->actions());
while (i.hasNext()) {
action = i.next();
action->setChecked(action->data().toInt() == Settings::width());
}
}
}
void MainWindow::updateWindowHeightMenu()
{
QAction *action = nullptr;
if (m_windowHeightMenu->isEmpty()) {
for (int i = 10; i <= 100; i += 10) {
action = m_windowHeightMenu->addAction(i18n("%1%", i));
action->setCheckable(true);
action->setData(i);
action->setChecked(i == Settings::height());
}
} else {
QListIterator<QAction *> i(m_windowHeightMenu->actions());
while (i.hasNext()) {
action = i.next();
action->setChecked(action->data().toInt() == Settings::height());
}
}
}
void MainWindow::configureKeys()
{
KShortcutsDialog dialog(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
dialog.addCollection(actionCollection());
const auto collections = m_sessionStack->getPartActionCollections();
if (collections.size() >= 1) {
dialog.addCollection(collections.at(0), QStringLiteral("Konsolepart"));
}
if (!dialog.configure()) {
return;
}
if (collections.size() >= 1) {
// We need to update all the other collections
// rootCollection is the collection which got updatet by the dialog
const auto rootCollection = collections.at(0);
// For all the other collections
for (auto i = 1; i < collections.size(); ++i) {
// Update all the action they share with rootCollection
const auto rootActions = rootCollection->actions();
for (const auto *action : rootActions) {
if (auto *destaction = collections.at(i)->action(action->objectName())) {
destaction->setShortcuts(action->shortcuts());
}
}
}
}
}
void MainWindow::configureNotifications()
{
KNotifyConfigWidget::configure(this);
}
void MainWindow::configureApp()
{
if (KConfigDialog::showDialog(QStringLiteral("settings")))
return;
KConfigDialog *settingsDialog = new KConfigDialog(this, QStringLiteral("settings"), Settings::self());
settingsDialog->setMinimumHeight(560);
settingsDialog->setFaceType(KPageDialog::List);
connect(settingsDialog, &KConfigDialog::settingsChanged, this, &MainWindow::applySettings);
WindowSettings *windowSettings = new WindowSettings(settingsDialog);
settingsDialog->addPage(windowSettings, xi18nc("@title Preferences page name", "Window"), QStringLiteral("preferences-system-windows-move"));
connect(windowSettings, SIGNAL(updateWindowGeometry(int, int, int)), this, SLOT(setWindowGeometry(int, int, int)));
QWidget *behaviorSettings = new QWidget(settingsDialog);
Ui::BehaviorSettings behaviorSettingsUi;
behaviorSettingsUi.setupUi(behaviorSettings);
settingsDialog->addPage(behaviorSettings, xi18nc("@title Preferences page name", "Behavior"), QStringLiteral("preferences-system-windows-actions"));
AppearanceSettings *appearanceSettings = new AppearanceSettings(settingsDialog);
settingsDialog->addPage(appearanceSettings, xi18nc("@title Preferences page name", "Appearance"), QStringLiteral("preferences-desktop-theme"));
connect(settingsDialog, &QDialog::rejected, appearanceSettings, &AppearanceSettings::resetSelection);
settingsDialog->button(QDialogButtonBox::Help)->hide();
settingsDialog->button(QDialogButtonBox::Cancel)->setFocus();
connect(settingsDialog, &QDialog::finished, [this]() {
m_toggleLock = true;
KWindowSystem::activateWindow(windowHandle());
if (KWindowSystem::isPlatformX11()) {
KX11Extras::forceActiveWindow(winId());
}
});
settingsDialog->show();
}
void MainWindow::applySettings()
{
if (Settings::dynamicTabTitles()) {
connect(m_sessionStack, SIGNAL(titleChanged(int, QString)), m_tabBar, SLOT(setTabTitleAutomated(int, QString)));
m_sessionStack->emitTitles();
} else {
disconnect(m_sessionStack, SIGNAL(titleChanged(int, QString)), m_tabBar, SLOT(setTabTitleAutomated(int, QString)));
}
m_animationTimer.setInterval(Settings::frames() ? 10 : 0);
m_tabBar->setVisible(Settings::showTabBar());
m_titleBar->setVisible(Settings::showTitleBar());
if (!Settings::showSystrayIcon() && m_notifierItem) {
delete m_notifierItem;
m_notifierItem = nullptr;
// Removing the notifier item deletes the menu
// add a new one
m_menu = new QMenu(this);
setupMenu();
m_titleBar->updateMenu();
} else if (Settings::showSystrayIcon() && !m_notifierItem) {
m_notifierItem = new KStatusNotifierItem(this);
m_notifierItem->setStandardActionsEnabled(false);
m_notifierItem->setIconByName(QStringLiteral("yakuake-symbolic"));
m_notifierItem->setStatus(KStatusNotifierItem::Active);
m_notifierItem->setContextMenu(m_menu);
// Prevent the default implementation of showing
// and instead run toggleWindowState
m_notifierItem->setAssociatedWindow(nullptr);
connect(m_notifierItem, &KStatusNotifierItem::activateRequested, this, &MainWindow::toggleWindowState);
updateTrayTooltip();
}
repaint(); // used to repaint skin borders if Settings::hideSkinBorders has been changed
setKeepOpen(Settings::keepOpen());
updateScreenMenu();
updateWindowSizeMenus();
updateUseTranslucency();
applySkin();
applyWindowGeometry();
applyWindowProperties();
}
void MainWindow::applySkin()
{
bool gotSkin = m_skin->load(Settings::skin(), Settings::skinInstalledWithKns());
if (!gotSkin) {
Settings::setSkin(QStringLiteral("default"));
gotSkin = m_skin->load(Settings::skin());
}
if (!gotSkin) {
KMessageBox::error(parentWidget(),
xi18nc("@info",
"<application>Yakuake</application> was unable to load a skin. It is likely that it was installed incorrectly.<nl/><nl/>"
"The application will now quit."),
xi18nc("@title:window", "Cannot Load Skin"));
QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
}
m_titleBar->applySkin();
m_tabBar->applySkin();
}
void MainWindow::applyWindowProperties()
{
if (m_isX11) {
if (Settings::keepOpen() && !Settings::keepAbove()) {
KX11Extras::clearState(winId(), NET::KeepAbove);
KX11Extras::setState(winId(), NET::SkipTaskbar | NET::SkipPager);
} else {
KX11Extras::setState(winId(), NET::KeepAbove | NET::SkipTaskbar | NET::SkipPager);
}
KX11Extras::setOnAllDesktops(winId(), Settings::showOnAllDesktops());
}
#if HAVE_KWAYLAND
if (m_isWayland && m_plasmaShellSurface) {
m_plasmaShellSurface->setSkipTaskbar(true);
m_plasmaShellSurface->setSkipSwitcher(true);
}
#endif
winId(); // make sure windowHandle() is created
KWindowEffects::enableBlurBehind(windowHandle(), m_sessionStack->wantsBlur());
}
void MainWindow::applyWindowGeometry()
{
int width, height;
QAction *action = actionCollection()->action(QStringLiteral("view-full-screen"));
if (action->isChecked()) {
width = 100;
height = 100;
} else {
width = Settings::width();
height = Settings::height();
}
setWindowGeometry(width, height, Settings::position());
}
void MainWindow::setWindowGeometry(int newWidth, int newHeight, int newPosition)
{
QRect workArea = getDesktopGeometry();
int maxHeight = workArea.height() * newHeight / 100;
int targetWidth = workArea.width() * newWidth / 100;
setGeometry(workArea.x() + workArea.width() * newPosition * (100 - newWidth) / 10000, workArea.y(), targetWidth, maxHeight);
#if HAVE_KWAYLAND
initWaylandSurface();
#endif
maxHeight -= m_titleBar->height();
m_titleBar->setGeometry(0, maxHeight, targetWidth, m_titleBar->height());
if (!isVisible())
m_titleBar->updateMask();
if (Settings::frames() > 0)
m_animationStepSize = maxHeight / Settings::frames();
else
m_animationStepSize = maxHeight;
auto borderWidth = Settings::hideSkinBorders() ? 0 : m_skin->borderWidth();
if (Settings::showTabBar()) {
if (m_skin->tabBarCompact()) {
m_tabBar->setGeometry(m_skin->tabBarLeft(), maxHeight, width() - m_skin->tabBarLeft() - m_skin->tabBarRight(), m_tabBar->height());
} else {
maxHeight -= m_tabBar->height();
m_tabBar->setGeometry(borderWidth, maxHeight - borderWidth, width() - 2 * borderWidth, m_tabBar->height());
}
}
m_sessionStack->setGeometry(borderWidth, 0, width() - 2 * borderWidth, maxHeight - borderWidth);
updateMask();
}
void MainWindow::setScreen(QAction *action)
{
Settings::setScreen(action->data().toInt());
Settings::self()->save();
applyWindowGeometry();
updateScreenMenu();
}
void MainWindow::setWindowWidth(int width)
{
Settings::setWidth(width);
Settings::self()->save();
applyWindowGeometry();
updateWindowWidthMenu();
}
void MainWindow::setWindowHeight(int height)
{
Settings::setHeight(height);
Settings::self()->save();
applyWindowGeometry();
updateWindowHeightMenu();
}
void MainWindow::setWindowWidth(QAction *action)
{
setWindowWidth(action->data().toInt());
}
void MainWindow::setWindowHeight(QAction *action)
{
setWindowHeight(action->data().toInt());
}
void MainWindow::increaseWindowWidth()
{
if (Settings::width() <= 90)
setWindowWidth(Settings::width() + 10);
}
void MainWindow::decreaseWindowWidth()
{
if (Settings::width() >= 20)
setWindowWidth(Settings::width() - 10);
}
void MainWindow::increaseWindowHeight()
{
if (Settings::height() <= 90)
setWindowHeight(Settings::height() + 10);
}
void MainWindow::decreaseWindowHeight()
{
if (Settings::height() >= 20)
setWindowHeight(Settings::height() - 10);
}
void MainWindow::updateMask()
{
QRegion region = m_titleBar->mask();
region.translate(0, m_titleBar->y());
region += QRegion(0, 0, width(), m_titleBar->y());
setMask(region);
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
if (useTranslucency()) {
painter.setOpacity(qreal(Settings::backgroundColorOpacity()) / 100);
painter.fillRect(rect(), Settings::backgroundColor());
painter.setOpacity(1.0);
} else
painter.fillRect(rect(), Settings::backgroundColor());
if (!Settings::hideSkinBorders()) {
const QRect leftBorder(0, 0, m_skin->borderWidth(), height() - m_titleBar->height());
painter.fillRect(leftBorder, m_skin->borderColor());
const QRect rightBorder(width() - m_skin->borderWidth(), 0, m_skin->borderWidth(), height() - m_titleBar->height());
painter.fillRect(rightBorder, m_skin->borderColor());
const QRect bottomBorder(0, height() - m_skin->borderWidth() - m_titleBar->height(), width(), m_skin->borderWidth());
painter.fillRect(bottomBorder, m_skin->borderColor());
}
KMainWindow::paintEvent(event);
}
void MainWindow::moveEvent(QMoveEvent *event)
{
const QList<QScreen *> screens = qApp->screens();
const QScreen *widgetScreen = QGuiApplication::screenAt(pos());
auto it = std::find(screens.begin(), screens.end(), widgetScreen);
const int currentScreenNumber = std::distance(screens.begin(), it);
if (Settings::screen() && currentScreenNumber != -1 && currentScreenNumber != getScreen()) {
Settings::setScreen(currentScreenNumber + 1);
updateScreenMenu();
applyWindowGeometry();
}
KMainWindow::moveEvent(event);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
KMainWindow::closeEvent(event);
if (event->isAccepted()) {
QApplication::quit();
}
}
void MainWindow::wmActiveWindowChanged()
{
if (m_toggleLock) {
m_toggleLock = false;
return;
}
const QWindow *focusWindow = QGuiApplication::focusWindow();
// Don't retract when opening one of our config windows
if (focusWindow && focusWindow->transientParent() == windowHandle()) {
return;
}
if (!Settings::keepOpen() && isVisible() && !isActiveWindow()) {
toggleWindowState();
}
}
void MainWindow::changeEvent(QEvent *event)
{
if (event->type() == QEvent::WindowStateChange && !m_isFullscreen) {
if (windowState().testFlag(Qt::WindowMaximized)) {
// Don't alter settings to new size so unmaximizing restores previous geometry.
setWindowGeometry(100, 100, Settings::position());
setWindowState(Qt::WindowMaximized);
} else {
setWindowGeometry(Settings::width(), Settings::height(), Settings::position());
}
}
KMainWindow::changeEvent(event);
}
bool MainWindow::focusNextPrevChild(bool)
{
return false;
}
void MainWindow::toggleWindowState()
{
if (m_isWayland) {
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.plasmashell"),
QStringLiteral("/StrutManager"),
QStringLiteral("org.kde.PlasmaShell.StrutManager"),
QStringLiteral("availableScreenRect"));
message.setArguments({QGuiApplication::screens().at(getScreen())->name()});
QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(message);
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [=, this]() {
QDBusPendingReply<QRect> reply = *watcher;
m_availableScreenRect = reply.isValid() ? reply.value() : QRect();
setWindowGeometry(Settings::width(), Settings::height(), Settings::position());
watcher->deleteLater();
});
_toggleWindowState();
} else {
_toggleWindowState();
}
}
void MainWindow::_toggleWindowState()
{
bool visible = isVisible();
if (visible && !isActiveWindow() && Settings::keepOpen()) {
// Window is open but doesn't have focus; it's set to stay open
// regardless of focus loss.
if (Settings::toggleToFocus()) {
// The open/retract action is set to focus the window when it's
// open but lacks focus. The following will cause it to receive
// focus, and in an environment with multiple virtual desktops
// will also cause the window manager to switch to the virtual
// desktop the window resides on.
KWindowSystem::activateWindow(windowHandle());
if (KWindowSystem::isPlatformX11()) {
KX11Extras::forceActiveWindow(winId());
}
return;
} else if (!Settings::showOnAllDesktops() && KWindowInfo(winId(), NET::WMDesktop).desktop() != KX11Extras::currentDesktop()) {
// The open/retract action isn't set to focus the window, but
// the window is currently on another virtual desktop (the option
// to show it on all of them is disabled), so closing it doesn't
// make sense and we're opting to show it instead to avoid
// requiring the user to invoke the action twice to get to see
// Yakuake. Just forcing focus would cause the window manager to
// switch to the virtual desktop the window currently resides on,
// so move the window to the current desktop before doing so.
if (KWindowSystem::isPlatformX11()) {
KX11Extras::setOnDesktop(winId(), KX11Extras::currentDesktop());
}
KWindowSystem::activateWindow(windowHandle());
if (KWindowSystem::isPlatformX11()) {
KX11Extras::forceActiveWindow(winId());
}
return;
}
}
#if HAVE_X11
if (!Settings::useWMAssist() && m_kwinAssistPropSet)
kwinAssistPropCleanup();
if (m_isX11 && Settings::useWMAssist() && KX11Extras::compositingActive())
kwinAssistToggleWindowState(visible);
else
#endif
if (!m_isWayland) {
xshapeToggleWindowState(visible);
} else {
if (visible) {
sharedPreHideWindow();
hide();
sharedAfterHideWindow();
} else {
sharedPreOpenWindow();
if (!m_isWayland) {
slideWindow();
}
show();
if (m_isWayland) {
slideWindow();
}
sharedAfterOpenWindow();
}
}
}
void MainWindow::slideWindow()
{
if (KWindowEffects::isEffectAvailable(KWindowEffects::Slide)) {
KWindowEffects::slideWindow(windowHandle(), KWindowEffects::TopEdge);
}
}
#if HAVE_X11
void MainWindow::kwinAssistToggleWindowState(bool visible)
{
bool gotEffect = false;
Display *display = QX11Info::display();
Atom atom = XInternAtom(display, "_KDE_SLIDE", false);
int count;
Atom *list = XListProperties(display, DefaultRootWindow(display), &count);
if (list != nullptr) {
gotEffect = (std::find(list, list + count, atom) != list + count);
XFree(list);
}
if (gotEffect) {
Atom atom = XInternAtom(display, "_KDE_SLIDE", false);
if (Settings::frames() > 0) {
QVarLengthArray<long, 1024> data(4);
data[0] = 0;
data[1] = 1;
data[2] = Settings::frames() * 10;
data[3] = Settings::frames() * 10;
XChangeProperty(display, winId(), atom, atom, 32, PropModeReplace, reinterpret_cast<unsigned char *>(data.data()), data.size());
m_kwinAssistPropSet = true;
} else
XDeleteProperty(display, winId(), atom);
if (visible) {
sharedPreHideWindow();
hide();
sharedAfterHideWindow();
} else {
sharedPreOpenWindow();
show();
sharedAfterOpenWindow();
}
return;
}
// Fall back to legacy animation strategy if kwin doesn't have the
// effect loaded.
xshapeToggleWindowState(visible);
}
void MainWindow::kwinAssistPropCleanup()
{
if (!QX11Info::isPlatformX11())
return;
Display *display = QX11Info::display();
Atom atom = XInternAtom(display, "_KDE_SLIDE", false);
XDeleteProperty(display, winId(), atom);
m_kwinAssistPropSet = false;
}
#endif
void MainWindow::xshapeToggleWindowState(bool visible)
{
if (m_animationTimer.isActive())
return;
if (visible) {
sharedPreHideWindow();
m_animationFrame = Settings::frames();
connect(&m_animationTimer, SIGNAL(timeout()), this, SLOT(xshapeRetractWindow()));
m_animationTimer.start();
} else {
m_animationFrame = 0;
connect(&m_animationTimer, SIGNAL(timeout()), this, SLOT(xshapeOpenWindow()));
m_animationTimer.start();
}
}
void MainWindow::xshapeOpenWindow()
{
if (m_animationFrame == 0) {
sharedPreOpenWindow();
show();
sharedAfterOpenWindow();
}
if (m_animationFrame == Settings::frames()) {
m_animationTimer.stop();
m_animationTimer.disconnect();
m_titleBar->move(0, height() - m_titleBar->height());
updateMask();
} else {
int maskHeight = m_animationStepSize * m_animationFrame;
QRegion newMask = m_titleBar->mask();
newMask.translate(0, maskHeight);
newMask += QRegion(0, 0, width(), maskHeight);
m_titleBar->move(0, maskHeight);
setMask(newMask);
m_animationFrame++;
}
}
void MainWindow::xshapeRetractWindow()
{
if (m_animationFrame == 0) {
m_animationTimer.stop();
m_animationTimer.disconnect();
hide();
sharedAfterHideWindow();
} else {
m_titleBar->move(0, m_titleBar->y() - m_animationStepSize);
setMask(QRegion(mask()).translated(0, -m_animationStepSize));
--m_animationFrame;
}
}
void MainWindow::sharedPreOpenWindow()
{
applyWindowGeometry();
updateUseTranslucency();
if (Settings::pollMouse())
toggleMousePoll(false);
if (Settings::rememberFullscreen())
setFullScreen(m_isFullscreen);
}
void MainWindow::sharedAfterOpenWindow()
{
if (!Settings::firstRun() && KWindowSystem::isPlatformX11()) {
KX11Extras::forceActiveWindow(winId());
}
connect(qGuiApp, &QGuiApplication::focusWindowChanged, this, &MainWindow::wmActiveWindowChanged);
applyWindowProperties();
#if HAVE_KWAYLAND
initWaylandSurface();
#endif
Q_EMIT windowOpened();
}
void MainWindow::sharedPreHideWindow()
{
disconnect(qGuiApp, &QGuiApplication::focusWindowChanged, this, &MainWindow::wmActiveWindowChanged);
}
void MainWindow::sharedAfterHideWindow()
{
if (Settings::pollMouse())
toggleMousePoll(true);
#if HAVE_KWAYLAND
delete m_plasmaShellSurface;
m_plasmaShellSurface = nullptr;
#endif
Q_EMIT windowClosed();
}
void MainWindow::activate()
{
KWindowSystem::activateWindow(windowHandle());
}
void MainWindow::toggleMousePoll(bool poll)
{
if (poll)
m_mousePoller.start(Settings::pollInterval());
else
m_mousePoller.stop();
}
void MainWindow::pollMouse()
{
QPoint pos = QCursor::pos();
QRect workArea = getDesktopGeometry();
int windowX = workArea.x() + workArea.width() * Settings::position() * (100 - Settings::width()) / 10000;
int windowWidth = workArea.width() * Settings::width() / 100;
if (pos.y() == 0 && pos.x() >= windowX && pos.x() <= (windowX + windowWidth))
toggleWindowState();
}
void MainWindow::setKeepOpen(bool keepOpen)
{
if (Settings::keepOpen() != keepOpen) {
Settings::setKeepOpen(keepOpen);
Settings::self()->save();
applyWindowProperties();
}
actionCollection()->action(QStringLiteral("keep-open"))->setChecked(keepOpen);
m_titleBar->setFocusButtonState(keepOpen);
}
void MainWindow::setFullScreen(bool state)
{
if (isVisible())
m_isFullscreen = state;
if (state) {
setWindowState(windowState() | Qt::WindowFullScreen);
setWindowGeometry(100, 100, Settings::position());
} else {
setWindowState(windowState() & ~Qt::WindowFullScreen);
setWindowGeometry(Settings::width(), Settings::height(), Settings::position());
}
}
int MainWindow::getScreen()
{
if (Settings::screen() <= 0 || Settings::screen() > QGuiApplication::screens().length()) {
auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
QStringLiteral("/KWin"),
QStringLiteral("org.kde.KWin"),
QStringLiteral("activeOutputName"));
QDBusReply<QString> reply = QDBusConnection::sessionBus().call(message);
if (reply.isValid()) {
const auto screens = QGuiApplication::screens();
for (int i = 0; i < screens.size(); ++i) {
if (screens[i]->name() == reply.value())
return i;
}
}
// Right after unplugging an external monitor and the Yakuake window was on
// that monitor, QGuiApplication::screenAt() can return nullptr so we fallback on
// the first monitor.
QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
return screen ? QGuiApplication::screens().indexOf(screen) : 0;
} else {
return Settings::screen() - 1;
}
}
QRect MainWindow::getScreenGeometry()
{
QScreen *screen = QGuiApplication::screens().at(getScreen());
QRect screenGeometry = screen->geometry();
screenGeometry.moveTo(screenGeometry.topLeft() / screen->devicePixelRatio());
return screenGeometry;
}
QRect MainWindow::getDesktopGeometry()
{
QRect screenGeometry = getScreenGeometry();
QAction *action = actionCollection()->action(QStringLiteral("view-full-screen"));
if (action->isChecked())
return screenGeometry;
if (m_isWayland) {
// on Wayland it's not possible to get the work area from KWindowSystem
// but plasmashell provides this through dbus
return m_availableScreenRect.isValid() ? m_availableScreenRect : screenGeometry;
}
if (QGuiApplication::screens().count() > 1) {
const QList<WId> allWindows = KX11Extras::windows();
QList<WId> offScreenWindows;
QListIterator<WId> i(allWindows);
while (i.hasNext()) {
WId windowId = i.next();
if (KX11Extras::hasWId(windowId)) {
KWindowInfo windowInfo = KWindowInfo(windowId, NET::WMDesktop | NET::WMGeometry, NET::WM2ExtendedStrut);
// If windowInfo is valid and the window is located at the same (current)
// desktop with the yakuake window...
if (windowInfo.valid() && windowInfo.isOnCurrentDesktop()) {
NETExtendedStrut strut = windowInfo.extendedStrut();
// Get the area covered by each strut.
QRect topStrut(strut.top_start, 0, strut.top_end - strut.top_start, strut.top_width);
QRect bottomStrut(strut.bottom_start,
screenGeometry.bottom() - strut.bottom_width,
strut.bottom_end - strut.bottom_start,
strut.bottom_width);
QRect leftStrut(0, strut.left_start, strut.left_width, strut.left_end - strut.left_start);
QRect rightStrut(screenGeometry.right() - strut.right_width, strut.right_start, strut.right_width, strut.right_end - strut.right_start);
// If the window has no strut, no need to bother further.
if (topStrut.isEmpty() && bottomStrut.isEmpty() && leftStrut.isEmpty() && rightStrut.isEmpty())
continue;
// If any of the strut and the window itself intersects with our screen geometry,
// it will be correctly handled by workArea(). If the window doesn't intersect
// with our screen geometry it's most likely a plasma panel and can/should be
// ignored
if ((topStrut.intersects(screenGeometry) || bottomStrut.intersects(screenGeometry) || leftStrut.intersects(screenGeometry)
|| rightStrut.intersects(screenGeometry))
&& windowInfo.geometry().intersects(screenGeometry)) {
continue;
}
// This window has a strut on the same desktop as us but which does not cover our screen
// geometry. It should be ignored, otherwise the returned work area will wrongly include
// the strut.
offScreenWindows << windowId;
}
}
}
return KX11Extras::workArea(offScreenWindows).intersected(screenGeometry);
}
#if HAVE_X11
return KX11Extras::workArea();
#else
return QRect();
#endif
}
void MainWindow::whatsThis()
{
QWhatsThis::enterWhatsThisMode();
}
void MainWindow::showFirstRunDialog()
{
if (!m_firstRunDialog) {
m_firstRunDialog = new FirstRunDialog(this);
connect(m_firstRunDialog, &QDialog::finished, this, &MainWindow::firstRunDialogFinished);
connect(m_firstRunDialog, &QDialog::accepted, this, &MainWindow::firstRunDialogOk);
}
m_firstRunDialog->show();
}
void MainWindow::firstRunDialogFinished()
{
Settings::setFirstRun(false);
Settings::self()->save();
m_firstRunDialog->deleteLater();
if (KWindowSystem::isPlatformX11()) {
KX11Extras::forceActiveWindow(winId());
}
}
void MainWindow::firstRunDialogOk()
{
QAction *action = static_cast<QAction *>(actionCollection()->action(QStringLiteral("toggle-window-state")));
KGlobalAccel::self()->setShortcut(action, QList<QKeySequence>() << m_firstRunDialog->keySequence(), KGlobalAccel::NoAutoloading);
actionCollection()->writeSettings();
}
void MainWindow::updateUseTranslucency()
{
m_useTranslucency = (Settings::translucency() && (m_isX11 ? KX11Extras::compositingActive() : true));
}
void MainWindow::updateTrayTooltip()
{
if (!m_notifierItem) {
return;
}
auto *action = actionCollection()->action(QStringLiteral("toggle-window-state"));
const QList<QKeySequence> &shortcuts = KGlobalAccel::self()->shortcut(action);
if (!shortcuts.isEmpty()) {
const QString shortcut(shortcuts.first().toString(QKeySequence::NativeText));
m_notifierItem->setToolTip(QStringLiteral("yakuake"), QStringLiteral("Yakuake"), xi18nc("@info", "Press <shortcut>%1</shortcut> to open", shortcut));
}
}
#include "moc_mainwindow.cpp"
|