Draw scrollbar on top of scrollable content

This commit is contained in:
Héctor Ramón Jiménez 2019-10-29 02:00:17 +01:00
parent 23ebfb707a
commit be488ac738
2 changed files with 41 additions and 39 deletions

View file

@ -47,7 +47,6 @@ pub struct Layer<'a> {
quads: Vec<Quad>, quads: Vec<Quad>,
images: Vec<Image>, images: Vec<Image>,
text: Vec<wgpu_glyph::Section<'a>>, text: Vec<wgpu_glyph::Section<'a>>,
layers: Vec<Layer<'a>>,
} }
impl<'a> Layer<'a> { impl<'a> Layer<'a> {
@ -58,7 +57,6 @@ impl<'a> Layer<'a> {
quads: Vec::new(), quads: Vec::new(),
images: Vec::new(), images: Vec::new(),
text: Vec::new(), text: Vec::new(),
layers: Vec::new(),
} }
} }
} }
@ -149,7 +147,8 @@ impl Renderer {
depth_stencil_attachment: None, depth_stencil_attachment: None,
}); });
let mut layer = Layer::new( let mut layers = Vec::new();
let mut current = Layer::new(
Rectangle { Rectangle {
x: 0, x: 0,
y: 0, y: 0,
@ -159,8 +158,17 @@ impl Renderer {
0, 0,
); );
self.draw_primitive(primitive, &mut layer); self.draw_primitive(primitive, &mut current, &mut layers);
self.flush(target.transformation, &layer, &mut encoder, &frame.view); layers.push(current);
for layer in layers {
self.flush(
target.transformation,
&layer,
&mut encoder,
&frame.view,
);
}
self.queue.submit(&[encoder.finish()]); self.queue.submit(&[encoder.finish()]);
@ -171,13 +179,14 @@ impl Renderer {
&mut self, &mut self,
primitive: &'a Primitive, primitive: &'a Primitive,
layer: &mut Layer<'a>, layer: &mut Layer<'a>,
layers: &mut Vec<Layer<'a>>,
) { ) {
match primitive { match primitive {
Primitive::None => {} Primitive::None => {}
Primitive::Group { primitives } => { Primitive::Group { primitives } => {
// TODO: Inspect a bit and regroup (?) // TODO: Inspect a bit and regroup (?)
for primitive in primitives { for primitive in primitives {
self.draw_primitive(primitive, layer) self.draw_primitive(primitive, layer, layers)
} }
} }
Primitive::Text { Primitive::Text {
@ -277,9 +286,9 @@ impl Renderer {
); );
// TODO: Primitive culling // TODO: Primitive culling
self.draw_primitive(content, &mut new_layer); self.draw_primitive(content, &mut new_layer, layers);
layer.layers.push(new_layer); layers.push(new_layer);
} }
} }
} }
@ -338,10 +347,6 @@ impl Renderer {
) )
.expect("Draw text"); .expect("Draw text");
} }
for layer in layer.layers.iter() {
self.flush(transformation, layer, encoder, target);
}
} }
} }

View file

@ -34,34 +34,31 @@ impl scrollable::Renderer for Renderer {
}; };
( (
Primitive::Group { if is_mouse_over && content_bounds.height > bounds.height {
primitives: if is_mouse_over let ratio = bounds.height / content_bounds.height;
&& content_bounds.height > bounds.height let scrollbar_height = bounds.height * ratio;
{ let y_offset = offset as f32 * ratio;
let ratio = bounds.height / content_bounds.height;
let scrollbar_height = bounds.height * ratio;
let y_offset = offset as f32 * ratio;
let scrollbar = Primitive::Quad { let scrollbar = Primitive::Quad {
bounds: Rectangle { bounds: Rectangle {
x: bounds.x + bounds.width - 12.0, x: bounds.x + bounds.width - 12.0,
y: bounds.y + y_offset, y: bounds.y + y_offset,
width: 10.0, width: 10.0,
height: scrollbar_height, height: scrollbar_height,
}, },
background: Background::Color(Color { background: Background::Color(Color {
r: 0.0, r: 0.0,
g: 0.0, g: 0.0,
b: 0.0, b: 0.0,
a: 0.5, a: 0.7,
}), }),
border_radius: 5, border_radius: 5,
}; };
Primitive::Group {
vec![primitive, scrollbar] primitives: vec![primitive, scrollbar],
} else { }
vec![primitive] } else {
}, primitive
}, },
mouse_cursor, mouse_cursor,
) )