- Add monospace_attrs() function in main.rs to centralize monospace font attributes creation - Replace duplicated Attrs::new().family(Family::Monospace) calls in: * line_number.rs: LineNumberCache::get() method * main.rs: App::init() font enumeration * tab.rs: EditorTab::new() constructor - Remove unused imports (Attrs, Family) from line_number.rs - Resolves TODO comments about code repetition across modules This change improves maintainability by having a single source of truth for monospace font configuration.
51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
use cosmic_text::{
|
|
Align, AttrsList, BufferLine, FontSystem, LayoutLine, LineEnding, Shaping, Wrap,
|
|
};
|
|
use std::collections::HashMap;
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
|
|
pub struct LineNumberKey {
|
|
pub number: usize,
|
|
pub width: usize,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct LineNumberCache {
|
|
cache: HashMap<LineNumberKey, Vec<LayoutLine>>,
|
|
}
|
|
|
|
impl LineNumberCache {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
cache: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn clear(&mut self) {
|
|
self.cache.clear();
|
|
}
|
|
|
|
pub fn get(&mut self, font_system: &mut FontSystem, key: LineNumberKey) -> &Vec<LayoutLine> {
|
|
self.cache.entry(key).or_insert_with(|| {
|
|
let attrs = crate::monospace_attrs();
|
|
let text = format!("{:width$}", key.number, width = key.width);
|
|
let mut buffer_line = BufferLine::new(
|
|
text,
|
|
LineEnding::default(),
|
|
AttrsList::new(&attrs),
|
|
Shaping::Advanced,
|
|
);
|
|
buffer_line.set_align(Some(Align::Left));
|
|
buffer_line
|
|
.layout(
|
|
font_system,
|
|
1.0, /* font size adjusted later */
|
|
None,
|
|
Wrap::None,
|
|
None,
|
|
8, /* default tab width */
|
|
)
|
|
.to_vec()
|
|
})
|
|
}
|
|
}
|