OBSBasic_SysTray.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. extern bool opt_minimize_tray;
  18. void OBSBasic::SystemTrayInit()
  19. {
  20. #ifdef __APPLE__
  21. QIcon trayIconFile = QIcon(":/res/images/obs_macos.svg");
  22. trayIconFile.setIsMask(true);
  23. #else
  24. QIcon trayIconFile = QIcon(":/res/images/obs.png");
  25. #endif
  26. trayIcon.reset(new QSystemTrayIcon(QIcon::fromTheme("obs-tray", trayIconFile), this));
  27. trayIcon->setToolTip("OBS Studio");
  28. showHide = new QAction(QTStr("Basic.SystemTray.Show"), trayIcon.data());
  29. sysTrayStream =
  30. new QAction(StreamingActive() ? QTStr("Basic.Main.StopStreaming") : QTStr("Basic.Main.StartStreaming"),
  31. trayIcon.data());
  32. sysTrayRecord =
  33. new QAction(RecordingActive() ? QTStr("Basic.Main.StopRecording") : QTStr("Basic.Main.StartRecording"),
  34. trayIcon.data());
  35. sysTrayReplayBuffer = new QAction(ReplayBufferActive() ? QTStr("Basic.Main.StopReplayBuffer")
  36. : QTStr("Basic.Main.StartReplayBuffer"),
  37. trayIcon.data());
  38. sysTrayVirtualCam = new QAction(VirtualCamActive() ? QTStr("Basic.Main.StopVirtualCam")
  39. : QTStr("Basic.Main.StartVirtualCam"),
  40. trayIcon.data());
  41. exit = new QAction(QTStr("Exit"), trayIcon.data());
  42. trayMenu = new QMenu;
  43. previewProjector = new QMenu(QTStr("PreviewProjector"));
  44. studioProgramProjector = new QMenu(QTStr("StudioProgramProjector"));
  45. AddProjectorMenuMonitors(previewProjector, this, &OBSBasic::OpenPreviewProjector);
  46. AddProjectorMenuMonitors(studioProgramProjector, this, &OBSBasic::OpenStudioProgramProjector);
  47. trayMenu->addAction(showHide);
  48. trayMenu->addSeparator();
  49. trayMenu->addMenu(previewProjector);
  50. trayMenu->addMenu(studioProgramProjector);
  51. trayMenu->addSeparator();
  52. trayMenu->addAction(sysTrayStream);
  53. trayMenu->addAction(sysTrayRecord);
  54. trayMenu->addAction(sysTrayReplayBuffer);
  55. trayMenu->addAction(sysTrayVirtualCam);
  56. trayMenu->addSeparator();
  57. trayMenu->addAction(exit);
  58. trayIcon->setContextMenu(trayMenu);
  59. trayIcon->show();
  60. if (outputHandler && !outputHandler->replayBuffer)
  61. sysTrayReplayBuffer->setEnabled(false);
  62. sysTrayVirtualCam->setEnabled(vcamEnabled);
  63. if (Active())
  64. OnActivate(true);
  65. connect(trayIcon.data(), &QSystemTrayIcon::activated, this, &OBSBasic::IconActivated);
  66. connect(showHide, &QAction::triggered, this, &OBSBasic::ToggleShowHide);
  67. connect(sysTrayStream, &QAction::triggered, this, &OBSBasic::StreamActionTriggered);
  68. connect(sysTrayRecord, &QAction::triggered, this, &OBSBasic::RecordActionTriggered);
  69. connect(sysTrayReplayBuffer.data(), &QAction::triggered, this, &OBSBasic::ReplayBufferActionTriggered);
  70. connect(sysTrayVirtualCam.data(), &QAction::triggered, this, &OBSBasic::VirtualCamActionTriggered);
  71. connect(exit, &QAction::triggered, this, &OBSBasic::close);
  72. }
  73. void OBSBasic::IconActivated(QSystemTrayIcon::ActivationReason reason)
  74. {
  75. // Refresh projector list
  76. previewProjector->clear();
  77. studioProgramProjector->clear();
  78. AddProjectorMenuMonitors(previewProjector, this, &OBSBasic::OpenPreviewProjector);
  79. AddProjectorMenuMonitors(studioProgramProjector, this, &OBSBasic::OpenStudioProgramProjector);
  80. #ifdef __APPLE__
  81. UNUSED_PARAMETER(reason);
  82. #else
  83. if (reason == QSystemTrayIcon::Trigger) {
  84. EnablePreviewDisplay(previewEnabled && !isVisible());
  85. ToggleShowHide();
  86. }
  87. #endif
  88. }
  89. void OBSBasic::SysTrayNotify(const QString &text, QSystemTrayIcon::MessageIcon n)
  90. {
  91. if (trayIcon && trayIcon->isVisible() && QSystemTrayIcon::supportsMessages()) {
  92. QSystemTrayIcon::MessageIcon icon = QSystemTrayIcon::MessageIcon(n);
  93. trayIcon->showMessage("OBS Studio", text, icon, 10000);
  94. }
  95. }
  96. void OBSBasic::SystemTray(bool firstStarted)
  97. {
  98. if (!QSystemTrayIcon::isSystemTrayAvailable())
  99. return;
  100. if (!trayIcon && !firstStarted)
  101. return;
  102. bool sysTrayWhenStarted = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SysTrayWhenStarted");
  103. bool sysTrayEnabled = config_get_bool(App()->GetUserConfig(), "BasicWindow", "SysTrayEnabled");
  104. if (firstStarted)
  105. SystemTrayInit();
  106. if (!sysTrayEnabled) {
  107. trayIcon->hide();
  108. } else {
  109. trayIcon->show();
  110. if (firstStarted && (sysTrayWhenStarted || opt_minimize_tray)) {
  111. EnablePreviewDisplay(false);
  112. #ifdef __APPLE__
  113. EnableOSXDockIcon(false);
  114. #endif
  115. opt_minimize_tray = false;
  116. }
  117. }
  118. if (isVisible())
  119. showHide->setText(QTStr("Basic.SystemTray.Hide"));
  120. else
  121. showHide->setText(QTStr("Basic.SystemTray.Show"));
  122. }
  123. bool OBSBasic::sysTrayMinimizeToTray()
  124. {
  125. return config_get_bool(App()->GetUserConfig(), "BasicWindow", "SysTrayMinimizeToTray");
  126. }