use gpui::{
actions, div, prelude::*, px, App, Context, ElementId, Entity, KeyBinding, MouseButton,
StyleRefinement, Window,
};
mod element;
mod ime;
mod layout;
mod state;
mod textarea;
use element::InputTextElement;
#[doc(hidden)]
pub use layout::{bidi_rows, needs_bidi_rows, BidiRow, DisplayLine};
pub use state::{InputEvent, InputState};
pub use textarea::{use_textarea_state, Textarea};
use super::focus_ring_outline;
use crate::sx::SxStyled;
use crate::{
icon::{Icon, IconName},
styles,
styles::ApplyStyleOverrides,
theme::ActiveTheme,
};
actions!(
rok_ui_input,
[
DeleteBackward,
DeleteForward,
MoveLeft,
MoveRight,
SelectLeft,
SelectRight,
SelectAll,
MoveToStart,
MoveToEnd,
Paste,
Cut,
Copy,
Submit,
MoveUp,
MoveDown,
SelectUp,
SelectDown,
]
);
#[doc(hidden)]
pub const INPUT_KEY_CONTEXT: &str = "RokUiInput";
#[doc(hidden)]
pub const TEXTAREA_KEY_CONTEXT: &str = "RokUiTextarea";
#[doc(hidden)]
pub fn bind_text_editing_keys(cx: &mut App) {
let context = Some(INPUT_KEY_CONTEXT);
cx.bind_keys([
KeyBinding::new("backspace", DeleteBackward, context),
KeyBinding::new("delete", DeleteForward, context),
KeyBinding::new("left", MoveLeft, context),
KeyBinding::new("right", MoveRight, context),
KeyBinding::new("shift-left", SelectLeft, context),
KeyBinding::new("shift-right", SelectRight, context),
KeyBinding::new("secondary-a", SelectAll, context),
KeyBinding::new("secondary-v", Paste, context),
KeyBinding::new("secondary-c", Copy, context),
KeyBinding::new("secondary-x", Cut, context),
KeyBinding::new("home", MoveToStart, context),
KeyBinding::new("end", MoveToEnd, context),
KeyBinding::new("enter", Submit, context),
KeyBinding::new("secondary-enter", Submit, context),
]);
let textarea_context = Some(TEXTAREA_KEY_CONTEXT);
cx.bind_keys([
KeyBinding::new("up", MoveUp, textarea_context),
KeyBinding::new("down", MoveDown, textarea_context),
KeyBinding::new("shift-up", SelectUp, textarea_context),
KeyBinding::new("shift-down", SelectDown, textarea_context),
]);
}
pub fn use_input_state(
key: impl Into<ElementId>,
window: &mut Window,
cx: &mut App,
configure: impl FnOnce(InputState) -> InputState,
) -> Entity<InputState> {
window.use_keyed_state(key, cx, |_, cx| configure(InputState::new(cx)))
}
#[derive(IntoElement)]
pub struct Input {
state: Entity<InputState>,
leading_icon: Option<IconName>,
disabled: bool,
invalid: bool,
focus_ring: bool,
sx: crate::sx::Sx,
style_overrides: StyleRefinement,
}
crate::implement_style_overrides!(Input);
impl Input {
#[must_use]
pub fn new(state: &Entity<InputState>) -> Self {
Self {
state: state.clone(),
leading_icon: None,
disabled: false,
invalid: false,
focus_ring: true,
sx: crate::sx::Sx::new(),
style_overrides: StyleRefinement::default(),
}
}
#[must_use]
pub fn leading_icon(mut self, icon: IconName) -> Self {
self.leading_icon = Some(icon);
self
}
#[must_use]
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
#[must_use]
pub fn invalid(mut self, invalid: bool) -> Self {
self.invalid = invalid;
self
}
#[cfg_attr(not(feature = "full"), allow(dead_code))]
#[doc(hidden)]
#[must_use]
pub fn without_focus_ring(mut self) -> Self {
self.focus_ring = false;
self
}
}
styles! {
INPUT = {
field: {
position: relative,
display: flex,
align: center,
gap: 2,
width: full,
height: 9,
padding_x: 3,
radius: md,
border: 1,
border_color: input,
background: transparent,
color: foreground,
text: sm,
line_height: 5,
},
focused: { border_color: ring },
invalid: { border_color: destructive_text },
enabled: { cursor: text },
disabled: { opacity: 0.5 },
text: { flex: 1, overflow: hidden },
}
}
impl RenderOnce for Input {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let colors = theme.colors.clone();
let focus_handle = self.state.read(cx).focus_handle.clone();
let is_focused = focus_handle.is_focused(window);
let ring_color = if self.invalid {
colors.destructive_text
} else {
colors.ring
};
let state = self.state.clone();
div()
.id(ElementId::NamedInteger(
"rok-ui-input".into(),
self.state.entity_id().as_u64(),
))
.sx((
&INPUT.field,
is_focused.then_some(&INPUT.focused),
self.invalid.then_some(&INPUT.invalid),
if self.disabled {
&INPUT.disabled
} else {
&INPUT.enabled
},
&self.sx,
))
.when(is_focused && self.focus_ring, |field| {
field.child(focus_ring_outline(ring_color, theme.radius_medium()))
})
.when_some(self.leading_icon, |field, icon| {
field.child(Icon::new(icon).size(px(16.)).color(colors.muted_foreground))
})
.when(!self.disabled, |field| {
field
.key_context(INPUT_KEY_CONTEXT)
.track_focus(&focus_handle)
.on_action(forward_to_state(&state, InputState::delete_backward))
.on_action(forward_to_state(&state, InputState::delete_forward))
.on_action(forward_to_state(&state, InputState::move_left))
.on_action(forward_to_state(&state, InputState::move_right))
.on_action(forward_to_state(&state, InputState::select_left))
.on_action(forward_to_state(&state, InputState::select_right))
.on_action(forward_to_state(&state, InputState::select_all))
.on_action(forward_to_state(&state, InputState::move_to_start))
.on_action(forward_to_state(&state, InputState::move_to_end))
.on_action(forward_to_state(&state, InputState::paste))
.on_action(forward_to_state(&state, InputState::cut))
.on_action(forward_to_state(&state, InputState::copy))
.on_action(forward_to_state(&state, InputState::submit))
.on_mouse_down(
MouseButton::Left,
forward_to_state(&state, InputState::on_mouse_down),
)
.on_mouse_up(
MouseButton::Left,
forward_to_state(&state, InputState::on_mouse_up),
)
.on_mouse_up_out(
MouseButton::Left,
forward_to_state(&state, InputState::on_mouse_up),
)
.on_mouse_move(forward_to_state(&state, InputState::on_mouse_move))
})
.child(div().sx(&INPUT.text).child(InputTextElement {
state: self.state,
placeholder_color: colors.muted_foreground,
cursor_color: colors.foreground,
selection_color: colors.ring.opacity(0.3),
}))
.apply_style_overrides(&self.style_overrides)
}
}
fn forward_to_state<Event: 'static>(
state: &Entity<InputState>,
handler: fn(&mut InputState, &Event, &mut Window, &mut Context<InputState>),
) -> impl Fn(&Event, &mut Window, &mut App) + 'static {
let state = state.clone();
move |event, window, cx| state.update(cx, |state, cx| handler(state, event, window, cx))
}