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
//! A Rust wrapper for pugl //! //! This crate is wrapper for [pugl](https://gitlab.com/lv2/pugl/), a //! minimal portable API for embeddable GUIs. //! //! Since this crate depends on `-sys` crates that use `bindgen` to //! create the C API bindings, you need to have clang installed on //! your machine. //! //! `pugl` in principle supports several graphical backends. This //! crate so far supports only the Cairo backend. So all the drawing //! is done using the [cairo-rs](https://crates.io/crates/cairo-rs) //! crate. //! //! # Usage //! //! `pugl-sys` provides (maybe unlike classical `*-sys` crates) not //! only access to the functionality of `pugl` via FFI but also a thin //! layer to use `pugl` in a safe Rust-like way. //! //! In the center there is the //! [`PuglViewTrait`](trait.PuglViewTrait.html). In order to use //! `pugl-sys` for your application (or more precisely an UI for an //! application, set up a struct that implements `PuglViewTrait` an //! application. //! //! # GUI-Toolkit stub `pugl-ui` //! //! The crate [`pugl-ui`](https://crates.io/crates/pugl-ui) provides a //! stub of a GUI toolkit. It features simple box layouting and event //! propagation as well as a trait to implement widgets. //! //! # Example //! //! ``` ignore //! use pugl_sys::*; //! use std::f64::consts::PI; //! //! // Make an struct which has all the application logic //! struct UI { //! view: PuglViewFFI, //! red: f64, //! green: f64, //! width: f64, //! height: f64, //! close_requested: bool //! } //! //! // Make the UI struct implement the PuglViewTrait //! impl PuglViewTrait for UI { //! // exposure events //! fn exposed(&mut self, _expose: &ExposeArea, cr: &cairo::Context) { //! let radius = self.width.min(self.height) / 3.0; //! //! cr.set_source_rgb(0., 0., 1.); //! cr.rectangle(0., 0., self.width, self.height); //! cr.fill(); //! //! cr.set_source_rgb(self.red, self.green, 0.0); //! cr.arc(self.width/2.0, self.height/2.0, radius, 0.0, 2.0 * PI); //! cr.fill(); //! } //! //! // input events //! fn event(&mut self, ev: Event) -> Status { //! match ev.data { //! EventType::MouseMove(_) => { //! let pos = ev.pos(); //! self.red = pos.x / self.width; //! self.green = pos.y / self.height; //! self.post_redisplay(); //! } //! _ => {} //! } //! Status::Success //! } //! //! // a window resize event //! fn resize(&mut self, size: Size) { //! self.width = size.w; //! self.height = size.h; //! self.post_redisplay(); //! } //! //! // a window close event //! fn close_request(&mut self) { //! self.close_requested = true; //! } //! //! fn view(&self) -> PuglViewFFI { //! self.view //! } //! } //! //! impl UI { //! fn new(view: PuglViewFFI) -> Self { //! Self { //! view, //! red: 1.0, //! green: 0.0, //! width: 800.0, //! height: 600.0, //! close_requested: false //! } //! } //! } //! //! fn example() { //! // Request a PuglView passing a closure that returns an initialized `UI`. //! let mut view = PuglView::<UI>::new(std::ptr::null_mut(), |pv| UI::new(pv)); //! // borrow the UI handle from the view and do some window initialization //! let ui = view.handle(); //! ui.set_window_title("Test Pugl"); //! ui.make_resizable(); //! ui.set_default_size(ui.width.round() as i32, ui.height.round() as i32); //! ui.show_window(); //! //! // event loop until a close even occurs //! while !ui.close_requested { //! ui.update(-1.0); //! } //! } //! //! example(); //! ``` #![allow(non_upper_case_globals)] #![allow(non_camel_case_types)] #![allow(non_snake_case)] extern crate cairo; extern crate cairo_sys; #[macro_use] extern crate bitflags; #[cfg(test)] #[macro_use] extern crate serial_test; #[doc(hidden)] pub(crate) mod pugl; #[doc(hidden)] #[macro_use] pub mod types; #[doc(inline)] pub use types::*; #[doc(hidden)] #[cfg(not(feature="testing"))] pub mod view; #[doc(inline)] #[cfg(not(feature="testing"))] pub use view::*; #[doc(hidden)] #[cfg(feature="testing")] pub mod view_test; #[doc(inline)] #[cfg(feature="testing")] pub use view_test::*;