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
//! Custom drawing trait for widgets that want to render their own content.
use crateRenderContext;
/// Custom drawing trait for widgets that want to render their own content.
/// Widgets implementing this trait can provide custom drawing logic instead of
/// relying solely on native platform rendering.
/// Implements [`crate::widget::Widget::as_draw_mut`] for a widget that paints itself.
///
/// # Why a macro and not a blanket impl
///
/// `impl<T: Draw + Widget> Widget for T` would collide with every concrete
/// `impl Widget for Foo` in the crate (coherence forbids both), and a blanket
/// `impl<T: Draw> Draw for T` cannot reach the `Self: Widget + 'static` bound
/// needed to cast to `&mut dyn Any`. The remaining option is an explicit,
/// per-type opt-in — which is also the honest design: a widget saying "yes, I
/// paint myself" should be a deliberate, visible statement.
///
/// Every widget that has an `impl Draw for X` belongs in its `impl Widget for X`:
///
/// ```
/// use rust_widgets::core::Rect;
/// use rust_widgets::widget::special_widgets::code_editor::CodeEditor;
/// use rust_widgets::widget::{Draw, Widget};
///
/// let mut editor = CodeEditor::new(Rect::new(0, 0, 80, 40));
/// // `as_draw_mut` reaches the widget's `Draw` implementation.
/// assert!(editor.as_draw_mut().is_some());
/// ```