Skip to content

Commit 9a2ccfd

Browse files
committed
Add plot controls UI components: Implement functionality for resizing plot dimensions, including width and height controls, reset functionality, and handling of resize button interactions. This enhances user experience by allowing dynamic adjustments to plot sizes.
1 parent 1dc67cd commit 9a2ccfd

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

src/ui/plot_controls/controls.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::ui::types::PlotResizeDirection;
2+
use eframe::egui;
3+
use std::time::Instant;
4+
5+
use super::dimension::render_dimension_control;
6+
use super::reset::render_reset_row;
7+
8+
pub fn render_plot_size_controls(
9+
ui: &mut egui::Ui,
10+
custom_plot_width: &mut f32,
11+
custom_plot_height: &mut f32,
12+
plot_resize_start_time: &mut Option<Instant>,
13+
plot_resize_direction: &mut PlotResizeDirection,
14+
plot_resize_last_repeat: &mut Option<Instant>,
15+
) {
16+
ui.vertical(|ui| {
17+
ui.label(format!("{} Plot Size", egui_phosphor::regular::CHART_BAR));
18+
ui.add_space(4.0);
19+
20+
render_dimension_control(
21+
ui,
22+
"Width:",
23+
custom_plot_width,
24+
PlotResizeDirection::WidthDecrease,
25+
PlotResizeDirection::WidthIncrease,
26+
plot_resize_start_time,
27+
plot_resize_direction,
28+
plot_resize_last_repeat,
29+
);
30+
31+
ui.add_space(4.0);
32+
33+
render_dimension_control(
34+
ui,
35+
"Height:",
36+
custom_plot_height,
37+
PlotResizeDirection::HeightDecrease,
38+
PlotResizeDirection::HeightIncrease,
39+
plot_resize_start_time,
40+
plot_resize_direction,
41+
plot_resize_last_repeat,
42+
);
43+
44+
ui.add_space(4.0);
45+
render_reset_row(
46+
ui,
47+
custom_plot_width,
48+
custom_plot_height,
49+
plot_resize_direction,
50+
plot_resize_start_time,
51+
plot_resize_last_repeat,
52+
);
53+
});
54+
}

src/ui/plot_controls/dimension.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use crate::ui::constants::{PLOT_MAX_SIZE, PLOT_MIN_SIZE, PLOT_RESIZEE_INCREMENT};
2+
use crate::ui::types::PlotResizeDirection;
3+
use eframe::egui;
4+
use std::time::Instant;
5+
6+
use super::handlers::handle_resize_button;
7+
8+
#[allow(clippy::too_many_arguments)]
9+
pub fn render_dimension_control(
10+
ui: &mut egui::Ui,
11+
label: &str,
12+
value: &mut f32,
13+
decrement_direction: PlotResizeDirection,
14+
increment_direction: PlotResizeDirection,
15+
plot_resize_start_time: &mut Option<Instant>,
16+
plot_resize_direction: &mut PlotResizeDirection,
17+
plot_resize_last_repeat: &mut Option<Instant>,
18+
) {
19+
ui.horizontal(|ui| {
20+
ui.label(label);
21+
let btn_size = egui::vec2(22.0, ui.spacing().interact_size.y);
22+
23+
let minus_response = ui.add_sized(btn_size, egui::Button::new("-"));
24+
handle_resize_button(
25+
ui,
26+
&minus_response,
27+
value,
28+
-PLOT_RESIZEE_INCREMENT,
29+
PLOT_MIN_SIZE,
30+
PLOT_MAX_SIZE,
31+
decrement_direction,
32+
plot_resize_direction,
33+
plot_resize_start_time,
34+
plot_resize_last_repeat,
35+
);
36+
37+
ui.label(format!("{value:.0}px"));
38+
39+
let plus_response = ui.add_sized(btn_size, egui::Button::new("+"));
40+
handle_resize_button(
41+
ui,
42+
&plus_response,
43+
value,
44+
PLOT_RESIZEE_INCREMENT,
45+
PLOT_MIN_SIZE,
46+
PLOT_MAX_SIZE,
47+
increment_direction,
48+
plot_resize_direction,
49+
plot_resize_start_time,
50+
plot_resize_last_repeat,
51+
);
52+
});
53+
}

src/ui/plot_controls/handlers.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use eframe::egui;
2+
use std::time::Instant;
3+
4+
use crate::ui::constants::{PLOT_RESIZE_INITIAL_DELAY, PLOT_RESIZE_REPEAT_RATE};
5+
use crate::ui::types::PlotResizeDirection;
6+
7+
#[allow(clippy::too_many_arguments)]
8+
pub fn handle_resize_button(
9+
ui: &egui::Ui,
10+
response: &egui::Response,
11+
value: &mut f32,
12+
delta: f32,
13+
min: f32,
14+
max: f32,
15+
direction: PlotResizeDirection,
16+
plot_resize_direction: &mut PlotResizeDirection,
17+
plot_resize_start_time: &mut Option<Instant>,
18+
plot_resize_last_repeat: &mut Option<Instant>,
19+
) {
20+
if response.is_pointer_button_down_on() {
21+
if *plot_resize_direction != direction {
22+
*value = (*value + delta).clamp(min, max);
23+
*plot_resize_start_time = Some(Instant::now());
24+
*plot_resize_direction = direction;
25+
*plot_resize_last_repeat = None;
26+
} else if let Some(start_time) = plot_resize_start_time {
27+
let elapsed = start_time.elapsed().as_secs_f32();
28+
if elapsed > PLOT_RESIZE_INITIAL_DELAY {
29+
let should_repeat = match plot_resize_last_repeat {
30+
Some(last_repeat) => {
31+
last_repeat.elapsed().as_secs_f32() >= PLOT_RESIZE_REPEAT_RATE
32+
}
33+
None => true,
34+
};
35+
36+
if should_repeat {
37+
*value = (*value + delta).clamp(min, max);
38+
*plot_resize_last_repeat = Some(Instant::now());
39+
}
40+
}
41+
}
42+
43+
ui.ctx().request_repaint();
44+
} else if *plot_resize_direction == direction {
45+
*plot_resize_direction = PlotResizeDirection::None;
46+
*plot_resize_start_time = None;
47+
*plot_resize_last_repeat = None;
48+
}
49+
}

src/ui/plot_controls/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mod controls;
2+
mod dimension;
3+
mod handlers;
4+
mod reset;
5+
6+
pub use controls::render_plot_size_controls;

src/ui/plot_controls/reset.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::ui::constants::{DEFAULT_PLOT_HEIGHT, DEFAULT_PLOT_WIDTH};
2+
use crate::ui::types::PlotResizeDirection;
3+
use eframe::egui;
4+
use egui::Ui;
5+
6+
pub fn render_reset_row(
7+
ui: &mut Ui,
8+
custom_plot_width: &mut f32,
9+
custom_plot_height: &mut f32,
10+
plot_resize_direction: &mut PlotResizeDirection,
11+
plot_resize_start_time: &mut Option<std::time::Instant>,
12+
plot_resize_last_repeat: &mut Option<std::time::Instant>,
13+
) {
14+
use egui::{Button, RichText};
15+
use egui_phosphor::regular::ARROW_COUNTER_CLOCKWISE;
16+
17+
ui.horizontal(|ui| {
18+
ui.label("Reset:");
19+
let btn_size = egui::vec2(22.0, ui.spacing().interact_size.y);
20+
if ui
21+
.add_sized(
22+
btn_size,
23+
Button::new(RichText::new(ARROW_COUNTER_CLOCKWISE)),
24+
)
25+
.clicked()
26+
{
27+
*custom_plot_width = DEFAULT_PLOT_WIDTH;
28+
*custom_plot_height = DEFAULT_PLOT_HEIGHT;
29+
*plot_resize_direction = PlotResizeDirection::None;
30+
*plot_resize_start_time = None;
31+
*plot_resize_last_repeat = None;
32+
}
33+
ui.label(format!(
34+
"{}×{}",
35+
DEFAULT_PLOT_WIDTH as u32, DEFAULT_PLOT_HEIGHT as u32
36+
));
37+
});
38+
}

0 commit comments

Comments
 (0)