This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.rs
111 lines (95 loc) · 3.84 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use gtk::prelude::*;
use gtk::{gio, glib};
use gtk::{
AboutDialog, AccelFlags, AccelGroup, ApplicationWindow, CheckMenuItem, IconSize, Image, Label,
Menu, MenuBar, MenuItem, WindowPosition,
};
fn main() {
gio::resources_register_include!("compiled.gresource").unwrap();
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.menu_bar"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: >k::Application) {
let window = ApplicationWindow::new(application);
window.set_title("MenuBar example");
window.set_position(WindowPosition::Center);
window.set_size_request(400, 400);
let v_box = gtk::Box::new(gtk::Orientation::Vertical, 10);
let menu = Menu::new();
let accel_group = AccelGroup::new();
window.add_accel_group(&accel_group);
let menu_bar = MenuBar::new();
let file = MenuItem::with_label("File");
let about = MenuItem::with_label("About");
let quit = MenuItem::with_label("Quit");
let file_item = MenuItem::new();
let file_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let file_image = Image::from_resource("/org/gtk-rs/examples/file.png");
let file_label = Label::new(Some("File"));
let folder_item = MenuItem::new();
let folder_box = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let folder_image = Image::from_icon_name(Some("folder-music-symbolic"), IconSize::Menu);
let folder_label = Label::new(Some("Folder"));
let check_item = CheckMenuItem::with_label("Click me!");
file_box.pack_start(&file_image, false, false, 0);
file_box.pack_start(&file_label, true, true, 0);
file_item.add(&file_box);
folder_box.pack_start(&folder_image, false, false, 0);
folder_box.pack_start(&folder_label, true, true, 0);
folder_item.add(&folder_box);
menu.append(&file_item);
menu.append(&folder_item);
menu.append(&check_item);
menu.append(&about);
menu.append(&quit);
file.set_submenu(Some(&menu));
menu_bar.append(&file);
let other_menu = Menu::new();
let sub_other_menu = Menu::new();
let other = MenuItem::with_label("Another");
let sub_other = MenuItem::with_label("Sub another");
let sub_other2 = MenuItem::with_label("Sub another 2");
let sub_sub_other2 = MenuItem::with_label("Sub sub another 2");
let sub_sub_other2_2 = MenuItem::with_label("Sub sub another2 2");
sub_other_menu.append(&sub_sub_other2);
sub_other_menu.append(&sub_sub_other2_2);
sub_other2.set_submenu(Some(&sub_other_menu));
other_menu.append(&sub_other);
other_menu.append(&sub_other2);
other.set_submenu(Some(&other_menu));
menu_bar.append(&other);
quit.connect_activate(glib::clone!(@weak window => move |_| {
window.close();
}));
// `Primary` is `Ctrl` on Windows and Linux, and `command` on macOS
// It isn't available directly through `gdk::ModifierType`, since it has
// different values on different platforms.
let (key, modifier) = gtk::accelerator_parse("<Primary>Q");
quit.add_accelerator("activate", &accel_group, key, modifier, AccelFlags::VISIBLE);
let label = Label::new(Some("MenuBar example"));
v_box.pack_start(&menu_bar, false, false, 0);
v_box.pack_start(&label, true, true, 0);
window.add(&v_box);
window.show_all();
about.connect_activate(move |_| {
let p = AboutDialog::new();
p.set_authors(&["gtk-rs developers"]);
p.set_website_label(Some("gtk-rs"));
p.set_website(Some("http://gtk-rs.org"));
p.set_authors(&["gtk-rs developers"]);
p.set_title("About!");
p.set_transient_for(Some(&window));
p.show_all();
});
check_item.connect_toggled(|w| {
w.set_label(if w.is_active() {
"Checked"
} else {
"Unchecked"
});
});
}