Skip to content

Commit

Permalink
[Perf] cache the function that used to calculate width and height of (#…
Browse files Browse the repository at this point in the history
…90)

dynamic text
  • Loading branch information
mistricky authored Apr 25, 2024
1 parent 6620649 commit ef08881
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 14 deletions.
157 changes: 150 additions & 7 deletions generator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ arboard = {features = ["wayland-data-control"], version = "3.3.2"}
thiserror = "1.0.58"
regex = "1.10.3"
two-face = "0.3.0"
cached = "0.49.3"

[lib]
crate-type = ["cdylib"]
4 changes: 3 additions & 1 deletion generator/src/code.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cached::proc_macro::cached;
use regex::Regex;

const MIN_WIDTH: f32 = 100.;
Expand All @@ -12,6 +13,7 @@ fn min_width(width: f32) -> f32 {

// Because the code block is input by users, we need to calculate the width and height
// to make sure render the width and height of the "editor" shape correctly
#[cached(key = "String", convert = r#"{ format!("{}", text) }"#)]
pub fn calc_wh(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) {
let trimmed_text = prepare_code(text);
let lines = trimmed_text.lines();
Expand All @@ -36,7 +38,7 @@ fn replace_tab_to_space(text: &str) -> String {
str::replace(text, "\t", &spaces)
}

// Find min indention of the line, and remove the same indention from subsequent lines
// Find min indention of code lines, and remove the same indention from subsequent lines
fn trim_space(text: &str) -> String {
let lines = text.split("\n").collect::<Vec<&str>>();
let regex = Regex::new(r"(?:^|\n)(\s*)").unwrap();
Expand Down
13 changes: 7 additions & 6 deletions generator/src/components/interface/component.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::sync::Arc;

use tiny_skia::Pixmap;

use crate::{config::TakeSnapshotParams, edges::edge::Edge};

use super::{
render_error,
style::{ComponentAlign, ComponentStyle, RawComponentStyle, Size, Style},
};
use crate::{config::TakeSnapshotParams, edges::edge::Edge};
use std::sync::Arc;
use tiny_skia::Pixmap;

pub struct ComponentContext {
pub scale_factor: f32,
Expand Down Expand Up @@ -152,6 +149,8 @@ pub trait Component {
Ok(render_params.clone())
}

// Dynamic calculate width and height of children, if the children is empty, get_dynamic_wh
// will return (0., 0.)
fn get_dynamic_wh(&self) -> (f32, f32) {
let children = self.children();
let calc_children_wh = |cb: fn((f32, f32), &Box<dyn Component>) -> (f32, f32)| {
Expand All @@ -160,11 +159,13 @@ pub trait Component {
let style = self.style();

match style.align {
// If align is row, width is sum of children width, height is max of children height
ComponentAlign::Row => calc_children_wh(|(w, h), child| {
let style = child.parsed_style();

(w + style.width, h.max(style.height))
}),
// If align is column, width is max of children width, height is sum of children height
ComponentAlign::Column => calc_children_wh(|(w, h), child| {
let style = child.parsed_style();

Expand Down

0 comments on commit ef08881

Please sign in to comment.