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>,
images: Vec<Image>,
text: Vec<wgpu_glyph::Section<'a>>,
layers: Vec<Layer<'a>>,
}
impl<'a> Layer<'a> {
@ -58,7 +57,6 @@ impl<'a> Layer<'a> {
quads: Vec::new(),
images: Vec::new(),
text: Vec::new(),
layers: Vec::new(),
}
}
}
@ -149,7 +147,8 @@ impl Renderer {
depth_stencil_attachment: None,
});
let mut layer = Layer::new(
let mut layers = Vec::new();
let mut current = Layer::new(
Rectangle {
x: 0,
y: 0,
@ -159,8 +158,17 @@ impl Renderer {
0,
);
self.draw_primitive(primitive, &mut layer);
self.flush(target.transformation, &layer, &mut encoder, &frame.view);
self.draw_primitive(primitive, &mut current, &mut layers);
layers.push(current);
for layer in layers {
self.flush(
target.transformation,
&layer,
&mut encoder,
&frame.view,
);
}
self.queue.submit(&[encoder.finish()]);
@ -171,13 +179,14 @@ impl Renderer {
&mut self,
primitive: &'a Primitive,
layer: &mut Layer<'a>,
layers: &mut Vec<Layer<'a>>,
) {
match primitive {
Primitive::None => {}
Primitive::Group { primitives } => {
// TODO: Inspect a bit and regroup (?)
for primitive in primitives {
self.draw_primitive(primitive, layer)
self.draw_primitive(primitive, layer, layers)
}
}
Primitive::Text {
@ -277,9 +286,9 @@ impl Renderer {
);
// 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");
}
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 {
primitives: if is_mouse_over
&& content_bounds.height > bounds.height
{
let ratio = bounds.height / content_bounds.height;
let scrollbar_height = bounds.height * ratio;
let y_offset = offset as f32 * ratio;
if is_mouse_over && content_bounds.height > bounds.height {
let ratio = bounds.height / content_bounds.height;
let scrollbar_height = bounds.height * ratio;
let y_offset = offset as f32 * ratio;
let scrollbar = Primitive::Quad {
bounds: Rectangle {
x: bounds.x + bounds.width - 12.0,
y: bounds.y + y_offset,
width: 10.0,
height: scrollbar_height,
},
background: Background::Color(Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.5,
}),
border_radius: 5,
};
vec![primitive, scrollbar]
} else {
vec![primitive]
},
let scrollbar = Primitive::Quad {
bounds: Rectangle {
x: bounds.x + bounds.width - 12.0,
y: bounds.y + y_offset,
width: 10.0,
height: scrollbar_height,
},
background: Background::Color(Color {
r: 0.0,
g: 0.0,
b: 0.0,
a: 0.7,
}),
border_radius: 5,
};
Primitive::Group {
primitives: vec![primitive, scrollbar],
}
} else {
primitive
},
mouse_cursor,
)