Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ bevy_undo = { path = "crates/bevy_undo" }
bevy_infinite_grid = { path = "crates/bevy_infinite_grid" }
bevy_editor_cam = { path = "crates/bevy_editor_cam" }
bevy_clipboard = { path = "crates/bevy_clipboard" }

# MacOS Native Dependencies
objc-rs = "0.2.8"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put these behind a cfg block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to do it in the main Cargo.toml(in this case because of the workspace)

but i could create a crate only for macos deps and use it wherever is necessary

Copy link
Contributor Author

@aminwhat aminwhat Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or move the them in the bevy_menu_bar and put them there behind a cfg

it seems that workspace dependencies in the main Cargo.toml does not support this:

# MacOS Native Dependencies (only included when building on macOS)
[workspace.target.'cfg(target_os = "macos")'.dependencies] # or [target.'cfg(target_os = "macos")'.dependencies]
objc-rs = "0.2.8"
objc2 = "0.6.2"
objc2-app-kit = "0.3.1"
objc2-foundation = "0.3.1"

rust analyzer says:

Caused by: failed to load manifest for dependency `bevy_menu_bar` Caused by: failed to parse manifest at `/Users/aminwhat/Desktop/Projects/bevy_editor_prototypes/bevy_widgets/bevy_menu_bar/Cargo.toml` Caused by: error inheriting `objc-rs` from workspace root manifest's `workspace.dependencies.objc-rs` Caused by: `dependency.objc-rs` was not found in `workspace.dependencies

objc2 = "0.6.2"
objc2-app-kit = "0.3.1"
objc2-foundation = "0.3.1"
7 changes: 7 additions & 0 deletions bevy_widgets/bevy_menu_bar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@ edition = "2024"
bevy.workspace = true
bevy_editor_styles.workspace = true

[target.'cfg(target_os = "macos")'.dependencies]
objc-rs.workspace = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here it works:

[package]
name = "bevy_menu_bar"
version = "0.1.0"
edition = "2024"

[dependencies]
bevy.workspace = true
bevy_editor_styles.workspace = true

[target.'cfg(target_os = "macos")'.dependencies]
objc-rs.workspace = true
objc2.workspace = true
objc2-app-kit.workspace = true
objc2-foundation.workspace = true


[lints]
workspace = true

but i'm not sure if it fixes that mac os native deps does not pull by the rust analyzer on windows or linux

objc2.workspace = true
objc2-app-kit.workspace = true
objc2-foundation.workspace = true


[lints]
workspace = true
126 changes: 123 additions & 3 deletions bevy_widgets/bevy_menu_bar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
//! such as "File", "Edit", "View", etc.

use bevy::{asset::embedded_asset, prelude::*};

use bevy_editor_styles::Theme;

#[cfg(target_os = "macos")]
use objc2::MainThreadMarker;

/// The root node for the menu bar.
#[derive(Component)]
pub struct MenuBarNode;
Expand All @@ -17,7 +19,17 @@ pub struct MenuBarPlugin;
impl Plugin for MenuBarPlugin {
fn build(&self, app: &mut App) {
embedded_asset!(app, "assets/logo/bevy_logo.png");
app.add_systems(Startup, menu_setup.in_set(MenuBarSet));

#[cfg(target_os = "macos")]
{
app.insert_non_send_resource(MainThreadMarker::new().unwrap());
app.add_systems(Startup, setup_native_macos_menu);
}

#[cfg(not(target_os = "macos"))]
{
app.add_systems(Startup, menu_setup.in_set(MenuBarSet));
}
}
}

Expand All @@ -42,7 +54,7 @@ pub enum TopBarItem {
Help,
}

/// The setup system for the menu bar.
/// The setup system for the menu bar (Not Macos).
fn menu_setup(
mut commands: Commands,
root: Query<Entity, With<MenuBarNode>>,
Expand Down Expand Up @@ -326,3 +338,111 @@ fn menu_setup(
commands.spawn(hover_over_observer);
commands.spawn(click_observer);
}

/// The setup system for the menu bar (macOS native).
#[allow(unsafe_code)]
#[cfg(target_os = "macos")]
fn setup_native_macos_menu(_marker: NonSend<MainThreadMarker>) {
use objc2::sel;
use objc2_app_kit::{NSApp, NSMenu, NSMenuItem};
use objc2_foundation::NSString;

unsafe {
let main_thread =
MainThreadMarker::new().expect("setup_native_macos_menu must run on the main thread");

let app = NSApp(main_thread);

// Ensure the app is activated so menus are visible
app.activate();

// Main menu bar
let menubar = NSMenu::new(main_thread);

// === App Menu ===
let app_menu_item = NSMenuItem::new(main_thread);
menubar.addItem(&app_menu_item);

let app_menu = NSMenu::new(main_thread);
app_menu.setTitle(NSString::from_str("App").as_ref());
app_menu.addItemWithTitle_action_keyEquivalent(
NSString::from_str("Quit").as_ref(),
sel!(terminate:).into(),
NSString::from_str("q").as_ref(),
);
app_menu_item.setSubmenu(Some(&app_menu));

// === File menu ===
let file_menu_item = NSMenuItem::new(main_thread);
menubar.addItem(&file_menu_item);

let file_menu = NSMenu::new(main_thread);
file_menu.setTitle(NSString::from_str("File").as_ref());
file_menu.addItemWithTitle_action_keyEquivalent(
NSString::from_str("Save").as_ref(),
None,
NSString::from_str("s").as_ref(),
);
file_menu_item.setSubmenu(Some(&file_menu));

// === Edit menu ===
let edit_menu_item = NSMenuItem::new(main_thread);
menubar.addItem(&edit_menu_item);

let edit_menu = NSMenu::new(main_thread);
edit_menu.setTitle(NSString::from_str("Edit").as_ref());
edit_menu.addItemWithTitle_action_keyEquivalent(
NSString::from_str("Undo").as_ref(),
sel!(undo:).into(),
NSString::from_str("z").as_ref(),
);
edit_menu.addItemWithTitle_action_keyEquivalent(
NSString::from_str("Redo").as_ref(),
sel!(redo:).into(),
NSString::from_str("Z").as_ref(),
);
edit_menu_item.setSubmenu(Some(&edit_menu));

// === Build menu ===
let build_menu_item = NSMenuItem::new(main_thread);
menubar.addItem(&build_menu_item);

let build_menu = NSMenu::new(main_thread);
build_menu.setTitle(NSString::from_str("Build").as_ref());
build_menu.addItemWithTitle_action_keyEquivalent(
NSString::from_str("Run").as_ref(),
None, // no action yet
NSString::from_str("r").as_ref(),
);
build_menu_item.setSubmenu(Some(&build_menu));

// === Window menu ===
let window_menu_item = NSMenuItem::new(main_thread);
menubar.addItem(&window_menu_item);

let window_menu = NSMenu::new(main_thread);
window_menu.setTitle(NSString::from_str("Window").as_ref());
window_menu.addItemWithTitle_action_keyEquivalent(
NSString::from_str("Minimize").as_ref(),
sel!(performMiniaturize:).into(),
NSString::from_str("m").as_ref(),
);
window_menu_item.setSubmenu(Some(&window_menu));

// === Help menu ===
let help_menu_item = NSMenuItem::new(main_thread);
menubar.addItem(&help_menu_item);

let help_menu = NSMenu::new(main_thread);
help_menu.setTitle(NSString::from_str("Help").as_ref());
help_menu.addItemWithTitle_action_keyEquivalent(
NSString::from_str("About").as_ref(),
None,
NSString::new().as_ref(),
);
help_menu_item.setSubmenu(Some(&help_menu));

// Attach to app
app.setMainMenu(Some(&menubar));
}
}