Skip to content

Commit

Permalink
Scrollable list wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Enoumy committed Dec 12, 2024
1 parent 0a419de commit c5f89b1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/capytui/tui/scrollable_list/src/capytui_scrollable_list.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
open! Core
open! Capytui
open! Bonsai
open Bonsai.Let_syntax

module Action = struct
type 'key t =
| Down
| Up
| Top
| Bottom
| Up_half_page
| Down_half_page
| Scroll_to of 'key
end

type 'key t =
{ view : Node.t
; inject : 'key Action.t -> unit Effect.t
; less_keybindings_handler : Event.t -> unit Effect.t
}

module type S = sig
type t [@@deriving sexp, compare]

include Comparable.S with type t := t
end

let component
(type key cmp)
(module M : S with type t = key and type comparator_witness = cmp)
~dimensions:(_ : Dimensions.t Value.t)
(items : (key, 'data, cmp) Core.Map.t Value.t)
~(render : key Value.t -> 'data Value.t -> Node.t Computation.t)
~compare:(_ : 'key * 'data -> 'key * 'data -> int)
=
let%sub rendered_nodes =
Bonsai.assoc (module M) items ~f:(fun key data -> render key data)
in
let%sub alist =
let%arr items = items in
Core.Map.to_alist items
in
Bonsai.const
{ view = Node.none
; inject = (fun _ -> Effect.Ignore)
; less_keybindings_handler = (fun _ -> Effect.Ignore)
}
;;
34 changes: 34 additions & 0 deletions lib/capytui/tui/scrollable_list/src/capytui_scrollable_list.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
open! Core
open Capytui
open Bonsai

module Action : sig
type 'key t =
| Down
| Up
| Top
| Bottom
| Up_half_page
| Down_half_page
| Scroll_to of 'key
end

type 'key t =
{ view : Node.t
; inject : 'key Action.t -> unit Effect.t
; less_keybindings_handler : Event.t -> unit Effect.t
}

module type S = sig
type t [@@deriving sexp, compare]

include Comparable.S with type t := t
end

val component
: (module S with type t = 'key and type comparator_witness = 'cmp)
-> dimensions:Dimensions.t Value.t
-> ('key, 'data, 'cmp) Core.Map.t Value.t
-> render:('key Value.t -> 'data Value.t -> Node.t Computation.t)
-> compare:('key * 'data -> 'key * 'data -> int)
-> 'key t Computation.t
5 changes: 5 additions & 0 deletions lib/capytui/tui/scrollable_list/src/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name capytui_scrollable_list)
(libraries core bonsai capytui)
(preprocess
(pps ppx_jane bonsai.ppx_bonsai)))

0 comments on commit c5f89b1

Please sign in to comment.