Skip to content

Commit

Permalink
Add color to each field.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter committed Aug 27, 2024
1 parent f27c4cf commit 8555f1a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
39 changes: 27 additions & 12 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,14 @@ impl Slot {
if cx.debug {
ui.label(format!("Item UID: {}", item_meta.item_uid.0));
}
for (field_id, field) in &item_meta.fields {
for (field_id, field, color) in &item_meta.fields {
let name = config.field_schema.get_name(*field_id).unwrap();
ui.label(format!("{}", FieldWithName(name, field)));
let text = format!("{}", FieldWithName(name, field));
if let Some(color) = color {
ui.label(RichText::new(text).color(*color));
} else {
ui.label(text);
}
}
ui.label("(Click to show details.)");
});
Expand Down Expand Up @@ -1266,7 +1271,7 @@ impl SearchState {
let field = self.search_field;
if field == self.title_field {
self.is_string_match(&item.title)
} else if let Some((_, value)) = item.fields.iter().find(|(x, _)| *x == field) {
} else if let Some((_, value, _)) = item.fields.iter().find(|(x, _, _)| *x == field) {
self.is_field_match(value)
} else {
false
Expand Down Expand Up @@ -2339,16 +2344,25 @@ impl ProfApp {

fn render_field_as_ui(
field: &Field,
color: Option<Color32>,
mode: ItemLinkNavigationMode,
ui: &mut egui::Ui,
) -> Option<(ItemLocator, Interval)> {
let mut result = None;
let label = |ui: &mut egui::Ui, v| {
ui.add(egui::Label::new(v).wrap(true));
if let Some(color) = color {
ui.add(egui::Label::new(RichText::new(v).color(color)).wrap(true));
} else {
ui.add(egui::Label::new(v).wrap(true));
}
};
let label_button = |ui: &mut egui::Ui, v, b| {
label(ui, v);
ui.button(b).clicked()
if let Some(color) = color {
ui.button(RichText::new(b).color(color)).clicked()
} else {
ui.button(b).clicked()
}
};
match field {
Field::I64(value) => label(ui, &format!("{value}")),
Expand Down Expand Up @@ -2376,7 +2390,7 @@ impl ProfApp {
ui.vertical(|ui| {
for f in fields {
ui.horizontal(|ui| {
if let Some(x) = Self::render_field_as_ui(f, mode, ui) {
if let Some(x) = Self::render_field_as_ui(f, color, mode, ui) {
result = Some(x);
}
});
Expand Down Expand Up @@ -2411,7 +2425,7 @@ impl ProfApp {
.column(Column::auto())
.column(Column::remainder())
.body(|mut body| {
let mut show_row = |k: &str, field: &Field| {
let mut show_row = |k: &str, field: &Field, color: Option<Color32>| {
// We need to manually work out the height of the labels
// so that the table knows how large to make each row.
let width = body.widths()[1];
Expand All @@ -2425,21 +2439,22 @@ impl ProfApp {
ui.strong(k);
});
row.col(|ui| {
if let Some(x) = Self::render_field_as_ui(field, cx.item_link_mode, ui)
if let Some(x) =
Self::render_field_as_ui(field, color, cx.item_link_mode, ui)
{
result = Some(x);
}
});
});
};

show_row("Title", &Field::String(item_meta.title.to_string()));
show_row("Title", &Field::String(item_meta.title.to_string()), None);
if cx.debug {
show_row("Item UID", &Field::U64(item_meta.item_uid.0));
show_row("Item UID", &Field::U64(item_meta.item_uid.0), None);
}
for (field_id, field) in &item_meta.fields {
for (field_id, field, color) in &item_meta.fields {
let name = field_schema.get_name(*field_id).unwrap();
show_row(name, field);
show_row(name, field, *color);
}
});
ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| {
Expand Down
2 changes: 1 addition & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub struct ItemMeta {
// entire duration of the original item, unexpanded and unsliced.
pub original_interval: Interval,
pub title: String,
pub fields: Vec<(FieldID, Field)>,
pub fields: Vec<(FieldID, Field, Option<Color32>)>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ impl RandomDataSource {
(
self.interval_field,
Field::Interval(Interval::new(start, stop)),
None,
),
(self.item_uid_field, Field::U64(item_uid.0)),
(self.item_uid_field, Field::U64(item_uid.0), None),
],
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/merge_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl MergeDeferredDataSource {
for items in &mut tile.data.items {
for item in items {
item.item_uid = self.map_src_to_dst_item_uid(idx, item.item_uid);
for (_, field) in &mut item.fields {
for (_, field, _) in &mut item.fields {
self.map_src_to_dst_field(idx, field);
}
}
Expand Down

0 comments on commit 8555f1a

Please sign in to comment.