Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlighting of entity paths and instance paths #4803

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions crates/re_data_ui/src/item_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//!
//! TODO(andreas): This is not a `data_ui`, can this go somewhere else, shouldn't be in `re_data_ui`.

use egui::Ui;
use re_entity_db::{EntityTree, InstancePath};
use re_log_types::{ComponentPath, EntityPath, TimeInt, Timeline};
use re_ui::SyntaxHighlighting;
use re_viewer_context::{
DataQueryId, HoverHighlight, Item, Selection, SpaceViewId, UiVerbosity, ViewerContext,
};
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn entity_path_button(
ui,
space_view_id,
&InstancePath::entity_splat(entity_path.clone()),
entity_path.to_string(),
entity_path.syntax_highlighted(ui.style()),
)
}

Expand Down Expand Up @@ -82,7 +82,7 @@ pub fn instance_path_button(
ui,
space_view_id,
instance_path,
instance_path.to_string(),
instance_path.syntax_highlighted(ui.style()),
)
}

Expand Down Expand Up @@ -351,14 +351,18 @@ pub fn select_hovered_on_click(
/// Displays the "hover card" (i.e. big tooltip) for an instance or an entity.
///
/// The entity hover card is displayed the provided instance path is a splat.
pub fn instance_hover_card_ui(ui: &mut Ui, ctx: &ViewerContext<'_>, instance_path: &InstancePath) {
pub fn instance_hover_card_ui(
ui: &mut egui::Ui,
ctx: &ViewerContext<'_>,
instance_path: &InstancePath,
) {
let subtype_string = if instance_path.instance_key.is_splat() {
"Entity"
} else {
"Entity Instance"
};
ui.strong(subtype_string);
ui.label(format!("Path: {instance_path}"));
ui.label(instance_path.syntax_highlighted(ui.style()));

// TODO(emilk): give data_ui an alternate "everything on this timeline" query?
// Then we can move the size view into `data_ui`.
Expand Down
3 changes: 3 additions & 0 deletions crates/re_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ all-features = true
default = []

[dependencies]
re_entity_db.workspace = true # syntax-highlighting for InstancePath. TODO(emilk): move InstancePath
Copy link
Member

@Wumpf Wumpf Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move instance path to where?

aren't these making re_ui a lot less lightweight?

Copy link
Member Author

@emilk emilk Jan 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move instance path to where?

It feels like it belongs in a more low-level crate, like re_types

aren't these making re_ui a lot less lightweight?

Yes. The alternative is to create yet another crate, or add more feature flags, and both kind of sucks.

re_log_types.workspace = true # syntax-highlighting for EntityPath

egui_commonmark.workspace = true
egui_extras.workspace = true
egui.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions crates/re_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod icons;
mod layout_job_builder;
pub mod list_item;
pub mod modal;
mod syntax_highlighting;
pub mod toasts;
mod toggle_switch;

Expand All @@ -16,6 +17,7 @@ pub use command_palette::CommandPalette;
pub use design_tokens::DesignTokens;
pub use icons::Icon;
pub use layout_job_builder::LayoutJobBuilder;
pub use syntax_highlighting::SyntaxHighlighting;
pub use toggle_switch::toggle_switch;

// ---------------------------------------------------------------------------
Expand Down
64 changes: 64 additions & 0 deletions crates/re_ui/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use re_entity_db::InstancePath;
use re_log_types::{EntityPath, EntityPathPart};

use egui::{text::LayoutJob, Color32, Style, TextFormat};

pub trait SyntaxHighlighting {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it actually syntax highlighting or just "make this an egui text"? ToLayoutJob might be just as appropriate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed live: we'll probably use this trait for more syntax highlighting stuff in the future, and that's what you want to search for in the code. Still, we can revisit the name later if we want to.

fn syntax_highlighted(&self, style: &Style) -> LayoutJob {
let mut job = LayoutJob::default();
self.syntax_highlight_into(style, &mut job);
job
}

fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob);
}

fn text_format(style: &Style) -> TextFormat {
TextFormat {
font_id: egui::TextStyle::Body.resolve(style),

// This color be replaced with appropriate color based on widget,
// and whether the widget is hovered, etc
color: Color32::PLACEHOLDER,

..Default::default()
}
}

fn faint_text_format(style: &Style) -> TextFormat {
TextFormat {
color: Color32::WHITE,

..text_format(style)
}
}

impl SyntaxHighlighting for EntityPathPart {
fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
job.append(&self.ui_string(), 0.0, text_format(style));
}
}

impl SyntaxHighlighting for EntityPath {
fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
job.append("/", 0.0, faint_text_format(style));

for (i, part) in self.iter().enumerate() {
if i != 0 {
job.append("/", 0.0, faint_text_format(style));
}
part.syntax_highlight_into(style, job);
}
}
}

impl SyntaxHighlighting for InstancePath {
fn syntax_highlight_into(&self, style: &Style, job: &mut LayoutJob) {
self.entity_path.syntax_highlight_into(style, job);
if !self.instance_key.is_splat() {
job.append("[", 0.0, faint_text_format(style));
job.append(&self.instance_key.to_string(), 0.0, text_format(style));
job.append("]", 0.0, faint_text_format(style));
}
}
}
9 changes: 6 additions & 3 deletions crates/re_viewer/src/ui/selection_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use re_types::{
};
use re_ui::list_item::ListItem;
use re_ui::ReUi;
use re_ui::SyntaxHighlighting as _;
use re_viewer_context::{
gpu_bridge::colormap_dropdown_button_ui, HoverHighlight, Item, SpaceViewClass,
SpaceViewClassIdentifier, SpaceViewId, SystemCommand, SystemCommandSender as _, UiVerbosity,
Expand Down Expand Up @@ -353,12 +354,14 @@ fn what_is_selected_ui(
"Entity instance"
};

let name = instance_path.syntax_highlighted(ui.style());

if let Some(space_view_id) = space_view_id {
if let Some(space_view) = viewport.space_view(space_view_id) {
item_title_ui(
ctx.re_ui,
ui,
instance_path.to_string().as_str(),
name,
None,
&format!(
"{typ} '{instance_path}' as shown in Space View {:?}",
Expand All @@ -375,7 +378,7 @@ fn what_is_selected_ui(
item_title_ui(
ctx.re_ui,
ui,
instance_path.to_string().as_str(),
name,
None,
&format!("{typ} '{instance_path}'"),
);
Expand Down Expand Up @@ -409,7 +412,7 @@ fn what_is_selected_ui(
fn item_title_ui(
re_ui: &re_ui::ReUi,
ui: &mut egui::Ui,
name: &str,
name: impl Into<egui::WidgetText>,
icon: Option<&re_ui::Icon>,
hover: &str,
) -> egui::Response {
Expand Down