-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Add Calculator demo #174
Add Calculator demo #174
Conversation
Relative spacing is currently really weird to do with the |
@OhadRau - this is awesome work! It's great to finally see an interactive demo come together. Testing on Windows: Like, it feels like you could build a usable app with this now! So cool!
I think the way you used state here is perfectly appropriate (and highlights the usage of hooks within
Good point. This was an oversight by me, we should have a Sorry about the CI build issues - fixing this in #178 Awesome example and thanks again! 👍 |
For this, we could bring in font-awesome, and render the backspace icon: https://fontawesome.com/icons/backspace?style=solid I had some prototype code for this that I just ported over to the Oni2 repo: onivim/oni2@2260c99 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good to me! I'm happy to tackle the remaining bullet points in separate PRs - whatever you'd prefer, @OhadRau .
(Also, if you bring in latest |
Awesome, I believe that @bmathews was working on exactly that in his |
That is what I was going for, yep, but I'm not a reason expert so there might be a better way to do it: https://github.com/revery-ui/revery/compare/master...bmathews:clickable-styles?expand=1 |
Hey @OhadRau @bryphe I was looking at this calculator example (which is awesome!! 🚀 ) and wanted to explore how I created a PR from this branch in https://github.com/OhadRau/revery/pull/1/files but this is the gist of the different types: type state = {
operator, /* Current operator being applied */
result: float, /* The actual numerical result */
display: string, /* The equation displayed */
number: string /* Current number being typed */
};
type action =
| BackspaceKeyPressed
| ClearKeyPressed(bool) /*AC pressed */
| DotKeyPressed
| NumberKeyPressed(string)
| OperationKeyPressed(operator)
| PlusMinusKeyPressed
| ResultKeyPressed; then the reducer (i'm skipping the let reducer = (state, action) =>
switch (action) {
| BackspaceKeyPressed =>
state.number == "" ?
state :
{
...state,
number: String.sub(state.number, 0, String.length(state.number) - 1),
}
| ClearKeyPressed(ac) =>
ac ?
{...state, operator: `Nop, result: 0., display: ""} :
{...state, number: ""}
| DotKeyPressed =>
String.contains(state.number, '.') ?
state : {...state, number: state.number ++ "."}
| NumberKeyPressed(n) => {...state, number: state.number ++ n}
| OperationKeyPressed(o) =>
let (result, display) = eval(state, o);
{operator: o, result, display, number: ""};
| PlusMinusKeyPressed =>
if (state.number != "" && state.number.[0] == '-') {
{
...state,
number: String.sub(state.number, 1, String.length(state.number) - 1),
};
} else {
{...state, number: "-" ++ state.number};
}
| ResultKeyPressed =>
let (result, display) = eval(state, `Nop);
{operator: `Nop, result, display, number: showFloat(result)};
}; and finally the consumption in the component: let ({display, number, _}, dispatch) =
useReducer(
reducer,
{operator: `Nop, result: 0., display: "", number: ""},
); I think either |
Yeah that actually looks good to me @bmathews. In terms of functions to extend style that's probably the cleanest we can get it. The only alternative I can really think of would be to define styles without the Style.make function and to use the {...spread} syntax for records, but that's up to the user to do on their own. |
@jchavarri Awesome! Didn't realize we have a useReducer function but I think that'll actually help me clean up the current code a lot. Since that's not in any examples already this could be a good place to add it. |
|
||
| _ => () | ||
}; | ||
/* TODO: Pretty sure this isn't supposed to go in the render() function. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good intuition, I think it is subscribing to the event on each re-render!
You could try this:
useEffect(() => {
let unsubscribe = Event.subscribe(window.onKeyDown, respondToKeys);
unsubscribe
});
On each re-render, it will unsubscribe from the previous event subscription.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jchavarri - I think it'd be reasonable (and better for perf)! Not sure if we'd hit that long-lived setState
issue though in revery-ui/reason-reactify#44 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bryphe Is useEffect
exposed right now? I don't see it in Hooks
and doesn't look like there's access to UiReact
anywhere. Want me to add this to the PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry - it wasn't exposed! Thanks for adding it @OhadRau 👍
One other cool thing - with revery-ui/reason-fontkit#13, and aside from a minor glyph bug (revery-ui/reason-fontkit#14 - not related to this PR)... this calculator actually works and is usable in the browser too as a WebGL app! 😍 |
@bmathews Actually, one thing I just thought of for clickable-styles is that it probably makes the most sense to extend a transform by concatenating two lists. For example, if |
Just checked the build - looks like it hit this error:
I fixed it inline - 🤞 it works! |
Thanks for the fix! Looks like the CI isn't working again, any chance you can restart it or merge this in without? @bryphe |
Ah looks like there is still one more build failure:
I pushed up a commit with the fix, you might just need to cherry-pick it ( EDIT: Realized I could just update this line too - done - 🤞 the build is really green this time. |
Merging now! Thanks for all the help |
Still a few things left to do, but this is a mostly working calculator.
Stuff (maybe) left to go:
I'm also not sure how great my use of state was here, but I guess since we don't do state the same way as ReasonReact (i.e. state+action) this would be pretty typical of a Revery app. Regardless, would like any suggestions for how to make this easier to understand or improve the code in general.