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
|
use crate::{
column, input::mouse, Element, Event, Hasher, Layout, Node, Point,
Rectangle, Style, Widget,
};
pub use iced_core::scrollable::State;
use std::hash::Hash;
/// A scrollable [`Column`].
///
/// [`Column`]: ../column/struct.Column.html
pub type Scrollable<'a, Message, Renderer> =
iced_core::Scrollable<'a, Element<'a, Message, Renderer>>;
impl<'a, Message, Renderer> Widget<Message, Renderer>
for Scrollable<'a, Message, Renderer>
where
Renderer: self::Renderer + column::Renderer,
{
fn node(&self, renderer: &Renderer) -> Node {
let mut content = self.content.node(renderer);
{
let mut style = content.0.style();
style.flex_shrink = 0.0;
content.0.set_style(style);
}
let mut style = Style::default()
.width(self.content.width)
.max_width(self.content.max_width)
.height(self.height)
.max_height(self.max_height)
.align_self(self.align_self);
style.0.flex_direction = stretch::style::FlexDirection::Column;
Node::with_children(style, vec![content])
}
fn on_event(
&mut self,
event: Event,
layout: Layout<'_>,
cursor_position: Point,
messages: &mut Vec<Message>,
renderer: &Renderer,
) {
let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position);
let content = layout.children().next().unwrap();
let content_bounds = content.bounds();
if is_mouse_over {
match event {
Event::Mouse(mouse::Event::WheelScrolled {
delta_y, ..
}) => {
self.state.scroll(delta_y, bounds, content_bounds);
}
_ => {}
}
}
let cursor_position = if is_mouse_over {
Point::new(
cursor_position.x,
cursor_position.y
+ self.state.offset(bounds, content_bounds) as f32,
)
} else {
// TODO: Make `cursor_position` an `Option<Point>` so we can encode
// cursor unavailability.
// This will probably happen naturally once we add multi-window
// support.
Point::new(cursor_position.x, -1.0)
};
self.content.on_event(
event,
content,
cursor_position,
messages,
renderer,
)
}
fn draw(
&self,
renderer: &mut Renderer,
layout: Layout<'_>,
cursor_position: Point,
) -> Renderer::Output {
let bounds = layout.bounds();
let content_layout = layout.children().next().unwrap();
self::Renderer::draw(
renderer,
&self,
bounds,
content_layout,
cursor_position,
)
}
fn hash_layout(&self, state: &mut Hasher) {
std::any::TypeId::of::<Scrollable<'static, (), ()>>().hash(state);
self.height.hash(state);
self.max_height.hash(state);
self.align_self.hash(state);
self.content.hash_layout(state)
}
}
pub trait Renderer: crate::Renderer + Sized {
fn draw<Message>(
&mut self,
scrollable: &Scrollable<'_, Message, Self>,
bounds: Rectangle,
content_layout: Layout<'_>,
cursor_position: Point,
) -> Self::Output;
}
impl<'a, Message, Renderer> From<Scrollable<'a, Message, Renderer>>
for Element<'a, Message, Renderer>
where
Renderer: 'a + self::Renderer + column::Renderer,
Message: 'static,
{
fn from(
scrollable: Scrollable<'a, Message, Renderer>,
) -> Element<'a, Message, Renderer> {
Element::new(scrollable)
}
}
|