iced-yoda/examples/visible_bounds/src/main.rs

166 lines
5.1 KiB
Rust
Raw Normal View History

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