Skip to content

Commit

Permalink
fix: trackpad scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
f0e committed Dec 1, 2024
1 parent d6635d0 commit 718ad10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/gui/ui/keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool keys::process_event(const os::Event& event) {

case os::Event::MouseWheel: {
if (event.preciseWheel()) // trackpad
scroll_delta = event.wheelDelta().y;
scroll_delta_precise = event.wheelDelta().y;
else // mouse
scroll_delta = event.wheelDelta().y * 2000.f;

Expand Down
1 change: 1 addition & 0 deletions src/gui/ui/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace keys {
inline std::vector<KeyPress> pressed_keys;

inline float scroll_delta = 0.f;
inline float scroll_delta_precise = 0.f;

bool process_event(const os::Event& event);
void on_input_end();
Expand Down
12 changes: 11 additions & 1 deletion src/gui/ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,23 @@ bool ui::update_container_input(Container& container) {
updated |= (*element.element->update_fn)(container, element);
}

if (keys::scroll_delta != 0.f) {
if (keys::scroll_delta != 0.f || keys::scroll_delta_precise != 0.f) {
if (container.rect.contains(keys::mouse_pos)) {
if (can_scroll(container)) {
float last_scroll_speed_y = container.scroll_speed_y;

container.scroll_speed_y += keys::scroll_delta;
keys::scroll_delta = 0.f;

if (keys::scroll_delta_precise != 0.f) {
container.scroll_y += keys::scroll_delta_precise;
keys::scroll_delta_precise = 0.f;

// immediately clamp to edges todo: overscroll with trackpad?
int max_scroll = get_max_scroll(container);
container.scroll_y = std::clamp(container.scroll_y, 0.f, (float)max_scroll);
}

updated = container.scroll_speed_y != last_scroll_speed_y;
}
}
Expand All @@ -278,6 +287,7 @@ bool ui::update_container_input(Container& container) {

void ui::on_update_end() {
keys::scroll_delta = 0.f;
keys::scroll_delta_precise = 0.f;
}

bool ui::update_container_frame(Container& container, float delta_time) {
Expand Down

0 comments on commit 718ad10

Please sign in to comment.