safex

Crates.iosafex
lib.rssafex
version0.0.5
sourcesrc
created_at2023-03-20 12:54:20.717712
updated_at2023-04-03 02:01:11.543267
descriptionSafe x11 binding for rust
homepage
repository
max_upload_size
id815245
size72,758
(Lattexshz)

documentation

README

SafeX

Safe x11 high level bindings

Concept

  • Safe use of the x11 API through the implementation of drop traits, etc.
  • AsRaw Trait allows you to get a raw handle, so you can connect to APIs that SafeX does not support.

Example

Example to display a window and draw

use safex::xlib::*;

fn main() {
    let display = Display::open(None);
    let screen = Screen::default(&display);
    let root = Window::root_window(&display, &screen);

    let cmap = ColorMap::default(&display, &screen);
    let color = Color::from_rgb(&display, &cmap, 65535, 0, 0);

    let white = Color::from_rgb(&display, &cmap, 65535, 65535, 65535).get_pixel();
    let black = Color::from_rgb(&display, &cmap, 0, 0, 0).get_pixel();

    let window = Window::create_simple(
        &display,
        &screen,
        Some(()),
        Some(root),
        0,
        0,
        500,
        500,
        1,
        0,
        white,
    );

    window.set_window_title("Hello World");

    let rect = Rectangle {
        x: 10,
        y: 10,
        width: 100,
        height: 100,
        pixel: color.get_pixel(),
    };

    let arc = Arc {
        x: 120,
        y: 10,
        width: 100,
        height: 100,
        angle1: 360 * 64,
        angle2: 360 * 64,
        pixel: color.get_pixel(),
    };

    let rect2 = Rectangle {
        x: 10,
        y: 120,
        width: 100,
        height: 100,
        pixel: color.get_pixel(),
    };

    let arc2 = Arc {
        x: 120,
        y: 120,
        width: 100,
        height: 100,
        angle1: 360 * 64,
        angle2: 360 * 64,
        pixel: color.get_pixel(),
    };

    window.map(&display);
    window.run(|event, control_flow| match event {
        WindowEvent::Expose => {
            window.set_window_background(white);
            window.fill_rectangle(rect);
            window.fill_arc(arc);
            window.draw_rectangle(rect2);
            window.draw_arc(arc2);
            window.draw_string("Hello World", 10, 240, black);
            window.copy_to_buffer();
        }
    })
}

Commit count: 0

cargo fmt