#embedded-graphics #layout #graphics #linear-layout

no-std embedded-layout

Extend embedded-graphics with simple layout functions

10 unstable releases (4 breaking)

0.4.1 Oct 10, 2023
0.4.0 Oct 10, 2023
0.3.2 Aug 23, 2023
0.3.1 May 14, 2023
0.0.2 Jun 25, 2020

#215 in Embedded development

Download history 66/week @ 2024-08-13 169/week @ 2024-08-20 135/week @ 2024-08-27 129/week @ 2024-09-03 123/week @ 2024-09-10 113/week @ 2024-09-17 189/week @ 2024-09-24 90/week @ 2024-10-01 107/week @ 2024-10-08 91/week @ 2024-10-15 89/week @ 2024-10-22 87/week @ 2024-10-29 96/week @ 2024-11-05 181/week @ 2024-11-12 184/week @ 2024-11-19 100/week @ 2024-11-26

568 downloads per month
Used in 2 crates

MIT license

105KB
2K SLoC

embedded-layout

embedded-layout extends embedded-graphics with basic layout functions.

embedded-layout consists of three main parts:

  • alignments that can be used to position two objects relative to one another
    • horizontal
      • NoAlignment, Left, Right, Center
      • LeftToRight, RightToLeft
    • vertical
      • NoAlignment, Top, Bottom, Center
      • TopToBottom, BottomToTop
  • layouts that can be used to arrange multiple views
    • LinearLayout
  • view groups which are collections of view objects
    • Chain to create ad-hoc collections (can hold views of different types)
    • Views to create view groups from arrays and slices (can only hold views of a single type)
    • derive(ViewGroup) to turn any plain old Rust struct into a view group

Example

The examples are based on the embedded-graphics simulator. The simulator is built on top of SDL2. See the simulator README for more information.

embedded-layout example

use embedded_graphics_simulator::{
    BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, Window,
};

use embedded_graphics::{
    mono_font::{ascii::FONT_6X9, MonoTextStyle},
    pixelcolor::BinaryColor,
    prelude::*,
    primitives::{Circle, PrimitiveStyle, Triangle},
    text::Text,
};
use embedded_layout::{layout::linear::LinearLayout, prelude::*};

fn main() -> Result<(), core::convert::Infallible> {
    let mut display: SimulatorDisplay<BinaryColor> = SimulatorDisplay::new(Size::new(128, 64));

    let output_settings = OutputSettingsBuilder::new()
        .theme(BinaryColorTheme::OledBlue)
        .build();

    // Create a Rectangle from the display's dimensions
    let display_area = display.bounding_box();

    // Style objects
    let text_style = MonoTextStyle::new(&FONT_6X9, BinaryColor::On);

    let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1);
    let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3);
    let fill_on = PrimitiveStyle::with_fill(BinaryColor::On);
    let fill_off = PrimitiveStyle::with_fill(BinaryColor::Off);

    // Primitives to be displayed
    let triangle = Triangle::new(Point::new(0, 0), Point::new(12, 0), Point::new(6, 12))
        .into_styled(thin_stroke);

    let circle = Circle::new(Point::zero(), 11).into_styled(thick_stroke);
    let circle2 = Circle::new(Point::zero(), 15).into_styled(fill_on);
    let triangle2 =
        Triangle::new(Point::new(0, 0), Point::new(10, 0), Point::new(5, 8)).into_styled(fill_off);
    let text = Text::new("embedded-layout", Point::zero(), text_style);

    // The layout
    LinearLayout::vertical(
        Chain::new(text)
            .append(LinearLayout::horizontal(Chain::new(triangle).append(circle)).arrange())
            .append(
                Chain::new(triangle2.align_to(&circle2, horizontal::Center, vertical::Top))
                    .append(circle2),
            ),
    )
    .with_alignment(horizontal::Center)
    .arrange()
    .align_to(&display_area, horizontal::Center, vertical::Center)
    .draw(&mut display)
    .unwrap();

    Window::new("Layout example", &output_settings).show_static(&display);
    Ok(())
}

Development setup

Minimum supported Rust version

The minimum supported Rust version for embedded-layout is 1.61.0 or greater.

Installation

For setup in general, follow the installation instructions for embedded-graphics.

To install SDL2 on Windows, see https://github.com/Rust-SDL2/rust-sdl2#windows-msvc

Dependencies

~4MB
~50K SLoC