Skip to content

Commit

Permalink
Fix cursor flickering when button is pressed, add south-west resize c…
Browse files Browse the repository at this point in the history
…ursor, refactoring
  • Loading branch information
hborchardt committed May 6, 2024
1 parent 889cf7f commit 18482fb
Showing 1 changed file with 56 additions and 22 deletions.
78 changes: 56 additions & 22 deletions alacritty/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use winit::event_loop::ActiveEventLoop;
use winit::keyboard::ModifiersState;
#[cfg(target_os = "macos")]
use winit::platform::macos::ActiveEventLoopExtMacOS;
use winit::window::CursorIcon;
use winit::window::{CursorIcon, ResizeDirection};

use alacritty_terminal::event::EventListener;
use alacritty_terminal::grid::{Dimensions, Scroll};
Expand Down Expand Up @@ -448,20 +448,19 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {

let inside_text_area = size_info.contains_point(x, y);
let cell_side = self.cell_side(x);

if !inside_text_area && self.ctx.config().window.decorations == Decorations::None {
// North padding of borderless window allows to move the window.
if y < size_info.padding_y() as usize {
self.ctx.window().set_mouse_cursor(CursorIcon::Move);
return;
}
// South-east corner of padding of borderless window allows to resize the window.
else if y > size_info.height() as usize - size_info.padding_y() as usize
&& x > size_info.width() as usize - size_info.padding_x() as usize
{
self.ctx.window().set_mouse_cursor(CursorIcon::SeResize);
return;
}
let (padding_hover_north, padding_hover_east, padding_hover_south, padding_hover_west) =
self.padding_hoverinfo(self.ctx.mouse().x, self.ctx.mouse().y, &size_info);

let mut padding_interaction = false;
if !inside_text_area
&& !lmb_pressed
&& !rmb_pressed
&& self.ctx.config().window.decorations == Decorations::None
{
// North padding of borderless window allows to move the window, south-east and
// south-west corner allow resize.
padding_interaction = padding_hover_north
|| (padding_hover_south && (padding_hover_east || padding_hover_west));
}

let point = self.ctx.mouse().point(&size_info, display_offset);
Expand All @@ -471,12 +470,14 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {
if !cell_changed
&& self.ctx.mouse().cell_side == cell_side
&& self.ctx.mouse().inside_text_area == inside_text_area
&& self.ctx.mouse().padding_interaction == padding_interaction
{
return;
}

self.ctx.mouse_mut().inside_text_area = inside_text_area;
self.ctx.mouse_mut().cell_side = cell_side;
self.ctx.mouse_mut().padding_interaction = padding_interaction;

// Update mouse state and check for URL change.
let mouse_state = self.cursor_state();
Expand Down Expand Up @@ -643,17 +644,25 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {
let point = self.ctx.mouse().point(&size_info, display_offset);

if self.ctx.config().window.decorations == Decorations::None {
let (
padding_hover_north,
padding_hover_east,
padding_hover_south,
padding_hover_west,
) = self.padding_hoverinfo(self.ctx.mouse().x, self.ctx.mouse().y, &size_info);
// When clicking the north padding, drag window.
if self.ctx.mouse().y < size_info.padding_y() as usize {
if padding_hover_north {
self.ctx.window().drag_window();
return;
}
// When clicking the south-east padding corner, resize window.
if self.ctx.mouse().y > size_info.height() as usize - size_info.padding_y() as usize
&& self.ctx.mouse().x
> size_info.width() as usize - size_info.padding_x() as usize
{
self.ctx.window().drag_resize_window();
if padding_hover_south && padding_hover_east {
self.ctx.window().drag_resize_window(ResizeDirection::SouthEast);
return;
}
// When clicking the south-west padding corner, resize window.
if padding_hover_south && padding_hover_west {
self.ctx.window().drag_resize_window(ResizeDirection::SouthWest);
return;
}
}
Expand Down Expand Up @@ -1085,11 +1094,21 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {
let display_offset = self.ctx.terminal().grid().display_offset();
let point = self.ctx.mouse().point(&self.ctx.size_info(), display_offset);
let hyperlink = self.ctx.terminal().grid()[point].hyperlink();
let (padding_hover_north, padding_hover_east, padding_hover_south, padding_hover_west) =
self.padding_hoverinfo(self.ctx.mouse().x, self.ctx.mouse().y, &self.ctx.size_info());
let button_pressed = self.ctx.mouse().left_button_state == ElementState::Pressed
|| self.ctx.mouse().right_button_state == ElementState::Pressed;

// Function to check if mouse is on top of a hint.
let hint_highlighted = |hint: &HintMatch| hint.should_highlight(point, hyperlink.as_ref());

if let Some(mouse_state) = self.message_bar_cursor_state() {
if !button_pressed && padding_hover_north {
CursorIcon::Move
} else if !button_pressed && padding_hover_south && padding_hover_east {
CursorIcon::SeResize
} else if !button_pressed && padding_hover_south && padding_hover_west {
CursorIcon::SwResize
} else if let Some(mouse_state) = self.message_bar_cursor_state() {
mouse_state
} else if self.ctx.display().highlighted_hint.as_ref().map_or(false, hint_highlighted) {
CursorIcon::Pointer
Expand Down Expand Up @@ -1134,6 +1153,21 @@ impl<T: EventListener, A: ActionContext<T>> Processor<T, A> {
scheduler.unschedule(timer_id);
scheduler.schedule(event, SELECTION_SCROLLING_INTERVAL, true, timer_id);
}

/// Whether the padding is hovered in the north, east, south and west direction, respectively.
fn padding_hoverinfo(
&self,
x: usize,
y: usize,
size_info: &SizeInfo,
) -> (bool, bool, bool, bool) {
let padding_x = size_info.padding_x() as usize;
let padding_y = size_info.padding_y() as usize;
let width = size_info.width() as usize;
let height = size_info.height() as usize;

(y < padding_y, x > width - padding_x, y > height - padding_y, x < padding_x)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 18482fb

Please sign in to comment.