# Allow dynamic \`set_theme\` based on \`Appearance\` \(\#42812\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/6216af9b5a19ef55b6192bf84b6ef3965c7b93c8)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 6216af9b5a19ef55b6192bf84b6ef3965c7b93c8

Requested commit: 6216af9b5a19ef55b6192bf84b6ef3965c7b93c8

Commit: 6216af9b5a19ef55b6192bf84b6ef3965c7b93c8

Tree: d4c9fd40fba4469839c9817c32237359ac47d351

Author: Connor Tsui

Committer: GitHub

## Message

```
Allow dynamic `set_theme` based on `Appearance` (#42812)

Tracking Issue (does not close):
https://github.com/zed-industries/zed/issues/35552

This is somewhat of a blocker for
https://github.com/zed-industries/zed/pull/40035 (but also the current
behavior doesn't really make sense).

The current behavior of `ThemeSelectorDelegate::set_theme` (the theme
selector menu) is to simply set the in-memory settings to `Static`,
regardless of if it is currently `Dynamic`. The reason this doesn't
matter now is that the `theme::set_theme` function that updates the
user's settings file _will_ make this check, so dynamic settings stay
dynamic in `settings.json`, but not in memory.

But this is also sort of strange, because `theme::set_theme` will set
the setting of whatever the old appearance was to the new theme name. In
other words, if I am currently on a light mode theme and I change my
theme to a dark mode theme using the theme selector, the `light` field
of `theme` in `settings.json` is set to a dark mode theme!

_I think this is because displaying the new theme in the theme selector
does not update the global context, so
`ThemeSettings::get_global(cx).theme.name(appearance).0` returns the
original theme appearance, not the new one._

---

This PR makes `ThemeSelectorDelegate::set_theme` keep the current
`ThemeSelection`, as well as changes the behavior of the
`theme::set_theme` call to always choose the correct setting to update.

One edge case that might be slightly strange now is that if the user has
specified the mode as `System`, this will now override that with the
appearance of the new theme. I think this is fine, as otherwise a user
might set a dark theme and nothing will change because the
`ThemeAppearanceMode` is set to `light` or `system` (where `system` is
also light).

I also have an `unreachable!` in there that I'm pretty sure is true but
I don't really know how to formally prove that...

Release Notes:

- N/A *or* Added/Fixed/Improved ...

---------

Signed-off-by: Connor Tsui <connor.tsui20@gmail.com>
```

## Parents

- [464c0be2b7e935c9a91353867563f2f54431d3c1](https://git.cafe/gitcafe/zed/commit/464c0be2b7e935c9a91353867563f2f54431d3c1?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/6216af9b5a19ef55b6192bf84b6ef3965c7b93c8?format=markdown)
