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
113 lines (83 loc) · 3.09 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
use std::f64::consts::PI;
use gtk::prelude::*;
use gtk::DrawingArea;
use gtk::cairo::{Context, FontSlant, FontWeight};
fn build_ui(application: >k::Application) {
drawable(application, 500, 500, |_, cr| {
cr.set_dash(&[3., 2., 1.], 1.);
assert_eq!(cr.dash(), (vec![3., 2., 1.], 1.));
cr.scale(500f64, 500f64);
cr.set_source_rgb(250.0 / 255.0, 224.0 / 255.0, 55.0 / 255.0);
cr.paint().expect("Invalid cairo surface state");
cr.set_line_width(0.05);
// border
cr.set_source_rgb(0.3, 0.3, 0.3);
cr.rectangle(0.0, 0.0, 1.0, 1.0);
cr.stroke().expect("Invalid cairo surface state");
cr.set_line_width(0.03);
// draw circle
cr.arc(0.5, 0.5, 0.4, 0.0, PI * 2.);
cr.stroke().expect("Invalid cairo surface state");
// mouth
let mouth_top = 0.68;
let mouth_width = 0.38;
let mouth_dx = 0.10;
let mouth_dy = 0.10;
cr.move_to(0.50 - mouth_width / 2.0, mouth_top);
cr.curve_to(
0.50 - mouth_dx,
mouth_top + mouth_dy,
0.50 + mouth_dx,
mouth_top + mouth_dy,
0.50 + mouth_width / 2.0,
mouth_top,
);
println!("Extents: {:?}", cr.fill_extents());
cr.stroke().expect("Invalid cairo surface state");
let eye_y = 0.38;
let eye_dx = 0.15;
cr.arc(0.5 - eye_dx, eye_y, 0.05, 0.0, PI * 2.);
cr.fill().expect("Invalid cairo surface state");
cr.arc(0.5 + eye_dx, eye_y, 0.05, 0.0, PI * 2.);
cr.fill().expect("Invalid cairo surface state");
glib::Propagation::Proceed
});
drawable(application, 500, 500, |_, cr| {
cr.scale(500f64, 500f64);
cr.select_font_face("Sans", FontSlant::Normal, FontWeight::Normal);
cr.set_font_size(0.35);
cr.move_to(0.04, 0.53);
cr.show_text("Hello").expect("Invalid cairo surface state");
cr.move_to(0.27, 0.65);
cr.text_path("void");
cr.set_source_rgb(0.5, 0.5, 1.0);
cr.fill_preserve().expect("Invalid cairo surface state");
cr.set_source_rgb(0.0, 0.0, 0.0);
cr.set_line_width(0.01);
cr.stroke().expect("Invalid cairo surface state");
cr.set_source_rgba(1.0, 0.2, 0.2, 0.6);
cr.arc(0.04, 0.53, 0.02, 0.0, PI * 2.);
cr.arc(0.27, 0.65, 0.02, 0.0, PI * 2.);
cr.fill().expect("Invalid cairo surface state");
glib::Propagation::Proceed
});
}
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.cairotest"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
pub fn drawable<F>(application: >k::Application, width: i32, height: i32, draw_fn: F)
where
F: Fn(&DrawingArea, &Context) -> glib::Propagation + 'static,
{
let window = gtk::ApplicationWindow::new(application);
let drawing_area = DrawingArea::new();
drawing_area.connect_draw(draw_fn);
window.set_default_size(width, height);
window.add(&drawing_area);
window.show_all();
}