Skip to content

Commit

Permalink
Add interface for Focus
Browse files Browse the repository at this point in the history
  • Loading branch information
ericluap committed May 25, 2020
1 parent cc6f52c commit d61321d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
13 changes: 6 additions & 7 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,12 +25,6 @@ let loseFocus = () => {
// If there is an active window, with text input active, turn off text input
};

let focusWithoutBlur = (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
Expand All @@ -39,5 +35,8 @@ let isFocused = (node: Node.node) =>
let focus = (node: Node.node) =>
if (!isFocused(node)) {
loseFocus();
focusWithoutBlur(node);

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);

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

0 comments on commit d61321d

Please sign in to comment.