Skip to content

Commit 9c86b47

Browse files
Add color to each field (#60)
1 parent 319ea40 commit 9c86b47

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

src/app.rs

+32-13
Original file line numberDiff line numberDiff line change
@@ -744,9 +744,14 @@ impl Slot {
744744
if cx.debug {
745745
ui.label(format!("Item UID: {}", item_meta.item_uid.0));
746746
}
747-
for (field_id, field) in &item_meta.fields {
747+
for (field_id, field, color) in &item_meta.fields {
748748
let name = config.field_schema.get_name(*field_id).unwrap();
749-
ui.label(format!("{}", FieldWithName(name, field)));
749+
let text = format!("{}", FieldWithName(name, field));
750+
if let Some(color) = color {
751+
ui.label(RichText::new(text).color(*color));
752+
} else {
753+
ui.label(text);
754+
}
750755
}
751756
ui.label("(Click to show details.)");
752757
});
@@ -1266,7 +1271,7 @@ impl SearchState {
12661271
let field = self.search_field;
12671272
if field == self.title_field {
12681273
self.is_string_match(&item.title)
1269-
} else if let Some((_, value)) = item.fields.iter().find(|(x, _)| *x == field) {
1274+
} else if let Some((_, value, _)) = item.fields.iter().find(|(x, _, _)| *x == field) {
12701275
self.is_field_match(value)
12711276
} else {
12721277
false
@@ -2339,16 +2344,25 @@ impl ProfApp {
23392344

23402345
fn render_field_as_ui(
23412346
field: &Field,
2347+
color: Option<Color32>,
23422348
mode: ItemLinkNavigationMode,
23432349
ui: &mut egui::Ui,
23442350
) -> Option<(ItemLocator, Interval)> {
23452351
let mut result = None;
23462352
let label = |ui: &mut egui::Ui, v| {
2347-
ui.add(egui::Label::new(v).wrap(true));
2353+
if let Some(color) = color {
2354+
ui.add(egui::Label::new(RichText::new(v).color(color)).wrap(true));
2355+
} else {
2356+
ui.add(egui::Label::new(v).wrap(true));
2357+
}
23482358
};
23492359
let label_button = |ui: &mut egui::Ui, v, b| {
23502360
label(ui, v);
2351-
ui.button(b).clicked()
2361+
if let Some(color) = color {
2362+
ui.button(RichText::new(b).color(color)).clicked()
2363+
} else {
2364+
ui.button(b).clicked()
2365+
}
23522366
};
23532367
match field {
23542368
Field::I64(value) => label(ui, &format!("{value}")),
@@ -2376,7 +2390,7 @@ impl ProfApp {
23762390
ui.vertical(|ui| {
23772391
for f in fields {
23782392
ui.horizontal(|ui| {
2379-
if let Some(x) = Self::render_field_as_ui(f, mode, ui) {
2393+
if let Some(x) = Self::render_field_as_ui(f, color, mode, ui) {
23802394
result = Some(x);
23812395
}
23822396
});
@@ -2411,7 +2425,7 @@ impl ProfApp {
24112425
.column(Column::auto())
24122426
.column(Column::remainder())
24132427
.body(|mut body| {
2414-
let mut show_row = |k: &str, field: &Field| {
2428+
let mut show_row = |k: &str, field: &Field, color: Option<Color32>| {
24152429
// We need to manually work out the height of the labels
24162430
// so that the table knows how large to make each row.
24172431
let width = body.widths()[1];
@@ -2422,24 +2436,29 @@ impl ProfApp {
24222436

24232437
body.row(height, |mut row| {
24242438
row.col(|ui| {
2425-
ui.strong(k);
2439+
if let Some(color) = color {
2440+
ui.label(RichText::new(k).color(color).strong());
2441+
} else {
2442+
ui.strong(k);
2443+
}
24262444
});
24272445
row.col(|ui| {
2428-
if let Some(x) = Self::render_field_as_ui(field, cx.item_link_mode, ui)
2446+
if let Some(x) =
2447+
Self::render_field_as_ui(field, color, cx.item_link_mode, ui)
24292448
{
24302449
result = Some(x);
24312450
}
24322451
});
24332452
});
24342453
};
24352454

2436-
show_row("Title", &Field::String(item_meta.title.to_string()));
2455+
show_row("Title", &Field::String(item_meta.title.to_string()), None);
24372456
if cx.debug {
2438-
show_row("Item UID", &Field::U64(item_meta.item_uid.0));
2457+
show_row("Item UID", &Field::U64(item_meta.item_uid.0), None);
24392458
}
2440-
for (field_id, field) in &item_meta.fields {
2459+
for (field_id, field, color) in &item_meta.fields {
24412460
let name = field_schema.get_name(*field_id).unwrap();
2442-
show_row(name, field);
2461+
show_row(name, field, *color);
24432462
}
24442463
});
24452464
ui.with_layout(egui::Layout::top_down(egui::Align::Center), |ui| {

src/data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub struct ItemMeta {
160160
// entire duration of the original item, unexpanded and unsliced.
161161
pub original_interval: Interval,
162162
pub title: String,
163-
pub fields: Vec<(FieldID, Field)>,
163+
pub fields: Vec<(FieldID, Field, Option<Color32>)>,
164164
}
165165

166166
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]

src/main.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,13 @@ impl RandomDataSource {
216216
(
217217
self.interval_field,
218218
Field::Interval(Interval::new(start, stop)),
219+
None,
220+
),
221+
(
222+
self.item_uid_field,
223+
Field::U64(item_uid.0),
224+
Some(Color32::RED),
219225
),
220-
(self.item_uid_field, Field::U64(item_uid.0)),
221226
],
222227
});
223228
}

src/merge_data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl MergeDeferredDataSource {
180180
for items in &mut tile.data.items {
181181
for item in items {
182182
item.item_uid = self.map_src_to_dst_item_uid(idx, item.item_uid);
183-
for (_, field) in &mut item.fields {
183+
for (_, field, _) in &mut item.fields {
184184
self.map_src_to_dst_field(idx, field);
185185
}
186186
}

0 commit comments

Comments
 (0)