Repository navigation
fix: prevent apps launched in terminal from changing its theme - #601
Conversation
1. Root cause: DTK stores the application theme in the shared key org.deepin.dtk.preference.themeType keyed by app id; processes started from the terminal inherit the terminal identity (DSG_APP_ID / cgroup), so their theme changes reach the terminal through themeTypeChanged and are written into the terminal config 2. Fix: set DGuiApplicationHelper::DontSaveApplicationTheme at startup so the terminal no longer reads, watches or writes the shared app theme key; persist the follow-system state in a local config option, migrate the old key once on upgrade, and record the choice at the theme menu commit point 3. Impact: the terminal keeps its own light/dark/follow-system theme and ignores theme changes from apps launched in it; those apps no longer inherit the current terminal theme, and the terminal no longer takes part in the shared application theme key Log: fix terminal theme being changed by apps launched from it Influence: 1. Launch dde-file-manager or ll-cli run org.deepin.editor from the terminal and change its theme, the terminal theme stays unchanged 2. Switch the system theme, only the follow-system mode of the terminal changes 3. Restart the terminal, the light/dark/follow-system selection is kept 4. Upgrade from an old version, the follow-system state is migrated once from the old application theme key fix: 修复终端主题被其启动的应用带跑的问题 1. 根因:DTK 的应用主题按应用身份存放在共享键 org.deepin.dtk.preference.themeType;终端里启动的进程继承终端身份 (DSG_APP_ID / cgroup),其主题修改经 themeTypeChanged 传到终端并 写入终端配置 2. 方案:启动时设置 DGuiApplicationHelper::DontSaveApplicationTheme, 终端不再读取、监听和写入共享应用主题键;跟随系统状态改为本地配置 持久化,升级时从旧键迁移一次,并在主题菜单提交点记录选择 3. 影响:终端保持自己的浅色/深色/跟随系统主题,不再被其启动的应用 影响;这些应用不再继承终端当前主题,终端也不再参与共享应用主题键 Log: 修复终端主题被其启动的应用带跑的问题 Influence: 1. 从终端启动 dde-file-manager 或 ll-cli run org.deepin.editor 并修改 其主题,终端主题不变 2. 切换系统主题,仅跟随系统模式下终端跟随变化 3. 重启终端,浅色/深色/跟随系统选择保持 4. 老版本升级,跟随系统状态从旧应用主题键迁移一次 PMS: BUG-365877
Reviewer's GuideSchema and migrationsAdd local configuration entries for persisting the follow-system selection and recording whether legacy theme state has been migrated.
APIExpose local theme-mode persistence, migration state, and migration-resolution helpers used by settings initialization and the theme menu.
Uses of the APIPersist the selected follow-system state when a theme-menu choice is committed, including after hover preview has already changed the palette type.
ImplementationStop DTK from sharing the terminal's application theme and replace shared-slot theme handling with local state, one-time legacy migration, and explicit palette application.
TestsCover theme-menu persistence after hover preview, legacy migration resolution, and local follow-system setting persistence.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 8 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/main/mainwindow.cpp" line_range="2959" />
<code_context>
+ // 只有点击提交才落盘“是否跟随系统”(hover 预览走 themeActionHoveredSlot)。
+ // 不能依赖 paletteTypeChanged 信号:悬停已把 paletteType 改成本次要选的类型时,
+ // 点击里的 setPaletteType 是空操作,不会发出信号,状态就会落回旧值。
+ Settings::instance()->setFollowSystemTheme(action == autoThemeAction);
}
</code_context>
<issue_to_address>
**Canceled dialog disables system following**
When the user is following the system and opens then cancels the custom-theme dialog, `themeActionTriggeredSlot()` calls `switchThemeAction()` to open the dialog, then stores `false` with `setFollowSystemTheme()` before a theme is applied. Canceling leaves the current palette unchanged, but `applyThemeMode()` restores a fixed theme on restart.
Only store `false` after a custom theme has actually been applied, and leave the follow-system setting unchanged when the dialog is canceled.
</issue_to_address>
### Comment 2
<location path="src/settings/settings.cpp" line_range="731" />
<code_context>
+{
+ auto helper = DGuiApplicationHelper::instance();
+ if (followSystemTheme() && extendColorScheme().isEmpty()) {
+ // 跟随系统模式:paletteType 保持 UnknownType,由系统主题驱动浅/深;
+ // colorScheme 可能因上次关闭后系统主题变化而过期,需同步为当前系统主题
+ if (helper->paletteType() != DGuiApplicationHelper::UnknownType) {
</code_context>
<issue_to_address>
**Explicit console color is replaced**
When a caller requests a fixed console color while follow-system mode is enabled, `setConsoleColorScheme()` changes the stored color and clears the extended scheme without clearing `follow_system`, so `applyThemeMode()` treats the request as follow-system mode and replaces the explicit color with the current system theme on restart.
Clear follow-system mode when `setConsoleColorScheme()` receives an explicit fixed color.
</issue_to_address>
### Comment 3
<location path="src/main/mainwindow.cpp" line_range="2959" />
<code_context>
+ // 只有点击提交才落盘“是否跟随系统”(hover 预览走 themeActionHoveredSlot)。
+ // 不能依赖 paletteTypeChanged 信号:悬停已把 paletteType 改成本次要选的类型时,
+ // 点击里的 setPaletteType 是空操作,不会发出信号,状态就会落回旧值。
+ Settings::instance()->setFollowSystemTheme(action == autoThemeAction);
}
</code_context>
<issue_to_address>
**Dynamic theme still follows system changes**
When a community dynamic theme is committed while follow-system mode is enabled, `switchThemeAction()` updates the extended scheme but leaves the palette type `UnknownType`; storing `false` with `setFollowSystemTheme()` does not change that palette type, so system theme changes still alter the terminal's appearance until restart.
Ensure committing a dynamic theme sets a palette type that no longer follows system theme changes.
</issue_to_address>
### Comment 4
<location path="src/settings/settings.cpp" line_range="190" />
<code_context>
setColorScheme("Dark");
setExtendColorScheme("");
+ setFollowSystemTheme(false);
+ setLegacyThemeModeMigrated(true);
qCDebug(tsettings) << "Color scheme initialized";
- } else if (DGuiApplicationHelper::instance()->paletteType() == DGuiApplicationHelper::UnknownType
</code_context>
<issue_to_address>
**Legacy follow mode is lost**
When an upgrade has an invalid or missing saved color scheme, or is built without `DTKCORE_CLASS_DConfigFile`, the migration is marked complete without preserving the old follow-system choice: the invalid-color-scheme branch forces dark and sets the migrated flag, while builds without DConfig compile out the legacy read and still mark migration complete. Upgraded users in either case lose their previous follow-system selection.
Only mark migration complete after the legacy state has been read or an explicit policy has chosen to discard it, and preserve the old follow state for upgrades that take the default-theme path.
Also at `src/settings/settings.cpp:708`.
</issue_to_address>
### Comment 5
<location path="src/settings/settings.cpp" line_range="708-724" />
<code_context>
#include <DGuiApplicationHelper>
#include <DKeySequenceEdit>
#include <DSysInfo>
+#ifdef DTKCORE_CLASS_DConfigFile
+#include <DConfig>
+#endif
</code_context>
<issue_to_address>
**Legacy follow mode is lost**
When the legacy DConfig group or `themeType` value is unavailable during the first upgraded launch, `migrateLegacyThemeMode()` converts an unreadable legacy value to `followSystem=false` and still sets `theme_migrated=true`, so the migration is never retried and an upgraded terminal that previously followed the system starts in a fixed theme.
Only mark migration complete after confirming the legacy setting was read, or preserve a retry path when the legacy value is unavailable.
</issue_to_address>
### Comment 6
<location path="src/settings/settings.cpp" line_range="708" />
<code_context>
#include <DGuiApplicationHelper>
#include <DKeySequenceEdit>
#include <DSysInfo>
+#ifdef DTKCORE_CLASS_DConfigFile
+#include <DConfig>
+#endif
</code_context>
<issue_to_address>
**Legacy follow mode is lost without DConfig**
When the terminal is upgraded on a build where `DTKCORE_CLASS_DConfigFile` is undefined and the prior theme mode was follow-system, `DTKCORE_CLASS_DConfigFile` being undefined skips the only read of the legacy theme key, leaving `legacy.valid` false; `resolveMigratedFollowSystem` then stores `false` and marks migration complete. Existing users on that build retain an explicit theme instead of their previous follow-system choice.
Provide a legacy-theme read for builds without DConfig, or avoid marking migration complete when the old value cannot be read.
</issue_to_address>
### Comment 7
<location path="src/main/main.cpp" line_range="73" />
<code_context>
+ // 避免从终端启动的应用修改主题时把终端一起带跑,也避免终端主题泄漏给子进程。
+ // 该属性只影响本进程对应用主题槽的读写,跟随系统主题仍由系统主题驱动。
+ // 必须在任何 DGuiApplicationHelper 主题接口调用之前设置。
+ DGuiApplicationHelper::setAttribute(DGuiApplicationHelper::DontSaveApplicationTheme, true);
qCDebug(mainprocess) << "Application starting with arguments:" << QCoreApplication::arguments();
if (checkImmutableMode())
</code_context>
<issue_to_address>
**Launched apps retain stale theme**
When an upgrade leaves a legacy `themeType` value for the terminal’s shared app identity, a legacy shared theme value remains after upgrade because the terminal only reads it during migration and never clears it; `DontSaveApplicationTheme` applies to the terminal process, not children sharing its app identity, so launched apps can still load the old terminal theme.
After migrating, remove or reset the legacy shared value if doing so is safe for other processes using that identity, or otherwise ensure launched apps cannot consume it.
Also at `src/settings/settings.cpp:712-715`.
</issue_to_address>
### Comment 8
<location path="tests/src/settings/ut_settings_test.cpp" line_range="270-273" />
<code_context>
+ const bool oldFollow = settings->followSystemTheme();
+ const bool oldMigrated = settings->legacyThemeModeMigrated();
+
+ settings->setFollowSystemTheme(true);
+ EXPECT_TRUE(settings->followSystemTheme());
+ settings->setFollowSystemTheme(false);
+ EXPECT_FALSE(settings->followSystemTheme());
+ settings->setLegacyThemeModeMigrated(true);
+ EXPECT_TRUE(settings->legacyThemeModeMigrated());
</code_context>
<issue_to_address>
**Persistence check misses lost settings**
When `setFollowSystemTheme` updates only the in-memory option and fails to persist it, `followSystemThemePersistence` immediately reads the same in-memory option after each setter, so a `setFollowSystemTheme` implementation that never commits to persistent storage still passes these assertions; callers then lose the selection after restart.
Reload settings or recreate the settings object before asserting the saved follow-system value.
</issue_to_address>| // 只有点击提交才落盘“是否跟随系统”(hover 预览走 themeActionHoveredSlot)。 | ||
| // 不能依赖 paletteTypeChanged 信号:悬停已把 paletteType 改成本次要选的类型时, | ||
| // 点击里的 setPaletteType 是空操作,不会发出信号,状态就会落回旧值。 | ||
| Settings::instance()->setFollowSystemTheme(action == autoThemeAction); |
There was a problem hiding this comment.
🟡 Medium · Canceled dialog disables system following
When the user is following the system and opens then cancels the custom-theme dialog, themeActionTriggeredSlot() calls switchThemeAction() to open the dialog, then stores false with setFollowSystemTheme() before a theme is applied. Canceling leaves the current palette unchanged, but applyThemeMode() restores a fixed theme on restart.
Only store false after a custom theme has actually been applied, and leave the follow-system setting unchanged when the dialog is canceled.
Prompt for AI agents
In `src/main/mainwindow.cpp` at line 2959:
**Canceled dialog disables system following**
When the user is following the system and opens then cancels the custom-theme dialog, `themeActionTriggeredSlot()` calls `switchThemeAction()` to open the dialog, then stores `false` with `setFollowSystemTheme()` before a theme is applied. Canceling leaves the current palette unchanged, but `applyThemeMode()` restores a fixed theme on restart.
Only store `false` after a custom theme has actually been applied, and leave the follow-system setting unchanged when the dialog is canceled.| { | ||
| auto helper = DGuiApplicationHelper::instance(); | ||
| if (followSystemTheme() && extendColorScheme().isEmpty()) { | ||
| // 跟随系统模式:paletteType 保持 UnknownType,由系统主题驱动浅/深; |
There was a problem hiding this comment.
🟡 Medium · Explicit console color is replaced
When a caller requests a fixed console color while follow-system mode is enabled, setConsoleColorScheme() changes the stored color and clears the extended scheme without clearing follow_system, so applyThemeMode() treats the request as follow-system mode and replaces the explicit color with the current system theme on restart.
Clear follow-system mode when setConsoleColorScheme() receives an explicit fixed color.
Prompt for AI agents
In `src/settings/settings.cpp` at line 731:
**Explicit console color is replaced**
When a caller requests a fixed console color while follow-system mode is enabled, `setConsoleColorScheme()` changes the stored color and clears the extended scheme without clearing `follow_system`, so `applyThemeMode()` treats the request as follow-system mode and replaces the explicit color with the current system theme on restart.
Clear follow-system mode when `setConsoleColorScheme()` receives an explicit fixed color.| // 只有点击提交才落盘“是否跟随系统”(hover 预览走 themeActionHoveredSlot)。 | ||
| // 不能依赖 paletteTypeChanged 信号:悬停已把 paletteType 改成本次要选的类型时, | ||
| // 点击里的 setPaletteType 是空操作,不会发出信号,状态就会落回旧值。 | ||
| Settings::instance()->setFollowSystemTheme(action == autoThemeAction); |
There was a problem hiding this comment.
🟠 High · Dynamic theme still follows system changes
When a community dynamic theme is committed while follow-system mode is enabled, switchThemeAction() updates the extended scheme but leaves the palette type UnknownType; storing false with setFollowSystemTheme() does not change that palette type, so system theme changes still alter the terminal's appearance until restart.
Ensure committing a dynamic theme sets a palette type that no longer follows system theme changes.
Prompt for AI agents
In `src/main/mainwindow.cpp` at line 2959:
**Dynamic theme still follows system changes**
When a community dynamic theme is committed while follow-system mode is enabled, `switchThemeAction()` updates the extended scheme but leaves the palette type `UnknownType`; storing `false` with `setFollowSystemTheme()` does not change that palette type, so system theme changes still alter the terminal's appearance until restart.
Ensure committing a dynamic theme sets a palette type that no longer follows system theme changes.| setColorScheme("Dark"); | ||
| setExtendColorScheme(""); | ||
| setFollowSystemTheme(false); | ||
| setLegacyThemeModeMigrated(true); |
There was a problem hiding this comment.
🟡 Medium · Legacy follow mode is lost
When an upgrade has an invalid or missing saved color scheme, or is built without DTKCORE_CLASS_DConfigFile, the migration is marked complete without preserving the old follow-system choice: the invalid-color-scheme branch forces dark and sets the migrated flag, while builds without DConfig compile out the legacy read and still mark migration complete. Upgraded users in either case lose their previous follow-system selection.
Only mark migration complete after the legacy state has been read or an explicit policy has chosen to discard it, and preserve the old follow state for upgrades that take the default-theme path.
Also at src/settings/settings.cpp:708.
Prompt for AI agents
In `src/settings/settings.cpp` at line 190:
**Legacy follow mode is lost**
When an upgrade has an invalid or missing saved color scheme, or is built without `DTKCORE_CLASS_DConfigFile`, the migration is marked complete without preserving the old follow-system choice: the invalid-color-scheme branch forces dark and sets the migrated flag, while builds without DConfig compile out the legacy read and still mark migration complete. Upgraded users in either case lose their previous follow-system selection.
Only mark migration complete after the legacy state has been read or an explicit policy has chosen to discard it, and preserve the old follow state for upgrades that take the default-theme path.
Also at `src/settings/settings.cpp:708`.| #ifdef DTKCORE_CLASS_DConfigFile | ||
| DConfig legacyConfig(QStringLiteral("org.deepin.dtk.preference")); | ||
| if (legacyConfig.isValid()) { | ||
| const QVariant legacyValue = legacyConfig.value(QStringLiteral("themeType")); | ||
| if (legacyValue.isValid()) { | ||
| legacy.valid = true; | ||
| legacy.themeType = legacyValue.toInt(); | ||
| } | ||
| } | ||
| #endif | ||
| const bool followSystem = resolveMigratedFollowSystem(legacy, extendColorScheme()); | ||
| qCInfo(tsettings) << "Migrating theme mode from legacy app theme slot, valid:" << legacy.valid | ||
| << ", legacyThemeType:" | ||
| << (legacy.valid ? QString::number(legacy.themeType) : QStringLiteral("unavailable")) | ||
| << ", followSystem:" << followSystem; | ||
| setFollowSystemTheme(followSystem); | ||
| setLegacyThemeModeMigrated(true); |
There was a problem hiding this comment.
🟡 Medium · Legacy follow mode is lost
When the legacy DConfig group or themeType value is unavailable during the first upgraded launch, migrateLegacyThemeMode() converts an unreadable legacy value to followSystem=false and still sets theme_migrated=true, so the migration is never retried and an upgraded terminal that previously followed the system starts in a fixed theme.
Only mark migration complete after confirming the legacy setting was read, or preserve a retry path when the legacy value is unavailable.
Prompt for AI agents
In `src/settings/settings.cpp` at lines 708-724:
**Legacy follow mode is lost**
When the legacy DConfig group or `themeType` value is unavailable during the first upgraded launch, `migrateLegacyThemeMode()` converts an unreadable legacy value to `followSystem=false` and still sets `theme_migrated=true`, so the migration is never retried and an upgraded terminal that previously followed the system starts in a fixed theme.
Only mark migration complete after confirming the legacy setting was read, or preserve a retry path when the legacy value is unavailable.| { | ||
| // 老版本升级:跟随系统的状态只存在于旧的应用主题槽里,读取一次后不再使用 | ||
| LegacyThemeState legacy{false, -1}; | ||
| #ifdef DTKCORE_CLASS_DConfigFile |
There was a problem hiding this comment.
🟡 Medium · Legacy follow mode is lost without DConfig
When the terminal is upgraded on a build where DTKCORE_CLASS_DConfigFile is undefined and the prior theme mode was follow-system, DTKCORE_CLASS_DConfigFile being undefined skips the only read of the legacy theme key, leaving legacy.valid false; resolveMigratedFollowSystem then stores false and marks migration complete. Existing users on that build retain an explicit theme instead of their previous follow-system choice.
Provide a legacy-theme read for builds without DConfig, or avoid marking migration complete when the old value cannot be read.
Prompt for AI agents
In `src/settings/settings.cpp` at line 708:
**Legacy follow mode is lost without DConfig**
When the terminal is upgraded on a build where `DTKCORE_CLASS_DConfigFile` is undefined and the prior theme mode was follow-system, `DTKCORE_CLASS_DConfigFile` being undefined skips the only read of the legacy theme key, leaving `legacy.valid` false; `resolveMigratedFollowSystem` then stores `false` and marks migration complete. Existing users on that build retain an explicit theme instead of their previous follow-system choice.
Provide a legacy-theme read for builds without DConfig, or avoid marking migration complete when the old value cannot be read.| // 避免从终端启动的应用修改主题时把终端一起带跑,也避免终端主题泄漏给子进程。 | ||
| // 该属性只影响本进程对应用主题槽的读写,跟随系统主题仍由系统主题驱动。 | ||
| // 必须在任何 DGuiApplicationHelper 主题接口调用之前设置。 | ||
| DGuiApplicationHelper::setAttribute(DGuiApplicationHelper::DontSaveApplicationTheme, true); |
There was a problem hiding this comment.
🟡 Medium · Launched apps retain stale theme
When an upgrade leaves a legacy themeType value for the terminal’s shared app identity, a legacy shared theme value remains after upgrade because the terminal only reads it during migration and never clears it; DontSaveApplicationTheme applies to the terminal process, not children sharing its app identity, so launched apps can still load the old terminal theme.
After migrating, remove or reset the legacy shared value if doing so is safe for other processes using that identity, or otherwise ensure launched apps cannot consume it.
Also at src/settings/settings.cpp:712-715.
Prompt for AI agents
In `src/main/main.cpp` at line 73:
**Launched apps retain stale theme**
When an upgrade leaves a legacy `themeType` value for the terminal’s shared app identity, a legacy shared theme value remains after upgrade because the terminal only reads it during migration and never clears it; `DontSaveApplicationTheme` applies to the terminal process, not children sharing its app identity, so launched apps can still load the old terminal theme.
After migrating, remove or reset the legacy shared value if doing so is safe for other processes using that identity, or otherwise ensure launched apps cannot consume it.
Also at `src/settings/settings.cpp:712-715`.| settings->setFollowSystemTheme(true); | ||
| EXPECT_TRUE(settings->followSystemTheme()); | ||
| settings->setFollowSystemTheme(false); | ||
| EXPECT_FALSE(settings->followSystemTheme()); |
There was a problem hiding this comment.
🟡 Medium · Persistence check misses lost settings
When setFollowSystemTheme updates only the in-memory option and fails to persist it, followSystemThemePersistence immediately reads the same in-memory option after each setter, so a setFollowSystemTheme implementation that never commits to persistent storage still passes these assertions; callers then lose the selection after restart.
Reload settings or recreate the settings object before asserting the saved follow-system value.
Prompt for AI agents
In `tests/src/settings/ut_settings_test.cpp` at lines 270-273:
**Persistence check misses lost settings**
When `setFollowSystemTheme` updates only the in-memory option and fails to persist it, `followSystemThemePersistence` immediately reads the same in-memory option after each setter, so a `setFollowSystemTheme` implementation that never commits to persistent storage still passes these assertions; callers then lose the selection after restart.
Reload settings or recreate the settings object before asserting the saved follow-system value.|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: Kakueeen, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
Root cause
DTK stores the application theme in the shared key
org.deepin.dtk.preference.themeTypekeyed by app id. Processes started from the terminal inherit the terminal identity (DSG_APP_ID/ cgroup), so their theme changes reach the terminal throughthemeTypeChangedand are written into the terminal config.Fix
DGuiApplicationHelper::DontSaveApplicationThemeat startup, so the terminal no longer reads, watches or writes the shared app theme key.basic.interface.follow_system,basic.interface.theme_migrated), migrate the old key once on upgrade, and record the choice at the theme menu commit point.migrateLegacyThemeMode()/applyThemeMode()inSettingsto keep the init logic readable.Impact
Test plan
dde-file-managerorll-cli run org.deepin.editorfrom the terminal and change its theme — the terminal theme stays unchanged.PMS: https://pms.uniontech.com/bug-view-365877.html
Summary by Sourcery
Isolate the terminal’s theme management from the shared application theme state while preserving existing user preferences.
Bug Fixes:
Enhancements:
Tests: