Extract `theme_selector` into its own crate

Antonio Scandurra created

Change summary

Cargo.lock                       | 16 ++++++++++++++++
crates/theme_selector/Cargo.toml | 15 +++++++++++++++
crates/theme_selector/src/lib.rs | 34 ++++++++++++++++++----------------
crates/zed/Cargo.toml            |  1 +
crates/zed/src/lib.rs            | 12 +++++++++++-
crates/zed/src/main.rs           |  7 ++-----
6 files changed, 63 insertions(+), 22 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -5326,6 +5326,21 @@ dependencies = [
  "toml 0.5.8",
 ]
 
+[[package]]
+name = "theme_selector"
+version = "0.1.0"
+dependencies = [
+ "editor",
+ "fuzzy",
+ "gpui",
+ "log",
+ "parking_lot",
+ "postage",
+ "smol",
+ "theme",
+ "workspace",
+]
+
 [[package]]
 name = "thiserror"
 version = "1.0.29"
@@ -6183,6 +6198,7 @@ dependencies = [
  "surf",
  "tempdir",
  "theme",
+ "theme_selector",
  "thiserror",
  "time 0.3.2",
  "tiny_http",

crates/theme_selector/Cargo.toml 🔗

@@ -0,0 +1,15 @@
+[package]
+name = "theme_selector"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+editor = { path = "../editor" }
+fuzzy = { path = "../fuzzy" }
+gpui = { path = "../gpui" }
+theme = { path = "../theme" }
+workspace = { path = "../workspace" }
+log = "0.4"
+parking_lot = "0.11.1"
+postage = { version = "0.4.1", features = ["futures-traits"] }
+smol = "1.2.5"

crates/zed/src/theme_selector.rs → crates/theme_selector/src/lib.rs 🔗

@@ -1,4 +1,3 @@
-use crate::{workspace::Workspace, AppState, Settings};
 use editor::{Editor, EditorSettings};
 use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
 use gpui::{
@@ -12,11 +11,19 @@ use parking_lot::Mutex;
 use postage::watch;
 use std::{cmp, sync::Arc};
 use theme::ThemeRegistry;
+use workspace::{Settings, Workspace};
+
+#[derive(Clone)]
+pub struct ThemeSelectorParams {
+    pub settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
+    pub settings: watch::Receiver<Settings>,
+    pub themes: Arc<ThemeRegistry>,
+}
 
 pub struct ThemeSelector {
     settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
     settings: watch::Receiver<Settings>,
-    registry: Arc<ThemeRegistry>,
+    themes: Arc<ThemeRegistry>,
     matches: Vec<StringMatch>,
     query_editor: ViewHandle<Editor>,
     list_state: UniformListState,
@@ -24,10 +31,10 @@ pub struct ThemeSelector {
 }
 
 action!(Confirm);
-action!(Toggle, Arc<AppState>);
-action!(Reload, Arc<AppState>);
+action!(Toggle, ThemeSelectorParams);
+action!(Reload, ThemeSelectorParams);
 
-pub fn init(app_state: &Arc<AppState>, cx: &mut MutableAppContext) {
+pub fn init(params: ThemeSelectorParams, cx: &mut MutableAppContext) {
     cx.add_action(ThemeSelector::confirm);
     cx.add_action(ThemeSelector::select_prev);
     cx.add_action(ThemeSelector::select_next);
@@ -35,9 +42,9 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut MutableAppContext) {
     cx.add_action(ThemeSelector::reload);
 
     cx.add_bindings(vec![
-        Binding::new("cmd-k cmd-t", Toggle(app_state.clone()), None),
-        Binding::new("cmd-k t", Reload(app_state.clone()), None),
-        Binding::new("escape", Toggle(app_state.clone()), Some("ThemeSelector")),
+        Binding::new("cmd-k cmd-t", Toggle(params.clone()), None),
+        Binding::new("cmd-k t", Reload(params.clone()), None),
+        Binding::new("escape", Toggle(params.clone()), Some("ThemeSelector")),
         Binding::new("enter", Confirm, Some("ThemeSelector")),
     ]);
 }
@@ -75,7 +82,7 @@ impl ThemeSelector {
         let mut this = Self {
             settings,
             settings_tx,
-            registry,
+            themes: registry,
             query_editor,
             matches: Vec::new(),
             list_state: Default::default(),
@@ -117,7 +124,7 @@ impl ThemeSelector {
 
     fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
         if let Some(mat) = self.matches.get(self.selected_index) {
-            match self.registry.get(&mat.string) {
+            match self.themes.get(&mat.string) {
                 Ok(theme) => {
                     self.settings_tx.lock().borrow_mut().theme = theme;
                     cx.refresh_windows();
@@ -144,15 +151,10 @@ impl ThemeSelector {
         cx.notify();
     }
 
-    // fn select(&mut self, selected_index: &usize, cx: &mut ViewContext<Self>) {
-    //     self.selected_index = *selected_index;
-    //     self.confirm(&(), cx);
-    // }
-
     fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
         let background = cx.background().clone();
         let candidates = self
-            .registry
+            .themes
             .list()
             .map(|name| StringMatchCandidate {
                 char_bag: name.as_str().into(),

crates/zed/Cargo.toml 🔗

@@ -39,6 +39,7 @@ project_panel = { path = "../project_panel" }
 rpc = { path = "../rpc" }
 sum_tree = { path = "../sum_tree" }
 theme = { path = "../theme" }
+theme_selector = { path = "../theme_selector" }
 util = { path = "../util" }
 workspace = { path = "../workspace" }
 anyhow = "1.0.38"

crates/zed/src/lib.rs 🔗

@@ -3,7 +3,6 @@ pub mod language;
 pub mod menus;
 #[cfg(any(test, feature = "test-support"))]
 pub mod test;
-pub mod theme_selector;
 
 pub use buffer;
 use buffer::LanguageRegistry;
@@ -25,6 +24,7 @@ pub use project::{self, fs};
 use project_panel::ProjectPanel;
 use std::{path::PathBuf, sync::Arc};
 use theme::ThemeRegistry;
+use theme_selector::ThemeSelectorParams;
 pub use workspace;
 use workspace::{Settings, Workspace, WorkspaceParams};
 
@@ -188,6 +188,16 @@ impl<'a> From<&'a AppState> for WorkspaceParams {
     }
 }
 
+impl<'a> From<&'a AppState> for ThemeSelectorParams {
+    fn from(state: &'a AppState) -> Self {
+        Self {
+            settings_tx: state.settings_tx.clone(),
+            settings: state.settings.clone(),
+            themes: state.themes.clone(),
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;

crates/zed/src/main.rs 🔗

@@ -10,10 +10,7 @@ use simplelog::SimpleLogger;
 use std::{fs, path::PathBuf, sync::Arc};
 use theme::ThemeRegistry;
 use workspace::{self, settings, OpenNew};
-use zed::{
-    self, assets::Assets, fs::RealFs, language, menus, theme_selector, AppState, OpenParams,
-    OpenPaths,
-};
+use zed::{self, assets::Assets, fs::RealFs, language, menus, AppState, OpenParams, OpenPaths};
 
 fn main() {
     init_logger();
@@ -56,7 +53,7 @@ fn main() {
         people_panel::init(cx);
         chat_panel::init(cx);
         project_panel::init(cx);
-        theme_selector::init(&app_state, cx);
+        theme_selector::init(app_state.as_ref().into(), cx);
 
         cx.set_menus(menus::menus(&app_state.clone()));