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
85 lines (68 loc) · 2.82 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
//! # Overlay example
//!
//! This example demonstrates how to create an element "floating" above others.
use gtk::prelude::*;
use gtk::{gdk, gio, glib};
fn button_clicked(button: >k::Button, overlay_text: >k::Label) {
overlay_text.set_text(&button.label().expect("Couldn't get button label"));
}
fn build_ui(application: >k::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_title("Overlay");
window.set_position(gtk::WindowPosition::Center);
// The overlay container.
let overlay = gtk::Overlay::new();
// The overlay label.
let overlay_text = gtk::Label::new(Some("0"));
// We need to name it in order to apply CSS on it.
overlay_text.set_widget_name("overlay-label");
// We put the overlay in the top-right corner of the window.
overlay_text.set_halign(gtk::Align::End);
overlay_text.set_valign(gtk::Align::Start);
// We add into the overlay container as the overlay element.
overlay.add_overlay(&overlay_text);
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 0);
let but1 = gtk::Button::with_label("Click me!");
let but2 = gtk::Button::with_label("Or me!");
let but3 = gtk::Button::with_label("Why not me?");
// When a button is clicked on, we set its label to the overlay label.
but1.connect_clicked(glib::clone!(@weak overlay_text => move |b| {
button_clicked(b, &overlay_text);
}));
but2.connect_clicked(glib::clone!(@weak overlay_text => move |b| {
button_clicked(b, &overlay_text);
}));
but3.connect_clicked(glib::clone!(@weak overlay_text => move |b| {
button_clicked(b, &overlay_text);
}));
hbox.add(&but1);
hbox.add(&but2);
hbox.add(&but3);
// We add the horizontal box into the overlay container "normally" (so this won't be an overlay
// element).
overlay.add(&hbox);
// Then we add the overlay container inside our window.
window.add(&overlay);
window.show_all();
}
fn main() {
let application =
gtk::Application::new(Some("com.github.overlay"), gio::ApplicationFlags::empty());
application.connect_startup(|_| {
// We add a bit of CSS in order to make the overlay label easier to be seen.
let provider = gtk::CssProvider::new();
// Basic CSS: we change background color, we set font color to black and we set it as bold.
let style = include_bytes!("style.css");
provider.load_from_data(style).expect("Failed to load CSS");
gtk::StyleContext::add_provider_for_screen(
&gdk::Screen::default().expect("Error initializing gtk css provider."),
&provider,
gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
);
});
application.connect_activate(|app| {
// We build the application UI.
build_ui(app);
});
application.run();
}