Extract theme_selector
into its own crate
This commit is contained in:
parent
47b29a5f21
commit
2280c75103
6 changed files with 63 additions and 22 deletions
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -5326,6 +5326,21 @@ dependencies = [
|
||||||
"toml 0.5.8",
|
"toml 0.5.8",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "theme_selector"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"editor",
|
||||||
|
"fuzzy",
|
||||||
|
"gpui",
|
||||||
|
"log",
|
||||||
|
"parking_lot",
|
||||||
|
"postage",
|
||||||
|
"smol",
|
||||||
|
"theme",
|
||||||
|
"workspace",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.29"
|
version = "1.0.29"
|
||||||
|
@ -6183,6 +6198,7 @@ dependencies = [
|
||||||
"surf",
|
"surf",
|
||||||
"tempdir",
|
"tempdir",
|
||||||
"theme",
|
"theme",
|
||||||
|
"theme_selector",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"time 0.3.2",
|
"time 0.3.2",
|
||||||
"tiny_http",
|
"tiny_http",
|
||||||
|
|
15
crates/theme_selector/Cargo.toml
Normal file
15
crates/theme_selector/Cargo.toml
Normal file
|
@ -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"
|
|
@ -1,4 +1,3 @@
|
||||||
use crate::{workspace::Workspace, AppState, Settings};
|
|
||||||
use editor::{Editor, EditorSettings};
|
use editor::{Editor, EditorSettings};
|
||||||
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
|
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
|
@ -12,11 +11,19 @@ use parking_lot::Mutex;
|
||||||
use postage::watch;
|
use postage::watch;
|
||||||
use std::{cmp, sync::Arc};
|
use std::{cmp, sync::Arc};
|
||||||
use theme::ThemeRegistry;
|
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 {
|
pub struct ThemeSelector {
|
||||||
settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
|
settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
|
||||||
settings: watch::Receiver<Settings>,
|
settings: watch::Receiver<Settings>,
|
||||||
registry: Arc<ThemeRegistry>,
|
themes: Arc<ThemeRegistry>,
|
||||||
matches: Vec<StringMatch>,
|
matches: Vec<StringMatch>,
|
||||||
query_editor: ViewHandle<Editor>,
|
query_editor: ViewHandle<Editor>,
|
||||||
list_state: UniformListState,
|
list_state: UniformListState,
|
||||||
|
@ -24,10 +31,10 @@ pub struct ThemeSelector {
|
||||||
}
|
}
|
||||||
|
|
||||||
action!(Confirm);
|
action!(Confirm);
|
||||||
action!(Toggle, Arc<AppState>);
|
action!(Toggle, ThemeSelectorParams);
|
||||||
action!(Reload, Arc<AppState>);
|
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::confirm);
|
||||||
cx.add_action(ThemeSelector::select_prev);
|
cx.add_action(ThemeSelector::select_prev);
|
||||||
cx.add_action(ThemeSelector::select_next);
|
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_action(ThemeSelector::reload);
|
||||||
|
|
||||||
cx.add_bindings(vec![
|
cx.add_bindings(vec![
|
||||||
Binding::new("cmd-k cmd-t", Toggle(app_state.clone()), None),
|
Binding::new("cmd-k cmd-t", Toggle(params.clone()), None),
|
||||||
Binding::new("cmd-k t", Reload(app_state.clone()), None),
|
Binding::new("cmd-k t", Reload(params.clone()), None),
|
||||||
Binding::new("escape", Toggle(app_state.clone()), Some("ThemeSelector")),
|
Binding::new("escape", Toggle(params.clone()), Some("ThemeSelector")),
|
||||||
Binding::new("enter", Confirm, Some("ThemeSelector")),
|
Binding::new("enter", Confirm, Some("ThemeSelector")),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -75,7 +82,7 @@ impl ThemeSelector {
|
||||||
let mut this = Self {
|
let mut this = Self {
|
||||||
settings,
|
settings,
|
||||||
settings_tx,
|
settings_tx,
|
||||||
registry,
|
themes: registry,
|
||||||
query_editor,
|
query_editor,
|
||||||
matches: Vec::new(),
|
matches: Vec::new(),
|
||||||
list_state: Default::default(),
|
list_state: Default::default(),
|
||||||
|
@ -117,7 +124,7 @@ impl ThemeSelector {
|
||||||
|
|
||||||
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
||||||
if let Some(mat) = self.matches.get(self.selected_index) {
|
if let Some(mat) = self.matches.get(self.selected_index) {
|
||||||
match self.registry.get(&mat.string) {
|
match self.themes.get(&mat.string) {
|
||||||
Ok(theme) => {
|
Ok(theme) => {
|
||||||
self.settings_tx.lock().borrow_mut().theme = theme;
|
self.settings_tx.lock().borrow_mut().theme = theme;
|
||||||
cx.refresh_windows();
|
cx.refresh_windows();
|
||||||
|
@ -144,15 +151,10 @@ impl ThemeSelector {
|
||||||
cx.notify();
|
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>) {
|
fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
let background = cx.background().clone();
|
let background = cx.background().clone();
|
||||||
let candidates = self
|
let candidates = self
|
||||||
.registry
|
.themes
|
||||||
.list()
|
.list()
|
||||||
.map(|name| StringMatchCandidate {
|
.map(|name| StringMatchCandidate {
|
||||||
char_bag: name.as_str().into(),
|
char_bag: name.as_str().into(),
|
|
@ -39,6 +39,7 @@ project_panel = { path = "../project_panel" }
|
||||||
rpc = { path = "../rpc" }
|
rpc = { path = "../rpc" }
|
||||||
sum_tree = { path = "../sum_tree" }
|
sum_tree = { path = "../sum_tree" }
|
||||||
theme = { path = "../theme" }
|
theme = { path = "../theme" }
|
||||||
|
theme_selector = { path = "../theme_selector" }
|
||||||
util = { path = "../util" }
|
util = { path = "../util" }
|
||||||
workspace = { path = "../workspace" }
|
workspace = { path = "../workspace" }
|
||||||
anyhow = "1.0.38"
|
anyhow = "1.0.38"
|
||||||
|
|
|
@ -3,7 +3,6 @@ pub mod language;
|
||||||
pub mod menus;
|
pub mod menus;
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
pub mod test;
|
pub mod test;
|
||||||
pub mod theme_selector;
|
|
||||||
|
|
||||||
pub use buffer;
|
pub use buffer;
|
||||||
use buffer::LanguageRegistry;
|
use buffer::LanguageRegistry;
|
||||||
|
@ -25,6 +24,7 @@ pub use project::{self, fs};
|
||||||
use project_panel::ProjectPanel;
|
use project_panel::ProjectPanel;
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{path::PathBuf, sync::Arc};
|
||||||
use theme::ThemeRegistry;
|
use theme::ThemeRegistry;
|
||||||
|
use theme_selector::ThemeSelectorParams;
|
||||||
pub use workspace;
|
pub use workspace;
|
||||||
use workspace::{Settings, Workspace, WorkspaceParams};
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -10,10 +10,7 @@ use simplelog::SimpleLogger;
|
||||||
use std::{fs, path::PathBuf, sync::Arc};
|
use std::{fs, path::PathBuf, sync::Arc};
|
||||||
use theme::ThemeRegistry;
|
use theme::ThemeRegistry;
|
||||||
use workspace::{self, settings, OpenNew};
|
use workspace::{self, settings, OpenNew};
|
||||||
use zed::{
|
use zed::{self, assets::Assets, fs::RealFs, language, menus, AppState, OpenParams, OpenPaths};
|
||||||
self, assets::Assets, fs::RealFs, language, menus, theme_selector, AppState, OpenParams,
|
|
||||||
OpenPaths,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
init_logger();
|
init_logger();
|
||||||
|
@ -56,7 +53,7 @@ fn main() {
|
||||||
people_panel::init(cx);
|
people_panel::init(cx);
|
||||||
chat_panel::init(cx);
|
chat_panel::init(cx);
|
||||||
project_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()));
|
cx.set_menus(menus::menus(&app_state.clone()));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue