2021-10-05 00:14:21 +00:00
|
|
|
use editor::{Editor, EditorSettings};
|
2021-10-04 23:45:05 +00:00
|
|
|
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
|
2021-08-02 21:55:27 +00:00
|
|
|
use gpui::{
|
2021-08-23 03:02:48 +00:00
|
|
|
action,
|
2021-09-03 11:01:12 +00:00
|
|
|
elements::*,
|
2021-08-23 03:02:48 +00:00
|
|
|
keymap::{self, menu, Binding},
|
2021-08-03 20:36:58 +00:00
|
|
|
AppContext, Axis, Element, ElementBox, Entity, MutableAppContext, RenderContext, View,
|
2021-08-02 21:55:27 +00:00
|
|
|
ViewContext, ViewHandle,
|
|
|
|
};
|
2021-08-04 21:52:23 +00:00
|
|
|
use parking_lot::Mutex;
|
2021-08-02 21:55:27 +00:00
|
|
|
use postage::watch;
|
2021-10-04 23:45:05 +00:00
|
|
|
use std::{cmp, sync::Arc};
|
2021-10-05 09:14:30 +00:00
|
|
|
use theme::ThemeRegistry;
|
2021-10-05 13:38:25 +00:00
|
|
|
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>,
|
|
|
|
}
|
2021-08-02 21:55:27 +00:00
|
|
|
|
2021-08-03 02:07:48 +00:00
|
|
|
pub struct ThemeSelector {
|
2021-08-02 21:55:27 +00:00
|
|
|
settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
|
|
|
|
settings: watch::Receiver<Settings>,
|
2021-10-05 13:38:25 +00:00
|
|
|
themes: Arc<ThemeRegistry>,
|
2021-08-02 21:55:27 +00:00
|
|
|
matches: Vec<StringMatch>,
|
2021-09-02 18:20:30 +00:00
|
|
|
query_editor: ViewHandle<Editor>,
|
2021-08-02 21:55:27 +00:00
|
|
|
list_state: UniformListState,
|
|
|
|
selected_index: usize,
|
|
|
|
}
|
|
|
|
|
2021-08-23 03:02:48 +00:00
|
|
|
action!(Confirm);
|
2021-10-05 13:38:25 +00:00
|
|
|
action!(Toggle, ThemeSelectorParams);
|
|
|
|
action!(Reload, ThemeSelectorParams);
|
2021-08-23 03:02:48 +00:00
|
|
|
|
2021-10-05 13:38:25 +00:00
|
|
|
pub fn init(params: ThemeSelectorParams, cx: &mut MutableAppContext) {
|
2021-08-23 03:02:48 +00:00
|
|
|
cx.add_action(ThemeSelector::confirm);
|
|
|
|
cx.add_action(ThemeSelector::select_prev);
|
|
|
|
cx.add_action(ThemeSelector::select_next);
|
|
|
|
cx.add_action(ThemeSelector::toggle);
|
|
|
|
cx.add_action(ThemeSelector::reload);
|
2021-08-02 21:55:27 +00:00
|
|
|
|
|
|
|
cx.add_bindings(vec![
|
2021-10-05 13:38:25 +00:00
|
|
|
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")),
|
2021-08-23 03:02:48 +00:00
|
|
|
Binding::new("enter", Confirm, Some("ThemeSelector")),
|
2021-08-02 21:55:27 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Event {
|
|
|
|
Dismissed,
|
|
|
|
}
|
|
|
|
|
2021-08-03 02:07:48 +00:00
|
|
|
impl ThemeSelector {
|
2021-08-02 21:55:27 +00:00
|
|
|
fn new(
|
|
|
|
settings_tx: Arc<Mutex<watch::Sender<Settings>>>,
|
|
|
|
settings: watch::Receiver<Settings>,
|
|
|
|
registry: Arc<ThemeRegistry>,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) -> Self {
|
2021-09-02 18:20:30 +00:00
|
|
|
let query_editor = cx.add_view(|cx| {
|
2021-09-16 19:20:12 +00:00
|
|
|
Editor::single_line(
|
|
|
|
{
|
|
|
|
let settings = settings.clone();
|
2021-10-04 22:06:12 +00:00
|
|
|
move |_| {
|
|
|
|
let settings = settings.borrow();
|
|
|
|
EditorSettings {
|
|
|
|
tab_size: settings.tab_size,
|
|
|
|
style: settings.theme.selector.input_editor.as_editor(),
|
|
|
|
}
|
|
|
|
}
|
2021-09-16 19:20:12 +00:00
|
|
|
},
|
|
|
|
cx,
|
|
|
|
)
|
2021-09-02 18:20:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
cx.subscribe(&query_editor, Self::on_query_editor_event)
|
2021-08-23 22:54:24 +00:00
|
|
|
.detach();
|
2021-08-02 21:55:27 +00:00
|
|
|
|
|
|
|
let mut this = Self {
|
|
|
|
settings,
|
|
|
|
settings_tx,
|
2021-10-05 13:38:25 +00:00
|
|
|
themes: registry,
|
2021-09-02 18:20:30 +00:00
|
|
|
query_editor,
|
2021-08-02 21:55:27 +00:00
|
|
|
matches: Vec::new(),
|
|
|
|
list_state: Default::default(),
|
|
|
|
selected_index: 0,
|
|
|
|
};
|
|
|
|
this.update_matches(cx);
|
|
|
|
this
|
|
|
|
}
|
|
|
|
|
2021-08-23 03:02:48 +00:00
|
|
|
fn toggle(workspace: &mut Workspace, action: &Toggle, cx: &mut ViewContext<Workspace>) {
|
2021-08-02 21:55:27 +00:00
|
|
|
workspace.toggle_modal(cx, |cx, _| {
|
2021-08-03 02:07:48 +00:00
|
|
|
let selector = cx.add_view(|cx| {
|
2021-08-02 21:55:27 +00:00
|
|
|
Self::new(
|
2021-08-23 03:02:48 +00:00
|
|
|
action.0.settings_tx.clone(),
|
|
|
|
action.0.settings.clone(),
|
|
|
|
action.0.themes.clone(),
|
2021-08-02 21:55:27 +00:00
|
|
|
cx,
|
|
|
|
)
|
|
|
|
});
|
2021-08-23 22:54:24 +00:00
|
|
|
cx.subscribe(&selector, Self::on_event).detach();
|
2021-08-03 02:07:48 +00:00
|
|
|
selector
|
2021-08-02 21:55:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-23 03:02:48 +00:00
|
|
|
fn reload(_: &mut Workspace, action: &Reload, cx: &mut ViewContext<Workspace>) {
|
|
|
|
let current_theme_name = action.0.settings.borrow().theme.name.clone();
|
|
|
|
action.0.themes.clear();
|
|
|
|
match action.0.themes.get(¤t_theme_name) {
|
2021-08-04 21:52:23 +00:00
|
|
|
Ok(theme) => {
|
2021-08-26 13:00:00 +00:00
|
|
|
cx.refresh_windows();
|
2021-08-23 03:02:48 +00:00
|
|
|
action.0.settings_tx.lock().borrow_mut().theme = theme;
|
2021-08-25 22:22:14 +00:00
|
|
|
log::info!("reloaded theme {}", current_theme_name);
|
2021-08-04 21:52:23 +00:00
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
log::error!("failed to load theme {}: {:?}", current_theme_name, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 03:02:48 +00:00
|
|
|
fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
2021-08-02 21:55:27 +00:00
|
|
|
if let Some(mat) = self.matches.get(self.selected_index) {
|
2021-10-05 13:38:25 +00:00
|
|
|
match self.themes.get(&mat.string) {
|
2021-08-05 20:04:46 +00:00
|
|
|
Ok(theme) => {
|
|
|
|
self.settings_tx.lock().borrow_mut().theme = theme;
|
2021-08-26 13:00:00 +00:00
|
|
|
cx.refresh_windows();
|
2021-08-05 20:04:46 +00:00
|
|
|
cx.emit(Event::Dismissed);
|
|
|
|
}
|
|
|
|
Err(error) => log::error!("error loading theme {}: {}", mat.string, error),
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 03:02:48 +00:00
|
|
|
fn select_prev(&mut self, _: &menu::SelectPrev, cx: &mut ViewContext<Self>) {
|
2021-08-02 21:55:27 +00:00
|
|
|
if self.selected_index > 0 {
|
|
|
|
self.selected_index -= 1;
|
|
|
|
}
|
|
|
|
self.list_state.scroll_to(self.selected_index);
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
|
2021-08-23 03:02:48 +00:00
|
|
|
fn select_next(&mut self, _: &menu::SelectNext, cx: &mut ViewContext<Self>) {
|
2021-08-02 21:55:27 +00:00
|
|
|
if self.selected_index + 1 < self.matches.len() {
|
|
|
|
self.selected_index += 1;
|
|
|
|
}
|
|
|
|
self.list_state.scroll_to(self.selected_index);
|
|
|
|
cx.notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_matches(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
let background = cx.background().clone();
|
|
|
|
let candidates = self
|
2021-10-05 13:38:25 +00:00
|
|
|
.themes
|
2021-08-02 21:55:27 +00:00
|
|
|
.list()
|
|
|
|
.map(|name| StringMatchCandidate {
|
|
|
|
char_bag: name.as_str().into(),
|
|
|
|
string: name,
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-09-02 18:20:30 +00:00
|
|
|
let query = self.query_editor.update(cx, |buffer, cx| buffer.text(cx));
|
2021-08-02 21:55:27 +00:00
|
|
|
|
|
|
|
self.matches = if query.is_empty() {
|
|
|
|
candidates
|
|
|
|
.into_iter()
|
|
|
|
.map(|candidate| StringMatch {
|
|
|
|
string: candidate.string,
|
|
|
|
positions: Vec::new(),
|
|
|
|
score: 0.0,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
smol::block_on(match_strings(
|
|
|
|
&candidates,
|
|
|
|
&query,
|
|
|
|
false,
|
|
|
|
100,
|
|
|
|
&Default::default(),
|
|
|
|
background,
|
|
|
|
))
|
|
|
|
};
|
2021-09-03 11:23:28 +00:00
|
|
|
cx.notify();
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_event(
|
|
|
|
workspace: &mut Workspace,
|
2021-08-03 02:07:48 +00:00
|
|
|
_: ViewHandle<ThemeSelector>,
|
2021-08-02 21:55:27 +00:00
|
|
|
event: &Event,
|
|
|
|
cx: &mut ViewContext<Workspace>,
|
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
Event::Dismissed => {
|
|
|
|
workspace.dismiss_modal(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_query_editor_event(
|
|
|
|
&mut self,
|
|
|
|
_: ViewHandle<Editor>,
|
|
|
|
event: &editor::Event,
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
editor::Event::Edited => self.update_matches(cx),
|
|
|
|
editor::Event::Blurred => cx.emit(Event::Dismissed),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 16:04:21 +00:00
|
|
|
fn render_matches(&self, cx: &mut RenderContext<Self>) -> ElementBox {
|
2021-08-02 21:55:27 +00:00
|
|
|
if self.matches.is_empty() {
|
|
|
|
let settings = self.settings.borrow();
|
|
|
|
return Container::new(
|
2021-09-07 21:42:19 +00:00
|
|
|
Label::new(
|
|
|
|
"No matches".into(),
|
|
|
|
settings.theme.selector.empty.label.clone(),
|
|
|
|
)
|
|
|
|
.boxed(),
|
2021-08-02 21:55:27 +00:00
|
|
|
)
|
2021-09-14 23:47:43 +00:00
|
|
|
.with_style(settings.theme.selector.empty.container)
|
2021-08-02 21:55:27 +00:00
|
|
|
.named("empty matches");
|
|
|
|
}
|
|
|
|
|
|
|
|
let handle = cx.handle();
|
|
|
|
let list = UniformList::new(
|
|
|
|
self.list_state.clone(),
|
|
|
|
self.matches.len(),
|
|
|
|
move |mut range, items, cx| {
|
|
|
|
let cx = cx.as_ref();
|
2021-08-03 02:07:48 +00:00
|
|
|
let selector = handle.upgrade(cx).unwrap();
|
|
|
|
let selector = selector.read(cx);
|
2021-08-02 21:55:27 +00:00
|
|
|
let start = range.start;
|
2021-08-03 02:07:48 +00:00
|
|
|
range.end = cmp::min(range.end, selector.matches.len());
|
2021-08-02 21:55:27 +00:00
|
|
|
items.extend(
|
2021-08-03 02:07:48 +00:00
|
|
|
selector.matches[range]
|
2021-08-02 21:55:27 +00:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2021-08-03 02:07:48 +00:00
|
|
|
.map(move |(i, path_match)| selector.render_match(path_match, start + i)),
|
2021-08-02 21:55:27 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Container::new(list.boxed())
|
|
|
|
.with_margin_top(6.0)
|
|
|
|
.named("matches")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_match(&self, theme_match: &StringMatch, index: usize) -> ElementBox {
|
|
|
|
let settings = self.settings.borrow();
|
2021-08-20 16:44:34 +00:00
|
|
|
let theme = &settings.theme;
|
2021-08-02 21:55:27 +00:00
|
|
|
|
2021-08-03 20:36:58 +00:00
|
|
|
let container = Container::new(
|
2021-08-02 21:55:27 +00:00
|
|
|
Label::new(
|
|
|
|
theme_match.string.clone(),
|
2021-08-26 22:06:00 +00:00
|
|
|
if index == self.selected_index {
|
|
|
|
theme.selector.active_item.label.clone()
|
|
|
|
} else {
|
|
|
|
theme.selector.item.label.clone()
|
|
|
|
},
|
2021-08-02 21:55:27 +00:00
|
|
|
)
|
2021-08-03 18:07:03 +00:00
|
|
|
.with_highlights(theme_match.positions.clone())
|
2021-08-02 21:55:27 +00:00
|
|
|
.boxed(),
|
|
|
|
)
|
2021-08-03 20:36:58 +00:00
|
|
|
.with_style(if index == self.selected_index {
|
2021-09-14 23:47:43 +00:00
|
|
|
theme.selector.active_item.container
|
2021-08-02 21:55:27 +00:00
|
|
|
} else {
|
2021-09-14 23:47:43 +00:00
|
|
|
theme.selector.item.container
|
2021-08-02 21:55:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
container.boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 02:07:48 +00:00
|
|
|
impl Entity for ThemeSelector {
|
2021-08-02 21:55:27 +00:00
|
|
|
type Event = Event;
|
|
|
|
}
|
|
|
|
|
2021-08-03 02:07:48 +00:00
|
|
|
impl View for ThemeSelector {
|
2021-08-02 21:55:27 +00:00
|
|
|
fn ui_name() -> &'static str {
|
2021-08-03 02:07:48 +00:00
|
|
|
"ThemeSelector"
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 15:51:26 +00:00
|
|
|
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
2021-08-02 21:55:27 +00:00
|
|
|
let settings = self.settings.borrow();
|
|
|
|
|
|
|
|
Align::new(
|
|
|
|
ConstrainedBox::new(
|
|
|
|
Container::new(
|
|
|
|
Flex::new(Axis::Vertical)
|
2021-09-02 18:20:30 +00:00
|
|
|
.with_child(ChildView::new(self.query_editor.id()).boxed())
|
2021-09-03 11:01:12 +00:00
|
|
|
.with_child(Flexible::new(1.0, self.render_matches(cx)).boxed())
|
2021-08-02 21:55:27 +00:00
|
|
|
.boxed(),
|
|
|
|
)
|
2021-09-14 23:47:43 +00:00
|
|
|
.with_style(settings.theme.selector.container)
|
2021-08-02 21:55:27 +00:00
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.with_max_width(600.0)
|
|
|
|
.with_max_height(400.0)
|
|
|
|
.boxed(),
|
|
|
|
)
|
|
|
|
.top()
|
2021-08-03 02:07:48 +00:00
|
|
|
.named("theme selector")
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_focus(&mut self, cx: &mut ViewContext<Self>) {
|
2021-09-02 18:20:30 +00:00
|
|
|
cx.focus(&self.query_editor);
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn keymap_context(&self, _: &AppContext) -> keymap::Context {
|
|
|
|
let mut cx = Self::default_keymap_context();
|
|
|
|
cx.set.insert("menu".into());
|
|
|
|
cx
|
|
|
|
}
|
|
|
|
}
|