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

feat(minttea): supporting ctrl-key modifiers #45

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
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
Binary file modified examples/altscreen-toggle/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/altscreen-toggle/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ let initial_model = { altscreen = false; quitting = false }

let update event model =
match event with
| Event.KeyDown (Key "q" | Escape) ->
| Event.KeyDown ((Key "q" | Escape), _modifier) ->
({ model with quitting = true }, Command.Quit)
| Event.KeyDown Space ->
| Event.KeyDown (Space, _modifier) ->
let cmd =
if model.altscreen then Command.Exit_alt_screen
else Command.Enter_alt_screen
Expand Down
Binary file modified examples/basic/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions examples/basic/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ let init _model = Command.Noop
let update event model =
match event with
(* if we press `q` or the escape key, we exit *)
| Event.KeyDown (Key "q" | Escape) -> (model, Command.Quit)
| Event.KeyDown ((Key "q" | Escape), _modifier) -> (model, Command.Quit)
(* if we press up or `k`, we move up in the list *)
| Event.KeyDown (Up | Key "k") ->
| Event.KeyDown ((Up | Key "k"), _modifier) ->
let cursor =
if model.cursor = 0 then List.length model.choices - 1
else model.cursor - 1
in
({ model with cursor }, Command.Noop)
(* if we press down or `j`, we move down in the list *)
| Event.KeyDown (Down | Key "j") ->
| Event.KeyDown ((Down | Key "j"), _modifier) ->
let cursor =
if model.cursor = List.length model.choices - 1 then 0
else model.cursor + 1
in
({ model with cursor }, Command.Noop)
(* when we press enter or space we toggle the item in the list
that the cursor points to *)
| Event.KeyDown (Enter | Space) ->
| Event.KeyDown ((Enter | Space), _modifier) ->
let toggle status =
match status with `selected -> `unselected | `unselected -> `selected
in
Expand Down
Binary file modified examples/border/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions examples/border/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ let initial_model = { text = "" }

let update event model =
match event with
| Event.KeyDown (Key "q" | Escape) -> (model, Command.Quit)
| Event.KeyDown (Key k) ->
| Event.KeyDown ((Key "q" | Escape), _) -> (model, Command.Quit)
| Event.KeyDown (Key k, _modifier) ->
let model = { text = model.text ^ k } in
(model, Command.Noop)
| Event.KeyDown Space ->
| Event.KeyDown (Space, _modifier) ->
let model = { text = model.text ^ " " } in
(model, Command.Noop)
| Event.KeyDown Enter ->
| Event.KeyDown (Enter, _modifier) ->
let model = { text = model.text ^ "\n" } in
(model, Command.Noop)
| _ -> (model, Command.Noop)
Expand Down
Binary file modified examples/counter/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/counter/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let initial_model = { counter = 0; keys = [] }

let update event model =
match event with
| Event.KeyDown (Key "q" | Escape) -> (model, Command.Quit)
| Event.KeyDown (Key k) ->
| Event.KeyDown ((Key "q" | Escape), _modifier) -> (model, Command.Quit)
| Event.KeyDown (Key k, _modifier) ->
let model = { counter = model.counter + 1; keys = model.keys @ [ k ] } in
(model, Command.Noop)
| _ -> (model, Command.Noop)
Expand Down
Binary file modified examples/emoji/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions examples/emoji/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,31 @@ let update event m =
[ (m.player_view, m.player_pos); (Sprite.view monkey, m.monkey_pos) ]
in
({ m with screen; monkey }, Command.Noop)
| Event.KeyDown (Down | Key "j") ->
| Event.KeyDown ((Down | Key "j"), _modifier) ->
let player_pos =
let x, y = m.player_pos in
(x, Int.min (Tilemap.h - 1) (y + 1))
in
({ m with player_pos }, Command.Noop)
| Event.KeyDown (Up | Key "k") ->
| Event.KeyDown ((Up | Key "k"), _modifier) ->
let player_pos =
let x, y = m.player_pos in
(x, Int.max 0 (y - 1))
in
({ m with player_pos }, Command.Noop)
| Event.KeyDown (Right | Key "l") ->
| Event.KeyDown ((Right | Key "l"), _modifier) ->
let player_pos =
let x, y = m.player_pos in
(Int.min (Tilemap.w - 1) (x + 1), y)
in
({ m with player_pos }, Command.Noop)
| Event.KeyDown (Left | Key "h") ->
| Event.KeyDown ((Left | Key "h"), _modifier) ->
let player_pos =
let x, y = m.player_pos in
(Int.max 0 (x - 1), y)
in
({ m with player_pos }, Command.Noop)
| Event.KeyDown (Key "q") -> (m, Command.Quit)
| Event.KeyDown (Key "q", _modifier) -> (m, Command.Quit)
| _ -> (m, Command.Noop)

let view m =
Expand Down
Binary file modified examples/fps/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/fps/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ let initial_model = { frames = 0; fps = 30; last_frame = Ptime_clock.now () }

let update event model =
match event with
| Event.KeyDown (Key "q") -> (model, Command.Quit)
| Event.KeyDown (Key "s") ->
| Event.KeyDown (Key "q", _modifier) -> (model, Command.Quit)
| Event.KeyDown (Key "s", _modifier) ->
sleep 0.5;
(model, Command.Noop)
| Event.Frame now ->
Expand Down
Binary file modified examples/fullscreen/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/fullscreen/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let initial_model = 3

let update event model =
match event with
| Event.KeyDown (Key "q" | Escape) -> (model, Command.Quit)
| Event.KeyDown ((Key "q" | Escape), _modifier) -> (model, Command.Quit)
| Event.Timer _ref ->
let model = model - 1 in
if model <= 0 then (model, Command.Quit)
Expand Down
Binary file modified examples/layout/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions examples/layout/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ let init _model = Command.Noop
(* Update function with pattern matching for events *)
let update event model =
match event with
| Event.KeyDown (Key "q" | Escape) -> (model, Command.Quit)
| Event.KeyDown (Up | Key "k") ->
| Event.KeyDown ((Key "q" | Escape), _modifier) -> (model, Command.Quit)
| Event.KeyDown ((Up | Key "k"), _modifier) ->
let cursor =
if model.cursor = 0 then List.length model.choices - 1
else model.cursor - 1
in
({ model with cursor }, Command.Noop)
| Event.KeyDown (Down | Key "j") ->
| Event.KeyDown ((Down | Key "j"), _modifier) ->
let cursor =
if model.cursor = List.length model.choices - 1 then 0
else model.cursor + 1
in
({ model with cursor }, Command.Noop)
| Event.KeyDown (Enter | Space) ->
| Event.KeyDown ((Enter | Space), _modifier) ->
let toggle status =
match status with `selected -> `unselected | `unselected -> `selected
in
Expand Down
Binary file modified examples/list/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions examples/list/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ let update event model : model * Command.t =
if model.edit_filter then
match event with
(* validate the search and go back to navigating the list *)
| Event.KeyDown Enter ->
| Event.KeyDown (Enter, _modifier) ->
let elements =
FList.show_string_contains model.elements
(Input.current_text model.filter_input)
in
({ model with elements; edit_filter = false }, Command.Noop)
(* cancel the search and go back to navigating the list *)
| Event.KeyDown Escape ->
| Event.KeyDown (Escape, _modifier) ->
let elements = FList.show_all model.elements in
( {
model with
Expand All @@ -73,13 +73,13 @@ let update event model : model * Command.t =
else
match event with
(* Validate the selection, print it and quit *)
| Event.KeyDown Enter ->
| Event.KeyDown (Enter, _modifier) ->
let elements = FList.get_selection model.elements in
({ model with choices = Some elements }, Command.Quit)
(* Quit right away *)
| Event.KeyDown (Key "q" | Escape) -> (model, Command.Quit)
| Event.KeyDown ((Key "q" | Escape), _modifier) -> (model, Command.Quit)
(* Open the search Text_input *)
| Event.KeyDown (Key "/") ->
| Event.KeyDown (Key "/", _modifier) ->
({ model with edit_filter = true }, Command.Noop)
(* Delegate the rest to the list *)
| _ ->
Expand Down
Binary file modified examples/paginator/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/paginator/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let init _ = Command.Hide_cursor

let update (event : Event.t) model =
match event with
| Event.KeyDown (Key "q") -> (model, Command.Quit)
| Event.KeyDown (Key "q", _modifier) -> (model, Command.Quit)
| _ ->
( { model with paginator = Paginator.update model.paginator event },
Command.Noop )
Expand Down
Binary file modified examples/progress/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/progress/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let initial_model =

let update event m =
match event with
| Event.KeyDown (Key "q" | Escape) -> (m, Command.Quit)
| Event.KeyDown ((Key "q" | Escape), _modifier) -> (m, Command.Quit)
| Event.Frame _now ->
let gray_bar = Progress.increment m.gray_bar 0.001 in
let color_bar = Progress.increment m.color_bar (Random.float 0.0001) in
Expand Down
Binary file modified examples/spinner/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/spinner/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ let update event model =
| Event.Frame now ->
let spinners = model.spinners |> List.map (Sprite.update ~now) in
({ spinners }, Command.Noop)
| Event.KeyDown (Key "q") -> (model, Command.Quit)
| Event.KeyDown (Key "q", _modifier) -> (model, Command.Quit)
| _ -> (model, Command.Noop)

let view model =
Expand Down
Binary file modified examples/stopwatch/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions examples/stopwatch/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ let initial_model =

let update event model =
match event with
| Event.KeyDown (Key "r") ->
| Event.KeyDown (Key "r", _modifier) ->
let start_time = Ptime_clock.now () in
let measured = 0. in
({ model with start_time; measured }, Command.Set_timer (ref, 0.01))
| Event.KeyDown (Key "s") ->
| Event.KeyDown (Key "s", _modifier) ->
let stopped = not model.stopped in
({ model with stopped }, Command.Set_timer (ref, 0.01))
| Event.KeyDown (Key "q" | Escape) ->
| Event.KeyDown ((Key "q" | Escape), _modifier) ->
({ model with quit = true }, Command.Quit)
| Event.Timer _ref ->
let model =
Expand Down
Binary file modified examples/table/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions examples/table/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ let init _ = Command.Noop

let update event model =
match event with
| Event.KeyDown (Key "q") -> (model, Command.Quit)
| Event.KeyDown (Key "b") ->
| Event.KeyDown (Key "q", _modifier) -> (model, Command.Quit)
| Event.KeyDown (Key "b", _modifier) ->
({ table = Table.update model.table Table.PageUp }, Command.Noop)
| Event.KeyDown (Key "f") | Event.KeyDown Space ->
| Event.KeyDown ((Key "f" | Space), _modifier) ->
({ table = Table.update model.table Table.PageDown }, Command.Noop)
| Event.KeyDown Up ->
| Event.KeyDown (Up, _modifier) ->
({ table = Table.update model.table Table.Up }, Command.Noop)
| Event.KeyDown Down ->
| Event.KeyDown (Down, _modifier) ->
({ table = Table.update model.table Table.Down }, Command.Noop)
| _ -> (model, Command.Noop)

Expand Down
Binary file modified examples/text-input/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/text-input/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let update (event : Event.t) model =
let s =
match event with
| e ->
if e = Event.KeyDown Enter then
if e = Event.KeyDown (Enter, No_modifier) then
({ model with quitting = true }, Command.Quit)
else
let text = Text_input.update model.text e in
Expand Down
Binary file modified examples/views/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions examples/views/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ exception Exit

let update event model =
try
if event = Event.KeyDown (Key "q") then raise Exit
if event = Event.KeyDown (Key "q", No_modifier) then raise Exit
else
let section, cmd =
match model.section with
Expand All @@ -106,10 +106,10 @@ let update event model =
| _ -> (model.section, Command.Noop))
| Selection_screen screen -> (
match event with
| Event.KeyDown Space -> transition model.section
| Event.KeyDown (Key "j" | Down) ->
| Event.KeyDown (Space, _modifier) -> transition model.section
| Event.KeyDown ((Key "j" | Down), _modifier) ->
(Selection_screen (select_next screen), Command.Noop)
| Event.KeyDown (Key "k" | Up) ->
| Event.KeyDown ((Key "k" | Up), _modifier) ->
(Selection_screen (select_prev screen), Command.Noop)
| Event.Timer ref ->
let timeout = screen.timeout - 1 in
Expand Down
10 changes: 5 additions & 5 deletions leaves/filtered_list.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ let next_visible cur shown =

let update event (model : t) =
match event with
| Event.KeyDown (Key "s" | Space) ->
| Event.KeyDown ((Key "s" | Space), _) ->
(* select current element *)
{
model with
Expand All @@ -99,7 +99,7 @@ let update event (model : t) =
if idx = model.cursor then (not s, e) else (s, e))
model.elements;
}
| Event.KeyDown (Up | Key "k") ->
| Event.KeyDown ((Up | Key "k"), _) ->
let len = List.length model.elements in
if len = 0 then model
else
Expand All @@ -110,7 +110,7 @@ let update event (model : t) =
| None -> (model.cursor + len - 1) mod len
| Some shown -> prev_visible model.cursor shown);
}
| Event.KeyDown (Down | Key "j") ->
| Event.KeyDown ((Down | Key "j"), _) ->
let len = List.length model.elements in
if len = 0 then model
else
Expand All @@ -121,10 +121,10 @@ let update event (model : t) =
| None -> (model.cursor + 1) mod len
| Some shown -> next_visible model.cursor shown);
}
| Event.KeyDown (Left | Key "h") ->
| Event.KeyDown ((Left | Key "h"), _) ->
(* previous page, not wrapping *)
{ model with cursor = max (model.cursor - model.max_height) 0 }
| Event.KeyDown (Right | Key "l") ->
| Event.KeyDown ((Right | Key "l"), _) ->
(* next page, not wrapping *)
{
model with
Expand Down
4 changes: 2 additions & 2 deletions leaves/paginator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ let make ?(style = Numerals) ?(page = 0) ?(per_page = 1) ?(total_pages = 1)

let update t (e : Minttea.Event.t) =
match e with
| KeyDown (Key "h" | Left) -> prev_page t
| KeyDown (Key "l" | Right) -> next_page t
| KeyDown ((Key "h" | Left), _) -> prev_page t
| KeyDown ((Key "l" | Right), _) -> next_page t
| _ -> t

let dots_view t text_style =
Expand Down
2 changes: 1 addition & 1 deletion leaves/text_input.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ let jump_to_end t = move_cursor t `Jump_to_end

let update t (e : Minttea.Event.t) =
match e with
| KeyDown k ->
| KeyDown (k, _) ->
{
(match k with
| Backspace -> backspace t
Expand Down
8 changes: 5 additions & 3 deletions minttea/event.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
open Riot

type modifier = No_modifier | Ctrl

type key =
| Up
| Down
Expand All @@ -21,17 +23,17 @@ let key_to_string key =
| Escape -> "<esc>"
| Backspace -> "<backspace>"
| Enter -> "<enter>"
| Key k -> k
| Key key -> key

type t =
| KeyDown of key
| KeyDown of key * modifier
| Timer of unit Ref.t
| Frame of Ptime.t
| Custom of Message.t

let pp fmt t =
match t with
| KeyDown key -> Format.fprintf fmt "KeyDown(%S)" (key_to_string key)
| KeyDown (key, _) -> Format.fprintf fmt "KeyDown(%S)" (key_to_string key)
| Timer ref -> Format.fprintf fmt "Timer(%a)" Ref.pp ref
| Frame delta -> Format.fprintf fmt "Frame(%a)" Ptime.pp delta
| Custom _msg -> Format.fprintf fmt "Custom"
Loading