Make attribute spans use ranges and update docs

This commit is contained in:
Jeremy Soller 2022-10-27 09:56:53 -06:00
parent 16f0eb9efe
commit e4c8d4ba6b
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
8 changed files with 147 additions and 123 deletions

View file

@ -24,6 +24,8 @@ use cosmic::{
},
};
use cosmic_text::{
Attrs,
AttrsList,
FontSystem,
SwashCache,
TextBuffer,
@ -63,11 +65,9 @@ fn main() -> cosmic::iced::Result {
pub struct Window {
theme: Theme,
path_opt: Option<PathBuf>,
attrs: Attrs<'static>,
buffer: Mutex<TextBuffer<'static>>,
cache: Mutex<SwashCache<'static>>,
bold: bool,
italic: bool,
monospaced: bool,
}
#[allow(dead_code)]
@ -88,12 +88,12 @@ impl Window {
match fs::read_to_string(&path) {
Ok(text) => {
log::info!("opened '{}'", path.display());
buffer.set_text(&text);
buffer.set_text(&text, self.attrs);
self.path_opt = Some(path);
},
Err(err) => {
log::error!("failed to open '{}': {}", path.display(), err);
buffer.set_text("");
buffer.set_text("", self.attrs);
self.path_opt = None;
}
}
@ -107,14 +107,13 @@ impl Application for Window {
type Theme = Theme;
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let font_size_i = 1; // Body
let attrs = cosmic_text::Attrs::new()
.monospaced(true)
.family(cosmic_text::Family::Monospace);
let buffer = TextBuffer::new(
&FONT_SYSTEM,
attrs,
FONT_SIZES[font_size_i],
FONT_SIZES[1 /* Body */],
);
let cache = SwashCache::new(&FONT_SYSTEM);
@ -122,11 +121,9 @@ impl Application for Window {
let mut window = Window {
theme: Theme::Dark,
path_opt: None,
attrs,
buffer: Mutex::new(buffer),
cache: Mutex::new(cache),
bold: false,
italic: false,
monospaced: true,
};
if let Some(arg) = env::args().nth(1) {
window.open(PathBuf::from(arg));
@ -157,7 +154,7 @@ impl Application for Window {
if let Some(path) = &self.path_opt {
let buffer = self.buffer.lock().unwrap();
let mut text = String::new();
for line in buffer.text_lines() {
for line in buffer.lines.iter() {
text.push_str(line.text());
text.push('\n');
}
@ -172,39 +169,42 @@ impl Application for Window {
}
},
Message::Bold(bold) => {
self.bold = bold;
let mut buffer = self.buffer.lock().unwrap();
let attrs = buffer.attrs().clone().weight(if bold {
self.attrs = self.attrs.weight(if bold {
cosmic_text::Weight::BOLD
} else {
cosmic_text::Weight::NORMAL
});
buffer.set_attrs(attrs);
},
Message::Italic(italic) => {
self.italic = italic;
let mut buffer = self.buffer.lock().unwrap();
let attrs = buffer.attrs().clone().style(if italic {
for line in buffer.lines.iter_mut() {
line.set_attrs_list(AttrsList::new(self.attrs));
}
},
Message::Italic(italic) => {
self.attrs = self.attrs.style(if italic {
cosmic_text::Style::Italic
} else {
cosmic_text::Style::Normal
});
buffer.set_attrs(attrs);
},
Message::Monospaced(monospaced) => {
self.monospaced = monospaced;
let mut buffer = self.buffer.lock().unwrap();
let attrs = buffer.attrs().clone()
for line in buffer.lines.iter_mut() {
line.set_attrs_list(AttrsList::new(self.attrs));
}
},
Message::Monospaced(monospaced) => {
self.attrs = self.attrs
.family(if monospaced {
cosmic_text::Family::Monospace
} else {
cosmic_text::Family::SansSerif
})
.monospaced(monospaced);
buffer.set_attrs(attrs);
let mut buffer = self.buffer.lock().unwrap();
for line in buffer.lines.iter_mut() {
line.set_attrs_list(AttrsList::new(self.attrs));
}
},
Message::MetricsChanged(metrics) => {
let mut buffer = self.buffer.lock().unwrap();
@ -246,11 +246,11 @@ impl Application for Window {
button!("Save").on_press(Message::Save),
horizontal_space(Length::Fill),
text("Bold:"),
toggler(None, self.bold, Message::Bold),
toggler(None, self.attrs.weight == cosmic_text::Weight::BOLD, Message::Bold),
text("Italic:"),
toggler(None, self.italic, Message::Italic),
toggler(None, self.attrs.style == cosmic_text::Style::Italic, Message::Italic),
text("Monospaced:"),
toggler(None, self.monospaced, Message::Monospaced),
toggler(None, self.attrs.monospaced, Message::Monospaced),
text("Theme:"),
theme_picker,
text("Font Size:"),

View file

@ -179,27 +179,6 @@ where
}
});
/*
// Draw scrollbar
{
let start_line = start_line_opt.unwrap_or(end_line);
let lines = buffer.text_lines().len();
let start_y = (start_line.get() * window.height() as usize) / lines;
let end_y = (end_line.get() * window.height() as usize) / lines;
if end_y > start_y {
window.rect(
window.width() as i32 - line_x as i32,
start_y as i32,
line_x as u32,
(end_y - start_y) as u32,
Color::from_rgba8(0xFF, 0xFF, 0xFF, 0.25),
);
}
}
buffer.redraw = false;
*/
let duration = instant.elapsed();
log::trace!("redraw: {:?}", duration);
}

View file

@ -78,12 +78,8 @@ fn main() {
let mut font_size_i = font_size_default;
let line_x = 8 * display_scale;
let attrs = Attrs::new()
.monospaced(true)
.family(Family::Monospace);
let mut buffer = TextBuffer::new(
&font_system,
attrs,
font_sizes[font_size_i]
);
@ -92,7 +88,10 @@ fn main() {
window.height() as i32
);
buffer.set_text(&text);
let attrs = Attrs::new()
.monospaced(true)
.family(Family::Monospace);
buffer.set_text(&text, attrs);
let mut bg_color = orbclient::Color::rgb(0x00, 0x00, 0x00);
let mut font_color = Color::rgb(0xFF, 0xFF, 0xFF);
@ -176,8 +175,7 @@ fn main() {
let mut attrs_list = AttrsList::new(attrs);
for (style, _, range) in ranges {
attrs_list.add_span(
range.start,
range.end,
range,
attrs
.color(Color::rgba(
style.foreground.r,
@ -242,19 +240,19 @@ fn main() {
window.rect(line_x + x, y, w, h, orbclient::Color { data: color.0 })
});
let mut start_line_opt = None;
let mut end_line = 0;
for run in buffer.layout_runs() {
end_line = run.line_i;
if start_line_opt == None {
start_line_opt = Some(end_line);
}
}
// Draw scrollbar
{
let mut start_line_opt = None;
let mut end_line = 0;
for run in buffer.layout_runs() {
end_line = run.line_i;
if start_line_opt == None {
start_line_opt = Some(end_line);
}
}
let start_line = start_line_opt.unwrap_or(end_line);
let lines = buffer.text_lines().len();
let lines = buffer.lines.len();
let start_y = (start_line * window.height() as usize) / lines;
let end_y = (end_line * window.height() as usize) / lines;
if end_y > start_y {

View file

@ -31,10 +31,8 @@ fn main() {
)
.unwrap();
let attrs = Attrs::new();
let mut buffer = TextBuffer::new(
&font_system,
attrs,
TextMetrics::new(32, 44).scale(display_scale)
);
@ -43,6 +41,7 @@ fn main() {
window.height() as i32
);
let attrs = Attrs::new();
let serif_attrs = attrs.family(Family::Serif);
let mono_attrs = attrs.monospaced(true).family(Family::Monospace);
let comic_attrs = attrs.family(Family::Name("Comic Neue"));
@ -117,7 +116,7 @@ fn main() {
let start = line_text.len();
line_text.push_str(text);
let end = line_text.len();
attrs_list.add_span(start, end, attrs);
attrs_list.add_span(start..end, attrs);
}
buffer.lines.push(TextBufferLine::new(line_text, attrs_list));
}