| 1 |
|
- |
//! Right detail panel: waveform display, metadata grid, tags, and copy-path button.
|
| 2 |
|
- |
|
| 3 |
|
- |
use egui;
|
| 4 |
|
- |
|
| 5 |
|
- |
use super::theme;
|
| 6 |
|
- |
use super::widgets;
|
| 7 |
|
- |
use crate::state::BrowserState;
|
| 8 |
|
- |
use crate::waveform;
|
| 9 |
|
- |
|
| 10 |
|
- |
/// Draw the detail panel content for the currently selected sample.
|
| 11 |
|
- |
pub fn draw_detail(ui: &mut egui::Ui, state: &mut BrowserState) {
|
| 12 |
|
- |
if state.nav.selection.count() > 1 {
|
| 13 |
|
- |
draw_multi_summary(ui, state);
|
| 14 |
|
- |
return;
|
| 15 |
|
- |
}
|
| 16 |
|
- |
|
| 17 |
|
- |
let Some(node) = state.selected_node() else {
|
| 18 |
|
- |
widgets::empty_state(ui, "Select a sample", None, None);
|
| 19 |
|
- |
return;
|
| 20 |
|
- |
};
|
| 21 |
|
- |
|
| 22 |
|
- |
// Waveform
|
| 23 |
|
- |
if let Some(ref waveform_data) = state.detail.selected_waveform {
|
| 24 |
|
- |
// Compute playback position as a 0.0–1.0 fraction for the waveform cursor.
|
| 25 |
|
- |
// Only valid when the currently-playing hash matches this node's hash.
|
| 26 |
|
- |
let playback_pos =
|
| 27 |
|
- |
if state.preview.previewing_hash.as_deref() == node.node.sample_hash.as_deref() {
|
| 28 |
|
- |
let playback = state.shared.preview.lock();
|
| 29 |
|
- |
if playback.playing {
|
| 30 |
|
- |
if let Some(ref buf) = playback.buffer {
|
| 31 |
|
- |
// During streaming, the buffer grows so use the metadata estimate
|
| 32 |
|
- |
// for a stable cursor. Fall back to current buffer size otherwise.
|
| 33 |
|
- |
let total_frames = if playback.streaming {
|
| 34 |
|
- |
playback
|
| 35 |
|
- |
.total_frames_estimate
|
| 36 |
|
- |
.unwrap_or(playback.decoded_frames)
|
| 37 |
|
- |
} else {
|
| 38 |
|
- |
buf.data.len() / 2
|
| 39 |
|
- |
};
|
| 40 |
|
- |
if total_frames > 0 {
|
| 41 |
|
- |
Some((playback.position_frac / total_frames as f64) as f32)
|
| 42 |
|
- |
} else {
|
| 43 |
|
- |
None
|
| 44 |
|
- |
}
|
| 45 |
|
- |
} else {
|
| 46 |
|
- |
None
|
| 47 |
|
- |
}
|
| 48 |
|
- |
} else {
|
| 49 |
|
- |
None
|
| 50 |
|
- |
}
|
| 51 |
|
- |
} else {
|
| 52 |
|
- |
None
|
| 53 |
|
- |
};
|
| 54 |
|
- |
|
| 55 |
|
- |
let resp = waveform::draw_waveform(ui, waveform_data, playback_pos, 120.0);
|
| 56 |
|
- |
// Hover indicator: paint a vertical action line at the cursor X
|
| 57 |
|
- |
// and a time label above it so the user can see where a click-to-seek
|
| 58 |
|
- |
// would land before committing.
|
| 59 |
|
- |
if resp.hovered()
|
| 60 |
|
- |
&& let Some(pos) = resp.hover_pos()
|
| 61 |
|
- |
{
|
| 62 |
|
- |
let rect = resp.rect;
|
| 63 |
|
- |
let normalized = ((pos.x - rect.left()) / rect.width()).clamp(0.0, 1.0);
|
| 64 |
|
- |
let total_secs = waveform_data.duration as f32;
|
| 65 |
|
- |
let cursor_secs = normalized * total_secs;
|
| 66 |
|
- |
ui.painter().line_segment(
|
| 67 |
|
- |
[
|
| 68 |
|
- |
egui::pos2(pos.x, rect.top()),
|
| 69 |
|
- |
egui::pos2(pos.x, rect.bottom()),
|
| 70 |
|
- |
],
|
| 71 |
|
- |
egui::Stroke::new(1.0, theme::action()),
|
| 72 |
|
- |
);
|
| 73 |
|
- |
let label = format!(
|
| 74 |
|
- |
"{:.0}:{:02.0}",
|
| 75 |
|
- |
(cursor_secs / 60.0).floor(),
|
| 76 |
|
- |
cursor_secs % 60.0,
|
| 77 |
|
- |
);
|
| 78 |
|
- |
ui.painter().text(
|
| 79 |
|
- |
egui::pos2(pos.x, rect.top() - 2.0),
|
| 80 |
|
- |
egui::Align2::CENTER_BOTTOM,
|
| 81 |
|
- |
label,
|
| 82 |
|
- |
egui::FontId::proportional(10.0),
|
| 83 |
|
- |
theme::content_secondary(),
|
| 84 |
|
- |
);
|
| 85 |
|
- |
}
|
| 86 |
|
- |
// Click-to-seek: map the click's X position to a 0.0–1.0 fraction
|
| 87 |
|
- |
// within the waveform rect, then set the playback cursor to that frame.
|
| 88 |
|
- |
if resp.clicked()
|
| 89 |
|
- |
&& let Some(pos) = resp.interact_pointer_pos()
|
| 90 |
|
- |
{
|
| 91 |
|
- |
let rect = resp.rect;
|
| 92 |
|
- |
let normalized = ((pos.x - rect.left()) / rect.width()).clamp(0.0, 1.0);
|
| 93 |
|
- |
if let Some(hash) = &node.node.sample_hash
|
| 94 |
|
- |
&& state.preview.previewing_hash.as_deref() == Some(hash)
|
| 95 |
|
- |
{
|
| 96 |
|
- |
let mut playback = state.shared.preview.lock();
|
| 97 |
|
- |
if let Some(ref buf) = playback.buffer {
|
| 98 |
|
- |
let total_frames = if playback.streaming {
|
| 99 |
|
- |
playback
|
| 100 |
|
- |
.total_frames_estimate
|
| 101 |
|
- |
.unwrap_or(playback.decoded_frames)
|
| 102 |
|
- |
} else {
|
| 103 |
|
- |
buf.data.len() / 2
|
| 104 |
|
- |
};
|
| 105 |
|
- |
playback.position_frac = (normalized as f64 * total_frames as f64)
|
| 106 |
|
- |
.min((playback.decoded_frames.max(1) - 1) as f64);
|
| 107 |
|
- |
}
|
| 108 |
|
- |
}
|
| 109 |
|
- |
}
|
| 110 |
|
- |
|
| 111 |
|
- |
ui.add_space(theme::section_spacing());
|
| 112 |
|
- |
}
|
| 113 |
|
- |
|
| 114 |
|
- |
// Sample name
|
| 115 |
|
- |
ui.label(egui::RichText::new(&node.node.name).strong().size(14.0));
|
| 116 |
|
- |
ui.add_space(theme::space::peer());
|
| 117 |
|
- |
|
| 118 |
|
- |
// Analysis metadata grid
|
| 119 |
|
- |
if let Some(ref analysis) = state.detail.selected_analysis {
|
| 120 |
|
- |
egui::CollapsingHeader::new("Metadata")
|
| 121 |
|
- |
.id_salt("detail_metadata_section")
|
| 122 |
|
- |
.default_open(true)
|
| 123 |
|
- |
.show(ui, |ui| {
|
| 124 |
|
- |
egui::Grid::new("detail_metadata")
|
| 125 |
|
- |
.num_columns(2)
|
| 126 |
|
- |
.spacing([8.0, theme::grid_row_spacing()])
|
| 127 |
|
- |
.show(ui, |ui| {
|
| 128 |
|
- |
ui.label(egui::RichText::new("Duration").color(theme::content_secondary()));
|
| 129 |
|
- |
ui.label(widgets::format_duration(analysis.duration));
|
| 130 |
|
- |
ui.end_row();
|
| 131 |
|
- |
|
| 132 |
|
- |
if let Some(bpm) = analysis.bpm {
|
| 133 |
|
- |
ui.label(egui::RichText::new("BPM").color(theme::content_secondary()));
|
| 134 |
|
- |
ui.label(widgets::format_bpm(bpm));
|
| 135 |
|
- |
ui.end_row();
|
| 136 |
|
- |
}
|
| 137 |
|
- |
|
| 138 |
|
- |
if let Some(ref key) = analysis.musical_key {
|
| 139 |
|
- |
ui.label(egui::RichText::new("Key").color(theme::content_secondary()));
|
| 140 |
|
- |
ui.label(key);
|
| 141 |
|
- |
ui.end_row();
|
| 142 |
|
- |
}
|
| 143 |
|
- |
|
| 144 |
|
- |
ui.label(
|
| 145 |
|
- |
egui::RichText::new("Sample Rate").color(theme::content_secondary()),
|
| 146 |
|
- |
);
|
| 147 |
|
- |
ui.label(format!("{} Hz", analysis.sample_rate));
|
| 148 |
|
- |
ui.end_row();
|
| 149 |
|
- |
|
| 150 |
|
- |
ui.label(egui::RichText::new("Channels").color(theme::content_secondary()));
|
| 151 |
|
- |
ui.label(format!("{}", analysis.channels));
|
| 152 |
|
- |
ui.end_row();
|
| 153 |
|
- |
|
| 154 |
|
- |
if let Some(peak) = analysis.peak_db {
|
| 155 |
|
- |
ui.label(egui::RichText::new("Peak").color(theme::content_secondary()));
|
| 156 |
|
- |
ui.label(format!("{peak:.1} dB"));
|
| 157 |
|
- |
ui.end_row();
|
| 158 |
|
- |
}
|
| 159 |
|
- |
|
| 160 |
|
- |
if let Some(rms) = analysis.rms_db {
|
| 161 |
|
- |
ui.label(egui::RichText::new("RMS").color(theme::content_secondary()));
|
| 162 |
|
- |
ui.label(format!("{rms:.1} dB"));
|
| 163 |
|
- |
ui.end_row();
|
| 164 |
|
- |
}
|
| 165 |
|
- |
|
| 166 |
|
- |
if let Some(lufs) = analysis.lufs {
|
| 167 |
|
- |
ui.label(egui::RichText::new("LUFS").color(theme::content_secondary()));
|
| 168 |
|
- |
ui.label(format!("{lufs:.1}"));
|
| 169 |
|
- |
ui.end_row();
|
| 170 |
|
- |
}
|
| 171 |
|
- |
|
| 172 |
|
- |
if let Some(is_loop) = analysis.is_loop {
|
| 173 |
|
- |
ui.label(egui::RichText::new("Loop").color(theme::content_secondary()));
|
| 174 |
|
- |
ui.label(if is_loop { "Yes" } else { "No" });
|
| 175 |
|
- |
ui.end_row();
|
| 176 |
|
- |
}
|
| 177 |
|
- |
});
|
| 178 |
|
- |
});
|
| 179 |
|
- |
}
|
| 180 |
|
- |
|
| 181 |
|
- |
ui.add_space(theme::section_spacing());
|
| 182 |
|
- |
|
| 183 |
|
- |
egui::CollapsingHeader::new("Tags")
|
| 184 |
|
- |
.id_salt("detail_tags_section")
|
| 185 |
|
- |
.default_open(true)
|
| 186 |
|
- |
.show(ui, |ui| {
|
| 187 |
|
- |
if state.detail.selected_tags.is_empty() {
|
| 188 |
|
- |
ui.label(egui::RichText::new("No tags").color(theme::content_muted()));
|
| 189 |
|
- |
} else {
|
| 190 |
|
- |
ui.horizontal_wrapped(|ui| {
|
| 191 |
|
- |
let tags = state.detail.selected_tags.clone();
|
| 192 |
|
- |
for tag in tags.iter() {
|
| 193 |
|
- |
if widgets::tag_chip_removable(ui, tag, true) {
|
| 194 |
|
- |
// Remove tag and push an undoable entry so Cmd+Z restores it.
|
| 195 |
|
- |
if let Some(ref hash) = node.node.sample_hash {
|
| 196 |
|
- |
let hash_str = hash.to_string();
|
| 197 |
|
- |
if state.backend.remove_tag(hash, tag).is_ok() {
|
| 198 |
|
- |
state.push_undo(crate::state::UndoOp::TagRemove {
|
| 199 |
|
- |
hash: hash_str,
|
| 200 |
|
- |
tag: tag.clone(),
|
| 201 |
|
- |
});
|
| 202 |
|
- |
state.status = format!("Removed tag \"{tag}\"");
|
| 203 |
|
- |
state.refresh_selected_tags();
|
| 204 |
|
- |
}
|
| 205 |
|
- |
}
|
| 206 |
|
- |
}
|
| 207 |
|
- |
}
|
| 208 |
|
- |
});
|
| 209 |
|
- |
}
|
| 210 |
|
- |
|
| 211 |
|
- |
// Tag input
|
| 212 |
|
- |
ui.horizontal(|ui| {
|
| 213 |
|
- |
let resp = widgets::text_field(
|
| 214 |
|
- |
ui,
|
| 215 |
|
- |
egui::TextEdit::singleline(&mut state.detail.tag_input)
|
| 216 |
|
- |
.hint_text("Add tag (use dots: genre.house)")
|
| 217 |
|
- |
.desired_width(ui.available_width() - 40.0),
|
| 218 |
|
- |
);
|
| 219 |
|
- |
// Honor the Tab-from-table shortcut: focus the tag input on this frame.
|
| 220 |
|
- |
if state.focus_tag_input {
|
| 221 |
|
- |
resp.request_focus();
|
| 222 |
|
- |
state.focus_tag_input = false;
|
| 223 |
|
- |
}
|
| 224 |
|
- |
if (resp.lost_focus() && ui.input(|i| i.key_pressed(egui::Key::Enter)))
|
| 225 |
|
- |
|| ui.small_button("+").on_hover_text("Add tag").clicked()
|
| 226 |
|
- |
{
|
| 227 |
|
- |
let tag = state.detail.tag_input.trim().to_string();
|
| 228 |
|
- |
if !tag.is_empty()
|
| 229 |
|
- |
&& let Some(ref hash) = node.node.sample_hash
|
| 230 |
|
- |
{
|
| 231 |
|
- |
if audiofiles_core::tags::validate_tag(&tag).is_ok() {
|
| 232 |
|
- |
let _ = state.backend.add_tag(hash, &tag);
|
| 233 |
|
- |
state.detail.tag_input.clear();
|
| 234 |
|
- |
state.refresh_selected_tags();
|
| 235 |
|
- |
} else {
|
| 236 |
|
- |
state.status = format!("Invalid tag: {tag}");
|
| 237 |
|
- |
}
|
| 238 |
|
- |
}
|
| 239 |
|
- |
}
|
| 240 |
|
- |
});
|
| 241 |
|
- |
|
| 242 |
|
- |
// Tag provenance: where each tag came from (manual vs rule/ml/cluster/folder),
|
| 243 |
|
- |
// so "why does this sample have this tag?" is answerable at a glance.
|
| 244 |
|
- |
if !state.detail.selected_tags.is_empty() {
|
| 245 |
|
- |
egui::CollapsingHeader::new("Tag sources")
|
| 246 |
|
- |
.id_salt("detail_tag_sources")
|
| 247 |
|
- |
.default_open(false)
|
| 248 |
|
- |
.show(ui, |ui| {
|
| 249 |
|
- |
let tags = state.detail.selected_tags.clone();
|
| 250 |
|
- |
for tag in tags.iter() {
|
| 251 |
|
- |
let (label, color) = match state.detail.selected_tag_sources.get(tag) {
|
| 252 |
|
- |
None => ("manual", theme::content_muted()),
|
| 253 |
|
- |
Some((source, _)) => match source.as_str() {
|
| 254 |
|
- |
"rule" => ("rule", theme::action()),
|
| 255 |
|
- |
"ml" => ("suggested", theme::category_five()),
|
| 256 |
|
- |
"cluster" => ("cluster", theme::category_six()),
|
| 257 |
|
- |
"harvest" => ("folder", theme::success()),
|
| 258 |
|
- |
other => (other, theme::content_muted()),
|
| 259 |
|
- |
},
|
| 260 |
|
- |
};
|
| 261 |
|
- |
ui.horizontal(|ui| {
|
| 262 |
|
- |
ui.label(egui::RichText::new(tag).small());
|
| 263 |
|
- |
ui.with_layout(
|
| 264 |
|
- |
egui::Layout::right_to_left(egui::Align::Center),
|
| 265 |
|
- |
|ui| {
|
| 266 |
|
- |
ui.label(egui::RichText::new(label).small().color(color));
|
| 267 |
|
- |
},
|
| 268 |
|
- |
);
|
| 269 |
|
- |
});
|
| 270 |
|
- |
}
|
| 271 |
|
- |
});
|
| 272 |
|
- |
}
|
| 273 |
|
- |
|
| 274 |
|
- |
// k-NN tag suggestions from acoustically similar samples (on demand, so the
|
| 275 |
|
- |
// exemplar index is only built when the user asks).
|
| 276 |
|
- |
ui.add_space(theme::space::bound());
|
| 277 |
|
- |
if ui
|
| 278 |
|
- |
.button("Suggest similar tags")
|
| 279 |
|
- |
.on_hover_text("Find tags from samples that sound similar to this one")
|
| 280 |
|
- |
.clicked()
|
| 281 |
|
- |
{
|
| 282 |
|
- |
state.suggest_ml_for_selected();
|
| 283 |
|
- |
}
|
| 284 |
|
- |
if !state.detail.selected_ml_suggestions.is_empty() {
|
| 285 |
|
- |
let suggestions = state.detail.selected_ml_suggestions.clone();
|
| 286 |
|
- |
let mut accept: Option<String> = None;
|
| 287 |
|
- |
for s in &suggestions {
|
| 288 |
|
- |
ui.horizontal(|ui| {
|
| 289 |
|
- |
if ui.small_button("Add").clicked() {
|
| 290 |
|
- |
accept = Some(s.tag.clone());
|
| 291 |
|
- |
}
|
| 292 |
|
- |
ui.label(egui::RichText::new(&s.tag).small())
|
| 293 |
|
- |
.on_hover_text(format!(
|
| 294 |
|
- |
"{} similar sample(s) carry this tag",
|
| 295 |
|
- |
s.neighbors.len()
|
| 296 |
|
- |
));
|
| 297 |
|
- |
ui.label(
|
| 298 |
|
- |
egui::RichText::new(format!("{:.0}%", s.score * 100.0))
|
| 299 |
|
- |
.small()
|
| 300 |
|
- |
.color(theme::content_muted()),
|
| 301 |
|
- |
);
|
| 302 |
|
- |
});
|
| 303 |
|
- |
}
|
| 304 |
|
- |
if let Some(tag) = accept {
|
| 305 |
|
- |
state.accept_ml_suggestion(&tag);
|
| 306 |
|
- |
}
|
| 307 |
|
- |
}
|
| 308 |
|
- |
}); // end of Tags CollapsingHeader
|
| 309 |
|
- |
|
| 310 |
|
- |
egui::CollapsingHeader::new("Actions")
|
| 311 |
|
- |
.id_salt("detail_actions_section")
|
| 312 |
|
- |
.default_open(true)
|
| 313 |
|
- |
.show(ui, |ui| {
|
| 314 |
|
- |
ui.horizontal(|ui| {
|
| 315 |
|
- |
if ui
|
| 316 |
|
- |
.button("Copy Path")
|
| 317 |
|
- |
.on_hover_text("Copy file path to clipboard")
|
| 318 |
|
- |
.clicked()
|
| 319 |
|
- |
&& let Some(path) = state.selected_sample_path()
|
| 320 |
|
- |
{
|
| 321 |
|
- |
state.status = format!("Copied: {path}");
|
| 322 |
|
- |
ui.ctx().copy_text(path);
|
| 323 |
|
- |
}
|
| 324 |
|
- |
if let Some(hash) = &node.node.sample_hash {
|
| 325 |
|
- |
let hash = hash.clone();
|
| 326 |
|
- |
if ui
|
| 327 |
|
- |
.button("Edit")
|
| 328 |
|
- |
.on_hover_text("Open sample editor (E)")
|
| 329 |
|
- |
.clicked()
|
| 330 |
|
- |
{
|
| 331 |
|
- |
state.open_edit_window(&hash);
|
| 332 |
|
- |
}
|
| 333 |
|
- |
if ui
|
| 334 |
|
- |
.button("Forge")
|
| 335 |
|
- |
.on_hover_text("Chop / conform / batch (F)")
|
| 336 |
|
- |
.clicked()
|
| 337 |
|
- |
{
|
| 338 |
|
- |
state.open_forge_window(&hash);
|
| 339 |
|
- |
}
|
| 340 |
|
- |
}
|
| 341 |
|
- |
});
|
| 342 |
|
- |
});
|
| 343 |
|
- |
|
| 344 |
|
- |
if let Some(hash) = &node.node.sample_hash {
|
| 345 |
|
- |
let hash = hash.clone();
|
| 346 |
|
- |
// M-10: gate Discovery on the analysis features each path needs.
|
| 347 |
|
- |
// Find Similar reads spectral_centroid / spectral_bandwidth;
|
| 348 |
|
- |
// Find Duplicates reads the peak-envelope fingerprint. Without
|
| 349 |
|
- |
// these the button "works" but always returns zero results,
|
| 350 |
|
- |
// which reads as a broken feature instead of a missing prereq.
|
| 351 |
|
- |
let has_spectral = state
|
| 352 |
|
- |
.detail
|
| 353 |
|
- |
.selected_analysis
|
| 354 |
|
- |
.as_ref()
|
| 355 |
|
- |
.is_some_and(|a| a.spectral_centroid.is_some() || a.spectral_bandwidth.is_some());
|
| 356 |
|
- |
let has_fingerprint = state
|
| 357 |
|
- |
.detail
|
| 358 |
|
- |
.selected_analysis
|
| 359 |
|
- |
.as_ref()
|
| 360 |
|
- |
.is_some_and(|a| a.fingerprint.is_some());
|
| 361 |
|
- |
egui::CollapsingHeader::new("Discovery")
|
| 362 |
|
- |
.id_salt("detail_discovery_section")
|
| 363 |
|
- |
.default_open(true)
|
| 364 |
|
- |
.show(ui, |ui| {
|
| 365 |
|
- |
ui.horizontal(|ui| {
|
| 366 |
|
- |
let similar_resp = ui.add_enabled(
|
| 367 |
|
- |
has_spectral,
|
| 368 |
|
- |
egui::Button::new("Find Similar"),
|
| 369 |
|
- |
);
|
| 370 |
|
- |
let similar_resp = if has_spectral {
|
| 371 |
|
- |
similar_resp.on_hover_text("Find similar samples (Shift+F)")
|
| 372 |
|
- |
} else {
|
| 373 |
|
- |
similar_resp.on_disabled_hover_text(
|
| 374 |
|
- |
"Re-analyze this sample with spectral features enabled to find similar samples.",
|
| 375 |
|
- |
)
|
| 376 |
|
- |
};
|
| 377 |
|
- |
if similar_resp.clicked() {
|
| 378 |
|
- |
state.find_similar(&hash);
|
| 379 |
|
- |
}
|
| 380 |
|
- |
let dup_resp = ui.add_enabled(
|
| 381 |
|
- |
has_fingerprint,
|
| 382 |
|
- |
egui::Button::new("Find Duplicates"),
|
| 383 |
|
- |
);
|
| 384 |
|
- |
let dup_resp = if has_fingerprint {
|
| 385 |
|
- |
dup_resp.on_hover_text("Find near-duplicates (Shift+D)")
|
| 386 |
|
- |
} else {
|
| 387 |
|
- |
dup_resp.on_disabled_hover_text(
|
| 388 |
|
- |
"Re-analyze this sample with fingerprinting enabled to find duplicates.",
|
| 389 |
|
- |
)
|
| 390 |
|
- |
};
|
| 391 |
|
- |
if dup_resp.clicked() {
|
| 392 |
|
- |
state.find_near_duplicates(&hash);
|
| 393 |
|
- |
}
|
| 394 |
|
- |
});
|
| 395 |
|
- |
});
|
| 396 |
|
- |
}
|
| 397 |
|
- |
}
|
| 398 |
|
- |
|
| 399 |
|
- |
/// Reduce a field across a multi-selection to one displayable value. Returns
|
| 400 |
|
- |
/// `None` when the selection is empty or the first item lacks the field (nothing
|
| 401 |
|
- |
/// to show), `Some(Err(()))` when the values differ or any item lacks the field
|
| 402 |
|
- |
/// (renders as "varies"), and `Some(Ok(v))` when every item shares value `v`.
|
| 403 |
|
- |
pub(crate) fn summarize<T, F, V>(items: &[T], extract: F) -> Option<Result<V, ()>>
|
| 404 |
|
- |
where
|
| 405 |
|
- |
F: Fn(&T) -> Option<V>,
|
| 406 |
|
- |
V: PartialEq,
|
| 407 |
|
- |
{
|
| 408 |
|
- |
let mut iter = items.iter().map(&extract);
|
| 409 |
|
- |
let first = iter.next()??;
|
| 410 |
|
- |
for v in iter {
|
| 411 |
|
- |
match v {
|
| 412 |
|
- |
Some(v) if v == first => {}
|
| 413 |
|
- |
Some(_) => return Some(Err(())),
|
| 414 |
|
- |
None => return Some(Err(())),
|
| 415 |
|
- |
}
|
| 416 |
|
- |
}
|
| 417 |
|
- |
Some(Ok(first))
|
| 418 |
|
- |
}
|
| 419 |
|
- |
|
| 420 |
|
- |
/// Draw a multi-selection summary: common metadata, union of tags, bulk-edit affordance.
|
| 421 |
|
- |
fn draw_multi_summary(ui: &mut egui::Ui, state: &mut BrowserState) {
|
| 422 |
|
- |
let nodes = state.selected_nodes();
|
| 423 |
|
- |
let samples: Vec<_> = nodes
|
| 424 |
|
- |
.iter()
|
| 425 |
|
- |
.filter(|n| n.node.sample_hash.is_some())
|
| 426 |
|
- |
.collect();
|
| 427 |
|
- |
let sample_count = samples.len();
|
| 428 |
|
- |
let folder_count = nodes.len().saturating_sub(sample_count);
|
| 429 |
|
- |
|
| 430 |
|
- |
let heading = if folder_count == 0 {
|
| 431 |
|
- |
format!("{sample_count} samples selected")
|
| 432 |
|
- |
} else {
|
| 433 |
|
- |
format!("{sample_count} samples \u{00B7} {folder_count} folders selected")
|
| 434 |
|
- |
};
|
| 435 |
|
- |
ui.label(egui::RichText::new(heading).strong().size(14.0));
|
| 436 |
|
- |
ui.add_space(theme::space::peer());
|
| 437 |
|
- |
|
| 438 |
|
- |
if sample_count == 0 {
|
| 439 |
|
- |
widgets::empty_state(
|
| 440 |
|
- |
ui,
|
| 441 |
|
- |
"No sample metadata to summarize",
|
| 442 |
|
- |
Some("Select one or more samples to see common fields"),
|
| 443 |
|
- |
None,
|
| 444 |
|
- |
);
|
| 445 |
|
- |
return;
|
| 446 |
|
- |
}
|
| 447 |
|
- |
|
| 448 |
|
- |
// Common metadata: show value if uniform across the selection, otherwise "varies".
|
| 449 |
|
- |
ui.group(|ui| {
|
| 450 |
|
- |
egui::Grid::new("detail_multi_metadata")
|
| 451 |
|
- |
.num_columns(2)
|
| 452 |
|
- |
.spacing([8.0, theme::grid_row_spacing()])
|
| 453 |
|
- |
.show(ui, |ui| {
|
| 454 |
|
- |
let bpm = summarize(&samples, |n| n.bpm);
|
| 455 |
|
- |
ui.label(egui::RichText::new("BPM").color(theme::content_secondary()));
|
| 456 |
|
- |
ui.label(match bpm {
|
| 457 |
|
- |
Some(Ok(v)) => widgets::format_bpm(v),
|
| 458 |
|
- |
Some(Err(())) => "varies".to_string(),
|
| 459 |
|
- |
None => "\u{2014}".to_string(),
|
| 460 |
|
- |
});
|
| 461 |
|
- |
ui.end_row();
|
| 462 |
|
- |
|
| 463 |
|
- |
let key = summarize(&samples, |n| n.musical_key.clone());
|
| 464 |
|
- |
ui.label(egui::RichText::new("Key").color(theme::content_secondary()));
|
| 465 |
|
- |
ui.label(match key {
|
| 466 |
|
- |
Some(Ok(v)) => v,
|
| 467 |
|
- |
Some(Err(())) => "varies".to_string(),
|
| 468 |
|
- |
None => "\u{2014}".to_string(),
|
| 469 |
|
- |
});
|
| 470 |
|
- |
ui.end_row();
|
| 471 |
|
- |
|
| 472 |
|
- |
let dur = summarize(&samples, |n| n.duration);
|
| 473 |
|
- |
ui.label(egui::RichText::new("Duration").color(theme::content_secondary()));
|
| 474 |
|
- |
ui.label(match dur {
|
| 475 |
|
- |
Some(Ok(v)) => widgets::format_duration(v),
|
| 476 |
|
- |
Some(Err(())) => "varies".to_string(),
|
| 477 |
|
- |
None => "\u{2014}".to_string(),
|
| 478 |
|
- |
});
|
| 479 |
|
- |
ui.end_row();
|
| 480 |
|
- |
});
|
| 481 |
|
- |
});
|
| 482 |
|
- |
|
| 483 |
|
- |
ui.add_space(theme::section_spacing());
|
| 484 |
|
- |
ui.separator();
|
| 485 |
|
- |
ui.add_space(theme::section_spacing() * 0.5);
|
| 486 |
|
- |
|
| 487 |
|
- |
// Tag union with per-tag count badges.
|
| 488 |
|
- |
widgets::subsection_label(ui, "Tags");
|
| 489 |
|
- |
let mut tag_counts: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
|
| 490 |
|
- |
for n in &samples {
|
| 491 |
|
- |
for tag in &n.tags {
|
| 492 |
|
- |
*tag_counts.entry(tag.clone()).or_insert(0) += 1;
|
| 493 |
|
- |
}
|
| 494 |
|
- |
}
|
| 495 |
|
- |
if tag_counts.is_empty() {
|
| 496 |
|
- |
ui.label(egui::RichText::new("No tags").color(theme::content_muted()));
|
| 497 |
|
- |
} else {
|
| 498 |
|
- |
let mut entries: Vec<_> = tag_counts.into_iter().collect();
|
| 499 |
|
- |
entries.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
|
| 500 |
|
- |
// Collect the hashes once so the closure that handles a badge click
|