| 1 | use std::{fmt::Display, future::Future}; |
| 2 | |
| 3 | use gpui_kit::{ |
| 4 | App, AsyncWindowContext, Context, Div, Entity, ParentElement, Styled, WeakEntity, Window, |
| 5 | component::{input::Input, input::InputState}, |
| 6 | div, px, |
| 7 | }; |
| 8 | |
| 9 | use super::{ |
| 10 | Palette, Yumush, notify_error, |
| 11 | style::{size, space}, |
| 12 | }; |
| 13 | |
| 14 | pub(super) fn panel(palette: Palette) -> Div { |
| 15 | super::parts::surface(palette.tokens) |
| 16 | } |
| 17 | |
| 18 | pub(super) fn field(palette: Palette, input: &Entity<InputState>) -> Div { |
| 19 | frame(palette, Input::new(input).token(super::glyph::token)) |
| 20 | } |
| 21 | |
| 22 | pub(super) fn compact_frame(palette: Palette) -> Div { |
| 23 | div() |
| 24 | .h(px(size::CONTROL_SM + space::XS)) |
| 25 | .flex() |
| 26 | .items_center() |
| 27 | .border_b_1() |
| 28 | .border_color(palette.border) |
| 29 | } |
| 30 | |
| 31 | pub(super) fn compact_field(palette: Palette, input: &Entity<InputState>) -> Div { |
| 32 | compact_frame(palette).child(bare_input( |
| 33 | palette, |
| 34 | Input::new(input).token(super::glyph::token), |
| 35 | )) |
| 36 | } |
| 37 | |
| 38 | pub(super) fn frame(palette: Palette, input: Input) -> Div { |
| 39 | div() |
| 40 | .h(px(size::CONTROL)) |
| 41 | .flex() |
| 42 | .items_center() |
| 43 | .border_b_1() |
| 44 | .border_color(palette.border) |
| 45 | .child(bare_input(palette, input)) |
| 46 | } |
| 47 | |
| 48 | pub(super) fn underline(palette: Palette, child: impl gpui_kit::IntoElement) -> Div { |
| 49 | div() |
| 50 | .h(px(size::CONTROL)) |
| 51 | .flex() |
| 52 | .items_center() |
| 53 | .border_b_1() |
| 54 | .border_color(palette.border) |
| 55 | .child(div().flex_1().min_w_0().child(child)) |
| 56 | } |
| 57 | |
| 58 | pub(super) fn bare_input(palette: Palette, input: Input) -> Input { |
| 59 | input |
| 60 | .appearance(false) |
| 61 | .text_color(palette.text) |
| 62 | .flex_1() |
| 63 | .min_w_0() |
| 64 | } |
| 65 | |
| 66 | pub(super) fn search_text(input: &Entity<InputState>, cx: &App) -> String { |
| 67 | input.read(cx).text().to_string().trim().to_lowercase() |
| 68 | } |
| 69 | |
| 70 | pub(super) fn ok_or_notify<T: 'static, V, E: Display>( |
| 71 | result: Result<V, E>, |
| 72 | this: &WeakEntity<T>, |
| 73 | cx: &mut AsyncWindowContext, |
| 74 | ) -> Option<V> { |
| 75 | result.map_err(|error| notify_error(this, cx, error)).ok() |
| 76 | } |
| 77 | |
| 78 | pub(super) fn spawn_done<T, E: Display + 'static>( |
| 79 | window: &mut Window, |
| 80 | cx: &mut Context<Yumush>, |
| 81 | call: impl Future<Output = Result<T, E>> + 'static, |
| 82 | ) { |
| 83 | cx.spawn_in(window, async move |this, cx| { |
| 84 | if let Err(error) = call.await { |
| 85 | notify_error(&this, cx, error); |
| 86 | } |
| 87 | }) |
| 88 | .detach(); |
| 89 | } |
| 90 | |
| 91 | pub(super) fn spawn_warn<T, E: Display + 'static>( |
| 92 | cx: &mut Context<Yumush>, |
| 93 | label: &'static str, |
| 94 | call: impl Future<Output = Result<T, E>> + 'static, |
| 95 | ) { |
| 96 | cx.spawn(async move |_, _| { |
| 97 | if let Err(error) = call.await { |
| 98 | tracing::warn!("{label} failed: {error}"); |
| 99 | } |
| 100 | }) |
| 101 | .detach(); |
| 102 | } |