Skip to content

Commit

Permalink
Updated the implementation for hscroll area
Browse files Browse the repository at this point in the history
  • Loading branch information
maha-sachin committed Apr 5, 2024
1 parent 4810e00 commit 951efa3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/interactive_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,19 @@ let view (patches : Patch.t list) =
| None -> failwith "zipper_of_list: empty list"
in
let curr_scroll_state = Lwd.var W.default_scroll_state in
let curr_scroll_state_h = Lwd.var W.default_scroll_state in
let change_scroll_state _action state =
let off_screen = state.W.position > state.W.bound in
if off_screen then
Lwd.set curr_scroll_state { state with position = state.W.bound }
else Lwd.set curr_scroll_state state
in
let change_scroll_state_h _action state =
let off_screen = state.W.position > state.W.bound in
if off_screen then
Lwd.set curr_scroll_state_h { state with position = state.W.bound }
else Lwd.set curr_scroll_state_h state
in
let ui =
let$* help_visible = Lwd.get help in
if help_visible then
Expand All @@ -149,9 +156,19 @@ let view (patches : Patch.t list) =
operation_info z_patches;
change_summary z_patches;
current_operation z_patches;
W.vscroll_area
~state:(Lwd.get curr_scroll_state)
~change:change_scroll_state
(* W.vscroll_area
~state:(Lwd.get curr_scroll_state)
~change:change_scroll_state
@@ W.hscroll_area
~state:(Lwd.get curr_scroll_state_h)
~change:change_scroll_state_h
@@ current_hunks z_patches; *)
W.hscroll_area
~state:(Lwd.get curr_scroll_state_h)
~change:change_scroll_state_h
@@ W.vscroll_area
~state:(Lwd.get curr_scroll_state)
~change:change_scroll_state
@@ current_hunks z_patches;
Lwd.pure
@@ Ui.keyboard_area
Expand Down
42 changes: 42 additions & 0 deletions vendor/lwd/lib/nottui/nottui_widgets.ml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,48 @@ let vscroll_area ~state ~change t =
|> Ui.mouse_area (scroll_handler state)
|> Ui.keyboard_area (focus_handler state)
end
let hscroll_area ~state ~change t =
let visible = ref (-1) in
let total = ref (-1) in
let scroll state delta =
let position = state.position + delta in
let position = clampi position ~min:0 ~max:state.bound in
if position <> state.position then
change `Action {state with position};
`Handled
in
let focus_handler state = function
| `Arrow `Left , [] -> scroll state (-scroll_step)
| `Arrow `Right, [] -> scroll state (+scroll_step)
| _ -> `Unhandled
in
(* let scroll_handler state ~x:_ ~y:_ = function
| `Scroll `Up -> scroll state (0)
| `Scroll `Down -> scroll state (0)
| _ -> `Unhandled
in *)
Lwd.map2 t state ~f:begin fun t state ->
t
|> Ui.shift_area state.position 0
|> Ui.resize ~w:0 ~sw:1
|> Ui.size_sensor (fun ~w ~h:_ ->
let tchange =
if !total <> (Ui.layout_spec t).Ui.w
then (total := (Ui.layout_spec t).Ui.w; true)
else false
in
let vchange =
if !visible <> w
then (visible := w; true)
else false
in
if tchange || vchange then
change `Content {state with visible = !visible; total = !total;
bound = maxi 0 (!total - !visible); }
)
(* |> Ui.mouse_area (scroll_handler state) *)
|> Ui.keyboard_area (focus_handler state)
end

let scroll_area ?(offset=0,0) t =
let offset = Lwd.var offset in
Expand Down
4 changes: 4 additions & 0 deletions vendor/lwd/lib/nottui/nottui_widgets.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ val vscroll_area :
state:scroll_state Lwd.t ->
change:([> `Action | `Content ] -> scroll_state -> unit) ->
ui Lwd.t -> ui Lwd.t
val hscroll_area :
state:scroll_state Lwd.t ->
change:([> `Action | `Content ] -> scroll_state -> unit) ->
ui Lwd.t -> ui Lwd.t

val scroll_area :
?offset:int * int -> ui Lwd.t -> ui Lwd.t
Expand Down

0 comments on commit 951efa3

Please sign in to comment.