Skip to content

Commit 3d7643f

Browse files
authored
fix: use context to share style_map instead of use global hashmap (#98)
1 parent 402c8ff commit 3d7643f

File tree

8 files changed

+28
-177
lines changed

8 files changed

+28
-177
lines changed

Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ rgb = "0.8.50"
2222
derive_builder = "0.20.2"
2323
chrono = "0.4.38"
2424
base64 = "0.22.1"
25-
once_cell = "1.20.2"
2625
serde_json = "1.0.132"
2726
ansi_term = "0.12.1"
2827
strip-ansi-escapes = "0.2.1"
2928
cansi = "2.2.1"
30-
lazy_static = "1.5.0"
3129
include_dir = "0.7.4"
3230
schemars = "0.8.22"
3331
reqwest = "0.12.15"

core/src/components/highlight_code_block.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
use std::{collections::HashMap, sync::Mutex};
2+
13
use crate::{config::HighlightLine, edges::padding::Padding, utils::color::RgbaColor};
24

35
use super::{
46
editor::code::CODE_LINE_HEIGHT,
57
interface::{
6-
component::{query_style, Component, ComponentContext, RenderParams},
8+
component::{Component, ComponentContext, RenderParams},
79
style::ComponentStyle,
810
},
911
};
@@ -46,6 +48,7 @@ impl Component for HighlightCodeBlock {
4648
}
4749
};
4850
let (rect, paint) = self.draw_highlight_line(
51+
&context.style_map,
4952
render_params,
5053
parent_style,
5154
*start_line_number,
@@ -81,6 +84,7 @@ impl HighlightCodeBlock {
8184

8285
fn draw_highlight_line(
8386
&self,
87+
style_map: &Mutex<HashMap<&'static str, ComponentStyle>>,
8488
render_params: &RenderParams,
8589
parent_style: &ComponentStyle,
8690
start_line_number: u32,
@@ -90,6 +94,7 @@ impl HighlightCodeBlock {
9094
// If the start_line_number is greater than end_line_number, swap them
9195
if start_line_number > end_line_number {
9296
return self.draw_highlight_line(
97+
style_map,
9398
render_params,
9499
parent_style,
95100
end_line_number,
@@ -98,7 +103,8 @@ impl HighlightCodeBlock {
98103
);
99104
}
100105

101-
let editor_style = query_style("RectInnerLayer").unwrap();
106+
let style_map = style_map.lock().unwrap();
107+
let editor_style = style_map.get("RectInnerLayer").unwrap();
102108
let end_line_number = end_line_number.min(self.code_line_count as u32);
103109
let mut paint = Paint::default();
104110
// If the start line number is start at n, the y offset should be (n - 1) * line_height

core/src/components/interface/component.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,18 @@ use crate::{
77
edges::edge::Edge,
88
utils::{text::FontRenderer, theme_provider::ThemeProvider},
99
};
10-
use lazy_static::lazy_static;
1110
use std::{
1211
collections::HashMap,
1312
sync::{Arc, Mutex},
1413
};
1514
use tiny_skia::Pixmap;
1615

17-
lazy_static! {
18-
// The style parse process is recursive, there may some components style to be reculculated
19-
// many times, so we cache the style to avoid reculculate
20-
// The key is the component name, which defined in the Component trait
21-
static ref STYLE_MAP: Mutex<HashMap<&'static str, Style<f32>>> = Mutex::new(HashMap::new());
22-
}
23-
24-
pub(crate) fn query_style(name: &'static str) -> Option<Style<f32>> {
25-
let style_map = STYLE_MAP.lock().unwrap();
26-
27-
style_map.get(name).cloned()
28-
}
29-
3016
pub struct ComponentContext {
3117
pub scale_factor: f32,
3218
pub take_snapshot_params: Arc<SnapshotConfig>,
3319
pub theme_provider: ThemeProvider,
3420
pub font_renderer: Mutex<FontRenderer>,
21+
pub style_map: Mutex<HashMap<&'static str, Style<f32>>>,
3522
}
3623

3724
#[derive(Default, Clone)]
@@ -127,7 +114,7 @@ pub trait Component {
127114
) -> Style<f32> {
128115
let name = self.name();
129116

130-
if let Some(style) = STYLE_MAP.lock().unwrap().get(name) {
117+
if let Some(style) = context.style_map.lock().unwrap().get(name) {
131118
if name != "STUB_COMPONENT" {
132119
return style.clone();
133120
}
@@ -166,7 +153,12 @@ pub trait Component {
166153
margin: style.margin,
167154
};
168155

169-
STYLE_MAP.lock().unwrap().insert(self.name(), style.clone());
156+
context
157+
.style_map
158+
.lock()
159+
.unwrap()
160+
.insert(self.name(), style.clone());
161+
170162
style
171163
}
172164

core/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub mod assets;
2626
mod components;
2727
pub mod config;
2828
pub mod edges;
29-
pub mod preset_background;
3029
pub mod snapshot;
3130
pub mod themes;
3231
pub mod utils;

core/src/preset_background.rs

Lines changed: 0 additions & 149 deletions
This file was deleted.

core/src/snapshot/image_snapshot.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use std::sync::{Arc, Mutex};
1+
use std::{
2+
collections::HashMap,
3+
sync::{Arc, Mutex},
4+
};
25

36
use crate::{
47
components::{
58
command_line::{
69
command_line_header::CommandLineHeader, command_line_output::CommandLineOutput,
710
},
8-
interface::component::Component,
11+
interface::{component::Component, style::Style},
912
layout::{column::Column, row::Row},
1013
},
1114
config::{self, CommandLineContent, SnapshotConfig, DEFAULT_WINDOW_MARGIN},
@@ -91,6 +94,10 @@ impl ImageSnapshot {
9194
window_padding: Padding,
9295
) -> Box<dyn Fn(Vec<Box<dyn Component>>) -> anyhow::Result<Pixmap>> {
9396
Box::new(move |render_content| {
97+
// The style parse process is recursive, there may some components style to be reculculated
98+
// many times, so we cache the style to avoid reculculate
99+
// The key is the component name, which defined in the Component trait
100+
let style_map: Mutex<HashMap<&'static str, Style<f32>>> = Mutex::new(HashMap::new());
94101
let editor_background_color = theme_provider.theme_background();
95102
let font_renderer = Mutex::new(FontRenderer::new(
96103
config.scale_factor as f32,
@@ -101,6 +108,7 @@ impl ImageSnapshot {
101108
take_snapshot_params: Arc::new(config.clone()),
102109
theme_provider: theme_provider.clone(),
103110
font_renderer,
111+
style_map,
104112
};
105113
let background_padding = Padding::from(config.window.margin.clone());
106114
let border_rgba_color: RgbaColor = config.window.border.color.as_str().into();

core/src/utils/text.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::sync::Arc;
22

33
use cosmic_text::{
4-
fontdb::{Database, Source},
5-
Align, Attrs, AttrsList, Buffer, BufferLine, Color, Family, FontSystem, LayoutRunIter,
6-
LineEnding, Metrics, Shaping, SwashCache,
4+
fontdb::Source, Align, Attrs, AttrsList, Buffer, BufferLine, Color, Family, FontSystem,
5+
LayoutRunIter, LineEnding, Metrics, Shaping, SwashCache,
76
};
87
use tiny_skia::{Paint, Pixmap, Rect, Transform};
98

0 commit comments

Comments
 (0)