Skip to content

Commit

Permalink
🌟(App): add selected sample tracking
Browse files Browse the repository at this point in the history
Add a `selected_sample` field to track the currently selected sample. Update the UI to visually indicate the selected sample and allow toggling the selection on click.

Signed-off-by: Benign X <[email protected]>
  • Loading branch information
W-Mai committed Nov 21, 2024
1 parent 96d3637 commit 11f0476
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct MainApp {

cache: MainAppCache,
samples_cache: BTreeMap<&'static str, MainAppCache>,
selected_sample: &'static str,

#[cfg(target_arch = "wasm32")]
is_loaded_from_url: bool,
Expand Down Expand Up @@ -114,6 +115,7 @@ impl Default for MainApp {
is_loaded_from_url: false,
side_panel_open: false,
panel_status: Default::default(),
selected_sample: "",
}
}
}
Expand Down Expand Up @@ -240,6 +242,7 @@ impl MainApp {
fn ui_samples_panel(&mut self, ui: &mut egui::Ui) {
egui::ScrollArea::vertical().show(ui, |ui| {
for (name, code) in SAMPLE_CODES_LIST {
let selected = self.selected_sample == name;
egui::containers::Frame::default()
.inner_margin(10.0)
.outer_margin(10.0)
Expand Down Expand Up @@ -299,11 +302,22 @@ impl MainApp {

let response = one_sample.response;

let visuals = ui.style().interact_selectable(&response, true);
let visuals = ui.style().interact_selectable(&response, selected);

let rect = response.rect;
let response = ui.allocate_rect(rect, Sense::click());
if response.hovered() || response.highlighted() || response.has_focus() {
if response.clicked() {
if selected {
self.selected_sample = ""
} else {
self.selected_sample = name;
}
}
if selected
|| response.hovered()
|| response.highlighted()
|| response.has_focus()
{
let rect = rect.expand(10.0);
let mut painter = ui.painter_at(rect);
let rect = rect.expand(-2.0);
Expand Down

0 comments on commit 11f0476

Please sign in to comment.