search_bar.rs

 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
 8pub(super) enum ActionButtonState {
 9    Disabled,
10    Toggled,
11}
12
13pub(super) fn render_action_button(
14    id_prefix: &'static str,
15    icon: ui::IconName,
16    button_state: Option<ActionButtonState>,
17    tooltip: &'static str,
18    action: &'static dyn Action,
19    focus_handle: FocusHandle,
20) -> impl IntoElement {
21    IconButton::new(
22        SharedString::from(format!("{id_prefix}-{}", action.name())),
23        icon,
24    )
25    .shape(IconButtonShape::Square)
26    .on_click({
27        let focus_handle = focus_handle.clone();
28        move |_, window, cx| {
29            if !focus_handle.is_focused(window) {
30                window.focus(&focus_handle);
31            }
32            window.dispatch_action(action.boxed_clone(), cx)
33        }
34    })
35    .tooltip(move |_window, cx| Tooltip::for_action_in(tooltip, action, &focus_handle, cx))
36    .when_some(button_state, |this, state| match state {
37        ActionButtonState::Toggled => this.toggle_state(true),
38        ActionButtonState::Disabled => this.disabled(true),
39    })
40}
41
42pub(crate) fn input_base_styles(border_color: Hsla, map: impl FnOnce(Div) -> Div) -> Div {
43    h_flex()
44        .map(map)
45        .min_w_32()
46        .h_8()
47        .pl_2()
48        .pr_1()
49        .border_1()
50        .border_color(border_color)
51        .rounded_md()
52}
53
54pub(crate) fn render_text_input(
55    editor: &Entity<Editor>,
56    color_override: Option<Color>,
57    app: &App,
58) -> impl IntoElement {
59    let (color, use_syntax) = if editor.read(app).read_only(app) {
60        (app.theme().colors().text_disabled, false)
61    } else {
62        match color_override {
63            Some(color_override) => (color_override.color(app), false),
64            None => (app.theme().colors().text, true),
65        }
66    };
67
68    let settings = ThemeSettings::get_global(app);
69    let text_style = TextStyle {
70        color,
71        font_family: settings.buffer_font.family.clone(),
72        font_features: settings.buffer_font.features.clone(),
73        font_fallbacks: settings.buffer_font.fallbacks.clone(),
74        font_size: rems(0.875).into(),
75        font_weight: settings.buffer_font.weight,
76        line_height: relative(1.3),
77        ..TextStyle::default()
78    };
79
80    let mut editor_style = EditorStyle {
81        background: app.theme().colors().toolbar_background,
82        local_player: app.theme().players().local(),
83        text: text_style,
84        ..EditorStyle::default()
85    };
86    if use_syntax {
87        editor_style.syntax = app.theme().syntax().clone();
88    }
89
90    EditorElement::new(editor, editor_style)
91}