This repository was archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmain.rs
50 lines (40 loc) · 1.4 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
use gtk::prelude::*;
use gtk::{cairo, gdk};
use gtk::{ApplicationWindow, Button, Fixed};
fn build_ui(application: >k::Application) {
let window = ApplicationWindow::new(application);
set_visual(&window, None);
window.connect_screen_changed(set_visual);
window.connect_draw(draw);
window.set_title("Alpha Demo");
window.set_default_size(500, 500);
window.set_app_paintable(true); // crucial for transparency
let fixed = Fixed::new();
window.add(&fixed);
let button = Button::with_label("Dummy");
button.set_size_request(100, 30);
fixed.add(&button);
window.show_all();
}
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.transparent_main_window"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn set_visual(window: &ApplicationWindow, _screen: Option<&gdk::Screen>) {
if let Some(screen) = GtkWindowExt::screen(window) {
if let Some(ref visual) = screen.rgba_visual() {
window.set_visual(Some(visual)); // crucial for transparency
}
}
}
fn draw(_window: &ApplicationWindow, ctx: &cairo::Context) -> glib::Propagation {
// crucial for transparency
ctx.set_source_rgba(1.0, 0.0, 0.0, 0.4);
ctx.set_operator(cairo::Operator::Screen);
ctx.paint().expect("Invalid cairo surface state");
glib::Propagation::Proceed
}