Skip to content

Commit

Permalink
API: Add internal Container API for testing (#210)
Browse files Browse the repository at this point in the history
* Bring back container API for testing

* Formatting

* Formatting
bryphe authored Jan 18, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent cbbf0c0 commit eea924d
Showing 4 changed files with 57 additions and 38 deletions.
10 changes: 9 additions & 1 deletion src/UI/Revery_UI.re
Original file line number Diff line number Diff line change
@@ -44,7 +44,15 @@ let start =

let rootNode = (new viewNode)();
let mouseCursor: Mouse.Cursor.t = Mouse.Cursor.make();
let ui = UiContainer.create(window, rootNode, mouseCursor, createOptions);
let container = React.Container.createContainer(rootNode);
let ui =
UiContainer.create(
window,
rootNode,
container,
mouseCursor,
createOptions,
);

let _ =
Revery_Core.Event.subscribe(
6 changes: 3 additions & 3 deletions src/UI/UiContainer.re
Original file line number Diff line number Diff line change
@@ -23,16 +23,16 @@ module Options = {

type t = {
rootNode: ViewNode.viewNode,
/* container: UiReact.t, */
container: ref(UiReact.Container.t),
window: Window.t,
mouseCursor: Mouse.Cursor.t,
options: Options.t,
};

let create = (window, rootNode, mouseCursor, options) => {
let create = (window, rootNode, container, mouseCursor, options) => {
window,
rootNode,
/* container, */
container: ref(container),
mouseCursor,
options,
};
43 changes: 43 additions & 0 deletions src/UI/UiReact.re
Original file line number Diff line number Diff line change
@@ -3,4 +3,47 @@ module Reconciler = UiReconciler.Reconciler;
let onStale = Reconciler.onStale;

module React = Brisk_reconciler.Make(Reconciler);

module Container = {
type state = {
rendered: React.RenderedElement.t,
previousElement: React.syntheticElement,
};

type t = {
node: UiReconciler.reveryNode,
state: option(state),
};

let createContainer: UiReconciler.reveryNode => t =
n => {node: n, state: None};

let updateContainer: (t, React.syntheticElement) => t =
({node, state}, element) => {
let newRendered =
switch (state) {
| None =>
let updates = React.RenderedElement.render(node, element);
React.RenderedElement.executeHostViewUpdates(updates) |> ignore;
updates |> React.RenderedElement.executePendingEffects;
| Some(s) =>
let nextElement =
React.RenderedElement.update(
~previousElement=s.previousElement,
~renderedElement=s.rendered,
element,
)
|> React.RenderedElement.flushPendingUpdates;
React.RenderedElement.executeHostViewUpdates(nextElement) |> ignore;
nextElement |> React.RenderedElement.executePendingEffects;
};

let ret: t = {
node,
state: Some({rendered: newRendered, previousElement: element}),
};
ret;
};
};

include React;
36 changes: 2 additions & 34 deletions src/UI/UiRender.re
Original file line number Diff line number Diff line change
@@ -18,44 +18,13 @@ open RenderPass;

let _projection = Mat4.create();

let lastUpdate: ref(option(UiReact.RenderedElement.t)) = ref(None);
let previousElement: ref(option(UiReact.syntheticElement)) = ref(None);

let render = (container: UiContainer.t, component: UiReact.syntheticElement) => {
let {rootNode, window, options, _} = container;
let {rootNode, window, container, options, _} = container;

AnimationTicker.tick();

/* Perform reconciliation */
/* TODO:
* - Refactor this to something like a 'Container' to better manage the state
* - Perf: Better logic to determine if we should just update or flush pending updates - both are not always needed.
*/
let latest =
switch (lastUpdate^, previousElement^) {
| (None, None) =>
let updates = UiReact.RenderedElement.render(rootNode, component);
UiReact.RenderedElement.executeHostViewUpdates(updates) |> ignore;
let updates = UiReact.RenderedElement.executePendingEffects(updates);
Some(updates);
| (Some(v), Some(previousElement)) =>
let nextElement =
UiReact.RenderedElement.update(
~previousElement,
~renderedElement=v,
component,
)
|> UiReact.RenderedElement.flushPendingUpdates;

UiReact.RenderedElement.executeHostViewUpdates(nextElement) |> ignore;

let ret = nextElement |> UiReact.RenderedElement.executePendingEffects;
Some(ret);
| _ => None
};

previousElement := Some(component);
lastUpdate := latest;
container := UiReact.Container.updateContainer(container^, component);

/* Layout */
let size = Window.getSize(window);
@@ -84,7 +53,6 @@ let render = (container: UiContainer.t, component: UiReact.syntheticElement) =>
};

/* Render */

Mat4.ortho(
_projection,
0.0,

0 comments on commit eea924d

Please sign in to comment.