Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kayak UI
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Bevy Forks
Kayak UI
Commits
ec6e2be2
Commit
ec6e2be2
authored
3 years ago
by
StarArawn
Browse files
Options
Downloads
Patches
Plain Diff
Showoff how we use the use_state! macro.
parent
9ab0bdd4
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+4
-9
4 additions, 9 deletions
README.md
examples/counter.rs
+4
-8
4 additions, 8 deletions
examples/counter.rs
kayak_core/src/lib.rs
+1
-1
1 addition, 1 deletion
kayak_core/src/lib.rs
with
9 additions
and
18 deletions
README.md
+
4
−
9
View file @
ec6e2be2
...
...
@@ -88,18 +88,13 @@ Widget's can create their own state and will re-render when that state changes.
```
rust
#[widget]
fn
Counter
(
context
:
&
mut
KayakContext
)
{
let
count
=
context
.create_state
(
0i32
)
.unwrap
();
// Since we move the variable into the closure we need to clone here.
// Similar cost to cloning an Arc
let
cloned_count
=
count
.clone
();
let
on_event
=
OnEvent
::
new
(
move
|
context
,
event
|
match
event
.event_type
{
EventType
::
Click
=>
{
cloned_count
.set
(
cloned_count
.get
()
+
1
);
}
let
(
count
,
set_count
)
=
use_state!
(
0i32
);
let
on_event
=
OnEvent
::
new
(
move
|
_
,
event
|
match
event
.event_type
{
EventType
::
Click
=>
set_count
(
count
+
1
),
_
=>
{}
});
let
count_text
=
format!
(
"Current Count: {}"
,
count
.get
()
);
let
count_text
=
format!
(
"Current Count: {}"
,
count
);
rsx!
{
<>
<
Window
position
=
{(
50.0
,
50.0
)}
size
=
{(
300.0
,
300.0
)}
title
=
{
"Counter Example"
.to_string
()}
>
...
...
This diff is collapsed.
Click to expand it.
examples/counter.rs
+
4
−
8
View file @
ec6e2be2
...
...
@@ -7,7 +7,7 @@ use kayak_ui::bevy::{BevyContext, BevyKayakUIPlugin, FontMapping, UICameraBundle
use
kayak_ui
::
core
::{
render
,
rsx
,
styles
::{
Style
,
StyleProp
,
Units
},
widget
,
Bound
,
EventType
,
Index
,
MutableBound
,
OnEvent
,
use_state
,
widget
,
Bound
,
EventType
,
Index
,
MutableBound
,
OnEvent
,
};
use
kayak_widgets
::{
App
,
Button
,
Text
,
Window
};
...
...
@@ -33,20 +33,16 @@ fn Counter(context: &mut KayakContext) {
..
Default
::
default
()
};
let
count
=
context
.create_state
(
0i32
)
.unwrap
();
let
cloned_count
=
count
.clone
();
let
(
count
,
set_count
)
=
use_state!
(
0i32
);
let
on_event
=
OnEvent
::
new
(
move
|
_
,
event
|
match
event
.event_type
{
EventType
::
Click
=>
{
cloned_count
.set
(
cloned_count
.get
()
+
1
);
}
EventType
::
Click
=>
set_count
(
count
+
1
),
_
=>
{}
});
let
count_value
=
count
.get
();
rsx!
{
<>
<
Window
position
=
{(
50.0
,
50.0
)}
size
=
{(
300.0
,
300.0
)}
title
=
{
"Counter Example"
.to_string
()}
>
<
Text
styles
=
{
Some
(
text_styles
)}
size
=
{
32.0
}
content
=
{
format!
(
"Current Count: {}"
,
count
_value
)
.to_string
()}
>
{}
</
Text
>
<
Text
styles
=
{
Some
(
text_styles
)}
size
=
{
32.0
}
content
=
{
format!
(
"Current Count: {}"
,
count
)
.to_string
()}
>
{}
</
Text
>
<
Button
on_event
=
{
Some
(
on_event
)}
>
<
Text
styles
=
{
Some
(
button_text_styles
)}
size
=
{
24.0
}
content
=
{
"Count!"
.to_string
()}
>
{}
</
Text
>
</
Button
>
...
...
This diff is collapsed.
Click to expand it.
kayak_core/src/lib.rs
+
1
−
1
View file @
ec6e2be2
...
...
@@ -26,7 +26,7 @@ pub use event::*;
pub
use
fragment
::
Fragment
;
pub
use
generational_arena
::{
Arena
,
Index
};
pub
use
input_event
::
*
;
pub
use
kayak_render_macros
::{
constructor
,
render
,
rsx
,
widget
};
pub
use
kayak_render_macros
::{
constructor
,
render
,
rsx
,
use_state
,
widget
};
pub
use
keys
::
KeyCode
;
pub
use
resources
::
Resources
;
pub
use
tree
::{
Tree
,
WidgetTree
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment