Skip to content

Commit

Permalink
Format scrollbar code
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-freezing-lava committed Sep 30, 2023
1 parent e58e2c5 commit 654cbb4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
11 changes: 8 additions & 3 deletions alacritty/src/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
43 changes: 33 additions & 10 deletions alacritty/src/display/scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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<Scroll> {
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
Expand All @@ -192,4 +215,4 @@ impl Scrollbar {
struct DragState {
cells_per_dragged_pixel: f32,
accumulated_cells: f32,
}
}
9 changes: 6 additions & 3 deletions alacritty/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {

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);
}
}
Expand Down Expand Up @@ -673,7 +674,9 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {
}

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();
Expand Down Expand Up @@ -845,7 +848,7 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {
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)
Expand Down

0 comments on commit 654cbb4

Please sign in to comment.