This commit is contained in:
Samuel Guerra 2019-08-21 13:46:49 -03:00
parent 078fb65eb7
commit d542a9e0d4
5 changed files with 53 additions and 37 deletions

View File

@ -1,4 +1,4 @@
use crate::ui::{Ui, InitContext};
use crate::ui::{InitContext, Ui};
use crate::window::{WebRenderEvent, Window};
use std::collections::HashMap;
@ -25,7 +25,7 @@ impl App {
mut self,
title: impl ToString,
background_color: webrender::api::ColorF,
content: impl Fn (&InitContext) -> Tcontent,
content: impl Fn(&InitContext) -> Tcontent,
) -> Self {
let win = Window::new(
title.to_string(),

View File

@ -6,16 +6,14 @@ use ui::*;
use webrender::api::{GradientStop, LayoutPoint};
fn main() {
app::App::new()//
.window(
"window1",
rgbaf(0.1, 0.2, 0.3, 1.0),
|c| text(c, "çHello World! pç", rgb(0, 0, 0)).background_color(rgb(255, 255, 255)).center(),
)
.window(
"window2",
rgbaf(0.3, 0.2, 0.1, 1.0),
|_| center(h_stack(vec![
app::App::new() //
.window("window1", rgbaf(0.1, 0.2, 0.3, 1.0), |c| {
text(c, "çHello World! pç", rgb(0, 0, 0))
.background_color(rgb(255, 255, 255))
.center()
})
.window("window2", rgbaf(0.3, 0.2, 0.1, 1.0), |_| {
center(h_stack(vec![
fill_gradient(
LayoutPoint::new(0., 0.),
LayoutPoint::new(1., 1.),
@ -33,7 +31,7 @@ fn main() {
.width(200.)
.margin(2.);
4
])),
)
]))
})
.run();
}

View File

@ -11,7 +11,7 @@ pub use webrender::api::{LayoutPoint, LayoutRect, LayoutSize};
pub struct InitContext {
pub api: RenderApi,
pub document_id: DocumentId
pub document_id: DocumentId,
}
pub struct RenderContext<'b> {
@ -69,16 +69,17 @@ impl<'b> RenderContext<'b> {
.push_gradient(&lpi, &sci, grad, final_rect.size, LayoutSize::default());
}
pub fn push_text(&mut self, final_rect: LayoutRect, glyphs: &[GlyphInstance], font_instance_key: FontInstanceKey, color: ColorF) {
pub fn push_text(
&mut self,
final_rect: LayoutRect,
glyphs: &[GlyphInstance],
font_instance_key: FontInstanceKey,
color: ColorF,
) {
let (lpi, sci) = self.layout_and_clip(final_rect);
self.builder.push_text(&lpi,
&sci,
&glyphs,
font_instance_key,
color,
None,
);
self.builder
.push_text(&lpi, &sci, &glyphs, font_instance_key, color, None);
}
}
@ -230,7 +231,7 @@ impl<T: Ui> BackgroundColor<T> {
impl<T: Ui> Ui for BackgroundColor<T> {
fn measure(&mut self, available_size: LayoutSize) -> LayoutSize {
self.child.measure(available_size)
self.child.measure(available_size)
}
fn arrange(&mut self, final_size: LayoutSize) {
self.child.arrange(final_size)
@ -248,4 +249,4 @@ pub trait BackgroundColorExt: Ui + Sized {
BackgroundColor::new(self, color)
}
}
impl<T: Ui> BackgroundColorExt for T {}
impl<T: Ui> BackgroundColorExt for T {}

View File

@ -1,8 +1,7 @@
use super::{LayoutSize, InitContext, RenderContext, Ui};
use super::{InitContext, LayoutSize, RenderContext, Ui};
use app_units::Au;
use font_loader::system_fonts;
use webrender::api::*;
use app_units::Au;
pub struct Text {
glyphs: Vec<GlyphInstance>,
@ -21,11 +20,23 @@ impl Text {
txn.add_raw_font(font_key, font, 0);
let font_instance_key = c.api.generate_font_instance_key();
txn.add_font_instance(font_instance_key, font_key, Au::from_px(32), None, None, Vec::new());
txn.add_font_instance(
font_instance_key,
font_key,
Au::from_px(32),
None,
None,
Vec::new(),
);
c.api.send_transaction(c.document_id, txn);
let indices: Vec<_> = c.api.get_glyph_indices(font_key, text).into_iter().filter_map(|i|i).collect();
let indices: Vec<_> = c
.api
.get_glyph_indices(font_key, text)
.into_iter()
.filter_map(|i| i)
.collect();
let dimensions = c.api.get_glyph_dimensions(font_instance_key, indices.clone());
let mut glyphs = Vec::with_capacity(indices.len());
@ -37,17 +48,20 @@ impl Text {
if let Some(dim) = dim {
glyphs.push(GlyphInstance {
index,
point: LayoutPoint::new(offset, 24.)
point: LayoutPoint::new(offset, 24.),
});
offset += dim.advance as f32;
}
}
let size = LayoutSize::new(offset, 32.);
let size = LayoutSize::new(offset, 32.);
glyphs.shrink_to_fit();
Text {
glyphs, size, font_instance_key, color
glyphs,
size,
font_instance_key,
color,
}
}
}
@ -62,6 +76,11 @@ impl Ui for Text {
}
fn render(&self, mut c: RenderContext) {
c.push_text(LayoutRect::from_size(self.size), &self.glyphs, self.font_instance_key, self.color)
c.push_text(
LayoutRect::from_size(self.size),
&self.glyphs,
self.font_instance_key,
self.color,
)
}
}

View File

@ -115,9 +115,7 @@ impl Window {
let epoch = Epoch(0);
let pipeline_id = PipelineId(0, 0);
let init_ctx = InitContext {
api, document_id
};
let init_ctx = InitContext { api, document_id };
let content = content(&init_ctx);
Window {