Skip to content

Commit

Permalink
New triangle collapse arrow for large collapsible header (#2920)
Browse files Browse the repository at this point in the history
### What

This PR implements the proper Figma-based collapsing arrow (with
"linearised" rounded corners) and uses it for the large collapsible
headers in the selection panel. This PR also fixes the vertical optical
alignment of the text within these headers.

Partially addresses #2738 

Zoom 150%:
<img width="438" alt="image"
src="https://github.com/rerun-io/rerun/assets/49431240/b51a41a3-7f77-4e56-8639-c3428a124829">


### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/2920) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/2920)
- [Docs
preview](https://rerun.io/preview/pr%3Aantoine%2Fcollapsing-triangle/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Aantoine%2Fcollapsing-triangle/examples)
  • Loading branch information
abey79 authored Aug 11, 2023
1 parent aa38913 commit 4b8b4fb
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions crates/re_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use std::{ops::RangeInclusive, sync::Arc};
use parking_lot::Mutex;

use crate::list_item::ListItem;
use egui::emath::Rot2;
use egui::{pos2, Align2, Color32, Mesh, NumExt, Rect, Shape, Vec2};

#[derive(Clone)]
Expand Down Expand Up @@ -576,12 +577,16 @@ impl ReUi {
egui::Vec2::splat(icon_width),
);
let icon_response = header_response.clone().with_new_rect(icon_rect);
egui::collapsing_header::paint_default_icon(ui, openness, &icon_response);
Self::paint_collapsing_triangle(ui, openness, &icon_response);

let visuals = ui.style().interact(&header_response);

let optical_vertical_alignment = 0.5; // improves perceived vertical alignment
let text_pos = icon_response.rect.right_center()
+ egui::vec2(space_after_icon, -0.5 * galley.size().y);
+ egui::vec2(
space_after_icon,
-0.5 * galley.size().y + optical_vertical_alignment,
);
ui.painter()
.galley_with_color(text_pos, galley, visuals.text_color());

Expand All @@ -607,6 +612,46 @@ impl ReUi {
});
}

/// Paint a collapsing triangle with rounded corners.
///
/// Alternative to [`egui::collapsing_header::paint_default_icon`].
pub fn paint_collapsing_triangle(ui: &mut egui::Ui, openness: f32, response: &egui::Response) {
let visuals = ui.style().interact(response);

let rect = response.rect;
let extent = response.rect.width().min(response.rect.height());

// Normalised in [0, 1]^2 space.
// Note on how these coords have been computed: https://github.com/rerun-io/rerun/pull/2920
// Discussion on the future of icons: https://github.com/rerun-io/rerun/issues/2960
let mut points = vec![
pos2(0.80387, 0.470537),
pos2(0.816074, 0.5),
pos2(0.80387, 0.529463),
pos2(0.316248, 1.017085),
pos2(0.286141, 1.029362),
pos2(0.257726, 1.017592),
pos2(0.245118, 0.987622),
pos2(0.245118, 0.012378),
pos2(0.257726, -0.017592),
pos2(0.286141, -0.029362),
pos2(0.316248, -0.017085),
pos2(0.80387, 0.470537),
];

use std::f32::consts::TAU;
let rotation = Rot2::from_angle(egui::remap(openness, 0.0..=1.0, 0.0..=TAU / 4.0));
for p in &mut points {
*p = rect.center() + rotation * (*p - pos2(0.5, 0.5)) * extent;
}

ui.painter().add(Shape::convex_polygon(
points,
visuals.fg_stroke.color,
egui::Stroke::NONE,
));
}

/// Workaround for putting a label into a grid at the top left of its row.
#[allow(clippy::unused_self)]
pub fn grid_left_hand_label(&self, ui: &mut egui::Ui, label: &str) -> egui::Response {
Expand Down

0 comments on commit 4b8b4fb

Please sign in to comment.