Highlight container bounds on hover in visible_bounds example

This commit is contained in:
Héctor Ramón Jiménez 2023-07-27 01:21:50 +02:00
parent 09f2887da5
commit 8961fcd501
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -2,9 +2,12 @@ use iced::executor;
use iced::mouse; use iced::mouse;
use iced::subscription::{self, Subscription}; use iced::subscription::{self, Subscription};
use iced::theme::{self, Theme}; use iced::theme::{self, Theme};
use iced::widget::{column, container, scrollable, text, vertical_space}; use iced::widget::{
column, container, horizontal_space, row, scrollable, text, vertical_space,
};
use iced::{ use iced::{
Application, Command, Element, Event, Length, Point, Rectangle, Settings, Alignment, Application, Color, Command, Element, Event, Font, Length,
Point, Rectangle, Settings,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -73,26 +76,52 @@ impl Application for Example {
} }
fn view(&self) -> Element<Message> { fn view(&self) -> Element<Message> {
let view_bounds = |label, bounds| { let data_row = |label, value, color| {
text(format!( row![
"The {label} container is {}", text(label),
horizontal_space(Length::Fill),
text(value).font(Font::MONOSPACE).size(14).style(color),
]
.height(40)
.align_items(Alignment::Center)
};
let view_bounds = |label, bounds: Option<Rectangle>| {
data_row(
label,
match bounds { match bounds {
Some(bounds) => format!("visible at {:?}", bounds), Some(bounds) => format!("{:?}", bounds),
None => "not visible".to_string(), None => "not visible".to_string(),
} },
)) if bounds
.zip(self.mouse_position)
.map(|(bounds, mouse_position)| {
bounds.contains(mouse_position)
})
.unwrap_or_default()
{
Color {
g: 1.0,
..Color::BLACK
}
.into()
} else {
theme::Text::Default
},
)
}; };
column![ column![
text(format!( data_row(
"Mouse position is {}", "Mouse position",
match self.mouse_position { match self.mouse_position {
Some(Point { x, y }) => format!("({x}, {y})"), Some(Point { x, y }) => format!("({x}, {y})"),
None => "unknown".to_string(), None => "unknown".to_string(),
} },
)), theme::Text::Default,
view_bounds("outer", self.outer_bounds), ),
view_bounds("inner", self.inner_bounds), view_bounds("Outer container", self.outer_bounds),
view_bounds("Inner container", self.inner_bounds),
scrollable( scrollable(
column![ column![
text("Scroll me!"), text("Scroll me!"),