mirror of https://github.com/wayvr-org/wayvr.git
wgui: fix cosmic text not rendering anything
This commit is contained in:
parent
000bc3c166
commit
9f854d0649
|
|
@ -1,6 +1,6 @@
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use cosmic_text::{Attrs, AttrsList, Buffer, Metrics, Shaping, Wrap};
|
use cosmic_text::{Attrs, AttrsList, BorrowedWithFontSystem, Buffer, Metrics, Shaping, Wrap};
|
||||||
use slotmap::Key;
|
use slotmap::Key;
|
||||||
use taffy::AvailableSpace;
|
use taffy::AvailableSpace;
|
||||||
|
|
||||||
|
|
@ -68,6 +68,16 @@ impl WidgetLabel {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn with_buffer<F, R>(buffer: &Rc<RefCell<Buffer>>, globals: &Globals, func: F) -> R
|
||||||
|
where
|
||||||
|
F: FnOnce(BorrowedWithFontSystem<'_, Buffer>) -> R,
|
||||||
|
{
|
||||||
|
let mut font_system = globals.font_system.system.lock();
|
||||||
|
let mut buffer = buffer.borrow_mut();
|
||||||
|
let buffer = buffer.borrow_with(&mut font_system);
|
||||||
|
func(buffer)
|
||||||
|
}
|
||||||
|
|
||||||
// set text without layout/re-render update.
|
// set text without layout/re-render update.
|
||||||
// Not recommended unless the widget wasn't rendered yet (first init).
|
// Not recommended unless the widget wasn't rendered yet (first init).
|
||||||
pub fn set_text_simple(&mut self, globals: &mut Globals, translation: Translation) -> bool {
|
pub fn set_text_simple(&mut self, globals: &mut Globals, translation: Translation) -> bool {
|
||||||
|
|
@ -78,14 +88,16 @@ impl WidgetLabel {
|
||||||
self.params.content = translation;
|
self.params.content = translation;
|
||||||
let attrs = Attrs::from(&self.params.style);
|
let attrs = Attrs::from(&self.params.style);
|
||||||
|
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
let text = self.params.content.generate(&mut globals.i18n_builtin);
|
||||||
buffer.set_rich_text(
|
|
||||||
[(self.params.content.generate(&mut globals.i18n_builtin).as_ref(), attrs)],
|
|
||||||
&Attrs::new(),
|
|
||||||
Shaping::Advanced,
|
|
||||||
self.params.style.align.map(Into::into),
|
|
||||||
);
|
|
||||||
|
|
||||||
|
WidgetLabel::with_buffer(&self.buffer, globals, |mut buffer| {
|
||||||
|
buffer.set_rich_text(
|
||||||
|
[(text.as_ref(), attrs)],
|
||||||
|
&Attrs::new(),
|
||||||
|
Shaping::Advanced,
|
||||||
|
self.params.style.align.map(Into::into),
|
||||||
|
);
|
||||||
|
});
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -118,8 +130,9 @@ impl WidgetObj for WidgetLabel {
|
||||||
fn draw(&mut self, state: &mut super::DrawState, _params: &super::DrawParams) {
|
fn draw(&mut self, state: &mut super::DrawState, _params: &super::DrawParams) {
|
||||||
let boundary = drawing::Boundary::construct_relative(state.transform_stack);
|
let boundary = drawing::Boundary::construct_relative(state.transform_stack);
|
||||||
|
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
WidgetLabel::with_buffer(&self.buffer, state.globals, |mut buffer| {
|
||||||
buffer.set_size(Some(boundary.size.x), Some(boundary.size.y));
|
buffer.set_size(Some(boundary.size.x), Some(boundary.size.y));
|
||||||
|
});
|
||||||
|
|
||||||
state.primitives.push(drawing::RenderPrimitive::Text(
|
state.primitives.push(drawing::RenderPrimitive::Text(
|
||||||
PrimitiveExtent {
|
PrimitiveExtent {
|
||||||
|
|
@ -133,7 +146,7 @@ impl WidgetObj for WidgetLabel {
|
||||||
|
|
||||||
fn measure(
|
fn measure(
|
||||||
&mut self,
|
&mut self,
|
||||||
_globals: &Globals,
|
globals: &Globals,
|
||||||
known_dimensions: taffy::Size<Option<f32>>,
|
known_dimensions: taffy::Size<Option<f32>>,
|
||||||
available_space: taffy::Size<taffy::AvailableSpace>,
|
available_space: taffy::Size<taffy::AvailableSpace>,
|
||||||
) -> taffy::Size<f32> {
|
) -> taffy::Size<f32> {
|
||||||
|
|
@ -144,19 +157,18 @@ impl WidgetObj for WidgetLabel {
|
||||||
AvailableSpace::Definite(width) => Some(width),
|
AvailableSpace::Definite(width) => Some(width),
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut buffer = self.buffer.borrow_mut();
|
WidgetLabel::with_buffer(&self.buffer, globals, |mut buffer| {
|
||||||
buffer.set_size(width_constraint, None);
|
buffer.set_size(width_constraint, None);
|
||||||
|
// Determine measured size of text
|
||||||
// Determine measured size of text
|
let (width, total_lines) = buffer.layout_runs().fold((0.0, 0usize), |(width, total_lines), run| {
|
||||||
let (width, total_lines) = buffer.layout_runs().fold((0.0, 0usize), |(width, total_lines), run| {
|
(run.line_w.max(width), total_lines + 1)
|
||||||
(run.line_w.max(width), total_lines + 1)
|
});
|
||||||
});
|
let height = total_lines as f32 * buffer.metrics().line_height;
|
||||||
let height = total_lines as f32 * buffer.metrics().line_height;
|
taffy::Size {
|
||||||
|
width: width + 1.0, /* f32 aliasing fix */
|
||||||
taffy::Size {
|
height,
|
||||||
width: width + 1.0, /* f32 aliasing fix */
|
}
|
||||||
height,
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_id(&self) -> WidgetID {
|
fn get_id(&self) -> WidgetID {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue