~prokop/lebka

3cea79202c04ccc84f610b7869baf2b014b4cd1a — Prokop Randacek 1 year, 4 months ago cf1ee99 master
cleanup
5 files changed, 102 insertions(+), 117 deletions(-)

M index.lua
M src/layout.rs
M src/lib.rs
M src/main.rs
M src/sandbox.rs
M index.lua => index.lua +11 -10
@@ 1,19 1,20 @@
local font = readfont(fetch("/usr/share/fonts/inconsolata/Inconsolata-Bold.otf"))
local bold = readfont(fetch("/usr/share/fonts/inconsolata/Inconsolata-Bold.otf"))
local font = readfont(fetch("/usr/share/fonts/inconsolata/Inconsolata-Regular.otf"))

local text_color = { 238 / 255, 238 / 255, 238 / 255 }
local link_color = { 140 / 255, 194 / 255, 221 / 255, }

function Title(text)
	return Box {
		text = text, font = font,
		fS = 42, Fc = text_color,
		text = text, font = bold,
		font_size = 42, Fc = text_color,
	}
end

function Link(text)
	return Box {
		text = text, font = font,
		fS = 24, Fc = link_color,
		font_size = 24, Fc = link_color,
		t = 20 * Px,
		r = 40 * Px,
	}


@@ 22,7 23,7 @@ end
function NavItem(text)
	return Box {
		text = text, font = font,
		fS = 24, Fc = link_color,
		font_size = 24, Fc = link_color,
		t = 20 * Px,
		r = 40 * Px,
	}


@@ 30,16 31,16 @@ end

function H1(text)
	return Box {
		text = text, font = font,
		fS = 52, Fc = text_color,
		text = text, font = bold,
		font_size = 52, Fc = text_color,
		t = 20 * Px,
	}
end

function H2(text)
	return Box {
		text = text, font = font,
		fS = 30, Fc = text_color,
		text = text, font = bold,
		font_size = 30, Fc = text_color,
		t = 20 * Px,
		b = 20 * Px,
	}


@@ 48,7 49,7 @@ end
function P(text)
	return Box {
		text = text, font = font,
		fS = 24, Fc = text_color,
		font_size = 24, Fc = text_color,
		t = 20 * Px,
	}
end

M src/layout.rs => src/layout.rs +12 -16
@@ 147,7 147,7 @@ impl Default for Box {
            padding_bottom: StyleValue::default(),
            padding_right: StyleValue::default(),
            padding_left: StyleValue::default(),
            background_color: Color::from_rgba(0.0, 0.0, 0.0, 1.1),
            background_color: Color::TRANSPARENT,
            ty: BoxType::default(),
        }
    }


@@ 174,7 174,6 @@ impl Box {
        cmds
    }

    /// calculates size of the box and writes commands to render it
    fn layout_intern(&self, max_size: Vec2, cmds: &mut Vec<Command>) -> Vec2 {
        match &self.ty {
            BoxType::Node { layout, children } => {


@@ 392,24 391,21 @@ impl Box {
                            TextOptions::new().with_wrap_to_width(text_width, TextAlignment::Left),
                        );

                        let text_width = f32::min(text_width, text_box.width()); // it could be smaller
                        let text_height = self.height.eval(max_size.y, text_box.height());

                        (
                            Vec2::new(text_width, text_height),
                            Vec2::new(
                                f32::min(text_width, text_box.width()),
                                self.height.eval(max_size.y, text_box.height()),
                            ),
                            RectContent::Text(*text_color, text_box),
                        )
                    }
                    LeafType::Image(image) => {
                        let image_width = self.width.eval(max_size.x, max_size.x);

                        let image_height = image.size().y as f32;

                        (
                            Vec2::new(image_width, image_height),
                            RectContent::Image(image.clone()),
                        )
                    }
                    LeafType::Image(image) => (
                        Vec2::new(
                            self.width.eval(max_size.x, max_size.x),
                            image.size().y as f32,
                        ),
                        RectContent::Image(image.clone()),
                    ),
                };

                cmds.push(Command::DrawRect(Rect::new(

M src/lib.rs => src/lib.rs +2 -7
@@ 31,15 31,10 @@ pub enum Error {
}

pub async fn run_url(url: &str) -> Result<(), Error> {
    let code = fetch::fetch(url).await?;
    run(code).await
    run(fetch::fetch(url).await?).await
}

pub async fn run(code: Vec<u8>) -> Result<(), Error> {
    let sandbox = Sandbox::new()?;
    let root = sandbox.spawn_frame(&code).await?;

    let render = Lebka::new(root);
    render.run();
    Lebka::new(Sandbox::new()?.spawn_frame(&code).await?).run();
    Ok(())
}

M src/main.rs => src/main.rs +2 -7
@@ 4,16 4,11 @@ use std::env;

#[tokio::main]
async fn main() -> Result<(), lebka::Error> {
    let args: Vec<String> = env::args().collect();

    let url = match args.len() {
        2 => &args[1],
    match &env::args().collect::<Vec<String>>()[..] {
        [_, url] => lebka::run_url(url).await?,
        _ => {
            println!("usage: lebka <url>");
            return Ok(());
        }
    };

    lebka::run_url(url).await?;
    Ok(())
}

M src/sandbox.rs => src/sandbox.rs +75 -77
@@ 145,33 145,33 @@ impl<'lua> FromLua<'lua> for LuaColor {
                let string = s.to_str()?;
                match string.len() {
                    3 => Ok(LuaColor {
                        color: Color::from_rgb(
                            f32::from(u8::from_str_radix(&string[0..=0], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[1..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=2], 16).to_lua_err()?) / 255.0,
                        color: Color::from_int_rgb(
                            u8::from_str_radix(&string[0..=0], 16).to_lua_err()?,
                            u8::from_str_radix(&string[1..=1], 16).to_lua_err()?,
                            u8::from_str_radix(&string[2..=2], 16).to_lua_err()?,
                        ),
                    }),
                    4 => Ok(LuaColor {
                        color: Color::from_rgba(
                            f32::from(u8::from_str_radix(&string[0..=0], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[1..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=2], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[3..=3], 16).to_lua_err()?) / 255.0,
                        color: Color::from_int_rgba(
                            u8::from_str_radix(&string[0..=0], 16).to_lua_err()?,
                            u8::from_str_radix(&string[1..=1], 16).to_lua_err()?,
                            u8::from_str_radix(&string[2..=2], 16).to_lua_err()?,
                            u8::from_str_radix(&string[3..=3], 16).to_lua_err()?,
                        ),
                    }),
                    6 => Ok(LuaColor {
                        color: Color::from_rgb(
                            f32::from(u8::from_str_radix(&string[0..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=3], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[4..=5], 16).to_lua_err()?) / 255.0,
                        color: Color::from_int_rgb(
                            u8::from_str_radix(&string[0..=1], 16).to_lua_err()?,
                            u8::from_str_radix(&string[2..=3], 16).to_lua_err()?,
                            u8::from_str_radix(&string[4..=5], 16).to_lua_err()?,
                        ),
                    }),
                    8 => Ok(LuaColor {
                        color: Color::from_rgba(
                            f32::from(u8::from_str_radix(&string[0..=1], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[2..=3], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[4..=5], 16).to_lua_err()?) / 255.0,
                            f32::from(u8::from_str_radix(&string[6..=7], 16).to_lua_err()?) / 255.0,
                        color: Color::from_int_rgba(
                            u8::from_str_radix(&string[0..=1], 16).to_lua_err()?,
                            u8::from_str_radix(&string[2..=3], 16).to_lua_err()?,
                            u8::from_str_radix(&string[4..=5], 16).to_lua_err()?,
                            u8::from_str_radix(&string[6..=7], 16).to_lua_err()?,
                        ),
                    }),
                    _ => Err(LuaError::RuntimeError(


@@ 233,78 233,76 @@ impl Sandbox {
    }

    async fn lua_fetch(_lua: &Lua, s: String) -> Result<Vec<u8>, mlua::Error> {
        let data = fetch::fetch(&s).await.to_lua_err()?;
        Ok(data)
        fetch::fetch(&s).await.to_lua_err()
    }

    async fn lua_frame(lua: &Lua, s: String) -> Result<(), mlua::Error> {
        let code = fetch::fetch(&s).await.to_lua_err()?;
        Self::spawn_frame_intern(lua, &code).await.to_lua_err()?;
        Self::spawn_frame_intern(lua, &fetch::fetch(&s).await.to_lua_err()?)
            .await
            .to_lua_err()?;
        Ok(())
    }

    #[allow(clippy::needless_pass_by_value)]
    fn lua_readfont(_lua: &Lua, data: Vec<u8>) -> Result<LuaFont, mlua::Error> {
        let font = Font::new(&data)
            .or(Err(Error::FontCreateError))
            .to_lua_err()?;
        Ok(LuaFont { font })
        Ok(LuaFont {
            font: Font::new(&data)
                .or(Err(Error::FontCreateError))
                .to_lua_err()?,
        })
    }

    #[allow(clippy::unnecessary_wraps)]
    #[allow(clippy::needless_pass_by_value)] // needed as this is a mlua callback
    fn lua_box(_lua: &Lua, t: Table) -> Result<Box, mlua::Error> {
        let mut b = Box::default();

        if let Ok(v) = t.get::<&str, String>("text") {
            b.ty = BoxType::Leaf(LeafType::Text {
                color: t
                    .get::<&str, LuaColor>("Fc")
                    .unwrap_or(LuaColor {
                        color: Color::WHITE,
                    })
                    .color,
                text: v,
                size: t.get("fS").unwrap_or(48.0),
                font: t
                    .get::<&str, LuaFont>("font")
                    .or(Err(Error::TextWithoutFont))
                    .to_lua_err()?
                    .font,
            });
        } else if t.get::<f32, Box>(1.0).is_ok() {
            b.ty = BoxType::Node {
                layout: NodeLayout {
                    direction: t.get("direction").unwrap_or(Direction::default()),
                    origin: t.get("origin").unwrap_or(Origin::default()),
                    spacing: t.get("spacing").unwrap_or(Spacing::default()),
                    align: Align::default(),
                    overflow: t.get("overflow").unwrap_or(Overflow::default()),
                },
                children: t
                    .clone()
                    .sequence_values::<Box>()
                    .map_while(|x| if let Ok(v) = x { Some(v) } else { None })
                    .collect(),
            };
        }

        b.width = t.get("w").unwrap_or_default();
        b.height = t.get("h").unwrap_or_default();

        b.padding_top = t.get("t").unwrap_or_default();
        b.padding_left = t.get("l").unwrap_or_default();
        b.padding_bottom = t.get("b").unwrap_or_default();
        b.padding_right = t.get("r").unwrap_or_default();

        b.background_color = t
            .get::<&str, LuaColor>("c")
            .unwrap_or(LuaColor {
                color: Color::TRANSPARENT,
            })
            .color;

        Ok(b)
        Ok(Box {
            width: t.get("w").unwrap_or_default(),
            height: t.get("h").unwrap_or_default(),
            padding_top: t.get("t").unwrap_or_default(),
            padding_bottom: t.get("b").unwrap_or_default(),
            padding_left: t.get("l").unwrap_or_default(),
            padding_right: t.get("r").unwrap_or_default(),
            background_color: t
                .get::<&str, LuaColor>("c")
                .unwrap_or(LuaColor {
                    color: Color::TRANSPARENT,
                })
                .color,
            ty: if let Ok(v) = t.get::<&str, String>("text") {
                BoxType::Leaf(LeafType::Text {
                    color: t
                        .get::<&str, LuaColor>("Fc")
                        .unwrap_or(LuaColor {
                            color: Color::WHITE,
                        })
                        .color,
                    text: v,
                    size: t.get("font_size").unwrap_or(48.0),
                    font: t
                        .get::<&str, LuaFont>("font")
                        .or(Err(Error::TextWithoutFont))
                        .to_lua_err()?
                        .font,
                })
            } else if t.get::<i32, Box>(1).is_ok() {
                BoxType::Node {
                    layout: NodeLayout {
                        direction: t.get("direction").unwrap_or(Direction::default()),
                        origin: t.get("origin").unwrap_or(Origin::default()),
                        spacing: t.get("spacing").unwrap_or(Spacing::default()),
                        align: Align::default(),
                        overflow: t.get("overflow").unwrap_or(Overflow::default()),
                    },
                    children: t
                        .clone()
                        .sequence_values::<Box>()
                        .map_while(|x| if let Ok(v) = x { Some(v) } else { None })
                        .collect(),
                }
            } else {
                BoxType::default()
            },
        })
    }

    fn generate_environment(lua: &Lua) -> Result<Table, Error> {