Skip to content
Snippets Groups Projects
readonly.rs 2.16 KiB
Newer Older
StaffEngineer's avatar
StaffEngineer committed
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_cosmic_edit::{
StaffEngineer's avatar
StaffEngineer committed
    bevy_color_to_cosmic, Attrs, AttrsOwned, CosmicAttrs, CosmicEditPlugin, CosmicEditUiBundle,
    CosmicFontConfig, CosmicMetrics, CosmicText, CosmicTextPosition, Family, Focus, ReadOnly,
StaffEngineer's avatar
StaffEngineer committed
};

fn setup(mut commands: Commands, windows: Query<&Window, With<PrimaryWindow>>) {
StaffEngineer's avatar
StaffEngineer committed
    let primary_window = windows.single();
    commands.spawn(Camera2dBundle::default());
    let root = commands
        .spawn(NodeBundle {
            style: Style {
                display: Display::Flex,
                width: Val::Percent(100.),
                height: Val::Percent(100.),
                ..default()
            },
            ..default()
        })
        .id();

    let mut attrs = Attrs::new();
    attrs = attrs.family(Family::Name("Victor Mono"));
    attrs = attrs.color(bevy_color_to_cosmic(Color::PURPLE));
sam edelsten's avatar
sam edelsten committed
    let cosmic_edit = CosmicEditUiBundle {
        style: Style {
            width: Val::Percent(100.),
            height: Val::Percent(100.),
            ..default()
        },
        cosmic_attrs: CosmicAttrs(AttrsOwned::new(attrs)),
        text_position: CosmicTextPosition::Center,
        background_color: BackgroundColor(Color::WHITE),
        cosmic_metrics: CosmicMetrics {
StaffEngineer's avatar
StaffEngineer committed
            font_size: 14.,
            line_height: 18.,
            scale_factor: primary_window.scale_factor() as f32,
        },
        text: CosmicText::OneStyle("😀😀😀 x => y\nRead only widget".to_string()),
        ..default()

    let mut id = None;
sam edelsten's avatar
sam edelsten committed
    // Spawn the CosmicEditUiBundle as a child of root
    commands.entity(root).with_children(|parent| {
        id = Some(parent.spawn(cosmic_edit).insert(ReadOnly).id());
StaffEngineer's avatar
StaffEngineer committed
    });
sam edelsten's avatar
sam edelsten committed
    // Set active editor
StaffEngineer's avatar
StaffEngineer committed
    commands.insert_resource(Focus(id));
StaffEngineer's avatar
StaffEngineer committed
}

fn main() {
    let font_bytes: &[u8] = include_bytes!("../assets/fonts/VictorMono-Regular.ttf");
    let font_config = CosmicFontConfig {
        fonts_dir_path: None,
        font_bytes: Some(vec![font_bytes]),
        load_system_fonts: true,
    };

StaffEngineer's avatar
StaffEngineer committed
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(CosmicEditPlugin { font_config })
StaffEngineer's avatar
StaffEngineer committed
        .add_systems(Startup, setup)
        .run();
}