Skip to content

Commit

Permalink
Implement horizontal scrollbar drag, fixes #59
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed May 31, 2024
1 parent fe28042 commit 0ae4866
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,21 @@ where
let y_logical = p.y - self.padding.top;
let mut x = x_logical * scale_factor - editor_offset_x as f32;
let y = y_logical * scale_factor;
if x >= 0.0 && x < buffer_size.0 && y >= 0.0 && y < buffer_size.1 {

// Do this first as the horizontal scrollbar is on top of the buffer
if let Some(scrollbar_h_rect) = state.scrollbar_h_rect.get() {
if scrollbar_h_rect.contains(Point::new(x_logical, y_logical)) {
state.dragging = Some(Dragging::ScrollbarH {
start_x: x,
start_scroll: editor.with_buffer(|buffer| buffer.scroll()),
});
}
}

if matches!(state.dragging, Some(Dragging::ScrollbarH { .. })) {
// The horizontal scrollbar is on top of the buffer,
// so we need to ignore clicks when it is being dragged
} else if x >= 0.0 && x < buffer_size.0 && y >= 0.0 && y < buffer_size.1 {
x += buffer_scroll.horizontal;
let click_kind =
if let Some((click_kind, click_time)) = state.click.take() {
Expand Down Expand Up @@ -1110,7 +1124,23 @@ where
start_x,
start_scroll,
} => {
//TODO: horizontal scrollbar drag
editor.with_buffer_mut(|buffer| {
//TODO: store this in state?
let mut max_line_width = 0.0;
for run in buffer.layout_runs() {
if run.line_w > max_line_width {
max_line_width = run.line_w;
}
}

let buffer_w = buffer.size().0;
let mut scroll = buffer.scroll();
scroll.horizontal = (((x - start_x) / buffer_w)
* max_line_width)
.max(0.0)
.min(max_line_width - buffer_w);
buffer.set_scroll(scroll);
});
}
}
}
Expand Down

0 comments on commit 0ae4866

Please sign in to comment.