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

Stop separately tracking focus for Input #859

Merged
merged 9 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
29 changes: 14 additions & 15 deletions src/UI/Focus.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let focused = ref(None);

module Log = (val Log.withNamespace("Revery.UI.Focus"));

let getFocused = () => focused^;

/* Should happen when user clicks anywhere where no focusable node exists */
let loseFocus = () => {
Log.trace("loseFocus()");
Expand All @@ -23,21 +25,18 @@ let loseFocus = () => {
// If there is an active window, with text input active, turn off text input
};

let focus = (node: Node.node) => {
Log.trace("focus()");
node#handleEvent(Focus);
focused := Some({handler: node#handleEvent, id: node#getInternalId()});
};
let isFocused = (node: Node.node) =>
switch (focused^) {
| Some({id, _}) => node#getInternalId() == id
| None => false
};

/* TODO perform checks if a node can be focused ? */
let dispatch = (node: Node.node) =>
switch (focused^) {
| Some({id, _}) =>
if (node#getInternalId() === id) {
();
} else {
loseFocus();
focus(node);
}
| None => focus(node)
let focus = (node: Node.node) =>
if (!isFocused(node)) {
loseFocus();

Log.trace("focus()");
node#handleEvent(Focus);
focused := Some({handler: node#handleEvent, id: node#getInternalId()});
};
15 changes: 15 additions & 0 deletions src/UI/Focus.rei
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
open NodeEvents;

type active = {
handler: event => unit,
id: int,
};
type focused = ref(option(active));

let getFocused: unit => option(active);
ericluap marked this conversation as resolved.
Show resolved Hide resolved

let loseFocus: unit => unit;

let isFocused: Node.node => bool;

let focus: Node.node => unit;
2 changes: 1 addition & 1 deletion src/UI/Keyboard.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Revery_Core;
open NodeEvents;

let dispatch = (event: Revery_Core.Events.internalKeyboardEvent) => {
let focused = Focus.focused^;
let focused = Focus.getFocused();

switch (focused) {
| None => ()
Expand Down
2 changes: 1 addition & 1 deletion src/UI/Mouse.re
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ let dispatch =

if (isMouseDownEv(eventToSend)) {
switch (getFirstFocusable(node, mouseX, mouseY)) {
| Some(node) => Focus.dispatch(node)
| Some(node) => Focus.focus(node)
| None => Focus.loseFocus()
};
};
Expand Down
50 changes: 23 additions & 27 deletions src/UI_Components/Input.re
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,10 @@ module Cursor = {
};

type state = {
isFocused: bool, // TODO: Violates single source of truth
value: string,
cursorPosition: int,
};

type action =
| Focus
| Blur
| TextInput(string, int);

let getStringParts = (index, str) => {
switch (index) {
| 0 => ("", str)
Expand Down Expand Up @@ -106,13 +100,6 @@ let addCharacter = (word, char, index) => {
(startStr ++ char ++ endStr, String.length(startStr) + 1);
};

let reducer = (action, state) =>
switch (action) {
| Focus => {...state, isFocused: true}
| Blur => {...state, isFocused: false}
| TextInput(value, cursorPosition) => {...state, value, cursorPosition}
};

module Constants = {
let defaultHeight = 50;
let defaultWidth = 200;
Expand Down Expand Up @@ -209,15 +196,11 @@ let%component make =
~isPassword=false,
(),
) => {
let%hook (state, dispatch) =
Hooks.reducer(
~initialState={
isFocused: false,
value: Option.value(value, ~default=""),
cursorPosition: Option.value(cursorPosition, ~default=0),
},
reducer,
);
let%hook (state, setState) =
Hooks.state({
value: Option.value(value, ~default=""),
cursorPosition: Option.value(cursorPosition, ~default=0),
});
let%hook textRef = Hooks.ref(None);
let%hook scrollOffset = Hooks.ref(0);

Expand Down Expand Up @@ -248,8 +231,16 @@ let%component make =
dimensions.width |> int_of_float;
};

let%hook clickableRef = Hooks.ref(None);
let isFocused = () => {
switch (clickableRef^) {
| Some(node) => Focus.isFocused(node)
| None => false
};
};

let%hook (cursorOpacity, resetCursor) =
Cursor.use(~interval=Time.ms(500), ~isFocused=state.isFocused);
Cursor.use(~interval=Time.ms(500), ~isFocused=isFocused());

let () = {
let cursorOffset =
Expand All @@ -275,14 +266,12 @@ let%component make =
resetCursor();
onFocus();
Sdl2.TextInput.start();
dispatch(Focus);
};

let handleBlur = () => {
resetCursor();
onBlur();
Sdl2.TextInput.stop();
dispatch(Blur);
};

// TODO:This ought to be in the reducer, but since reducer calls are deferred
Expand All @@ -291,7 +280,7 @@ let%component make =
// Refactor when https://github.com/briskml/brisk-reconciler/issues/54 has been fixed
let update = (value, cursorPosition) => {
onChange(value, cursorPosition);
dispatch(TextInput(value, cursorPosition));
setState(_ => {value, cursorPosition});
};

let handleTextInput = (event: NodeEvents.textInputEventParams) => {
Expand Down Expand Up @@ -350,6 +339,13 @@ let%component make =
};
};

let handleRef = node => {
clickableRef := Some(node);
if (autofocus) {
Focus.focus(node);
};
};

let cursor = () => {
let (startStr, _) = getStringParts(cursorPosition, value);
let textWidth = measureTextWidth(startStr);
Expand Down Expand Up @@ -392,7 +388,7 @@ let%component make =
<Clickable
onFocus=handleFocus
onBlur=handleBlur
componentRef={autofocus ? Focus.focus : ignore}
componentRef=handleRef
onAnyClick=handleClick
onKeyDown=handleKeyDown
onTextInput=handleTextInput>
Expand Down