Skip to content

Commit

Permalink
context menu and is_checked
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Feb 8, 2025
1 parent 0e08f4e commit b21c46c
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 84 deletions.
32 changes: 30 additions & 2 deletions examples/gtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ fn on_startup(_: &gtk4::Application) {

#[cfg(target_os = "linux")]
fn on_activate(application: &gtk4::Application) {
use muda::ContextMenu;

let window = gtk4::ApplicationWindow::builder()
.application(application)
.title("Menubar Example")
Expand All @@ -35,15 +37,21 @@ fn on_activate(application: &gtk4::Application) {

window.present();

let menubar = {
let (menubar, file_menu) = {
let file_menu = {
let about_menu_item = muda::MenuItem::new("About", true, None);

let check = muda::CheckMenuItem::new(
"Check",
true,
true,
Some(Accelerator::new(Modifiers::CONTROL, Code::KeyQ)),
);

let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png");
let icon = load_icon(std::path::Path::new(path));
let icon_menu_item = muda::IconMenuItem::new("Icon", true, Some(icon), None);

let quit_menu_item = muda::MenuItem::with_id(
"quit",
"&Quit",
Expand All @@ -54,21 +62,41 @@ fn on_activate(application: &gtk4::Application) {
let file_menu = muda::Submenu::new("&File", true);
file_menu.append(&about_menu_item).unwrap();
file_menu.append(&check).unwrap();
file_menu.append(&icon_menu_item).unwrap();
file_menu.append(&quit_menu_item).unwrap();
file_menu
};

let menubar = muda::Menu::new();
menubar.append(&file_menu).unwrap();

menubar
(menubar, file_menu)
};

let vbox = gtk4::Box::new(gtk4::Orientation::Vertical, 0);
menubar.init_for_gtk_window(&window, Some(&vbox)).unwrap();

let btn = gtk4::Button::with_label("ASdasd");
let w = window.clone();
btn.connect_clicked(move |_| {
file_menu.show_context_menu_for_gtk_window(w.dynamic_cast_ref().unwrap(), None);
});
vbox.append(&btn);

window.set_child(Some(&vbox));
}

#[cfg(not(target_os = "linux"))]
fn main() {}

fn load_icon(path: &std::path::Path) -> muda::Icon {
let (icon_rgba, icon_width, icon_height) = {
let image = image::open(path)
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
muda::Icon::from_rgba(icon_rgba, icon_width, icon_height).expect("Failed to open icon")
}
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum Error {
AlreadyInitialized,
#[error(transparent)]
AcceleratorParseError(#[from] AcceleratorParseError),
#[error("Gtk Window doesn't have an application")]
GtkWindowWithoutApplication,
}

/// Convenient type alias of Result type for muda.
Expand Down
Loading

0 comments on commit b21c46c

Please sign in to comment.