-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Component v2 #1646
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
Closed
Closed
Component v2 #1646
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c7a2114
Components v2
jstarry 6c22fcf
Remove most of the stdweb examples
jstarry 7c11af5
Fix tests
jstarry c3acb2a
More fixes
jstarry 68cc68c
fmt
jstarry 4c72967
Update yew-functional/src/use_context_hook.rs
jstarry 53fe13d
Update yew-functional/src/use_context_hook.rs
jstarry 14ee6e6
Update docs/advanced-topics/optimizations.md
jstarry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,10 @@ | ||
| use settings::Settings; | ||
| use simulation::Simulation; | ||
| use slider::Slider; | ||
| use yew::{html, Component, ComponentLink, Html, ShouldRender}; | ||
| use slider::Slider as LegacySlider; | ||
| use yew::component::{Component, Context}; | ||
| use yew::{html, Html, Legacy, ShouldRender}; | ||
|
|
||
| type Slider = Legacy<LegacySlider>; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't migrate this |
||
|
|
||
| mod boid; | ||
| mod math; | ||
|
|
@@ -17,25 +20,24 @@ pub enum Msg { | |
| } | ||
|
|
||
| pub struct Model { | ||
| link: ComponentLink<Self>, | ||
| settings: Settings, | ||
| generation: usize, | ||
| paused: bool, | ||
| } | ||
|
|
||
| impl Component for Model { | ||
| type Message = Msg; | ||
| type Properties = (); | ||
|
|
||
| fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { | ||
| fn create(_ctx: &Context<Self>) -> Self { | ||
| Self { | ||
| link, | ||
| settings: Settings::load(), | ||
| generation: 0, | ||
| paused: false, | ||
| } | ||
| } | ||
|
|
||
| fn update(&mut self, msg: Self::Message) -> ShouldRender { | ||
| fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> ShouldRender { | ||
| match msg { | ||
| Msg::ChangeSettings(settings) => { | ||
| self.settings = settings; | ||
|
|
@@ -58,108 +60,101 @@ impl Component for Model { | |
| } | ||
| } | ||
|
|
||
| fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
| false | ||
| } | ||
|
|
||
| fn view(&self) -> Html { | ||
| fn view(&self, ctx: &Context<Self>) -> Html { | ||
| let Self { | ||
| ref settings, | ||
| generation, | ||
| paused, | ||
| .. | ||
| } = *self; | ||
| } = self; | ||
|
|
||
| html! { | ||
| <> | ||
| <h1 class="title">{ "Boids" }</h1> | ||
| <Simulation settings=settings.clone() generation=generation paused=paused /> | ||
| { self.view_panel() } | ||
| { self.view_panel(ctx) } | ||
| </> | ||
| } | ||
| } | ||
| } | ||
| impl Model { | ||
| fn view_panel(&self) -> Html { | ||
| let link = &self.link; | ||
| fn view_panel(&self, ctx: &Context<Self>) -> Html { | ||
| let pause_text = if self.paused { "Resume" } else { "Pause" }; | ||
| html! { | ||
| <div class="panel"> | ||
| { self.view_settings() } | ||
| { self.view_settings(ctx) } | ||
| <div class="panel__buttons"> | ||
| <button onclick=link.callback(|_| Msg::TogglePause)>{ pause_text }</button> | ||
| <button onclick=link.callback(|_| Msg::ResetSettings)>{ "Use Defaults" }</button> | ||
| <button onclick=link.callback(|_| Msg::RestartSimulation)>{ "Restart" }</button> | ||
| <button onclick=ctx.callback(|_| Msg::TogglePause)>{ pause_text }</button> | ||
| <button onclick=ctx.callback(|_| Msg::ResetSettings)>{ "Use Defaults" }</button> | ||
| <button onclick=ctx.callback(|_| Msg::RestartSimulation)>{ "Restart" }</button> | ||
| </div> | ||
| </div> | ||
| } | ||
| } | ||
|
|
||
| fn view_settings(&self) -> Html { | ||
| let Self { link, settings, .. } = self; | ||
|
|
||
| fn view_settings(&self, ctx: &Context<Self>) -> Html { | ||
| // This helper macro creates a callback which applies the new value to the current settings and sends `Msg::ChangeSettings`. | ||
| // Thanks to this, we don't need to have "ChangeBoids", "ChangeCohesion", etc. messages, | ||
| // but it comes at the cost of cloning the `Settings` struct each time. | ||
| macro_rules! settings_callback { | ||
| ($link:expr, $settings:ident; $key:ident as $ty:ty) => {{ | ||
| let settings = $settings.clone(); | ||
| $link.callback(move |value| { | ||
| ($key:ident as $ty:ty) => {{ | ||
| let settings = self.settings.clone(); | ||
| ctx.callback(move |value| { | ||
| let mut settings = settings.clone(); | ||
| settings.$key = value as $ty; | ||
| Msg::ChangeSettings(settings) | ||
| }) | ||
| }}; | ||
| ($link:expr, $settings:ident; $key:ident) => { | ||
| settings_callback!($link, $settings; $key as f64) | ||
| } | ||
| ($key:ident) => { | ||
| settings_callback!($key as f64) | ||
| }; | ||
| } | ||
|
|
||
| let settings = &self.settings; | ||
| html! { | ||
| <div class="settings"> | ||
| <Slider label="Number of Boids" | ||
| min=1.0 max=600.0 | ||
| onchange=settings_callback!(link, settings; boids as usize) | ||
| value=settings.boids as f64 | ||
| onchange=settings_callback!(amount_of_boids as usize) | ||
| value=settings.amount_of_boids as f64 | ||
| /> | ||
| <Slider label="View Distance" | ||
| max=500.0 step=10.0 | ||
| onchange=settings_callback!(link, settings; visible_range) | ||
| onchange=settings_callback!(visible_range) | ||
| value=settings.visible_range | ||
| /> | ||
| <Slider label="Spacing" | ||
| max=100.0 | ||
| onchange=settings_callback!(link, settings; min_distance) | ||
| onchange=settings_callback!(min_distance) | ||
| value=settings.min_distance | ||
| /> | ||
| <Slider label="Max Speed" | ||
| max=50.0 | ||
| onchange=settings_callback!(link, settings; max_speed) | ||
| onchange=settings_callback!(max_speed) | ||
| value=settings.max_speed | ||
| /> | ||
| <Slider label="Cohesion" | ||
| max=0.5 percentage=true | ||
| onchange=settings_callback!(link, settings; cohesion_factor) | ||
| onchange=settings_callback!(cohesion_factor) | ||
| value=settings.cohesion_factor | ||
| /> | ||
| <Slider label="Separation" | ||
| max=1.0 percentage=true | ||
| onchange=settings_callback!(link, settings; separation_factor) | ||
| onchange=settings_callback!(separation_factor) | ||
| value=settings.separation_factor | ||
| /> | ||
| <Slider label="Alignment" | ||
| max=0.5 percentage=true | ||
| onchange=settings_callback!(link, settings; alignment_factor) | ||
| onchange=settings_callback!(alignment_factor) | ||
| value=settings.alignment_factor | ||
| /> | ||
| <Slider label="Turn Speed" | ||
| max=1.5 percentage=true | ||
| onchange=settings_callback!(link, settings; turn_speed_ratio) | ||
| onchange=settings_callback!(turn_speed_ratio) | ||
| value=settings.turn_speed_ratio | ||
| /> | ||
| <Slider label="Color Adaption" | ||
| max=1.5 percentage=true | ||
| onchange=settings_callback!(link, settings; color_adapt_factor) | ||
| onchange=settings_callback!(color_adapt_factor) | ||
| value=settings.color_adapt_factor | ||
| /> | ||
| </div> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.