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
use std::{
    borrow::Cow,
    cmp::Ordering,
    fmt::Display,
    ops::Range,
};

use dioxus_sdk::clipboard::UseClipboard;
use freya_elements::events::keyboard::{
    Code,
    Key,
    Modifiers,
};

/// Holds the position of a cursor in a text
#[derive(Clone, Default, PartialEq, Debug)]
pub struct TextCursor(usize);

impl TextCursor {
    /// Construct a new [TextCursor]
    pub fn new(pos: usize) -> Self {
        Self(pos)
    }

    /// Get the position
    pub fn pos(&self) -> usize {
        self.0
    }

    /// Set the position
    pub fn set(&mut self, pos: usize) {
        self.0 = pos;
    }

    /// Write the position
    pub fn write(&mut self) -> &mut usize {
        &mut self.0
    }
}

/// A text line from a [TextEditor]
#[derive(Clone)]
pub struct Line<'a> {
    pub text: Cow<'a, str>,
    pub utf16_len: usize,
}

impl Line<'_> {
    /// Get the length of the line
    pub fn utf16_len(&self) -> usize {
        self.utf16_len
    }
}

impl Display for Line<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.text)
    }
}

bitflags::bitflags! {
    /// Events for [TextEditor]
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
    pub struct TextEvent: u8 {
         /// Cursor position has been moved
        const CURSOR_CHANGED = 0x01;
        /// Text has changed
        const TEXT_CHANGED = 0x02;
        /// Selected text has changed
        const SELECTION_CHANGED = 0x04;
    }
}

/// Common trait for editable texts
pub trait TextEditor {
    type LinesIterator<'a>: Iterator<Item = Line<'a>>
    where
        Self: 'a;

    fn set(&mut self, text: &str);

    /// Iterator over all the lines in the text.
    fn lines(&self) -> Self::LinesIterator<'_>;

    /// Insert a character in the text in the given position.
    fn insert_char(&mut self, char: char, char_idx: usize) -> usize;

    /// Insert a string in the text in the given position.
    fn insert(&mut self, text: &str, char_idx: usize) -> usize;

    /// Remove a part of the text.
    fn remove(&mut self, range: Range<usize>) -> usize;

    /// Get line from the given char
    fn char_to_line(&self, char_idx: usize) -> usize;

    /// Get the first char from the given line
    fn line_to_char(&self, line_idx: usize) -> usize;

    fn utf16_cu_to_char(&self, utf16_cu_idx: usize) -> usize;

    fn char_to_utf16_cu(&self, idx: usize) -> usize;

    /// Get a line from the text
    fn line(&self, line_idx: usize) -> Option<Line<'_>>;

    /// Total of lines
    fn len_lines(&self) -> usize;

    /// Total of chars
    fn len_chars(&self) -> usize;

    /// Total of utf16 code units
    fn len_utf16_cu(&self) -> usize;

    /// Get a readable cursor
    fn cursor(&self) -> &TextCursor;

    /// Get a mutable cursor
    fn cursor_mut(&mut self) -> &mut TextCursor;

    /// Get the cursor row
    fn cursor_row(&self) -> usize {
        let pos = self.cursor_pos();
        let pos_utf8 = self.utf16_cu_to_char(pos);
        self.char_to_line(pos_utf8)
    }

    /// Get the cursor column
    fn cursor_col(&self) -> usize {
        let pos = self.cursor_pos();
        let pos_utf8 = self.utf16_cu_to_char(pos);
        let line = self.char_to_line(pos_utf8);
        let line_char_utf8 = self.line_to_char(line);
        let line_char = self.char_to_utf16_cu(line_char_utf8);
        pos - line_char
    }

    /// Get the cursor row and col
    fn cursor_row_and_col(&self) -> (usize, usize) {
        (self.cursor_row(), self.cursor_col())
    }

    /// Move the cursor 1 line down
    fn cursor_down(&mut self) -> bool {
        let old_row = self.cursor_row();
        let old_col = self.cursor_col();

        match old_row.cmp(&(self.len_lines() - 1)) {
            Ordering::Less => {
                // One line below
                let new_row = old_row + 1;
                let new_row_char = self.char_to_utf16_cu(self.line_to_char(new_row));
                let new_row_len = self.line(new_row).unwrap().utf16_len();
                let new_col = old_col.min(new_row_len);
                self.cursor_mut().set(new_row_char + new_col);

                true
            }
            Ordering::Equal => {
                let end = self.len_utf16_cu();
                // Reached max
                self.cursor_mut().set(end);

                true
            }
            Ordering::Greater => {
                // Can't go further

                false
            }
        }
    }

    /// Move the cursor 1 line up
    fn cursor_up(&mut self) -> bool {
        let pos = self.cursor_pos();
        let old_row = self.cursor_row();
        let old_col = self.cursor_col();

        if pos > 0 {
            // Reached max
            if old_row == 0 {
                self.cursor_mut().set(0);
            } else {
                let new_row = old_row - 1;
                let new_row_char = self.line_to_char(new_row);
                let new_row_len = self.line(new_row).unwrap().utf16_len();
                let new_col = old_col.min(new_row_len);
                self.cursor_mut().set(new_row_char + new_col);
            }

            true
        } else {
            false
        }
    }

    /// Move the cursor 1 char to the right
    fn cursor_right(&mut self) -> bool {
        if self.cursor_pos() < self.len_utf16_cu() {
            *self.cursor_mut().write() += 1;

            true
        } else {
            false
        }
    }

    /// Move the cursor 1 char to the left
    fn cursor_left(&mut self) -> bool {
        if self.cursor_pos() > 0 {
            *self.cursor_mut().write() -= 1;

            true
        } else {
            false
        }
    }

    /// Get the cursor position
    fn cursor_pos(&self) -> usize {
        self.cursor().pos()
    }

    /// Set the cursor position
    fn set_cursor_pos(&mut self, pos: usize) {
        self.cursor_mut().set(pos);
    }

    // Check if has any selection at all
    fn has_any_selection(&self) -> bool;

    // Return the selected text
    fn get_selection(&self) -> Option<(usize, usize)>;

    // Return the visible selected text from a given editor Id
    fn get_visible_selection(&self, editor_id: usize) -> Option<(usize, usize)>;

    // Remove the selection
    fn clear_selection(&mut self);

    // Select some text
    fn set_selection(&mut self, selected: (usize, usize));

    // Measure a new text selection
    fn measure_new_selection(&self, from: usize, to: usize, editor_id: usize) -> (usize, usize);

    // Measure a new cursor
    fn measure_new_cursor(&self, to: usize, editor_id: usize) -> TextCursor;

    // Update the selection with a new cursor
    fn expand_selection_to_cursor(&mut self);

    fn get_clipboard(&mut self) -> &mut UseClipboard;

    // Process a Keyboard event
    fn process_key(&mut self, key: &Key, code: &Code, modifiers: &Modifiers) -> TextEvent {
        let mut event = if self.has_any_selection() {
            TextEvent::SELECTION_CHANGED
        } else {
            TextEvent::empty()
        };

        match key {
            Key::Shift => {
                event.remove(TextEvent::SELECTION_CHANGED);
            }
            Key::Control => {
                event.remove(TextEvent::SELECTION_CHANGED);
            }
            Key::Alt => {
                event.remove(TextEvent::SELECTION_CHANGED);
            }
            Key::Escape => {
                event.insert(TextEvent::SELECTION_CHANGED);
            }
            Key::ArrowDown => {
                if modifiers.contains(Modifiers::SHIFT) {
                    event.remove(TextEvent::SELECTION_CHANGED);
                    self.expand_selection_to_cursor();
                }

                if self.cursor_down() {
                    event.insert(TextEvent::CURSOR_CHANGED);
                }

                if modifiers.contains(Modifiers::SHIFT) {
                    self.expand_selection_to_cursor();
                }
            }
            Key::ArrowLeft => {
                if modifiers.contains(Modifiers::SHIFT) {
                    event.remove(TextEvent::SELECTION_CHANGED);
                    self.expand_selection_to_cursor();
                }

                if self.cursor_left() {
                    event.insert(TextEvent::CURSOR_CHANGED);
                }

                if modifiers.contains(Modifiers::SHIFT) {
                    self.expand_selection_to_cursor();
                }
            }
            Key::ArrowRight => {
                if modifiers.contains(Modifiers::SHIFT) {
                    event.remove(TextEvent::SELECTION_CHANGED);
                    self.expand_selection_to_cursor();
                }

                if self.cursor_right() {
                    event.insert(TextEvent::CURSOR_CHANGED);
                }

                if modifiers.contains(Modifiers::SHIFT) {
                    self.expand_selection_to_cursor();
                }
            }
            Key::ArrowUp => {
                if modifiers.contains(Modifiers::SHIFT) {
                    event.remove(TextEvent::SELECTION_CHANGED);
                    self.expand_selection_to_cursor();
                }

                if self.cursor_up() {
                    event.insert(TextEvent::CURSOR_CHANGED);
                }

                if modifiers.contains(Modifiers::SHIFT) {
                    self.expand_selection_to_cursor();
                }
            }
            Key::Backspace => {
                let cursor_pos = self.cursor_pos();
                let selection = self.get_selection_range();

                if let Some((start, end)) = selection {
                    self.remove(start..end);
                    self.set_cursor_pos(start);
                    event.insert(TextEvent::TEXT_CHANGED);
                } else if cursor_pos > 0 {
                    // Remove the character to the left if there is any
                    let removed_text_len = self.remove(cursor_pos - 1..cursor_pos);
                    self.set_cursor_pos(cursor_pos - removed_text_len);
                    event.insert(TextEvent::TEXT_CHANGED);
                }
            }
            Key::Delete => {
                let cursor_pos = self.cursor_pos();
                let selection = self.get_selection_range();

                if let Some((start, end)) = selection {
                    self.remove(start..end);
                    self.set_cursor_pos(start);
                    event.insert(TextEvent::TEXT_CHANGED);
                } else if cursor_pos < self.len_utf16_cu() {
                    // Remove the character to the right if there is any
                    self.remove(cursor_pos..cursor_pos + 1);
                    event.insert(TextEvent::TEXT_CHANGED);
                }
            }
            Key::Enter => {
                // Breaks the line
                let cursor_pos = self.cursor_pos();
                self.insert_char('\n', cursor_pos);
                self.cursor_right();

                event.insert(TextEvent::TEXT_CHANGED);
            }
            Key::Tab => {
                // Inserts a tab
                let text = " ".repeat(self.get_identation().into());
                let cursor_pos = self.cursor_pos();
                self.insert(&text, cursor_pos);
                self.set_cursor_pos(cursor_pos + text.chars().count());

                event.insert(TextEvent::TEXT_CHANGED);
            }
            Key::Character(character) => {
                let meta_or_ctrl = if cfg!(target_os = "macos") {
                    modifiers.meta()
                } else {
                    modifiers.ctrl()
                };

                match code {
                    Code::Delete => {}
                    Code::Space => {
                        // Simply adds an space
                        let cursor_pos = self.cursor_pos();
                        self.insert_char(' ', cursor_pos);
                        self.cursor_right();

                        event.insert(TextEvent::TEXT_CHANGED);
                    }

                    // Select all text
                    Code::KeyA if meta_or_ctrl => {
                        let len = self.len_utf16_cu();
                        self.set_selection((0, len));
                        event.remove(TextEvent::SELECTION_CHANGED);
                    }

                    // Copy selected text
                    Code::KeyC if meta_or_ctrl => {
                        let selected = self.get_selected_text();
                        if let Some(selected) = selected {
                            self.get_clipboard().set(selected).ok();
                        }
                        event.remove(TextEvent::SELECTION_CHANGED);
                    }

                    // Cut selected text
                    Code::KeyX if meta_or_ctrl => {
                        let selection = self.get_selection_range();
                        if let Some((start, end)) = selection {
                            let text = self.get_selected_text().unwrap();
                            self.remove(start..end);
                            self.get_clipboard().set(text).ok();
                            self.set_cursor_pos(start);
                            event.insert(TextEvent::TEXT_CHANGED);
                        }
                    }

                    // Paste copied text
                    Code::KeyV if meta_or_ctrl => {
                        let copied_text = self.get_clipboard().get();
                        if let Ok(copied_text) = copied_text {
                            let cursor_pos = self.cursor_pos();
                            self.insert(&copied_text, cursor_pos);
                            let last_idx = copied_text.encode_utf16().count() + cursor_pos;
                            self.set_cursor_pos(last_idx);
                            event.insert(TextEvent::TEXT_CHANGED);
                        }
                    }

                    // Undo last change
                    Code::KeyZ if meta_or_ctrl => {
                        let undo_result = self.undo();

                        if let Some(idx) = undo_result {
                            self.set_cursor_pos(idx);
                            event.insert(TextEvent::TEXT_CHANGED);
                        }
                    }

                    // Redo last change
                    Code::KeyY if meta_or_ctrl => {
                        let redo_result = self.redo();

                        if let Some(idx) = redo_result {
                            self.set_cursor_pos(idx);
                            event.insert(TextEvent::TEXT_CHANGED);
                        }
                    }

                    _ => {
                        // Remove selected text
                        let selection = self.get_selection_range();
                        if let Some((start, end)) = selection {
                            self.remove(start..end);
                            self.set_cursor_pos(start);
                            event.insert(TextEvent::TEXT_CHANGED);
                        }

                        if let Ok(ch) = character.parse::<char>() {
                            // Inserts a character
                            let cursor_pos = self.cursor_pos();
                            let inserted_text_len = self.insert_char(ch, cursor_pos);
                            self.set_cursor_pos(cursor_pos + inserted_text_len);

                            event.insert(TextEvent::TEXT_CHANGED);
                        } else {
                            // Inserts a text
                            let cursor_pos = self.cursor_pos();
                            let inserted_text_len = self.insert(character, cursor_pos);
                            self.set_cursor_pos(cursor_pos + inserted_text_len);

                            event.insert(TextEvent::TEXT_CHANGED);
                        }
                    }
                }
            }
            _ => {}
        }

        if event.contains(TextEvent::SELECTION_CHANGED) {
            self.clear_selection();
        }

        event
    }

    fn get_selected_text(&self) -> Option<String>;

    fn undo(&mut self) -> Option<usize>;

    fn redo(&mut self) -> Option<usize>;

    fn get_selection_range(&self) -> Option<(usize, usize)>;

    fn get_identation(&self) -> u8;
}