2022-10-27 16:16:28 -06:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
|
|
|
|
|
use cosmic::iced_native::{
|
|
|
|
|
{Color, Element, Length, Point, Rectangle, Size, Theme},
|
|
|
|
|
layout::{self, Layout},
|
|
|
|
|
renderer,
|
|
|
|
|
widget::{self, tree, Widget},
|
|
|
|
|
};
|
|
|
|
|
use cosmic_text::{
|
|
|
|
|
Attrs,
|
|
|
|
|
AttrsList,
|
|
|
|
|
SwashCache,
|
2022-10-31 11:24:36 -06:00
|
|
|
BufferLine,
|
|
|
|
|
Metrics,
|
2022-10-27 16:16:28 -06:00
|
|
|
};
|
|
|
|
|
use std::{
|
|
|
|
|
cmp,
|
|
|
|
|
sync::Mutex,
|
|
|
|
|
time::Instant,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub struct Appearance {
|
|
|
|
|
background_color: Option<Color>,
|
2022-10-27 18:20:29 -06:00
|
|
|
text_color: Color,
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait StyleSheet {
|
|
|
|
|
fn appearance(&self) -> Appearance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl StyleSheet for Theme {
|
|
|
|
|
fn appearance(&self) -> Appearance {
|
2022-10-27 18:20:29 -06:00
|
|
|
match self {
|
|
|
|
|
Theme::Dark => Appearance {
|
|
|
|
|
background_color: None,
|
|
|
|
|
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
|
|
|
|
|
},
|
|
|
|
|
Theme::Light => Appearance {
|
|
|
|
|
background_color: None,
|
|
|
|
|
text_color: Color::from_rgb8(0x00, 0x00, 0x00),
|
|
|
|
|
},
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Text {
|
2022-10-31 11:24:36 -06:00
|
|
|
line: BufferLine<'static>,
|
|
|
|
|
metrics: Metrics,
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Text {
|
|
|
|
|
pub fn new(string: &str) -> Self {
|
2022-10-27 17:39:47 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
//TODO: make it possible to set attrs
|
2022-10-31 11:24:36 -06:00
|
|
|
let mut line = BufferLine::new(
|
2022-10-27 17:39:47 -06:00
|
|
|
string,
|
|
|
|
|
AttrsList::new(Attrs::new())
|
|
|
|
|
);
|
|
|
|
|
|
2022-10-31 11:40:01 -06:00
|
|
|
//TODO: do we have to immediately shape?
|
2022-10-27 17:39:47 -06:00
|
|
|
line.shape(&crate::FONT_SYSTEM);
|
|
|
|
|
|
|
|
|
|
let text = Self {
|
|
|
|
|
line,
|
2022-10-31 11:24:36 -06:00
|
|
|
metrics: Metrics::new(14, 20),
|
2022-10-27 17:39:47 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
log::debug!("Text::new in {:?}", instant.elapsed());
|
|
|
|
|
|
|
|
|
|
text
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn text(string: &str) -> Text {
|
|
|
|
|
Text::new(string)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<Message, Renderer> Widget<Message, Renderer> for Text
|
|
|
|
|
where
|
|
|
|
|
Renderer: renderer::Renderer,
|
|
|
|
|
Renderer::Theme: StyleSheet,
|
|
|
|
|
{
|
|
|
|
|
fn tag(&self) -> tree::Tag {
|
|
|
|
|
tree::Tag::of::<State>()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn state(&self) -> tree::State {
|
|
|
|
|
tree::State::new(State::new())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn width(&self) -> Length {
|
|
|
|
|
Length::Shrink
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn height(&self) -> Length {
|
|
|
|
|
Length::Shrink
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
|
&self,
|
|
|
|
|
_renderer: &Renderer,
|
|
|
|
|
limits: &layout::Limits,
|
|
|
|
|
) -> layout::Node {
|
2022-10-27 17:39:47 -06:00
|
|
|
let instant = Instant::now();
|
2022-10-27 16:16:28 -06:00
|
|
|
|
2022-10-27 17:39:47 -06:00
|
|
|
let shape = self.line.shape_opt().as_ref().unwrap();
|
|
|
|
|
|
|
|
|
|
//TODO: can we cache this?
|
|
|
|
|
let mut layout_lines = Vec::new();
|
|
|
|
|
shape.layout(
|
|
|
|
|
self.metrics.font_size,
|
|
|
|
|
limits.max().width as i32,
|
|
|
|
|
&mut layout_lines,
|
|
|
|
|
0,
|
|
|
|
|
self.line.wrap_simple()
|
|
|
|
|
);
|
2022-10-27 16:16:28 -06:00
|
|
|
|
2022-10-27 20:31:20 -06:00
|
|
|
let mut width = 0;
|
|
|
|
|
let mut height = 0;
|
2022-10-27 17:39:47 -06:00
|
|
|
|
|
|
|
|
for layout_line in layout_lines {
|
2022-10-27 16:16:28 -06:00
|
|
|
for glyph in layout_line.glyphs.iter() {
|
2022-10-27 20:31:20 -06:00
|
|
|
width = cmp::max(width, (glyph.x + glyph.w) as i32 + 1);
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
2022-10-27 20:31:20 -06:00
|
|
|
height += self.metrics.line_height;
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
|
|
|
|
|
2022-10-27 20:31:20 -06:00
|
|
|
let size = Size::new(width as f32, height as f32);
|
2022-10-27 17:39:47 -06:00
|
|
|
|
|
|
|
|
log::debug!("layout {:?} in {:?}", size, instant.elapsed());
|
2022-10-27 16:16:28 -06:00
|
|
|
|
2022-10-27 17:39:47 -06:00
|
|
|
layout::Node::new(size)
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
|
&self,
|
|
|
|
|
tree: &widget::Tree,
|
|
|
|
|
renderer: &mut Renderer,
|
|
|
|
|
theme: &Renderer::Theme,
|
|
|
|
|
_style: &renderer::Style,
|
|
|
|
|
layout: Layout<'_>,
|
|
|
|
|
_cursor_position: Point,
|
|
|
|
|
_viewport: &Rectangle,
|
|
|
|
|
) {
|
2022-10-27 17:39:47 -06:00
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
2022-10-27 16:16:28 -06:00
|
|
|
let state = tree.state.downcast_ref::<State>();
|
|
|
|
|
|
2022-10-27 18:20:29 -06:00
|
|
|
let appearance = theme.appearance();
|
|
|
|
|
|
|
|
|
|
if let Some(background_color) = appearance.background_color {
|
2022-10-27 16:16:28 -06:00
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds: layout.bounds(),
|
|
|
|
|
border_radius: 0.0,
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
background_color
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 18:20:29 -06:00
|
|
|
let text_color = cosmic_text::Color::rgba(
|
|
|
|
|
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
|
|
|
|
|
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
|
|
|
|
|
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
|
|
|
|
|
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
|
|
|
|
|
);
|
|
|
|
|
|
2022-10-27 17:39:47 -06:00
|
|
|
let shape = self.line.shape_opt().as_ref().unwrap();
|
|
|
|
|
|
|
|
|
|
//TODO: can we cache this?
|
|
|
|
|
let mut layout_lines = Vec::new();
|
|
|
|
|
shape.layout(
|
|
|
|
|
self.metrics.font_size,
|
2022-10-27 20:31:20 -06:00
|
|
|
layout.bounds().width as i32,
|
2022-10-27 17:39:47 -06:00
|
|
|
&mut layout_lines,
|
|
|
|
|
0,
|
|
|
|
|
self.line.wrap_simple()
|
|
|
|
|
);
|
|
|
|
|
|
2022-10-27 16:16:28 -06:00
|
|
|
let mut cache = state.cache.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
let mut line_y = self.metrics.font_size;
|
2022-10-27 17:39:47 -06:00
|
|
|
for layout_line in layout_lines {
|
2022-10-27 16:16:28 -06:00
|
|
|
for glyph in layout_line.glyphs.iter() {
|
|
|
|
|
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);
|
|
|
|
|
|
|
|
|
|
let glyph_color = match glyph.color_opt {
|
|
|
|
|
Some(some) => some,
|
2022-10-27 18:20:29 -06:00
|
|
|
None => text_color,
|
2022-10-27 16:16:28 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
|
|
|
|
|
let a = color.a();
|
|
|
|
|
if a > 0 {
|
|
|
|
|
let r = color.r();
|
|
|
|
|
let g = color.g();
|
|
|
|
|
let b = color.b();
|
|
|
|
|
renderer.fill_quad(
|
|
|
|
|
renderer::Quad {
|
|
|
|
|
bounds: Rectangle::new(
|
|
|
|
|
Point::new(
|
|
|
|
|
layout.bounds().x + (x_int + x) as f32,
|
|
|
|
|
layout.bounds().y + (line_y + y_int + y) as f32
|
|
|
|
|
),
|
|
|
|
|
Size::new(1.0, 1.0)
|
|
|
|
|
),
|
|
|
|
|
border_radius: 0.0,
|
|
|
|
|
border_width: 0.0,
|
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
|
},
|
|
|
|
|
Color::from_rgba8(r, g, b, a as f32 / 255.0),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
line_y += self.metrics.line_height;
|
|
|
|
|
}
|
2022-10-27 17:39:47 -06:00
|
|
|
|
2022-10-27 20:31:20 -06:00
|
|
|
log::trace!("draw {:?} in {:?}", layout.bounds(), instant.elapsed());
|
2022-10-27 16:16:28 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message, Renderer> From<Text> for Element<'a, Message, Renderer>
|
|
|
|
|
where
|
|
|
|
|
Renderer: renderer::Renderer,
|
|
|
|
|
Renderer::Theme: StyleSheet,
|
|
|
|
|
{
|
|
|
|
|
fn from(text: Text) -> Self {
|
|
|
|
|
Self::new(text)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct State {
|
|
|
|
|
cache: Mutex<SwashCache<'static>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
|
/// Creates a new [`State`].
|
|
|
|
|
pub fn new() -> State {
|
|
|
|
|
let instant = Instant::now();
|
|
|
|
|
|
|
|
|
|
let state = State {
|
|
|
|
|
cache: Mutex::new(SwashCache::new(&crate::FONT_SYSTEM)),
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-27 17:39:47 -06:00
|
|
|
log::debug!("created state in {:?}", instant.elapsed());
|
2022-10-27 16:16:28 -06:00
|
|
|
|
|
|
|
|
state
|
|
|
|
|
}
|
|
|
|
|
}
|