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;

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{FontSystem, TextAction, TextBuffer, TextMetrics};
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, thread, time::{Duration, Instant}};
@ -83,6 +83,8 @@ fn main() {
default_text.to_string()
};
let mut swash_cache = SwashCache::new();
let line_x = 8 * display_scale;
let mut buffer = TextBuffer::new(
font_matches,
@ -110,7 +112,7 @@ fn main() {
window.set(bg_color);
buffer.draw(font_color.data, |x, y, w, h, color| {
buffer.draw(&mut swash_cache, font_color.data, |x, y, w, h, color| {
window.rect(line_x + x, y, w, h, Color { data: color });
});

View file

@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic_text::{FontSystem, TextAction, TextBuffer, TextMetrics};
use cosmic_text::{FontSystem, SwashCache, TextAction, TextBuffer, TextMetrics};
use orbclient::{Color, EventOption, Renderer, Window, WindowFlag};
use std::{env, fs, process, thread, time::{Duration, Instant}};
use unicode_segmentation::UnicodeSegmentation;
fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>) {
fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>, swash_cache: &mut SwashCache) {
let bg_color = Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
@ -16,7 +16,7 @@ fn redraw(window: &mut Window, buffer: &mut TextBuffer<'_>) {
window.set(bg_color);
buffer.draw(font_color.data, |x, y, w, h, color| {
buffer.draw(swash_cache, font_color.data, |x, y, w, h, color| {
window.rect(x, y, w, h, Color { data: color });
});
@ -94,6 +94,8 @@ fn main() {
window.height() as i32
);
let mut swash_cache = SwashCache::new();
let text = if let Some(arg) = env::args().nth(1) {
fs::read_to_string(&arg).expect("failed to open file")
} else {
@ -158,7 +160,7 @@ fn main() {
// Finally, normal enter
buffer.action(TextAction::Enter);
redraw(&mut window, &mut buffer);
redraw(&mut window, &mut buffer, &mut swash_cache);
for event in window.events() {
match event.to_option() {