appcui 0.4.8

A feature-rich and cross-platform TUI/CUI framework for Rust, enabling modern terminal-based applications on Windows, Linux, and macOS. Includes built-in UI components like buttons, menus, list views, tree views, checkboxes, and more. Perfect for building fast and interactive CLI tools and text-based interfaces.
Documentation
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
pub(crate) mod linkregistry;
pub(crate) mod parser;
use self::components::ScrollBars;
use crate::prelude::*;
use crate::system::Theme;
use crate::ui::markdown::initialization_flags::Flags;
use linkregistry::LinkRegistry;
use parser::{InlineElement, MarkdownElement, MarkdownParser, Table};
use std::cell::RefCell;

use super::events::EventData;

#[CustomControl(overwrite=OnPaint+OnResize+OnMouseEvent+OnKeyPressed, internal=true)]
pub struct Markdown {
    w: u32,
    h: u32,
    x: i32,
    y: i32,
    background: Option<Character>,
    flags: Flags,
    drag_point: Option<Point>,
    scrollbars: ScrollBars,
    link_registry: RefCell<LinkRegistry>,
    elements: Vec<MarkdownElement>,
}

impl Markdown {
    /// Creates a new markdown component with a specified content, layout, and flags.
    /// This method initializes a markdown rendering control with the provided parameters.
    ///
    /// # Parameters
    /// - `content`: The markdown-formatted string to be displayed.
    /// - `layout`: The layout configuration for positioning and sizing.
    /// - `flags`: Initialization flags that define specific behaviors (e.g., enabling scrollbars).
    ///
    /// # Example
    /// ```rust,no_run
    /// use appcui::prelude::*;
    /// let m = Markdown::new("< a markdown text >", layout!("a: c"), markdown::Flags::ScrollBars);
    /// ```
    pub fn new(content: &str, layout: Layout, flags: Flags) -> Self {
        let (width, height) = Self::compute_dimension(content);
        Self {
            base: ControlBase::with_status_flags(
                layout,
                (StatusFlags::Visible | StatusFlags::Enabled | StatusFlags::AcceptInput)
                    | if flags == Flags::ScrollBars {
                        StatusFlags::IncreaseBottomMarginOnFocus | StatusFlags::IncreaseRightMarginOnFocus
                    } else {
                        StatusFlags::None
                    },
            ),
            w: width,
            h: height,
            x: 0,
            y: 0,
            flags,
            background: None,
            drag_point: None,
            scrollbars: ScrollBars::new(flags == Flags::ScrollBars),
            link_registry: RefCell::new(LinkRegistry::new()),
            elements: MarkdownParser::parse(content),
        }
    }

    /// Sets new content in the markdown component.
    /// This method resets the scroll position, reparses the content,
    /// and adjusts the surface and scrollbars accordingly.
    ///
    /// # Parameters
    /// - `content`: The new markdown content to be set.
    pub fn set_content(&mut self, content: &str) {
        self.x = 0;
        self.y = 0;
        self.elements = MarkdownParser::parse(content);
        self.link_registry.replace(LinkRegistry::new());

        (self.w, self.h) = Self::compute_dimension(content);

        self.scrollbars.resize(self.w as u64, self.h as u64, &self.base);
        self.move_scroll_to(self.x, self.y);
    }

    fn compute_dimension(content: &str) -> (u32, u32) {
        let lines: Vec<&str> = content.lines().collect();
        let height = lines.len() as u32;
        let width = lines.iter().map(|line| line.len()).max().unwrap_or(0) as u32;

        (width, height)
    }

    fn move_scroll_to(&mut self, x: i32, y: i32) {
        let sz = self.size();
        self.x = if self.w <= sz.width {
            0
        } else {
            x.max((sz.width as i32) - (self.w as i32))
        };
        self.y = if self.h <= sz.height {
            0
        } else {
            y.max((sz.height as i32) - (self.h as i32))
        };
        self.x = self.x.min(0);
        self.y = self.y.min(0);
        self.scrollbars.set_indexes((-self.x) as u64, (-self.y) as u64);
    }

    fn update_scroll_pos_from_scrollbars(&mut self) {
        let h = -(self.scrollbars.horizontal_index() as i32);
        let v = -(self.scrollbars.vertical_index() as i32);
        self.move_scroll_to(h, v);
    }

    fn get_element_style(element: &InlineElement, theme: &Theme, hovered: bool) -> CharAttribute {
        match element {
            InlineElement::Text(_) => theme.markdown.text,
            InlineElement::Bold(_) => theme.markdown.bold,
            InlineElement::Italic(_) => theme.markdown.italic,
            InlineElement::Link(_, _) => {
                if !hovered {
                    theme.markdown.link
                } else {
                    theme.text.highlighted
                }
            }
            InlineElement::Code(_) => theme.markdown.code,
        }
    }

    fn register_if_link(link_registry: &mut LinkRegistry, element: &InlineElement, x: i32, y: i32) -> Option<String> {
        if let InlineElement::Link(display_str, link) = element {
            let link_str = if link.starts_with("#") {
                link.trim_start_matches("#").to_string()
            } else {
                link.to_string()
            };

            let link_width = display_str.chars().count() as i32;
            link_registry.register_link_position(&link_str, x, y, link_width, !link.starts_with("#"));

            return Some(link_str);
        }
        None
    }

    fn process_list_element(
        elements: &[InlineElement],
        indent: i32,
        p: &mut Point,
        xlsurface: &mut Surface,
        prefix: Option<String>,
        link_registry: &mut LinkRegistry,
        theme: &Theme,
        inactive: bool,
    ) {                
        for (i, element) in elements.iter().enumerate() {
            if i == 0 {
                p.x = indent;
            }

            let link_identifier = Self::register_if_link(link_registry, element, p.x, p.y);
            let is_hovered = if let Some(ref id) = link_identifier {
                link_registry.is_hovered(id)
            } else {
                false
            };
            let style = Self::get_element_style(element, theme, is_hovered);
            let attr = if !inactive { style } else { theme.text.inactive };

            let content_str = element.to_string();
            let formatted_content = if i == 0 {
                if let Some(ref prefix) = prefix {
                    format!("{prefix} {content_str}")
                } else {
                    format!("â—‹ {content_str}")
                }
            } else {
                content_str
            };

            xlsurface.write_string(p.x, p.y, &Self::replace_tabs(&formatted_content), attr, false);

            p.x += formatted_content.chars().count() as i32;
        }
    }

    fn process_nested_list(
        depth: u8,
        nested_items: &MarkdownElement,
        p: &mut Point,
        // x_pos: &mut i32,
        // y_pos: &mut i32,
        xlsurface: &mut Surface,
        link_registry: &mut LinkRegistry,
        theme: &Theme,
        inactive: bool,
    ) {
        let indent = p.x + (depth as i32) * 4;

        match *nested_items {
            MarkdownElement::UnorderedList(ref items) => {
                for item in items.iter() {
                    match item {
                        parser::ListItem::Simple(ref elements) => {
                            //let mut x = p.x;
                            Self::process_list_element(elements, indent, p, xlsurface, None, link_registry, theme, inactive);
                            p.y += 1;
                        }
                        parser::ListItem::Nested(ref nested) => {
                            Self::process_nested_list(depth + 1, nested, p, xlsurface, link_registry, theme, inactive);
                        }
                    }
                }
            }
            MarkdownElement::OrderedList(ref items) => {
                let mut index = 1;
                for item in items.iter() {
                    match item {
                        parser::ListItem::Simple(ref elements) => {
                            //let mut x = p.x;
                            Self::process_list_element(
                                elements,
                                indent,
                                p,
                                xlsurface,
                                Some(format!("{index}.")),
                                link_registry,
                                theme,
                                inactive,
                            );
                            index += 1;
                            p.y += 1;
                        }
                        parser::ListItem::Nested(ref nested) => {
                            Self::process_nested_list(depth + 1, nested, p, xlsurface, link_registry, theme, inactive);
                        }
                    }
                }
            }
            _ => {}
        }
    }

    fn replace_tabs(string_to_print: &str) -> String {
        string_to_print.replace("\t", "    ")
    }

    fn paint_codeblock(&self, code: &str, y_pos: &mut i32, surface: &mut Surface, theme: &Theme, left_padding: Option<i32>) {
        let left_padding = left_padding.unwrap_or(4);
        let content = Self::replace_tabs(code);
        let code_lines: Vec<&str> = content.lines().collect();
        let max_width = code_lines.iter().map(|line| line.len()).max().unwrap_or(0);

        let attr = if self.is_enabled() {
            theme.markdown.code_block
        } else {
            theme.text.inactive
        };

        for line in code_lines {
            let formatted_line = format!(" {line:max_width$} ");
            surface.write_string(self.x + left_padding - 1, *y_pos, &Self::replace_tabs(&formatted_line), attr, false);
            *y_pos += 1;
        }
    }

    fn paint_header(&self, content: &str, y_pos: i32, level: &usize, surface: &mut Surface, theme: &Theme) {
        let content = Self::replace_tabs(content);
        let header_style = match level {
            1 => theme.markdown.h1,
            2 => theme.markdown.h2,
            _ => theme.markdown.h3,
        };

        let attr = if self.is_enabled() { header_style } else { theme.text.inactive };

        self.link_registry.borrow_mut().register_header_position(&content, y_pos);
        surface.write_string(self.x, y_pos, &Self::replace_tabs(&content), attr, false);
    }

    fn paint_table(&self, table: &Table, y_pos: &mut i32, surface: &mut Surface, theme: &Theme) {
        let (attr, attr_header) = if self.is_enabled() {
            (theme.markdown.table, theme.markdown.table_header)
        } else {
            (theme.text.inactive, theme.text.inactive)
        };

        fn compute_column_widths(table: &Table) -> Vec<usize> {
            let mut column_widths = Vec::new();

            for (i, header) in table.headers.iter().enumerate() {
                let header_len = header.iter().map(|e| e.to_string().chars().count()).sum::<usize>();
                if column_widths.len() <= i {
                    column_widths.push(header_len);
                } else {
                    column_widths[i] = column_widths[i].max(header_len);
                }
            }

            for row in &table.rows {
                for (i, cell) in row.iter().enumerate() {
                    let cell_len = cell.iter().map(|e| e.to_string().chars().count()).sum::<usize>();
                    if column_widths.len() <= i {
                        column_widths.push(cell_len);
                    } else {
                        column_widths[i] = column_widths[i].max(cell_len);
                    }
                }
            }

            column_widths
        }

        let lines_count = table.rows.len() + 2; // the header and the separator
        let column_widths = compute_column_widths(table);

        let table_width: usize = column_widths.iter().sum();
        let suplimentar_padding: usize = column_widths.len() * 3;

        // draw contour
        let mut x_pos = self.x;
        let rect = Rect::new(
            x_pos,
            *y_pos,
            x_pos + (table_width + suplimentar_padding) as i32,
            *y_pos + 1 + lines_count as i32,
        );
        surface.draw_rect(rect, LineType::Single, attr);

        // draw horizontal line
        x_pos += 1;
        *y_pos += 2;
        surface.draw_horizontal_line(
            self.x + 1,
            *y_pos,
            self.x + (table_width + suplimentar_padding) as i32 - 1,
            LineType::Single,
            attr,
        );
        *y_pos -= 1;

        // write headers
        for (i, header) in table.headers.iter().enumerate() {
            let header_str = header.iter().map(|e| e.to_string()).collect::<String>();
            let padded_header = format!("{:width$}", header_str, width = column_widths[i] + 2);
            let content = Self::replace_tabs(&padded_header);
            surface.write_string(x_pos, *y_pos, &content, attr_header, false);
            x_pos += column_widths[i] as i32 + 3;

            // draw vertical line
            surface.draw_vertical_line(x_pos - 1, *y_pos, *y_pos - 1 + lines_count as i32, LineType::Single, attr);
        }
        *y_pos += 2;

        for (row_index, row) in table.rows.iter().enumerate() {
            x_pos = self.x + 1;
            for (i, cell) in row.iter().enumerate() {
                let cell_str = cell.iter().map(|e| e.to_string()).collect::<String>();
                let padded_cell = format!("{:width$}", cell_str, width = column_widths[i] + 2);
                let content = Self::replace_tabs(&padded_cell);
                surface.write_string(x_pos, *y_pos, &content, attr, false);
                x_pos += column_widths[i] as i32 + 3;
                if row_index == 0 && i < (row.len() - 1) {
                    // cross separators
                    surface.write_char(x_pos - 1, *y_pos - 1, Character::with_attributes(SpecialChar::BoxCrossSingleLine, attr));

                    // horizontal separators
                    surface.write_char(rect.left(), *y_pos - 1, Character::with_attributes(SpecialChar::BoxMidleLeft, attr));
                    surface.write_char(rect.right(), *y_pos - 1, Character::with_attributes(SpecialChar::BoxMidleRight, attr));

                    // vertical separators
                    surface.write_char(x_pos - 1, rect.top(), Character::with_attributes(SpecialChar::BoxMidleTop, attr));
                    surface.write_char(x_pos - 1, rect.bottom(), Character::with_attributes(SpecialChar::BoxMidleBottom, attr));
                }
            }
            *y_pos += 1;
        }
    }

    fn paint_paragraph(&self, content: &[InlineElement], y_pos: i32, surface: &mut Surface, theme: &Theme) {
        let mut x_pos: i32 = self.x;

        for element in content.iter() {
            let link_identifier = {
                let mut registry = self.link_registry.borrow_mut();
                Self::register_if_link(&mut registry, element, x_pos, y_pos)
            };

            let is_hovered = if let Some(ref id) = link_identifier {
                self.link_registry.borrow().is_hovered(id)
            } else {
                false
            };

            let style = Self::get_element_style(element, theme, is_hovered);
            let attr = if self.is_enabled() { style } else { theme.text.inactive };
            let content_str = element.to_string();

            surface.write_string(x_pos, y_pos, &Self::replace_tabs(&content_str), attr, false);
            x_pos += content_str.chars().count() as i32;
        }
    }

    fn paint_unordered_list(&self, items: &[parser::ListItem], mut y_pos: i32, surface: &mut Surface, theme: &Theme) -> i32 {
        for item in items.iter() {
            let mut x_pos: i32;

            let elements = match item {
                parser::ListItem::Simple(elements) => {
                    x_pos = self.x + 4;
                    elements
                }
                parser::ListItem::Nested(items) => {
                    let mut p = Point::new(self.x + 4, y_pos);
                    Self::process_nested_list(1, items, &mut p, surface, &mut self.link_registry.borrow_mut(), theme, !self.is_enabled());
                    //x_pos = p.x;
                    y_pos = p.y;
                    continue;
                }
            };

            for (i, element) in elements.iter().enumerate() {
                let link_identifier = {
                    let mut registry = self.link_registry.borrow_mut();
                    Self::register_if_link(&mut registry, element, x_pos, y_pos)
                };
                let is_hovered = if let Some(ref id) = link_identifier {
                    self.link_registry.borrow().is_hovered(id)
                } else {
                    false
                };
                let style = Self::get_element_style(element, theme, is_hovered);
                let attr = if self.is_enabled() { style } else { theme.text.inactive };
                let content_str = element.to_string();

                let formatted_content = if i == 0 {
                    let prefix = "•";
                    format!("{prefix} {content_str}").to_string()
                } else {
                    content_str
                };

                surface.write_string(x_pos, y_pos, &Self::replace_tabs(&formatted_content), attr, false);

                x_pos += formatted_content.chars().count() as i32;
            }

            y_pos += 1;
        }
        y_pos
    }

    fn send_link(&self, link: &str) {
        self.raise_event(ControlEvent {
            emitter: self.handle,
            receiver: self.event_processor,
            data: ControlEventData::Markdown(EventData {
                event_type: markdown::events::Data::LinkClickEvent(link.to_string()),
            }),
        });
    }

    fn send_back_navigation_command(&self) {
        self.raise_event(ControlEvent {
            emitter: self.handle,
            receiver: self.event_processor,
            data: ControlEventData::Markdown(EventData {
                event_type: markdown::events::Data::BackEvent,
            }),
        });
    }

    fn paint_ordered_list(&self, items: &[parser::ListItem], mut y_pos: i32, surface: &mut Surface, theme: &Theme) -> i32 {
        let mut index = 1;
        for item in items.iter() {
            let mut x_pos: i32 = self.x + 4;

            let elements = match item {
                parser::ListItem::Simple(elements) => elements,
                parser::ListItem::Nested(items) => {
                    let mut p = Point::new(x_pos, y_pos);
                    Self::process_nested_list(1, items, &mut p, surface, &mut self.link_registry.borrow_mut(), theme, !self.is_enabled());
                    //x_pos = p.x;
                    y_pos = p.y;
                    continue;
                }
            };

            for (i, element) in elements.iter().enumerate() {
                let link_identifier = {
                    let mut registry = self.link_registry.borrow_mut();
                    Self::register_if_link(&mut registry, element, x_pos, y_pos)
                };
                let is_hovered = if let Some(ref id) = link_identifier {
                    self.link_registry.borrow().is_hovered(id)
                } else {
                    false
                };
                let style = Self::get_element_style(element, theme, is_hovered);
                let attr = if self.is_enabled() { style } else { theme.text.inactive };
                let content_str = element.to_string();

                let formatted_content = if i == 0 {
                    let prefix = index;
                    index += 1;
                    format!("{prefix}. {content_str}").to_string()
                } else {
                    content_str
                };

                surface.write_string(x_pos, y_pos, &Self::replace_tabs(&formatted_content), attr, false);

                x_pos += formatted_content.chars().count() as i32;
            }

            y_pos += 1;
        }
        y_pos
    }
}

impl OnPaint for Markdown {
    fn on_paint(&self, surface: &mut Surface, theme: &Theme) {
        if (self.has_focus()) && (self.flags == Flags::ScrollBars) {
            self.scrollbars.paint(surface, theme, self);
            surface.reduce_clip_by(0, 0, 1, 1);
        }
        if let Some(back) = self.background {
            surface.clear(back);
        }

        // Inititialize vertical offset.
        let mut y_pos = self.y;

        for element in &self.elements {
            match element {
                MarkdownElement::Header(content, level) => self.paint_header(content, y_pos, level, surface, theme),
                MarkdownElement::Paragraph(content) => self.paint_paragraph(content, y_pos, surface, theme),
                MarkdownElement::UnorderedList(items) => y_pos = self.paint_unordered_list(items, y_pos, surface, theme),
                MarkdownElement::OrderedList(items) => y_pos = self.paint_ordered_list(items, y_pos, surface, theme),
                MarkdownElement::HorizontalRule => {
                    surface.draw_horizontal_line(self.x, y_pos, surface.size().width as i32, LineType::Single, theme.markdown.text);
                    y_pos += 1;
                }
                MarkdownElement::CodeBlock(code) => self.paint_codeblock(code, &mut y_pos, surface, theme, None),
                MarkdownElement::Table(table) => self.paint_table(table, &mut y_pos, surface, theme),
            }
            y_pos += 1;
        }
    }
}

impl OnResize for Markdown {
    fn on_resize(&mut self, _old_size: Size, _new_size: Size) {
        self.scrollbars.resize(self.w as u64, self.h as u64, &self.base);
        self.move_scroll_to(self.x, self.y);
    }
}

impl OnKeyPressed for Markdown {
    fn on_key_pressed(&mut self, key: Key, _character: char) -> EventProcessStatus {
        match key.value() {
            key!("Left") => {
                self.move_scroll_to(self.x + 1, self.y);
                EventProcessStatus::Processed
            }
            key!("Right") => {
                self.move_scroll_to(self.x - 1, self.y);
                EventProcessStatus::Processed
            }
            key!("Up") => {
                self.move_scroll_to(self.x, self.y + 1);
                EventProcessStatus::Processed
            }
            key!("Down") => {
                self.move_scroll_to(self.x, self.y - 1);
                EventProcessStatus::Processed
            }
            key!("Backspace") => {
                self.send_back_navigation_command();
                EventProcessStatus::Processed
            }
            _ => EventProcessStatus::Ignored,
        }
    }
}

impl OnMouseEvent for Markdown {
    fn on_mouse_event(&mut self, event: &MouseEvent) -> EventProcessStatus {
        if self.scrollbars.process_mouse_event(event) {
            self.update_scroll_pos_from_scrollbars();
            return EventProcessStatus::Processed;
        }
        match event {
            MouseEvent::Over(data) => {
                let mut tmp = None;
                if let Some(link_header_id) = self.link_registry.borrow().check_for_link_at_position(data.x, data.y) {
                    tmp = Some(link_header_id);
                }

                if let Some(link_header_id) = tmp {
                    self.link_registry.borrow_mut().set_link_hovered(&link_header_id);
                } else {
                    self.link_registry.borrow_mut().clear_hovered();
                }
                EventProcessStatus::Processed
            }
            MouseEvent::Pressed(data) => {
                self.drag_point = Some(Point::new(data.x, data.y));
                EventProcessStatus::Processed
            }
            MouseEvent::Released(data) => {
                let mut y_header: Option<i32> = None;

                if let Some(link_id) = self.link_registry.borrow().check_for_link_at_position(data.x, data.y) {
                    if let Some(is_external) = self.link_registry.borrow().is_link_external(&link_id) {
                        if is_external {
                            self.send_link(&link_id);
                            return EventProcessStatus::Processed;
                        } else if let Some(header_position) = self.link_registry.borrow().get_header_position(&link_id) {
                            y_header = Some(header_position);
                        }
                    }
                }

                if let Some(header_position) = y_header {
                    self.move_scroll_to(0, self.y - header_position);
                    return EventProcessStatus::Processed;
                }

                if let Some(p) = self.drag_point {
                    self.move_scroll_to(self.x + data.x - p.x, self.y + data.y - p.y);
                    self.drag_point = None;
                    return EventProcessStatus::Processed;
                }
                EventProcessStatus::Processed
            }
            MouseEvent::Drag(data) => {
                if let Some(p) = self.drag_point {
                    self.move_scroll_to(self.x + data.x - p.x, self.y + data.y - p.y);
                }
                self.drag_point = Some(Point::new(data.x, data.y));
                EventProcessStatus::Processed
            }
            MouseEvent::Wheel(dir) => {
                match dir {
                    MouseWheelDirection::Up => self.move_scroll_to(self.x, self.y + 1),
                    MouseWheelDirection::Down => self.move_scroll_to(self.x, self.y - 1),
                    _ => {}
                };
                EventProcessStatus::Processed
            }
            _ => EventProcessStatus::Ignored,
        }
    }
}