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
193 lines (161 loc) · 6.7 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use gtk::prelude::*;
use gtk::{gdk, gio, glib};
use gtk::{
AboutDialog, AppChooserDialog, ApplicationWindow, Builder, Button, Dialog, Entry,
FileChooserAction, FileChooserDialog, FontChooserDialog, RecentChooserDialog, ResponseType,
Scale, SpinButton, Spinner, Switch, Window,
};
fn main() {
gio::resources_register_include!("compiled.gresource").unwrap();
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.gtktest"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: >k::Application) {
println!(
"Major: {}, Minor: {}",
gtk::major_version(),
gtk::minor_version()
);
let glade_src = include_str!("gtk_test.ui");
let builder = Builder::from_string(glade_src);
let spinner: Spinner = builder.object("spinner").expect("Couldn't get spinner");
spinner.start();
let scale: Scale = builder.object("scale").expect("Couldn't get scale");
scale.connect_format_value(|scale, value| {
let digits = scale.digits() as usize;
format!("<{value:.digits$}>")
});
let spin_button: SpinButton = builder
.object("spin_button")
.expect("Couldn't get spin_button");
spin_button.connect_input(|spin_button| {
let text = spin_button.text();
println!("spin_button_input: \"{text}\"");
match text.parse::<f64>() {
Ok(value) if value >= 90. => {
println!("circular right");
Some(Ok(10.))
}
Ok(value) if value <= 10. => {
println!("circular left");
Some(Ok(90.))
}
Ok(value) => Some(Ok(value)),
Err(_) => Some(Err(())),
}
});
let window: ApplicationWindow = builder.object("window").expect("Couldn't get window");
window.set_application(Some(application));
let button: Button = builder.object("button").expect("Couldn't get button");
let entry: Entry = builder.object("entry").expect("Couldn't get entry");
button.connect_clicked(glib::clone!(@weak window, @weak entry => move |_| {
let dialog = Dialog::with_buttons(Some("Hello!"),
Some(&window),
gtk::DialogFlags::MODAL,
&[("No", ResponseType::No),
("Yes", ResponseType::Yes),
("Custom", ResponseType::Other(0))]);
dialog.connect_response(glib::clone!(@weak entry => move |dialog, response| {
entry.set_text(&format!("Clicked {response}"));
dialog.close();
}));
dialog.show_all();
}));
let button_font: Button = builder
.object("button_font")
.expect("Couldn't get button_font");
button_font.connect_clicked(glib::clone!(@weak window => move |_| {
let dialog = FontChooserDialog::new(Some("Font chooser test"), Some(&window));
dialog.connect_response(|dialog, _| dialog.close());
dialog.show_all();
}));
let button_recent: Button = builder
.object("button_recent")
.expect("Couldn't get button_recent");
button_recent.connect_clicked(glib::clone!(@weak window => move |_| {
let dialog = RecentChooserDialog::new(Some("Recent chooser test"), Some(&window));
dialog.add_buttons(&[
("Ok", ResponseType::Ok),
("Cancel", ResponseType::Cancel)
]);
dialog.connect_response(|dialog, _| dialog.close());
dialog.show_all();
}));
let file_button: Button = builder
.object("file_button")
.expect("Couldn't get file_button");
file_button.connect_clicked(glib::clone!(@weak window => move |_| {
// entry.set_text("Clicked!");
let dialog = FileChooserDialog::new(Some("Choose a file"), Some(&window),
FileChooserAction::Open);
dialog.add_buttons(&[
("Open", ResponseType::Ok),
("Cancel", ResponseType::Cancel)
]);
dialog.set_select_multiple(true);
dialog.connect_response(|dialog, response| {
if response == ResponseType::Ok {
let files = dialog.filenames();
println!("Files: {files:?}");
}
dialog.close();
});
dialog.show_all();
}));
let app_button: Button = builder
.object("app_button")
.expect("Couldn't get app_button");
app_button.connect_clicked(glib::clone!(@weak window => move |_| {
// entry.set_text("Clicked!");
let dialog = AppChooserDialog::for_content_type(Some(&window),
gtk::DialogFlags::MODAL,
"sh");
dialog.connect_response(|dialog, _| dialog.close());
dialog.show_all();
}));
let switch: Switch = builder.object("switch").expect("Couldn't get switch");
switch.connect_changed_active(glib::clone!(@weak entry => move |switch| {
if switch.is_active() {
entry.set_text("Switch On");
} else {
entry.set_text("Switch Off");
}
}));
let button_about: Button = builder
.object("button_about")
.expect("Couldn't get button_about");
let dialog: AboutDialog = builder.object("dialog").expect("Couldn't get dialog");
button_about.connect_clicked(move |x| about_clicked(x, &dialog));
window.connect_key_press_event(
glib::clone!(@weak entry => @default-return glib::Propagation::Proceed, move |_, key| {
let keyval = key.keyval();
let keystate = key.state();
println!("key pressed: {keyval} / {keystate:?}");
println!("text: {}", entry.text());
if keystate.intersects(gdk::ModifierType::CONTROL_MASK) {
println!("You pressed Ctrl!");
}
glib::Propagation::Proceed
}),
);
window.show_all();
}
fn about_clicked(button: &Button, dialog: &AboutDialog) {
if let Some(window) = button.toplevel().and_then(|w| w.downcast::<Window>().ok()) {
dialog.set_transient_for(Some(&window));
}
// We only want to hide the dialog when it's closed and not completely destroy it
// as otherwise we can't show it again a second time.
dialog.connect_delete_event(|dialog, _| {
dialog.hide();
glib::Propagation::Stop
});
println!("Authors: {:?}", dialog.authors());
println!("Artists: {:?}", dialog.artists());
println!("Documenters: {:?}", dialog.documenters());
dialog.show_all();
}