Current section
Files
Jump to
Current section
Files
src/germinal@types.erl
-module(germinal@types).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([get_keycode/1, get_keyevent_kind/1, get_key_modifiers/1, get_keyevent_state/1, none_state/0, keypad_state/0, num_lock_state/0, caps_lock_state/0, or_state/2, contains_state/2, none_modifier/0, shift_modifier/0, control_modifier/0, alt_modifier/0, or_modifiers/2, contains_modifiers/2]).
-export_type([key_event_kind/0, key_code/0, key_event/0, key_modifiers/0, key_event_state/0, event/0]).
-type key_event_kind() :: press | repeat | release.
-type key_code() :: backspace |
enter |
left |
right |
up |
down |
home |
'end' |
page_up |
page_down |
tab |
back_tab |
delete |
insert |
{f, integer()} |
{char, binary()} |
null |
esc |
caps_lock |
scroll_lock |
num_lock |
print_screen |
pause |
menu |
keypad_begin.
-type key_event() :: any().
-type key_modifiers() :: any().
-type key_event_state() :: any().
-type event() :: focus_gained |
focus_lost |
{key, key_event()} |
{paste, binary()} |
{resize, integer(), integer()}.
-spec expect(binary()) -> fun(({ok, FWC} | {error, any()}) -> FWC).
expect(Msg) ->
fun(Res) -> case Res of
{ok, Res@1} ->
Res@1;
{error, _} ->
(erlang:error(#{gleam_error => panic,
message => <<"panic expression evaluated"/utf8>>,
module => <<"germinal/types"/utf8>>,
function => <<"expect"/utf8>>,
line => 68}))(Msg)
end end.
-spec unsafe_coerce(any()) -> any().
unsafe_coerce(Anything) ->
_pipe = Anything,
_pipe@1 = gleam@dynamic:from(_pipe),
gleam@dynamic:unsafe_coerce(_pipe@1).
-spec get_keycode(key_event()) -> key_code().
get_keycode(Event) ->
_pipe = gleam@dict:get(
unsafe_coerce(Event),
erlang:binary_to_atom(<<"code"/utf8>>)
),
(expect(<<"Should have code"/utf8>>))(_pipe).
-spec get_keyevent_kind(key_event()) -> key_event_kind().
get_keyevent_kind(Event) ->
_pipe = gleam@dict:get(
unsafe_coerce(Event),
erlang:binary_to_atom(<<"kind"/utf8>>)
),
(expect(<<"Should have kind"/utf8>>))(_pipe).
-spec get_key_modifiers(key_event()) -> key_modifiers().
get_key_modifiers(Event) ->
_pipe = gleam@dict:get(
unsafe_coerce(Event),
erlang:binary_to_atom(<<"modifiers"/utf8>>)
),
(expect(<<"Should have modifiers"/utf8>>))(_pipe).
-spec get_keyevent_state(key_event()) -> key_event_state().
get_keyevent_state(Event) ->
_pipe = gleam@dict:get(
unsafe_coerce(Event),
erlang:binary_to_atom(<<"state"/utf8>>)
),
(expect(<<"Should have state"/utf8>>))(_pipe).
-spec none_state() -> key_event_state().
none_state() ->
unsafe_coerce(2#0000).
-spec keypad_state() -> key_event_state().
keypad_state() ->
unsafe_coerce(2#0001).
-spec num_lock_state() -> key_event_state().
num_lock_state() ->
unsafe_coerce(2#1000).
-spec caps_lock_state() -> key_event_state().
caps_lock_state() ->
unsafe_coerce(2#1000).
-spec or_state(key_event_state(), key_event_state()) -> key_event_state().
or_state(State1, State2) ->
_pipe = erlang:'bor'(unsafe_coerce(State1), unsafe_coerce(State2)),
unsafe_coerce(_pipe).
-spec contains_state(key_event_state(), key_event_state()) -> boolean().
contains_state(Parent_state, Child_state) ->
erlang:'band'(unsafe_coerce(Parent_state), unsafe_coerce(Child_state)) =:= unsafe_coerce(
Child_state
).
-spec none_modifier() -> key_modifiers().
none_modifier() ->
unsafe_coerce(2#0000).
-spec shift_modifier() -> key_modifiers().
shift_modifier() ->
unsafe_coerce(2#0001).
-spec control_modifier() -> key_modifiers().
control_modifier() ->
unsafe_coerce(2#0010).
-spec alt_modifier() -> key_modifiers().
alt_modifier() ->
unsafe_coerce(2#0100).
-spec or_modifiers(key_modifiers(), key_modifiers()) -> key_modifiers().
or_modifiers(Modifiers1, Modifiers2) ->
_pipe = erlang:'bor'(unsafe_coerce(Modifiers1), unsafe_coerce(Modifiers2)),
unsafe_coerce(_pipe).
-spec contains_modifiers(key_modifiers(), key_modifiers()) -> boolean().
contains_modifiers(Parent_modifiers, Child_modifiers) ->
erlang:'band'(
unsafe_coerce(Parent_modifiers),
unsafe_coerce(Child_modifiers)
)
=:= unsafe_coerce(Child_modifiers).