|
@@ -17,6 +17,10 @@ pub enum CursorPosition {
|
|
|
End,
|
|
|
}
|
|
|
|
|
|
+/*impl CursorPosition {
|
|
|
+
|
|
|
+}*/
|
|
|
+
|
|
|
pub struct TextEdit<C: Component> {
|
|
|
lnode: LeafLayoutNode,
|
|
|
text: TextCache,
|
|
@@ -39,7 +43,7 @@ impl<C: Component> TextEdit<C> {
|
|
|
uih.theme().ui_font.clone(),
|
|
|
uih.theme().ui_font_size,
|
|
|
),
|
|
|
- cursor_pos: CursorPosition::End,
|
|
|
+ cursor_pos: CursorPosition::Start,
|
|
|
onchange: None,
|
|
|
focused: false,
|
|
|
}
|
|
@@ -65,6 +69,7 @@ impl<C: Component> Widget<C> for TextEdit<C> {
|
|
|
};
|
|
|
|
|
|
if istate.is_focused(self.lnode.id()) {
|
|
|
+ let mut key_press_eaten = true;
|
|
|
if let Some(kp) = &istate.keypress {
|
|
|
match kp {
|
|
|
Key::Named(NamedKey::Backspace) => {
|
|
@@ -75,18 +80,26 @@ impl<C: Component> Widget<C> for TextEdit<C> {
|
|
|
self.text.change(String::pop);
|
|
|
self.lnode.render_needed();
|
|
|
}
|
|
|
+ Key::Named(NamedKey::Space) => {
|
|
|
+ self.text.change(|v| v.push(' '));
|
|
|
+ self.lnode.render_needed();
|
|
|
+ }
|
|
|
Key::Named(NamedKey::Enter) => {
|
|
|
istate.clear_focus();
|
|
|
}
|
|
|
- _ => {
|
|
|
- if let Some(inp) = &istate.text_input {
|
|
|
- self.text.change(|s| s.push_str(inp.as_str()));
|
|
|
- self.lnode.render_needed();
|
|
|
- }
|
|
|
- }
|
|
|
+ Key::Named(_) => {}
|
|
|
+ _ => key_press_eaten = false,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !key_press_eaten {
|
|
|
+ if let Some(text) = &istate.text_input {
|
|
|
+ println!("adding text {text:?}");
|
|
|
+ self.text.change(|s| s.push_str(text.as_str()));
|
|
|
+ self.lnode.render_needed();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
let render_area = self.lnode.render_area().unwrap();
|
|
|
if istate
|
|
|
.mouse_status_in(render_area, MouseButton::Left)
|
|
@@ -121,7 +134,13 @@ impl<C: Component> Widget<C> for TextEdit<C> {
|
|
|
.simple_text(&self.text, ColourChoice::Foreground)
|
|
|
.seq(|target| {
|
|
|
if self.focused {
|
|
|
- let cbar_offset = self.text.size_hint().width;
|
|
|
+ let cbar_offset = match self.cursor_pos {
|
|
|
+ CursorPosition::Start => 0,
|
|
|
+ CursorPosition::Index(idx) => {
|
|
|
+ *self.text.x_offsets.borrow().get(idx).unwrap() as i32
|
|
|
+ }
|
|
|
+ CursorPosition::End => self.text.size_hint().width,
|
|
|
+ };
|
|
|
target.fill_relative_box(
|
|
|
PixelBox::from_origin_and_size(
|
|
|
PixelPoint::new(cbar_offset, 0),
|