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
|
use iced::event::{self, Event};
use iced::mouse;
use iced::widget::{
column, container, horizontal_space, row, scrollable, text, vertical_space,
};
use iced::window;
use iced::{
Center, Color, Element, Fill, Font, Point, Rectangle, Subscription, Task,
Theme,
};
pub fn main() -> iced::Result {
iced::application("Visible Bounds - Iced", Example::update, Example::view)
.subscription(Example::subscription)
.theme(|_| Theme::Dark)
.run()
}
#[derive(Default)]
struct Example {
mouse_position: Option<Point>,
outer_bounds: Option<Rectangle>,
inner_bounds: Option<Rectangle>,
}
#[derive(Debug, Clone, Copy)]
enum Message {
MouseMoved(Point),
WindowResized,
Scrolled,
OuterBoundsFetched(Option<Rectangle>),
InnerBoundsFetched(Option<Rectangle>),
}
impl Example {
fn update(&mut self, message: Message) -> Task<Message> {
match message {
Message::MouseMoved(position) => {
self.mouse_position = Some(position);
Task::none()
}
Message::Scrolled | Message::WindowResized => Task::batch(vec![
container::visible_bounds(OUTER_CONTAINER.clone())
.map(Message::OuterBoundsFetched),
container::visible_bounds(INNER_CONTAINER.clone())
.map(Message::InnerBoundsFetched),
]),
Message::OuterBoundsFetched(outer_bounds) => {
self.outer_bounds = outer_bounds;
Task::none()
}
Message::InnerBoundsFetched(inner_bounds) => {
self.inner_bounds = inner_bounds;
Task::none()
}
}
}
fn view(&self) -> Element<Message> {
let data_row = |label, value, color| {
row![
text(label),
horizontal_space(),
text(value)
.font(Font::MONOSPACE)
.size(14)
.color_maybe(color),
]
.height(40)
.align_y(Center)
};
let view_bounds = |label, bounds: Option<Rectangle>| {
data_row(
label,
match bounds {
Some(bounds) => format!("{bounds:?}"),
None => "not visible".to_string(),
},
if bounds
.zip(self.mouse_position)
.map(|(bounds, mouse_position)| {
bounds.contains(mouse_position)
})
.unwrap_or_default()
{
Some(Color {
g: 1.0,
..Color::BLACK
})
} else {
None
},
)
};
column![
data_row(
"Mouse position",
match self.mouse_position {
Some(Point { x, y }) => format!("({x}, {y})"),
None => "unknown".to_string(),
},
None,
),
view_bounds("Outer container", self.outer_bounds),
view_bounds("Inner container", self.inner_bounds),
scrollable(
column![
text("Scroll me!"),
vertical_space().height(400),
container(text("I am the outer container!"))
.id(OUTER_CONTAINER.clone())
.padding(40)
.style(container::rounded_box),
vertical_space().height(400),
scrollable(
column![
text("Scroll me!"),
vertical_space().height(400),
container(text("I am the inner container!"))
.id(INNER_CONTAINER.clone())
.padding(40)
.style(container::rounded_box),
vertical_space().height(400),
]
.padding(20)
)
.on_scroll(|_| Message::Scrolled)
.width(Fill)
.height(300),
]
.padding(20)
)
.on_scroll(|_| Message::Scrolled)
.width(Fill)
.height(300),
]
.spacing(10)
.padding(20)
.into()
}
fn subscription(&self) -> Subscription<Message> {
event::listen_with(|event, _status, _window| match event {
Event::Mouse(mouse::Event::CursorMoved { position }) => {
Some(Message::MouseMoved(position))
}
Event::Window(window::Event::Resized { .. }) => {
Some(Message::WindowResized)
}
_ => None,
})
}
}
use once_cell::sync::Lazy;
static OUTER_CONTAINER: Lazy<container::Id> =
Lazy::new(|| container::Id::new("outer"));
static INNER_CONTAINER: Lazy<container::Id> =
Lazy::new(|| container::Id::new("inner"));
|