summaryrefslogtreecommitdiffstats
path: root/examples/visible_bounds
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-07-27 01:21:50 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-07-27 01:21:50 +0200
commit8961fcd50179d76fa07edfeedee9edc1a0aa93ec (patch)
treee8b21957d8a56a47d1a656701e3ef9d6a332673f /examples/visible_bounds
parent09f2887da582ed7d39a56b2c0b37d0b24b6c1e57 (diff)
downloadiced-8961fcd50179d76fa07edfeedee9edc1a0aa93ec.tar.gz
iced-8961fcd50179d76fa07edfeedee9edc1a0aa93ec.tar.bz2
iced-8961fcd50179d76fa07edfeedee9edc1a0aa93ec.zip
Highlight container bounds on hover in `visible_bounds` example
Diffstat (limited to 'examples/visible_bounds')
-rw-r--r--examples/visible_bounds/src/main.rs57
1 files changed, 43 insertions, 14 deletions
diff --git a/examples/visible_bounds/src/main.rs b/examples/visible_bounds/src/main.rs
index bd7ccdc0..8bc645b7 100644
--- a/examples/visible_bounds/src/main.rs
+++ b/examples/visible_bounds/src/main.rs
@@ -2,9 +2,12 @@ use iced::executor;
use iced::mouse;
use iced::subscription::{self, Subscription};
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::{
- Application, Command, Element, Event, Length, Point, Rectangle, Settings,
+ Alignment, Application, Color, Command, Element, Event, Font, Length,
+ Point, Rectangle, Settings,
};
pub fn main() -> iced::Result {
@@ -73,26 +76,52 @@ impl Application for Example {
}
fn view(&self) -> Element<Message> {
- let view_bounds = |label, bounds| {
- text(format!(
- "The {label} container is {}",
+ let data_row = |label, value, color| {
+ row![
+ 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 {
- Some(bounds) => format!("visible at {:?}", 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()
+ {
+ Color {
+ g: 1.0,
+ ..Color::BLACK
+ }
+ .into()
+ } else {
+ theme::Text::Default
+ },
+ )
};
column![
- text(format!(
- "Mouse position is {}",
+ data_row(
+ "Mouse position",
match self.mouse_position {
Some(Point { x, y }) => format!("({x}, {y})"),
None => "unknown".to_string(),
- }
- )),
- view_bounds("outer", self.outer_bounds),
- view_bounds("inner", self.inner_bounds),
+ },
+ theme::Text::Default,
+ ),
+ view_bounds("Outer container", self.outer_bounds),
+ view_bounds("Inner container", self.inner_bounds),
scrollable(
column![
text("Scroll me!"),