diff options
author | Joe | 2022-07-05 10:44:16 +0000 |
---|---|---|
committer | GitHub | 2022-07-05 10:44:16 +0000 |
commit | b26e7e2e8fc900b5637d9772ecb74874e8794ecc (patch) | |
tree | 774d9e0a13cf88082a418eaa1779e00e16a141b4 /helix-view/src | |
parent | 85411bed83615895f4138fa080c07c257631d7f7 (diff) |
Add live preview to theme picker (#1798)
* Add theme picker with live preview
* Add live theme preview to :theme command
* cargo fmt
* Fix clippy warnings
* Remove picker variant
* Remove unused import
* Cleanup
* Change current_theme to last_theme
* Fix accidental comment flash deletion
* Typo
* Remove theme cache
* Add some comments
* Refactor some theme handling
TIL flatmap on Option is called and_then
* Remove unnecessary renames
* Constrain last_theme theme preview lifecycle
* Switch to bitflag implementation
* Better handling of last_theme
* Sort theme names
* Better memory juggling
* Missed a branch
* Remove name from theme, switch bitand to &
* cargo fmt
* Update helix-view/src/editor.rs
* Switch boolean to enum
* Remove bitflag impl
* cargo fmt
* Remove un-needed type arg
* cargo fmt
Diffstat (limited to 'helix-view/src')
-rw-r--r-- | helix-view/src/editor.rs | 41 | ||||
-rw-r--r-- | helix-view/src/theme.rs | 8 |
2 files changed, 47 insertions, 2 deletions
diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 1ed27e99..a2943af9 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -464,7 +464,6 @@ pub struct Editor { pub registers: Registers, pub macro_recording: Option<(char, Vec<KeyEvent>)>, pub macro_replaying: Vec<char>, - pub theme: Theme, pub language_servers: helix_lsp::Registry, pub diagnostics: BTreeMap<lsp::Url, Vec<lsp::Diagnostic>>, @@ -476,6 +475,12 @@ pub struct Editor { pub syn_loader: Arc<syntax::Loader>, pub theme_loader: Arc<theme::Loader>, + /// last_theme is used for theme previews. We store the current theme here, + /// and if previewing is cancelled, we can return to it. + pub last_theme: Option<Theme>, + /// The currently applied editor theme. While previewing a theme, the previewed theme + /// is set here. + pub theme: Theme, pub status_msg: Option<(Cow<'static, str>, Severity)>, pub autoinfo: Option<Info>, @@ -500,6 +505,11 @@ pub enum ConfigEvent { Update(Box<Config>), } +enum ThemeAction { + Set, + Preview, +} + #[derive(Debug, Clone)] pub struct CompleteAction { pub trigger_offset: usize, @@ -544,6 +554,7 @@ impl Editor { breakpoints: HashMap::new(), syn_loader, theme_loader, + last_theme: None, registers: Registers::default(), clipboard_provider: get_clipboard_provider(), status_msg: None, @@ -613,7 +624,22 @@ impl Editor { .unwrap_or(false) } + pub fn unset_theme_preview(&mut self) { + if let Some(last_theme) = self.last_theme.take() { + self.set_theme(last_theme); + } + // None likely occurs when the user types ":theme" and then exits before previewing + } + + pub fn set_theme_preview(&mut self, theme: Theme) { + self.set_theme_impl(theme, ThemeAction::Preview); + } + pub fn set_theme(&mut self, theme: Theme) { + self.set_theme_impl(theme, ThemeAction::Set); + } + + fn set_theme_impl(&mut self, theme: Theme, preview: ThemeAction) { // `ui.selection` is the only scope required to be able to render a theme. if theme.find_scope_index("ui.selection").is_none() { self.set_error("Invalid theme: `ui.selection` required"); @@ -623,7 +649,18 @@ impl Editor { let scopes = theme.scopes(); self.syn_loader.set_scopes(scopes.to_vec()); - self.theme = theme; + match preview { + ThemeAction::Preview => { + let last_theme = std::mem::replace(&mut self.theme, theme); + // only insert on first preview: this will be the last theme the user has saved + self.last_theme.get_or_insert(last_theme); + } + ThemeAction::Set => { + self.last_theme = None; + self.theme = theme; + } + } + self._refresh(); } diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index 3f45aac6..fa5fa702 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -77,6 +77,14 @@ impl Loader { names } + pub fn default_theme(&self, true_color: bool) -> Theme { + if true_color { + self.default() + } else { + self.base16_default() + } + } + /// Returns the default theme pub fn default(&self) -> Theme { DEFAULT_THEME.clone() |