Add function to get layout runs

This commit is contained in:
Jeremy Soller 2022-10-25 11:40:10 -06:00
parent 5603e30a29
commit 330a736136
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
6 changed files with 254 additions and 238 deletions

View file

@ -25,6 +25,7 @@ use cosmic::{
use cosmic_text::{
FontMatches,
FontSystem,
SwashCache,
TextBuffer,
TextMetrics,
};
@ -63,6 +64,7 @@ pub struct Window {
theme: Theme,
path_opt: Option<PathBuf>,
buffer: Mutex<TextBuffer<'static>>,
cache: Mutex<SwashCache>,
}
#[allow(dead_code)]
@ -139,6 +141,7 @@ impl Application for Window {
theme: Theme::Dark,
path_opt: None,
buffer: Mutex::new(buffer),
cache: Mutex::new(SwashCache::new()),
};
if let Some(arg) = env::args().nth(1) {
window.open(PathBuf::from(arg));
@ -230,7 +233,7 @@ impl Application for Window {
.align_items(Alignment::Center)
.spacing(8)
,
text_box(&self.buffer)
text_box(&self.buffer, &self.cache)
]
.spacing(8)
.padding(16)

View file

@ -14,6 +14,7 @@ use cosmic::iced_native::{
widget::{self, tree, Widget},
};
use cosmic_text::{
SwashCache,
TextAction,
TextBuffer,
};
@ -62,16 +63,17 @@ impl StyleSheet for Theme {
pub struct TextBox<'a> {
buffer: &'a Mutex<TextBuffer<'static>>,
cache: &'a Mutex<SwashCache>,
}
impl<'a> TextBox<'a> {
pub fn new(buffer: &'a Mutex<TextBuffer<'static>>) -> Self {
Self { buffer }
pub fn new(buffer: &'a Mutex<TextBuffer<'static>>, cache: &'a Mutex<SwashCache>) -> Self {
Self { buffer, cache }
}
}
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>) -> TextBox<'a> {
TextBox::new(buffer)
pub fn text_box<'a>(buffer: &'a Mutex<TextBuffer<'static>>, cache: &'a Mutex<SwashCache>) -> TextBox<'a> {
TextBox::new(buffer, cache)
}
impl<'a, Message, Renderer> Widget<Message, Renderer> for TextBox<'a>
@ -153,12 +155,13 @@ where
}
let mut buffer = self.buffer.lock().unwrap();
let mut cache = self.cache.lock().unwrap();
buffer.shape_until_cursor();
let buffer_x = layout.bounds().x;
let buffer_y = layout.bounds().y;
buffer.draw(text_color_u32, |x, y, w, h, color| {
buffer.draw(&mut cache, text_color_u32, |x, y, w, h, color| {
let a = (color >> 24) as u8;
if a > 0 {
let r = (color >> 16) as u8;