use blit::{KeyboardEvent, LogicalRect, Paint, Sides, StrSource, Widget};
use blit_gui::{
Context,
color::Color,
rectangle::{self, Rectangle},
text::{self, FrameText, TextOptions, TextRequest, TextStyle, TextWrap, VerticalAlign},
};
pub use blit_widget::text_input::{Edit, Response as TextInputResponse, default_keymap};
pub type TextInputState = blit_widget::text_input::State<f32>;
blit::builder! {
pub struct TextInput<'a> {
new(state: &'a mut TextInputState),
multiline: bool = false,
keymap: fn(KeyboardEvent<'_>, bool) -> Option<Edit> = default_keymap,
style: TextStyle<'a> = TextStyle::new(),
padding: Sides<f32> = Sides::all(0.0),
#[into]
placeholder: StrSource<'a> = StrSource::default(),
placeholder_color: Color = Color::GRAY,
selection_background: Color = Color::rgba(64, 128, 255, 128),
cursor_background: Color = Color::BLACK,
}
}
impl<C> Widget<C> for TextInput<'_>
where
C: Context + blit_widget::Context + blit_widget::Clipboard,
C: Paint<rectangle::FrameRectangle> + Paint<text::FrameText>,
{
type Response = TextInputResponse;
fn build(self, mut ui: blit::Ui<'_, C>) -> Self::Response {
let Self {
state,
multiline,
keymap,
style,
padding,
placeholder,
placeholder_color,
selection_background,
cursor_background,
} = self;
let options = if multiline {
TextOptions::new().wrap(TextWrap::Word)
} else {
TextOptions::new().max_lines(1).vertical_align(VerticalAlign::Center)
};
let (response, text) = blit_widget::text_input::drive(
&mut ui,
state,
multiline,
keymap,
padding,
|cx, text| cx.text().text_run(text, style),
|cx, run, area, offset, position| {
let request = TextRequest::new(run).offset(offset).options(options);
cx.text().offset_at_position(area, &request, position)
},
|cx, run, area, offset, cursor, upstream| {
let request = TextRequest::new(run).offset(offset).options(options);
cx.text().caret_rect(area, &request, cursor, upstream)
},
);
let (display, color) = if state.value().is_empty() {
let placeholder = ui.scratch().str(placeholder);
if placeholder.is_empty() {
(text, style.color)
} else {
(ui.context_mut().text().text_run(&placeholder, style), placeholder_color)
}
} else {
(text, style.color)
};
let (cursor, anchor, upstream, offset) = (state.cursor, state.anchor, state.upstream, state.offset);
let pad = padding.size();
ui.measure(move |cx, constraints| {
let max_width = (multiline && constraints.max.width.is_finite())
.then_some((constraints.max.width - pad.width).max(0.0));
let mut fit = cx.text().measure(display, max_width, options);
fit.min.width = 0.0;
if multiline {
fit.min.height = 0.0;
}
blit::Fit::new(fit.min + pad, fit.natural + pad)
});
ui.paint(move |cx, area| {
let scratch = cx.scratch();
let context = cx.context_mut();
let area = area.inset(padding);
let request = TextRequest::new(text).offset(offset).options(options);
let (start, end) = (cursor.min(anchor), cursor.max(anchor));
if start != end {
let rects = context.text().range_rects(area, &request, start..end, scratch);
for rect in &*rects {
if let Some(selection) = rect.intersection(area) {
Rectangle::new()
.background(selection_background)
.paint(selection, context);
}
}
}
if response.focused {
let caret = context.text().caret_rect(area, &request, cursor, upstream);
if let Some(caret) = LogicalRect::new(caret.x, caret.y, 1.0, caret.height).intersection(area) {
Rectangle::new().background(cursor_background).paint(caret, context);
}
}
let colors = context.store_mut().slice_from(&[color]);
let text = FrameText::new(display, colors).offset(offset).options(options);
context.paint(area, &text);
});
response
}
}