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, window, 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 .py_1()
50 .border_1()
51 .border_color(border_color)
52 .rounded_md()
53}
54
55pub(crate) fn render_text_input(
56 editor: &Entity<Editor>,
57 color_override: Option<Color>,
58 app: &App,
59) -> impl IntoElement {
60 let (color, use_syntax) = if editor.read(app).read_only(app) {
61 (app.theme().colors().text_disabled, false)
62 } else {
63 match color_override {
64 Some(color_override) => (color_override.color(app), false),
65 None => (app.theme().colors().text, true),
66 }
67 };
68
69 let settings = ThemeSettings::get_global(app);
70 let text_style = TextStyle {
71 color,
72 font_family: settings.buffer_font.family.clone(),
73 font_features: settings.buffer_font.features.clone(),
74 font_fallbacks: settings.buffer_font.fallbacks.clone(),
75 font_size: rems(0.875).into(),
76 font_weight: settings.buffer_font.weight,
77 line_height: relative(1.3),
78 ..TextStyle::default()
79 };
80
81 let mut editor_style = EditorStyle {
82 background: app.theme().colors().toolbar_background,
83 local_player: app.theme().players().local(),
84 text: text_style,
85 ..EditorStyle::default()
86 };
87 if use_syntax {
88 editor_style.syntax = app.theme().syntax().clone();
89 }
90
91 EditorElement::new(editor, editor_style)
92}