OBSBasicSettings_Appearance.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "OBSBasicSettings.hpp"
  2. #include <OBSApp.hpp>
  3. #include <utility/platform.hpp>
  4. #include <qt-wrappers.hpp>
  5. void OBSBasicSettings::InitAppearancePage()
  6. {
  7. savedTheme = App()->GetTheme();
  8. const QString currentBaseTheme = savedTheme->isBaseTheme ? savedTheme->id : savedTheme->parent;
  9. for (const OBSTheme &theme : App()->GetThemes()) {
  10. if (theme.isBaseTheme && (HighContrastEnabled() || theme.isVisible || theme.id == currentBaseTheme)) {
  11. ui->theme->addItem(theme.name, theme.id);
  12. }
  13. }
  14. int idx = ui->theme->findData(currentBaseTheme);
  15. if (idx != -1)
  16. ui->theme->setCurrentIndex(idx);
  17. ui->themeVariant->setPlaceholderText(QTStr("Basic.Settings.Appearance.General.NoVariant"));
  18. }
  19. void OBSBasicSettings::LoadThemeList(bool reload)
  20. {
  21. ProfileScope("OBSBasicSettings::LoadThemeList");
  22. const OBSTheme *currentTheme = App()->GetTheme();
  23. const QString currentBaseTheme = currentTheme->isBaseTheme ? currentTheme->id : currentTheme->parent;
  24. /* Nothing to do if current and last base theme were the same */
  25. const QString baseThemeId = ui->theme->currentData().toString();
  26. if (reload && baseThemeId == currentBaseTheme)
  27. return;
  28. ui->themeVariant->blockSignals(true);
  29. ui->themeVariant->clear();
  30. auto themes = App()->GetThemes();
  31. std::sort(themes.begin(), themes.end(), [](const OBSTheme &a, const OBSTheme &b) -> bool {
  32. return QString::compare(a.name, b.name, Qt::CaseInsensitive) < 0;
  33. });
  34. QString defaultVariant;
  35. const OBSTheme *baseTheme = App()->GetTheme(baseThemeId);
  36. for (const OBSTheme &theme : themes) {
  37. /* Skip non-visible themes */
  38. if (!theme.isVisible || theme.isHighContrast)
  39. continue;
  40. /* Skip non-child themes */
  41. if (theme.isBaseTheme || theme.parent != baseThemeId)
  42. continue;
  43. ui->themeVariant->addItem(theme.name, theme.id);
  44. if (baseTheme && theme.filename == baseTheme->filename)
  45. defaultVariant = theme.id;
  46. }
  47. int idx = ui->themeVariant->findData(currentTheme->id);
  48. if (idx != -1)
  49. ui->themeVariant->setCurrentIndex(idx);
  50. ui->themeVariant->setEnabled(ui->themeVariant->count() > 0);
  51. ui->themeVariant->blockSignals(false);
  52. /* If no variant is selected but variants are available set the first one. */
  53. if (idx == -1 && ui->themeVariant->count() > 0) {
  54. idx = ui->themeVariant->findData(defaultVariant);
  55. ui->themeVariant->setCurrentIndex(idx != -1 ? idx : 0);
  56. }
  57. }
  58. void OBSBasicSettings::LoadAppearanceSettings(bool reload)
  59. {
  60. LoadThemeList(reload);
  61. if (reload) {
  62. QString themeId = ui->theme->currentData().toString();
  63. if (ui->themeVariant->currentIndex() != -1)
  64. themeId = ui->themeVariant->currentData().toString();
  65. App()->SetTheme(themeId);
  66. }
  67. }
  68. void OBSBasicSettings::SaveAppearanceSettings()
  69. {
  70. config_t *config = App()->GetUserConfig();
  71. OBSTheme *currentTheme = App()->GetTheme();
  72. if (savedTheme != currentTheme) {
  73. config_set_string(config, "Appearance", "Theme", QT_TO_UTF8(currentTheme->id));
  74. }
  75. }
  76. void OBSBasicSettings::on_theme_activated(int)
  77. {
  78. LoadAppearanceSettings(true);
  79. }
  80. void OBSBasicSettings::on_themeVariant_activated(int)
  81. {
  82. LoadAppearanceSettings(true);
  83. }