Integrate highly customisable widgets and themes for any Rust application or GUI library.
In your "Cargo.toml" file:
toml
[dependencies]
haussmann = "*"
Check the current version on crates.io
This is an hypothetical example, drawing a label and a clickable button on a window which allows to draw on its surface and which is able to capture events.
See the imports for the following code parts
rust
use haussmann::widgets::{ Button, Layout, StaticLayout };
use haussmann::{ Align, Overflow };
use haussmann::graphics;
All draws will be performed on the canvas
.
rust
let window = Window::new(1280, 720);
let canvas = window.into_canvas();
Here are the widgets.
```rust
let label = Label::new("Don't touch this button:");
// Red button yelling "you touched me" when tapped.
let mut button = Button::new([300, 150], RGBA::new(255, 0, 0, 255));
button.ontap = || {
println!("you touched me!");
};
And the layout containing these two widgets.
rust
// The main layout containing all the widgets and layouts.
let layout = FixedLayout::new(
// The fixed position of the layout.
[0, 0],
// The size of the layout.
[window.width(), window.height()],
// The widgets contained into the layout.
&[label, button],
// Overflowing rule.
Overflow::Hide,
// X and Y aligns of the widgets inside.
Align::Center, Align::Center
);
In the running loop :
rust
loop {
...
}
- Event handling
rust
match event {
Event::Quit => break,
Event::Tap(x, y) => {
for widget in layout.touchablewidgets() {
// Whether the widget has been tapped.
if graphics::istapped(x, y, &widget) {
// Calls the on_tap()
callback.
widget.ontap();
}
}
},
_ => {}
}
- And widgets rendering
rust
// The layout fits the window.
layout.resize([window.width(), window.height()]);
// Draws the layout on the canvas.
canvas.draw_rect([layout.x, layout.y, layout.width, layout.height]);
// Draws the layout's widgets on the canvas.
for widget in layout.widgets() {
// Draws each shape of the widget.
for shape in widget.shapes() {
let (x, y) = graphics::calculate_position(&layout, &shape);
shape.move(x, y);
canvas.set_colour(shape.fill_colour());
canvas.draw_from_points(shape.points());
}
}
```
That's it, you used two widgets for your application, without calculating anything !