From 654cbb404ebebd87a33ca98a93cd112c9bb3d5f3 Mon Sep 17 00:00:00 2001 From: Lars Frost Date: Sat, 30 Sep 2023 18:29:02 +0200 Subject: [PATCH] Format scrollbar code --- alacritty/src/display/mod.rs | 11 +++++--- alacritty/src/display/scrollbar.rs | 43 +++++++++++++++++++++++------- alacritty/src/input.rs | 9 ++++--- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/alacritty/src/display/mod.rs b/alacritty/src/display/mod.rs index 05803a9a7c1..ec5be42aba2 100644 --- a/alacritty/src/display/mod.rs +++ b/alacritty/src/display/mod.rs @@ -1108,9 +1108,14 @@ impl Display { let bg_rect = self.scrollbar.bg_rect(self.size_info); let scrollbar_rect = self.scrollbar.rect_from_bg_rect(bg_rect, self.size_info); let y = self.size_info.height - (scrollbar_rect.y + scrollbar_rect.height) as f32; - rects.push( - RenderRect::new(scrollbar_rect.x as f32, y, scrollbar_rect.width as f32, scrollbar_rect.height as f32, config.color, opacity) - ); + rects.push(RenderRect::new( + scrollbar_rect.x as f32, + y, + scrollbar_rect.width as f32, + scrollbar_rect.height as f32, + config.color, + opacity, + )); if did_position_change { self.damage_rects.push(bg_rect); } else if config.mode == ScrollbarMode::Fading && opacity < config.opacity.as_f32() { diff --git a/alacritty/src/display/scrollbar.rs b/alacritty/src/display/scrollbar.rs index 072a44e3848..f4d30d2ba8d 100644 --- a/alacritty/src/display/scrollbar.rs +++ b/alacritty/src/display/scrollbar.rs @@ -21,7 +21,13 @@ pub struct Scrollbar { impl From<&ScrollbarConfig> for Scrollbar { fn from(value: &ScrollbarConfig) -> Self { - Scrollbar { config: value.clone(), display_offset: 0, total_lines: 0, last_change: None, drag_state: None } + Scrollbar { + config: value.clone(), + display_offset: 0, + total_lines: 0, + last_change: None, + drag_state: None, + } } } @@ -85,7 +91,8 @@ impl Scrollbar { pub fn bg_rect(&self, display_size: SizeInfo) -> Rect { let scrollbar_margin_y = display_size.padding_y(); - let scrollbar_margin_x = display_size.padding_right() - self.config.additional_padding(display_size.cell_width, display_size.padding_left()); + let scrollbar_margin_x = display_size.padding_right() + - self.config.additional_padding(display_size.cell_width, display_size.padding_left()); let background_area_height: f32 = display_size.height - 2. * scrollbar_margin_y; @@ -119,7 +126,12 @@ impl Scrollbar { } } - pub fn contains_mouse_pos(&mut self, display_size: SizeInfo, mouse_x: usize, mouse_y: usize) -> bool { + pub fn contains_mouse_pos( + &mut self, + display_size: SizeInfo, + mouse_x: usize, + mouse_y: usize, + ) -> bool { let intensity = if let Some(intensity) = self.intensity() { intensity } else { @@ -134,14 +146,22 @@ impl Scrollbar { let mouse_x = mouse_x as f32; let mouse_y = display_size.height - mouse_y as f32; - if !(scrollbar_rect.x as f32 .. (scrollbar_rect.x + scrollbar_rect.width) as f32).contains(&mouse_x) { + if !(scrollbar_rect.x as f32..(scrollbar_rect.x + scrollbar_rect.width) as f32) + .contains(&mouse_x) + { return false; } - (scrollbar_rect.y as f32 ..(scrollbar_rect.y+scrollbar_rect.height) as f32).contains(&mouse_y) + (scrollbar_rect.y as f32..(scrollbar_rect.y + scrollbar_rect.height) as f32) + .contains(&mouse_y) } - pub fn try_start_drag(&mut self, display_size: SizeInfo, mouse_x: usize, mouse_y: usize) -> bool { + pub fn try_start_drag( + &mut self, + display_size: SizeInfo, + mouse_x: usize, + mouse_y: usize, + ) -> bool { if !self.contains_mouse_pos(display_size, mouse_x, mouse_y) { return false; } @@ -150,7 +170,8 @@ impl Scrollbar { let rect = self.rect_from_bg_rect(bg_rect, display_size); if bg_rect.height == rect.height || self.total_lines <= display_size.screen_lines { - self.drag_state = Some(DragState { cells_per_dragged_pixel: 0.0, accumulated_cells: 0. }); + self.drag_state = + Some(DragState { cells_per_dragged_pixel: 0.0, accumulated_cells: 0. }); return true; } @@ -171,10 +192,12 @@ impl Scrollbar { self.drag_state = None; } - #[must_use = "The actual scroll is not applied but returned and has to be applied by the callside"] + #[must_use = "The actual scroll is not applied but returned and has to be applied by the \ + callside"] pub fn apply_mouse_delta(&mut self, mouse_y_delta_in_pixel: f32) -> Option { if let Some(drag_state) = self.drag_state.as_mut() { - drag_state.accumulated_cells += mouse_y_delta_in_pixel * drag_state.cells_per_dragged_pixel; + drag_state.accumulated_cells += + mouse_y_delta_in_pixel * drag_state.cells_per_dragged_pixel; let cells = drag_state.accumulated_cells as i32; // round towards zero if cells == 0 { None @@ -192,4 +215,4 @@ impl Scrollbar { struct DragState { cells_per_dragged_pixel: f32, accumulated_cells: f32, -} \ No newline at end of file +} diff --git a/alacritty/src/input.rs b/alacritty/src/input.rs index 9e95af570b1..027c46e5cbe 100644 --- a/alacritty/src/input.rs +++ b/alacritty/src/input.rs @@ -432,7 +432,8 @@ impl> Processor { if self.ctx.config().scrollbar.mode != ScrollbarMode::Never { let mouse_y_delta = y as f32 - self.ctx.mouse().y as f32; - if let Some(drag_event) = self.ctx.display().scrollbar.apply_mouse_delta(mouse_y_delta) { + if let Some(drag_event) = self.ctx.display().scrollbar.apply_mouse_delta(mouse_y_delta) + { self.ctx.scroll(drag_event); } } @@ -673,7 +674,9 @@ impl> Processor { } fn on_mouse_release(&mut self, button: MouseButton) { - if self.ctx.config().scrollbar.mode != ScrollbarMode::Never && self.ctx.display().scrollbar.is_dragging() { + if self.ctx.config().scrollbar.mode != ScrollbarMode::Never + && self.ctx.display().scrollbar.is_dragging() + { self.ctx.display().scrollbar.stop_dragging(); // Mouse icon is different, when not scrolling. let mouse_state = self.cursor_state(); @@ -845,7 +848,7 @@ impl> Processor { let size_info = self.ctx.size_info(); let mouse_x = touch.location.x as usize; let mouse_y = touch.location.y as usize; - if self.ctx.display().scrollbar.try_start_drag( size_info, mouse_x, mouse_y) { + if self.ctx.display().scrollbar.try_start_drag(size_info, mouse_x, mouse_y) { TouchPurpose::ScrollbarDrag(touch) } else { TouchPurpose::Tap(touch)