Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record origin position of scrollbars to improve grab mechanics #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions lib/nottui/nottui_widgets.ml
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ let scrollbar_click_step = 3 (* Clicking scrolls one third of the screen *)
let scrollbar_wheel_step = 8 (* Wheel event scrolls 1/8th of the screen *)

let hscrollbar visible total offset ~set =
let origin_x = ref 0 in
let update_origin ~x ~y:_ ~w:_ ~h:_ () = origin_x := x in
let prefix = offset * visible / total in
let suffix = (total - offset - visible) * visible / total in
let handle = visible - prefix - suffix in
Expand All @@ -725,7 +727,7 @@ let hscrollbar visible total offset ~set =
else if x > prefix + handle then
(set (offset + max 1 (visible / scrollbar_click_step)); `Handled)
else `Grab (
(fun ~x:x' ~y:_ -> set (offset + (x' - x) * total / visible)),
(fun ~x:x' ~y:_ -> set (offset + (x' - x - !origin_x) * total / visible)),
(fun ~x:_ ~y:_ -> ())
)
| `Scroll dir ->
Expand All @@ -735,13 +737,15 @@ let hscrollbar visible total offset ~set =
| _ -> `Unhandled
in
let (++) = Ui.join_x in
Ui.mouse_area mouse_handler (
render prefix scrollbar_bg ++
render handle scrollbar_fg ++
render suffix scrollbar_bg
)
render prefix scrollbar_bg ++
render handle scrollbar_fg ++
render suffix scrollbar_bg
|> Ui.mouse_area mouse_handler
|> Ui.permanent_sensor update_origin

let vscrollbar visible total offset ~set =
let origin_y = ref 0 in
let update_origin ~x:_ ~y ~w:_ ~h:_ () = origin_y := y in
let prefix = offset * visible / total in
let suffix = (total - offset - visible) * visible / total in
let handle = visible - prefix - suffix in
Expand All @@ -753,7 +757,7 @@ let vscrollbar visible total offset ~set =
else if y > prefix + handle then
(set (offset + max 1 (visible / scrollbar_click_step)); `Handled)
else `Grab (
(fun ~x:_ ~y:y' -> set (offset + (y' - y) * total / visible)),
(fun ~x:_ ~y:y' -> set (offset + (y' - y - !origin_y) * total / visible)),
(fun ~x:_ ~y:_ -> ())
)
| `Scroll dir ->
Expand All @@ -763,11 +767,11 @@ let vscrollbar visible total offset ~set =
| _ -> `Unhandled
in
let (++) = Ui.join_y in
Ui.mouse_area mouse_handler (
render prefix scrollbar_bg ++
render handle scrollbar_fg ++
render suffix scrollbar_bg
)
render prefix scrollbar_bg ++
render handle scrollbar_fg ++
render suffix scrollbar_bg
|> Ui.mouse_area mouse_handler
|> Ui.permanent_sensor update_origin

let scrollbox t =
(* Keep track of scroll state *)
Expand Down