From c50601c23a8ee1f44dd6dda2a5c83118c38c4cf8 Mon Sep 17 00:00:00 2001 From: demenik Date: Tue, 9 Dec 2025 13:06:01 +0100 Subject: [PATCH] fix: Make popup child a closure --- src/features/hud/ui/settings.rs | 188 ++++++++++++-------------------- src/features/ui/ui/mod.rs | 2 +- src/features/ui/ui/popups.rs | 94 ++++++++-------- 3 files changed, 124 insertions(+), 160 deletions(-) diff --git a/src/features/hud/ui/settings.rs b/src/features/hud/ui/settings.rs index cb82cc4..f0c8247 100644 --- a/src/features/hud/ui/settings.rs +++ b/src/features/hud/ui/settings.rs @@ -3,127 +3,83 @@ use super::timer_settings::timer_settings; use crate::prelude::*; pub fn open_settings(commands: &mut Commands) { - commands - .spawn(( - RootMarker::Settings, - Node { - position_type: PositionType::Absolute, - width: percent(100), - height: percent(100), - ..Node::center() - }, - ZIndex(1), - BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.8)), - GlobalTransform::default(), - )) - .with_children(|parent| { - parent - .spawn(( - Node { - width: px(700), - padding: UiRect::all(px(20.0)), - ..Node::vstack(px(20)) - }, - BackgroundColor(Color::srgb(0.2, 0.2, 0.2)), - BorderRadius::all(px(10.0)), - )) - .with_children(|parent| { - parent.spawn(( + spawn_popup( + commands, + RootMarker::Settings, + "Spiel Einstellungen", + Node { + width: px(700), + ..default() + }, + |parent| { + parent.spawn(( + Node::vstack(px(10)), + children![ + button( + ButtonType::SettingsExit, + ButtonVariant::Secondary, + Node::from_padding(UiRect::all(px(10))), + |color| text("Spiel verlassen", 24.0, color) + ), + button( + ButtonType::SettingsSave, + ButtonVariant::Secondary, + Node::from_padding(UiRect::all(px(10))), + |color| text("Spiel speichern", 24.0, color) + ),( Node { - justify_content: JustifyContent::SpaceBetween, - ..Node::hstack(px(20)) + justify_content: JustifyContent::Center, + ..Node::hstack(px(30)) }, children![ - text("Spiel Einstellungen", 40.0, Color::WHITE), - pill_button( - ButtonType::SettingsClose, - ButtonVariant::Destructive, + ( Node { - width: px(40), - height: px(40), - ..default() - }, - |color| text("X", 24.0, color) - ), - ], - )); - - parent - .spawn(Node::vstack(px(10))) - .with_children(|parent| { - parent.spawn(button( - ButtonType::SettingsExit, - ButtonVariant::Secondary, - Node { - padding: UiRect::all(px(10)), - ..default() - }, - |color| text("Spiel verlassen", 24.0, color) - )); - - parent.spawn(button( - ButtonType::SettingsSave, - ButtonVariant::Secondary, - Node { - padding: UiRect::all(px(10)), - ..default() - }, - |color| text("Spiel speichern", 24.0, color) - )); - - parent.spawn(( - Node { - justify_content: JustifyContent::Center, - ..Node::hstack(px(30)) + width: percent(40), + ..Node::vstack(px(10)) }, children![ - ( - Node { - width: percent(40), - ..Node::vstack(px(10)) - }, - children![ - text("Spiel Einstellungen", 18.0, Color::WHITE), - text( - "Tipp: Benutze [Umstellen] um in 10er Schritten zu inkrementieren oder dekrementieren!", - 16.0, - Color::WHITE - ), - ] + text("Spiel Einstellungen", 18.0, Color::WHITE), + text( + "Tipp: Benutze [Umstellen] um in 10er Schritten zu inkrementieren oder dekrementieren!", + 16.0, + Color::WHITE ), - ( - Node { - align_items: AlignItems::Center, - ..Node::vstack(px(10)) - }, - children![ - text("Fokus Phase", 12.0, Color::WHITE), - timer_settings(TimerType::Focus) - ] - ), - ( - Node { - align_items: AlignItems::Center, - ..Node::vstack(px(10)) - }, - children![ - text("Kurze Pause", 12.0, Color::WHITE), - timer_settings(TimerType::ShortBreak) - ] - ), - ( - Node { - align_items: AlignItems::Center, - ..Node::vstack(px(10)) - }, - children![ - text("Lange Pause", 12.0, Color::WHITE), - timer_settings(TimerType::LongBreak) - ] - ) - ], - )); - }); - }); - }); + ] + ), + ( + Node { + align_items: AlignItems::Center, + ..Node::vstack(px(10)) + }, + children![ + text("Fokus Phase", 12.0, Color::WHITE), + timer_settings(TimerType::Focus) + ] + ), + ( + Node { + align_items: AlignItems::Center, + ..Node::vstack(px(10)) + }, + children![ + text("Kurze Pause", 12.0, Color::WHITE), + timer_settings(TimerType::ShortBreak) + ] + ), + ( + Node { + align_items: AlignItems::Center, + ..Node::vstack(px(10)) + }, + children![ + text("Lange Pause", 12.0, Color::WHITE), + timer_settings(TimerType::LongBreak) + ] + ) + ], + ) + ], + )); + }, + ); } diff --git a/src/features/ui/ui/mod.rs b/src/features/ui/ui/mod.rs index f491984..d6f4ea3 100644 --- a/src/features/ui/ui/mod.rs +++ b/src/features/ui/ui/mod.rs @@ -5,5 +5,5 @@ pub mod texts; pub use button::{button, pill_button}; pub use flexbox::Flexbox; -pub use popups::popup; +pub use popups::spawn_popup; pub use texts::{text, text_with_component}; diff --git a/src/features/ui/ui/popups.rs b/src/features/ui/ui/popups.rs index 142ca89..dedc8af 100644 --- a/src/features/ui/ui/popups.rs +++ b/src/features/ui/ui/popups.rs @@ -1,3 +1,5 @@ +use bevy::ecs::relationship::RelatedSpawnerCommands; + use super::super::messages::ClosePopupMessage; use crate::prelude::*; @@ -7,57 +9,63 @@ pub struct PopupRoot; #[derive(Component)] pub struct PopupCloseButton; -pub fn popup( +pub fn spawn_popup( + commands: &mut Commands, root: impl Component, title: impl Into, mut node: Node, - child: impl Bundle, + child: impl FnOnce(&mut RelatedSpawnerCommands), ) -> impl Bundle { node.flex_direction = FlexDirection::Column; node.row_gap = px(10); node.padding = UiRect::all(px(20)); - ( - PopupRoot, - root, - Node { - position_type: PositionType::Absolute, - width: percent(100), - height: percent(100), - ..Node::center() - }, - ZIndex(100), - BackgroundColor(Color::srgba(0., 0., 0., 0.8)), - GlobalTransform::default(), - children![( - node, - BackgroundColor(Color::srgb(0.2, 0.2, 0.2)), - BorderRadius::all(px(10)), - children![ - ( - Node { - justify_content: JustifyContent::SpaceBetween, - align_items: AlignItems::Center, - ..Node::hstack(px(20)) - }, - children![ - text(title, 40.0, Color::WHITE), - button( - PopupCloseButton, - ButtonVariant::Destructive, - Node { - width: px(40), - height: px(40), - ..Node::center() - }, - |color| text("X", 24.0, color) - ) - ] - ), - child - ] - )], - ) + commands + .spawn(( + PopupRoot, + root, + Node { + position_type: PositionType::Absolute, + width: percent(100), + height: percent(100), + ..Node::center() + }, + ZIndex(100), + BackgroundColor(Color::srgba(0., 0., 0., 0.8)), + GlobalTransform::default(), + )) + .with_children(|parent| { + parent + .spawn(( + node, + BackgroundColor(Color::srgb(0.2, 0.2, 0.2)), + BorderRadius::all(px(10)), + )) + .with_children(|parent| { + parent.spawn(( + Node { + justify_content: JustifyContent::SpaceBetween, + align_items: AlignItems::Center, + ..Node::hstack(px(20)) + }, + children![ + text(title, 40.0, Color::WHITE), + button( + PopupCloseButton, + ButtonVariant::Destructive, + Node { + width: px(40), + height: px(40), + ..Node::center() + }, + |color| text("X", 24.0, color) + ) + ], + )); + + child(parent); + }); + }); } pub fn handle_popup_close(