Skip to content

Commit d904c4a

Browse files
committed
refactor(ui): Optimize mouse area generation for volume and node widgets
- Improve code formatting and readability in node_widget.rs - Reduce code complexity by breaking long lines and improving indentation - Optimize mouse area generation with more precise segment calculations - Add more consistent spacing and alignment in volume and node widget implementations - Minimize code duplication in mouse area generation methods
1 parent 749d283 commit d904c4a

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

src/node_widget.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,11 @@ impl<'a> NodeWidget<'a> {
8787

8888
// Add 2 for vertical borders and 2 for highlight symbol
8989
let width = max_target_length.saturating_add(4) as u16;
90-
let height = std::cmp::min(MAX_DROPDOWN_VISIBLE_ITEMS, object_list.targets.len())
91-
.saturating_add(2) as u16; // Plus 2 for horizontal borders
90+
let height = std::cmp::min(
91+
MAX_DROPDOWN_VISIBLE_ITEMS,
92+
object_list.targets.len(),
93+
)
94+
.saturating_add(2) as u16; // Plus 2 for horizontal borders
9295

9396
// Align to the right of the list area
9497
let x = list_area.right().saturating_sub(width);
@@ -107,10 +110,10 @@ impl StatefulWidget for NodeWidget<'_> {
107110

108111
// Pre-calculate common mouse areas to reduce allocations
109112
let object_id = self.node.object_id;
110-
113+
111114
// Reserve space for mouse areas to reduce reallocations
112115
mouse_areas.reserve(8);
113-
116+
114117
mouse_areas.extend([
115118
(
116119
area,
@@ -120,10 +123,7 @@ impl StatefulWidget for NodeWidget<'_> {
120123
(
121124
area,
122125
smallvec![MouseEventKind::Down(MouseButton::Right)],
123-
smallvec![
124-
Action::SelectObject(object_id),
125-
Action::SetDefault
126-
],
126+
smallvec![Action::SelectObject(object_id), Action::SetDefault],
127127
),
128128
(
129129
area,
@@ -442,16 +442,14 @@ impl StatefulWidget for VolumeWidget<'_> {
442442
let object_id = self.node.object_id;
443443

444444
// Reserve space for mouse areas to reduce reallocations
445-
let estimated_areas = 3 + self.calculate_volume_segments(volume_bar.width);
445+
let estimated_areas =
446+
3 + self.calculate_volume_segments(volume_bar.width);
446447
mouse_areas.reserve(estimated_areas);
447448

448449
mouse_areas.push((
449450
volume_label,
450451
smallvec![MouseEventKind::Down(MouseButton::Left)],
451-
smallvec![
452-
Action::SelectObject(object_id),
453-
Action::ToggleMute
454-
],
452+
smallvec![Action::SelectObject(object_id), Action::ToggleMute],
455453
));
456454

457455
// Add mousewheel support for volume control on the volume bar
@@ -473,7 +471,12 @@ impl StatefulWidget for VolumeWidget<'_> {
473471
));
474472

475473
// Optimize volume mouse areas by using segments instead of per-pixel areas
476-
self.add_volume_mouse_areas(mouse_areas, volume_bar, max_volume, object_id);
474+
self.add_volume_mouse_areas(
475+
mouse_areas,
476+
volume_bar,
477+
max_volume,
478+
object_id,
479+
);
477480
}
478481
}
479482

@@ -483,9 +486,10 @@ impl VolumeWidget<'_> {
483486
if width == 0 {
484487
return 0;
485488
}
486-
489+
487490
// Use segments to reduce mouse area count while maintaining good precision
488-
let segment_size = VOLUME_SEGMENT_SIZE.max(width / MIN_VOLUME_SEGMENTS).max(1);
491+
let segment_size =
492+
VOLUME_SEGMENT_SIZE.max(width / MIN_VOLUME_SEGMENTS).max(1);
489493
width.div_ceil(segment_size) as usize
490494
}
491495

@@ -502,7 +506,9 @@ impl VolumeWidget<'_> {
502506
}
503507

504508
// Calculate segment size for mouse areas
505-
let segment_size = VOLUME_SEGMENT_SIZE.max(volume_bar.width / MIN_VOLUME_SEGMENTS).max(1);
509+
let segment_size = VOLUME_SEGMENT_SIZE
510+
.max(volume_bar.width / MIN_VOLUME_SEGMENTS)
511+
.max(1);
506512
let num_segments = volume_bar.width.div_ceil(segment_size);
507513

508514
for segment in 0..num_segments {
@@ -525,14 +531,15 @@ impl VolumeWidget<'_> {
525531
let middle_pos = start_x + segment_width / 2;
526532
let volume_step = max_volume / volume_bar.width as f32;
527533
let volume = volume_step * middle_pos as f32;
528-
534+
529535
// Make the volume sticky around 100%. Otherwise it's often not
530536
// possible to select by mouse.
531-
let sticky_volume = if (1.0 - volume).abs() <= volume_step * segment_size as f32 {
532-
1.0
533-
} else {
534-
volume
535-
};
537+
let sticky_volume =
538+
if (1.0 - volume).abs() <= volume_step * segment_size as f32 {
539+
1.0
540+
} else {
541+
volume
542+
};
536543

537544
mouse_areas.push((
538545
volume_area,

0 commit comments

Comments
 (0)