1use editor::{Editor, EditorElement, EditorStyle};
2use gpui::{Action, Entity, FocusHandle, Hsla, IntoElement, TextStyle};
3use settings::Settings;
4use theme::ThemeSettings;
5use ui::{IconButton, IconButtonShape};
6use ui::{Tooltip, prelude::*};
7
8use crate::ToggleReplace;
9
10pub(super) fn render_nav_button(
11 icon: ui::IconName,
12 active: bool,
13 tooltip: &'static str,
14 action: &'static dyn Action,
15 focus_handle: FocusHandle,
16) -> impl IntoElement {
17 IconButton::new(
18 SharedString::from(format!("search-nav-button-{}", action.name())),
19 icon,
20 )
21 .shape(IconButtonShape::Square)
22 .on_click({
23 let focus_handle = focus_handle.clone();
24 move |_, window, cx| {
25 if !focus_handle.is_focused(&window) {
26 window.focus(&focus_handle);
27 }
28 window.dispatch_action(action.boxed_clone(), cx)
29 }
30 })
31 .tooltip(move |window, cx| Tooltip::for_action_in(tooltip, action, &focus_handle, window, cx))
32 .disabled(!active)
33}
34
35pub(crate) fn input_base_styles(border_color: Hsla, map: impl FnOnce(Div) -> Div) -> Div {
36 h_flex()
37 .min_w_32()
38 .map(map)
39 .h_8()
40 .pl_2()
41 .pr_1()
42 .py_1()
43 .border_1()
44 .border_color(border_color)
45 .rounded_lg()
46}
47
48pub(crate) fn toggle_replace_button(
49 id: &'static str,
50 focus_handle: FocusHandle,
51 replace_enabled: bool,
52 on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut App) + 'static,
53) -> IconButton {
54 IconButton::new(id, IconName::Replace)
55 .shape(IconButtonShape::Square)
56 .style(ButtonStyle::Subtle)
57 .when(replace_enabled, |button| button.style(ButtonStyle::Filled))
58 .on_click(on_click)
59 .toggle_state(replace_enabled)
60 .tooltip({
61 move |window, cx| {
62 Tooltip::for_action_in("Toggle Replace", &ToggleReplace, &focus_handle, window, cx)
63 }
64 })
65}
66
67pub(crate) fn render_text_input(
68 editor: &Entity<Editor>,
69 color_override: Option<Color>,
70 app: &App,
71) -> impl IntoElement {
72 let (color, use_syntax) = if editor.read(app).read_only(app) {
73 (app.theme().colors().text_disabled, false)
74 } else {
75 match color_override {
76 Some(color_override) => (color_override.color(app), false),
77 None => (app.theme().colors().text, true),
78 }
79 };
80
81 let settings = ThemeSettings::get_global(app);
82 let text_style = TextStyle {
83 color,
84 font_family: settings.buffer_font.family.clone(),
85 font_features: settings.buffer_font.features.clone(),
86 font_fallbacks: settings.buffer_font.fallbacks.clone(),
87 font_size: rems(0.875).into(),
88 font_weight: settings.buffer_font.weight,
89 line_height: relative(1.3),
90 ..TextStyle::default()
91 };
92
93 let mut editor_style = EditorStyle {
94 background: app.theme().colors().toolbar_background,
95 local_player: app.theme().players().local(),
96 text: text_style,
97 ..EditorStyle::default()
98 };
99 if use_syntax {
100 editor_style.syntax = app.theme().syntax().clone();
101 }
102
103 EditorElement::new(editor, editor_style)
104}