Correctly compute placeholder text for buffer search query editor (#3749)
Rather than relying on the focused element, instead explicitly pass the focus handle for the query editor when determining the prev/next bindings. Only compute these values once. Release Notes: - N/A
This commit is contained in:
commit
4680aad885
3 changed files with 55 additions and 55 deletions
|
@ -1995,13 +1995,20 @@ impl Editor {
|
||||||
self.collaboration_hub = Some(hub);
|
self.collaboration_hub = Some(hub);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn placeholder_text(&self) -> Option<&str> {
|
||||||
|
self.placeholder_text.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_placeholder_text(
|
pub fn set_placeholder_text(
|
||||||
&mut self,
|
&mut self,
|
||||||
placeholder_text: impl Into<Arc<str>>,
|
placeholder_text: impl Into<Arc<str>>,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) {
|
) {
|
||||||
self.placeholder_text = Some(placeholder_text.into());
|
let placeholder_text = Some(placeholder_text.into());
|
||||||
cx.notify();
|
if self.placeholder_text != placeholder_text {
|
||||||
|
self.placeholder_text = placeholder_text;
|
||||||
|
cx.notify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_cursor_shape(&mut self, cursor_shape: CursorShape, cx: &mut ViewContext<Self>) {
|
pub fn set_cursor_shape(&mut self, cursor_shape: CursorShape, cx: &mut ViewContext<Self>) {
|
||||||
|
|
|
@ -1722,11 +1722,12 @@ impl EditorElement {
|
||||||
return Vec::new();
|
return Vec::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the editor is empty and unfocused, then show the placeholder.
|
// Show the placeholder when the editor is empty
|
||||||
if snapshot.is_empty() {
|
if snapshot.is_empty() {
|
||||||
let font_size = self.style.text.font_size.to_pixels(cx.rem_size());
|
let font_size = self.style.text.font_size.to_pixels(cx.rem_size());
|
||||||
let placeholder_color = cx.theme().styles.colors.text_placeholder;
|
let placeholder_color = cx.theme().styles.colors.text_placeholder;
|
||||||
let placeholder_text = snapshot.placeholder_text();
|
let placeholder_text = snapshot.placeholder_text();
|
||||||
|
|
||||||
let placeholder_lines = placeholder_text
|
let placeholder_lines = placeholder_text
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or("", AsRef::as_ref)
|
.map_or("", AsRef::as_ref)
|
||||||
|
|
|
@ -102,65 +102,57 @@ impl EventEmitter<Event> for BufferSearchBar {}
|
||||||
impl EventEmitter<workspace::ToolbarItemEvent> for BufferSearchBar {}
|
impl EventEmitter<workspace::ToolbarItemEvent> for BufferSearchBar {}
|
||||||
impl Render for BufferSearchBar {
|
impl Render for BufferSearchBar {
|
||||||
type Element = Div;
|
type Element = Div;
|
||||||
|
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
|
||||||
// let query_container_style = if self.query_contains_error {
|
|
||||||
// theme.search.invalid_editor
|
|
||||||
// } else {
|
|
||||||
// theme.search.editor.input.container
|
|
||||||
// };
|
|
||||||
if self.dismissed {
|
if self.dismissed {
|
||||||
return div();
|
return div();
|
||||||
}
|
}
|
||||||
|
|
||||||
let supported_options = self.supported_options();
|
let supported_options = self.supported_options();
|
||||||
|
|
||||||
let previous_query_keystrokes = cx
|
if self.query_editor.read(cx).placeholder_text().is_none() {
|
||||||
.bindings_for_action(&PreviousHistoryQuery {})
|
let query_focus_handle = self.query_editor.focus_handle(cx);
|
||||||
.into_iter()
|
let up_keystrokes = cx
|
||||||
.next()
|
.bindings_for_action_in(&PreviousHistoryQuery {}, &query_focus_handle)
|
||||||
.map(|binding| {
|
.into_iter()
|
||||||
binding
|
.next()
|
||||||
.keystrokes()
|
.map(|binding| {
|
||||||
.iter()
|
binding
|
||||||
.map(|k| k.to_string())
|
.keystrokes()
|
||||||
.collect::<Vec<_>>()
|
.iter()
|
||||||
});
|
.map(|k| k.to_string())
|
||||||
let next_query_keystrokes = cx
|
.collect::<Vec<_>>()
|
||||||
.bindings_for_action(&NextHistoryQuery {})
|
});
|
||||||
.into_iter()
|
let down_keystrokes = cx
|
||||||
.next()
|
.bindings_for_action_in(&NextHistoryQuery {}, &query_focus_handle)
|
||||||
.map(|binding| {
|
.into_iter()
|
||||||
binding
|
.next()
|
||||||
.keystrokes()
|
.map(|binding| {
|
||||||
.iter()
|
binding
|
||||||
.map(|k| k.to_string())
|
.keystrokes()
|
||||||
.collect::<Vec<_>>()
|
.iter()
|
||||||
});
|
.map(|k| k.to_string())
|
||||||
let new_placeholder_text = match (previous_query_keystrokes, next_query_keystrokes) {
|
.collect::<Vec<_>>()
|
||||||
(Some(previous_query_keystrokes), Some(next_query_keystrokes)) => {
|
});
|
||||||
format!(
|
|
||||||
"Search ({}/{} for previous/next query)",
|
let placeholder_text =
|
||||||
previous_query_keystrokes.join(" "),
|
up_keystrokes
|
||||||
next_query_keystrokes.join(" ")
|
.zip(down_keystrokes)
|
||||||
)
|
.map(|(up_keystrokes, down_keystrokes)| {
|
||||||
|
Arc::from(format!(
|
||||||
|
"Search ({}/{} for previous/next query)",
|
||||||
|
up_keystrokes.join(" "),
|
||||||
|
down_keystrokes.join(" ")
|
||||||
|
))
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Some(placeholder_text) = placeholder_text {
|
||||||
|
self.query_editor.update(cx, |editor, cx| {
|
||||||
|
editor.set_placeholder_text(placeholder_text, cx);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
(None, Some(next_query_keystrokes)) => {
|
}
|
||||||
format!(
|
|
||||||
"Search ({} for next query)",
|
|
||||||
next_query_keystrokes.join(" ")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
(Some(previous_query_keystrokes), None) => {
|
|
||||||
format!(
|
|
||||||
"Search ({} for previous query)",
|
|
||||||
previous_query_keystrokes.join(" ")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
(None, None) => String::new(),
|
|
||||||
};
|
|
||||||
let new_placeholder_text = Arc::from(new_placeholder_text);
|
|
||||||
self.query_editor.update(cx, |editor, cx| {
|
|
||||||
editor.set_placeholder_text(new_placeholder_text, cx);
|
|
||||||
});
|
|
||||||
self.replacement_editor.update(cx, |editor, cx| {
|
self.replacement_editor.update(cx, |editor, cx| {
|
||||||
editor.set_placeholder_text("Replace with...", cx);
|
editor.set_placeholder_text("Replace with...", cx);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue