Skip to content

Commit

Permalink
Simplify context menu bugfix + option border radius adjustment (#160)
Browse files Browse the repository at this point in the history
* Simpler solution

* Adjust context menu option border radius
  • Loading branch information
tim-blackbird authored Dec 2, 2024
1 parent aa81d9b commit 6b92f2c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 49 deletions.
55 changes: 12 additions & 43 deletions bevy_widgets/bevy_context_menu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,28 @@ pub struct ContextMenuPlugin;

impl Plugin for ContextMenuPlugin {
fn build(&self, app: &mut App) {
app.add_observer(on_secondary_button_down_entity_with_context_menu)
.insert_resource(OpenedContextMenu(None));
app.add_observer(on_secondary_button_down_entity_with_context_menu);
}
}

struct OpenedContextMenuInfo {
owner: Entity,
entity: Entity,
}
#[derive(Resource)]
struct OpenedContextMenu(Option<OpenedContextMenuInfo>);

fn on_secondary_button_down_entity_with_context_menu(
trigger: Trigger<Pointer<Down>>,
mut trigger: Trigger<Pointer<Down>>,
mut commands: Commands,
query: Query<&ContextMenu>,
query_zindex: Query<&ZIndex>,
theme: Res<Theme>,
mut opened_context_menu: ResMut<OpenedContextMenu>,
) {
let event = trigger.event();
if event.button != PointerButton::Secondary {
if trigger.event().button != PointerButton::Secondary {
return;
}
let target = trigger.entity();

let target = trigger.entity();
let Ok(menu) = query.get(target) else {
return;
};

if let Some(opened_context_menu) = &opened_context_menu.0 {
let owner_z_index = query_zindex
.get(opened_context_menu.owner)
.unwrap_or(&ZIndex(0));
let target_z_index = query_zindex.get(target).unwrap_or(&ZIndex(0));
if target_z_index.0 < owner_z_index.0 {
return;
}
commands
.entity(opened_context_menu.entity)
.despawn_recursive();
}
trigger.propagate(false);

let event = trigger.event();

// Prevent all other entities from being picked by placing a node over the entire window.
let root = commands
Expand All @@ -65,17 +45,12 @@ fn on_secondary_button_down_entity_with_context_menu(
},
ZIndex(10),
))
.observe(
|trigger: Trigger<Pointer<Down>>,
mut commands: Commands,
mut opened_context_menu: ResMut<OpenedContextMenu>| {
commands.entity(trigger.entity()).despawn_recursive();
*opened_context_menu = OpenedContextMenu(None);
},
)
.observe(|trigger: Trigger<Pointer<Down>>, mut commands: Commands| {
commands.entity(trigger.entity()).despawn_recursive();
})
.id();

let context_menu_entity = spawn_context_menu(
spawn_context_menu(
&mut commands,
&theme,
menu,
Expand All @@ -86,13 +61,7 @@ fn on_secondary_button_down_entity_with_context_menu(
// Prevent the context menu root from despawning the context menu when clicking on the menu
trigger.propagate(false);
})
.set_parent(root)
.id();

*opened_context_menu = OpenedContextMenu(Some(OpenedContextMenuInfo {
owner: target,
entity: context_menu_entity,
}));
.set_parent(root);
}

/// Entities with this component will have a context menu.
Expand Down
9 changes: 3 additions & 6 deletions bevy_widgets/bevy_context_menu/src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{prelude::*, window::SystemCursorIcon, winit::cursor::CursorIcon};
use bevy_editor_styles::Theme;

use crate::{ContextMenu, OpenedContextMenu};
use crate::ContextMenu;

pub(crate) fn spawn_context_menu<'a>(
commands: &'a mut Commands,
Expand Down Expand Up @@ -54,7 +54,7 @@ pub(crate) fn spawn_option<'a>(
flex_grow: 1.,
..default()
},
theme.button.border_radius,
theme.context_menu.option_border_radius,
))
.observe(
|trigger: Trigger<Pointer<Over>>,
Expand Down Expand Up @@ -92,16 +92,13 @@ pub(crate) fn spawn_option<'a>(
move |trigger: Trigger<Pointer<Up>>,
mut commands: Commands,
parent_query: Query<&Parent>,
mut query: Query<&mut ContextMenu>,
mut opened_context_menu: ResMut<OpenedContextMenu>| {
mut query: Query<&mut ContextMenu>| {
// Despawn the context menu when an option is selected
let root = parent_query
.iter_ancestors(trigger.entity())
.last()
.unwrap();
commands.entity(root).despawn_recursive();
// Invalidate opened context menu
*opened_context_menu = OpenedContextMenu(None);

// Run the option callback
let callback = &mut query.get_mut(target).unwrap().options[index].f;
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_editor_styles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub struct ContextMenuStyles {
pub background_color: BackgroundColor,
/// The hover color of the context menu.
pub hover_color: BackgroundColor,
/// The border radius of the context menu options.
pub option_border_radius: BorderRadius,
}

/// The styles for viewports in the editor.
Expand Down Expand Up @@ -145,6 +147,7 @@ impl FromWorld for Theme {
context_menu: ContextMenuStyles {
background_color: BackgroundColor(Color::oklch(0.209, 0., 0.)),
hover_color: BackgroundColor(Color::oklch(0.3677, 0., 0.)),
option_border_radius: BorderRadius::all(Val::Px(5.)),
},
viewport: ViewportStyles {
background_color: Color::oklch(0.3677, 0.0, 0.0),
Expand Down

0 comments on commit 6b92f2c

Please sign in to comment.