Skip to content

Commit

Permalink
examples: Add a menubar one
Browse files Browse the repository at this point in the history
  • Loading branch information
ezicheq authored and bilelmoussaoui committed Jan 21, 2024
1 parent dbfd8a9 commit e87d5e7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ required-features = ["im-rc"]
name = "list_view_apps_launcher"
path = "list_view_apps_launcher/main.rs"

[[bin]]
name = "menubar"
path = "menubar/main.rs"

[[bin]]
name = "rotation_bin"
path = "rotation_bin/main.rs"
Expand Down
6 changes: 6 additions & 0 deletions examples/menubar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Menubar

This example demonstrates how to create a menubar in the top-level `ApplicationWindow`, add
menu items to it, and connect signals to those items.

![Screenshot](screenshot.png)
54 changes: 54 additions & 0 deletions examples/menubar/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use gtk::{gio, prelude::*};

fn main() {
// Create a new application
let application = gtk::Application::builder()
.application_id("com.github.gtk-rs.examples.menubar")
.build();
application.connect_startup(on_startup);
application.connect_activate(on_activate);
application.run();
}

fn on_startup(app: &gtk::Application) {
let about = gio::ActionEntry::builder("about")
.activate(|_, _, _| println!("About was pressed"))
.build();

let quit = gio::ActionEntry::builder("quit")
.activate(|app: &gtk::Application, _, _| app.quit())
.build();

app.add_action_entries([about, quit]);

let menubar = {
let file_menu = {
let about_menu_item = gio::MenuItem::new(Some("About"), Some("app.about"));
let quit_menu_item = gio::MenuItem::new(Some("Quit"), Some("app.quit"));

let file_menu = gio::Menu::new();
file_menu.append_item(&about_menu_item);
file_menu.append_item(&quit_menu_item);
file_menu
};

let menubar = gio::Menu::new();
menubar.append_submenu(Some("File"), &file_menu);

menubar
};

app.set_menubar(Some(&menubar));
}

fn on_activate(application: &gtk::Application) {
let window = gtk::ApplicationWindow::builder()
.application(application)
.title("Menubar Example")
.default_width(350)
.default_height(350)
.show_menubar(true)
.build();

window.present();
}
Binary file added examples/menubar/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e87d5e7

Please sign in to comment.