1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use ribir_core::{prelude::*, ticker::FrameMsg};
mod caret;
mod caret_state;
mod glyphs_helper;
mod handle;
mod selected_text;
mod text_selectable;
use crate::{
  input::{
    caret::Caret,
    handle::{edit_handle, edit_key_handle, TextCaretWriter},
    selected_text::SelectedHighLight,
    text_selectable::{bind_point_listener, select_key_handle, SelectableText},
  },
  layout::{ConstrainedBox, OnlySizedByParent, Stack, StackFit},
  prelude::Text,
};
pub use caret_state::{CaretPosition, CaretState};
pub use selected_text::SelectedHighLightStyle;
use std::{ops::Range, rc::Rc};
pub use text_selectable::TextSelectable;

pub struct Placeholder(CowArc<str>);

impl Placeholder {
  #[inline]
  pub fn new(str: impl Into<CowArc<str>>) -> Self { Self(str.into()) }
}

#[derive(Clone)]
pub struct PlaceholderStyle {
  pub text_style: CowArc<TextStyle>,
  pub foreground: Brush,
}

impl CustomStyle for PlaceholderStyle {
  fn default_style(ctx: &BuildCtx) -> Self {
    Self {
      foreground: Palette::of(ctx).on_surface_variant().into(),
      text_style: TypographyTheme::of(ctx).body_medium.text.clone(),
    }
  }
}

#[derive(Clone, PartialEq)]
pub struct InputStyle {
  pub size: Option<f32>,
}

impl CustomStyle for InputStyle {
  fn default_style(_: &BuildCtx) -> Self { InputStyle { size: Some(20.) } }
}

#[derive(Clone, PartialEq)]
pub struct TextAreaStyle {
  pub rows: Option<f32>,
  pub cols: Option<f32>,
}

impl CustomStyle for TextAreaStyle {
  fn default_style(_: &BuildCtx) -> Self { TextAreaStyle { rows: Some(2.), cols: Some(20.) } }
}

pub trait EditableText: Sized {
  fn text(&self) -> &CowArc<str>;

  fn caret(&self) -> CaretState;

  fn set_text_with_caret(&mut self, text: &str, caret: CaretState);

  fn writer(&mut self) -> TextCaretWriter<Self> { TextCaretWriter::new(self) }
}

#[derive(Declare)]
pub struct Input {
  #[declare(default = TypographyTheme::of(ctx!()).body_large.text.clone())]
  pub style: CowArc<TextStyle>,
  #[declare(skip)]
  text: CowArc<str>,
  #[declare(skip)]
  caret: CaretState,
  #[declare(default = InputStyle::of(ctx!()).size)]
  size: Option<f32>,
}

#[derive(Declare)]
pub struct TextArea {
  #[declare(default = TypographyTheme::of(ctx!()).body_large.text.clone())]
  pub style: CowArc<TextStyle>,
  #[declare(default = true)]
  pub auto_wrap: bool,
  #[declare(skip)]
  text: CowArc<str>,
  #[declare(skip)]
  caret: CaretState,
  #[declare(default = TextAreaStyle::of(ctx!()).rows)]
  rows: Option<f32>,
  #[declare(default = TextAreaStyle::of(ctx!()).cols)]
  cols: Option<f32>,
}

impl Input {
  /// set the text and the caret selection will be reset to the start.
  pub fn set_text(&mut self, text: &str) { self.set_text_with_caret(text, CaretState::default()); }
}

impl TextArea {
  /// set the text and the caret selection will be reset to the start.
  pub fn set_text(&mut self, text: &str) { self.set_text_with_caret(text, CaretState::default()); }
}

impl SelectableText for Input {
  fn select_range(&self) -> Range<usize> { self.caret.select_range() }

  fn text(&self) -> &CowArc<str> { &self.text }

  fn caret(&self) -> CaretState { self.caret }

  fn set_caret(&mut self, caret: CaretState) { self.caret = caret; }
}

impl EditableText for Input {
  fn text(&self) -> &CowArc<str> { &self.text }
  fn caret(&self) -> CaretState { self.caret }
  fn set_text_with_caret(&mut self, text: &str, caret: CaretState) {
    let new_text = text.replace(['\r', '\n'], " ");
    self.text = new_text.into();
    self.caret = caret;
  }
}

impl SelectableText for TextArea {
  fn select_range(&self) -> Range<usize> { self.caret.select_range() }

  fn text(&self) -> &CowArc<str> { &self.text }

  fn caret(&self) -> CaretState { self.caret }

  fn set_caret(&mut self, caret: CaretState) { self.caret = caret; }
}

impl EditableText for TextArea {
  fn text(&self) -> &CowArc<str> { &self.text }

  fn caret(&self) -> CaretState { self.caret }

  fn set_text_with_caret(&mut self, text: &str, caret: CaretState) {
    self.text = text.to_string().into();
    self.caret = caret;
  }
}

#[derive(Debug)]
struct PreEditState {
  position: usize,
  value: Option<String>,
}

struct ImeHandle<H> {
  host: H,
  pre_edit: Option<PreEditState>,
  guard: Option<SubscriptionGuard<BoxSubscription<'static>>>,
  window: Rc<Window>,
  caret_id: LazyWidgetId,
}

impl<E, H> ImeHandle<H>
where
  E: EditableText + 'static,
  H: StateWriter<Value = E>,
{
  fn new(window: Rc<Window>, host: H, caret_id: LazyWidgetId) -> Self {
    Self {
      window,
      host,
      pre_edit: None,
      guard: None,
      caret_id,
    }
  }
  fn ime_allowed(&mut self) {
    self.window.set_ime_allowed(true);
    self.track_cursor();
  }

  fn ime_disallowed(&mut self) {
    self.window.set_ime_allowed(false);
    self.guard = None;
  }

  fn update_pre_edit(&mut self, e: &ImePreEditEvent) {
    match &e.pre_edit {
      ImePreEdit::Begin => {
        let mut host = self.host.write();
        let rg = host.caret().select_range();
        host.writer().delete_byte_range(&rg);
        self.pre_edit = Some(PreEditState { position: rg.start, value: None });
      }
      ImePreEdit::PreEdit { value, cursor } => {
        let Some(PreEditState { position, value: edit_value }) = self.pre_edit.as_mut() else {
          return;
        };
        let mut host = self.host.write();
        let mut writer = host.writer();
        if let Some(txt) = edit_value {
          writer.delete_byte_range(&(*position..*position + txt.len()));
        }
        writer.insert_str(value);
        writer.set_to(*position + cursor.map_or(0, |(start, _)| start));
        *edit_value = Some(value.clone());
      }
      ImePreEdit::End => {
        if let Some(PreEditState { value: Some(txt), position, .. }) = self.pre_edit.take() {
          let mut host = self.host.write();
          let mut writer = host.writer();
          writer.delete_byte_range(&(position..position + txt.len()));
        }
      }
    }
    if self.pre_edit.is_none() {
      self.track_cursor();
    } else {
      self.guard = None;
    }
  }

  fn track_cursor(&mut self) {
    if self.guard.is_some() {
      return;
    }

    let window = self.window.clone();
    let wid = self.caret_id.clone();

    let pos = window.map_to_global(Point::zero(), wid.assert_id());
    let size = window.layout_size(wid.assert_id()).unwrap_or_default();
    window.set_ime_cursor_area(&Rect::new(pos, size));

    let tick_of_layout_ready = window
      .frame_tick_stream()
      .filter(|msg| matches!(msg, FrameMsg::LayoutReady(_)));
    self.guard = Some(
      self
        .host
        .modifies()
        .sample(tick_of_layout_ready)
        .box_it()
        .subscribe(move |_| {
          let pos = window.map_to_global(Point::zero(), wid.assert_id());
          let size = window.layout_size(wid.assert_id()).unwrap_or_default();
          window.set_ime_cursor_area(&Rect::new(pos, size));
        })
        .unsubscribe_when_dropped(),
    );
  }
}

impl ComposeChild for Input {
  type Child = Option<State<Placeholder>>;
  fn compose_child(
    this: impl StateWriter<Value = Self>,
    placeholder: Self::Child,
  ) -> impl WidgetBuilder {
    fn_widget! {
      let text = @Text {
        text: pipe!($this.text.clone()),
        text_style: pipe!($this.style.clone()),
      };
      @FocusScope {
        @ConstrainedBox {
          clamp: pipe!(size_clamp(&$this.style, Some(1.), $this.size)),
          @ {
            EditableTextExtraWidget::edit_area(
              &this,
              text,
              BoxPipe::value(Scrollable::X).into_pipe(),
              placeholder
            )
          }
        }
      }
    }
  }
}

impl ComposeChild for TextArea {
  type Child = Option<State<Placeholder>>;
  fn compose_child(
    this: impl StateWriter<Value = Self>,
    placeholder: Self::Child,
  ) -> impl WidgetBuilder {
    fn_widget! {
      let text = @Text {
        text: pipe!($this.text.clone()),
        text_style: pipe!($this.style.clone()),
        overflow: pipe!($this.auto_wrap).map(|auto_wrap| match auto_wrap {
          true => Overflow::AutoWrap,
          false => Overflow::Clip,
        }),
      };

      let scroll_dir = pipe!($this.auto_wrap).map(|auto_wrap| match auto_wrap {
        true => Scrollable::Y,
        false => Scrollable::Both,
      });
      @FocusScope {
        @ConstrainedBox {
          clamp: pipe!(size_clamp(&$this.style, $this.rows, $this.cols)),
          @ { EditableTextExtraWidget::edit_area(&this, text, scroll_dir, placeholder) }
        }
      }
    }
  }
}

trait EditableTextExtraWidget: EditableText + SelectableText
where
  Self: 'static,
{
  fn edit_area(
    this: &impl StateWriter<Value = Self>,
    text: FatObj<State<Text>>,
    scroll_dir: impl Pipe<Value = Scrollable> + 'static,
    placeholder: Option<State<Placeholder>>,
  ) -> impl WidgetBuilder {
    fn_widget! {
      let mut text = @$text{};
      let layout_box = text.get_layout_box_widget().clone_reader();
      let only_text = text.clone_reader();

      let mut stack = @Stack {
        fit: StackFit::Passthrough,
        scrollable: scroll_dir,
      };

      let caret_box = @ConstrainedBox {
        clamp: pipe!(
            $this.current_line_height(&$text, $text.layout_size()).unwrap_or(0.)
          ).map(BoxClamp::fixed_height),
      };

      let caret_box_id = caret_box.lazy_host_id();
      let mut caret_box = @$caret_box {
        anchor: pipe!(
          let pos = $this.caret_position(&$text, $text.layout_size()).unwrap_or_default();
          Anchor::left_top(pos.x, pos.y)
        ),
      };
      let scrollable = stack.get_scrollable_widget();
      let tick_of_layout_ready = ctx!().window()
        .frame_tick_stream()
        .filter(|msg| matches!(msg, FrameMsg::LayoutReady(_)));
      watch!(SelectableText::caret(&*$this))
        .distinct_until_changed()
        .sample(tick_of_layout_ready)
        .map(move |_| $this.caret_position(&$text, $text.layout_size()).unwrap_or_default())
        .scan_initial((Point::zero(), Point::zero()), |pair, v| (pair.1, v))
        .subscribe(move |(before, after)| {
          let mut scrollable = $scrollable.silent();
          let pos = auto_scroll_pos(&scrollable, before, after, $caret_box.layout_size());
          scrollable.jump_to(pos);
      });

      let placeholder = @ {
        placeholder.map(move |holder| @Text {
          visible: pipe!(SelectableText::text(&*$this).is_empty()),
          text: pipe!((*$holder).0.clone()),
        })
      };

      let ime_handle = Stateful::new(
        ImeHandle::new(ctx!().window(), this.clone_writer(), caret_box_id)
      );
      let mut stack = @ $stack {
        on_focus: move |_| $ime_handle.write().ime_allowed(),
        on_blur: move |_| $ime_handle.write().ime_disallowed(),
        on_chars: move |c| {
          let _hint_capture_writer = || $this.write();
          edit_handle(&this, c);
        },
        on_key_down: move |k| {
          let _hint_capture_writer = || $this.write();
          select_key_handle(&this, &$only_text, &$layout_box, k);
          edit_key_handle(&this, k);
        },
        on_ime_pre_edit: move |e| {
          $ime_handle.write().update_pre_edit(e);
        },
      };

      let high_light_rect = @UnconstrainedBox {
        clamp_dim: ClampDim::MIN_SIZE,
        @OnlySizedByParent {
          @SelectedHighLight {
            visible: pipe!($stack.has_focus()),
            rects: pipe! {
              $this.select_text_rect(&$only_text, $text.layout_size())
            }
          }
        }
      };

      let caret = @UnconstrainedBox {
        clamp_dim: ClampDim::MIN_SIZE,
        @OnlySizedByParent {
          @$caret_box {
            @Caret { focused: pipe!($stack.has_focus()) }
          }
        }
      };

      let text_widget = text.build(ctx!());
      let text_widget = bind_point_listener(
        this.clone_writer(),
        text_widget,
        only_text,
        layout_box
      );

      @ $stack {
        padding: EdgeInsets::horizontal(2.),
        @ { placeholder }
        @ { high_light_rect }
        @ { caret }
        @ { text_widget }
      }
    }
  }
}

impl EditableTextExtraWidget for TextArea {}

impl EditableTextExtraWidget for Input {}

fn size_clamp(style: &TextStyle, rows: Option<f32>, cols: Option<f32>) -> BoxClamp {
  let mut clamp: BoxClamp = BoxClamp {
    min: Size::new(0., 0.),
    max: Size::new(f32::INFINITY, f32::INFINITY),
  };
  if let Some(cols) = cols {
    let width = cols * style.font_size.into_pixel().value();
    clamp = clamp.with_fixed_width(width);
  }
  if let Some(rows) = rows {
    let height: Pixel = style
      .line_height
      .unwrap_or(style.font_size.into_em())
      .into();

    clamp = clamp.with_fixed_height(rows * height.value());
  }
  clamp
}

fn auto_scroll_pos(container: &ScrollableWidget, before: Point, after: Point, size: Size) -> Point {
  let view_size = container.scroll_view_size();
  let content_size = container.scroll_content_size();
  let current = container.scroll_pos;
  if view_size.contains(content_size) {
    return current;
  }

  let calc_offset = |current, before, after, max_size, size| {
    let view_after = current + after;
    let view_before = current + before;
    let best_position = if !(0. <= view_before + size && view_before < max_size) {
      (max_size - size) / 2.
    } else if view_after < 0. {
      0.
    } else if view_after > max_size - size {
      max_size - size
    } else {
      view_after
    };
    current + best_position - view_after
  };
  Point::new(
    calc_offset(current.x, before.x, after.x, view_size.width, size.width),
    calc_offset(current.y, before.y, after.y, view_size.height, size.height),
  )
}

#[cfg(test)]
mod tests {
  use super::{EditableText, Input};
  use crate::layout::SizedBox;
  use ribir_core::{
    prelude::*,
    reset_test_env,
    test_helper::{split_value, TestWindow},
  };
  use winit::event::{DeviceId, ElementState, MouseButton, WindowEvent};

  #[test]
  fn input_edit() {
    reset_test_env!();
    let (input_value, input_value_writer) = split_value(String::default());
    let w = fn_widget! {
      let input = @Input {
        auto_focus: true,
      };
      watch!($input.text().clone())
        .subscribe(move |text| {
          *input_value_writer.write() = text.to_string();
        });
      @ { input }
    };

    let mut wnd = TestWindow::new_with_size(w, Size::new(200., 200.));
    wnd.draw_frame();
    assert_eq!(*input_value.read(), "");

    wnd.processes_receive_chars("hello\nworld".into());
    wnd.draw_frame();
    assert_eq!(*input_value.read(), "hello world");
  }

  #[test]
  fn input_tap_focus() {
    reset_test_env!();
    let (input_value, input_value_writer) = split_value(String::default());
    let w = fn_widget! {
      let input = @Input { size: None };
      watch!($input.text().clone())
        .subscribe(move |text| {
          *input_value_writer.write() = text.to_string();
        });

      @SizedBox {
        size: Size::new(200., 24.),
        @ { input }
      }
    };

    let mut wnd = TestWindow::new_with_size(w, Size::new(200., 200.));
    wnd.draw_frame();
    assert_eq!(*input_value.read(), "");

    let device_id = unsafe { DeviceId::dummy() };
    #[allow(deprecated)]
    wnd.processes_native_event(WindowEvent::CursorMoved {
      device_id,
      position: (50., 10.).into(),
    });

    wnd.process_mouse_input(device_id, ElementState::Pressed, MouseButton::Left);
    wnd.process_mouse_input(device_id, ElementState::Released, MouseButton::Left);
    wnd.draw_frame();

    wnd.processes_receive_chars("hello".into());
    wnd.draw_frame();
    assert_eq!(*input_value.read(), "hello");
  }
}