Skip to content

Commit

Permalink
feat: multiple examples in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Mar 27, 2024
1 parent f911cc5 commit 90fba29
Show file tree
Hide file tree
Showing 17 changed files with 590 additions and 500 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ tracing-tree.workspace = true
tracing-subscriber = { version = "0.3", features = [
"env-filter",
] }
violet-demo = { path = "violet-demo" }


[profile.dev.package.image]
Expand Down
62 changes: 2 additions & 60 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
use futures::StreamExt;
use futures_signals::signal::Mutable;
use glam::Vec2;
use palette::Srgba;
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, registry, util::SubscriberInitExt, EnvFilter,
};
use tracing_tree::HierarchicalLayer;
use violet::core::{
state::{State, StateStream},
style::{danger_background, Background, SizeExt},
unit::Unit,
widget::{
card, col, label, pill, row, Rectangle, SliderWithLabel, StreamWidget, Text, TextInput,
},
Widget,
};
use violet_wgpu::renderer::RendererConfig;

pub fn main() -> anyhow::Result<()> {
Expand All @@ -29,51 +16,6 @@ pub fn main() -> anyhow::Result<()> {
.init();

violet_wgpu::AppBuilder::new()
.with_renderer_config(RendererConfig { debug_mode: true })
.run(app())
}

fn app() -> impl Widget {
let name = Mutable::new("".to_string());
let quest = Mutable::new("".to_string());
let color = Mutable::new(Srgba::new(0.0, 0.61, 0.388, 1.0));

// Map a `Mutable<Srgba>` into a `StateDuplex<f32>` for each field
let r = color.clone().map_ref(|v| &v.red, |v| &mut v.red);
let g = color.clone().map_ref(|v| &v.green, |v| &mut v.green);
let b = color.clone().map_ref(|v| &v.blue, |v| &mut v.blue);

let speed = Mutable::new(None as Option<f32>);

col((
card(row((label("What is your name?"), TextInput::new(name)))),
card(row((label("What is your quest?"), TextInput::new(quest)))),
card(col((
label("What is your favorite colour?"),
SliderWithLabel::new(r, 0.0, 1.0).round(0.01),
SliderWithLabel::new(g, 0.0, 1.0).round(0.01),
SliderWithLabel::new(b, 0.0, 1.0).round(0.01),
StreamWidget(color.stream().map(|v| {
Rectangle::new(v)
.with_maximize(Vec2::X)
.with_min_size(Unit::px2(100.0, 100.0))
})),
))),
card(row((
label("What is the airspeed velocity of an unladen swallow?"),
// Fallibly parse and fill in the None at the same time using the `State` trait
// combinators
TextInput::new(speed.clone().prevent_feedback().filter_map(
|v| v.map(|v| v.to_string()),
|v| Some(v.parse::<f32>().ok()),
)),
StreamWidget(speed.stream().map(|v| {
match v {
Some(v) => pill(Text::new(format!("{v} m/s"))),
None => pill(Text::new("×".to_string()))
.with_background(Background::new(danger_background())),
}
})),
))),
))
.with_renderer_config(RendererConfig { debug_mode: false })
.run(violet_demo::basic::app())
}
8 changes: 3 additions & 5 deletions examples/scroll.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use futures::StreamExt;
use futures_signals::signal::Mutable;
use glam::{vec2, BVec2, Vec2};
use glam::{BVec2, Vec2};
use itertools::Itertools;
use palette::{FromColor, Hsl, Hsv, IntoColor, Oklcha, Srgba};
use palette::{Hsl, Hsv, IntoColor, Oklcha, Srgba};
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, registry, util::SubscriberInitExt, EnvFilter,
};
use tracing_tree::HierarchicalLayer;
use violet::core::{
style::{spacing_small, SizeExt},
style::SizeExt,
unit::Unit,
widget::{col, Rectangle},
Widget,
Expand Down Expand Up @@ -42,7 +42,6 @@ pub fn main() -> anyhow::Result<()> {
enum ColorSpace {
Oklcha,
Hsv,
Hsl,
}

fn app() -> impl Widget {
Expand Down Expand Up @@ -74,7 +73,6 @@ fn app() -> impl Widget {
let color: Srgba = match color_space {
ColorSpace::Oklcha => Oklcha::new(0.5, 0.37, hue, 1.0).into_color(),
ColorSpace::Hsv => Hsv::new(hue, 1.0, 1.0).into_color(),
ColorSpace::Hsl => Hsl::new(hue, 1.0, 0.5).into_color(),
};

Rectangle::new(color)
Expand Down
10 changes: 7 additions & 3 deletions recipes.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@
]
},
"build web": {
"cmd": "wasm-pack build --target web --dev",
"cmd": "wasm-pack build --target web --dev --out-dir ./public/pkg",
"cwd": "./violet-demo/"
},
"build web profile": {
"cmd": "wasm-pack build --target web --profiling",
"cmd": "wasm-pack build --target web --profiling --out-dir ./public/pkg",
"cwd": "./violet-demo/"
},
"build web release": {
"cmd": "wasm-pack build --target web --out-dir ./public/pkg",
"cwd": "./violet-demo/"
},
"host": {
"cmd": "python3 -m http.server 8080",
"cwd": "./violet-demo/"
"cwd": "./violet-demo/public"
}
}
12 changes: 6 additions & 6 deletions violet-core/src/layout/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ impl FloatLayout {
pub(crate) fn apply(
&self,
world: &World,
entity: &EntityRef,
_: &EntityRef,
children: &[Entity],
args: LayoutArgs,
preferred_size: Vec2,
offset: Vec2,
_: Vec2,
_: Vec2,
) -> Block {
puffin::profile_function!();
let _span = tracing::debug_span!("FloatLayout::apply").entered();

let blocks = children.iter().for_each(|&child| {
children.iter().for_each(|&child| {
let entity = world.entity(child).expect("invalid child");

// let pos = resolve_pos(&entity, content_area, preferred_size);
Expand Down Expand Up @@ -62,10 +62,10 @@ impl FloatLayout {
world: &World,
children: &[Entity],
args: QueryArgs,
preferred_size: Vec2,
_: Vec2,
) -> Sizing {
puffin::profile_function!();
let min_rect = Rect::from_size(args.limits.min_size);
// let min_rect = Rect::from_size(args.limits.min_size);

let mut hints = SizingHints::default();

Expand Down
4 changes: 2 additions & 2 deletions violet-core/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
components::{children, handles},
effect::Effect,
input::InputEventHandler,
stored::{Handle, UntypedHandle, WeakHandle},
stored::{UntypedHandle, WeakHandle},
style::get_stylesheet_from_entity,
systems::widget_template,
Frame, FutureEffect, StreamEffect, Widget,
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<'a> ScopeRef<'a> {

/// Returns the active stylesheet for this scope
pub fn stylesheet(&self) -> EntityRef {
get_stylesheet_from_entity(&self.entity())
get_stylesheet_from_entity(self.entity())
}

/// Spawns an effect scoped to the lifetime of this entity and scope
Expand Down
12 changes: 12 additions & 0 deletions violet-core/src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ impl Background {
}
}

impl From<Component<Srgba>> for Background {
fn from(v: Component<Srgba>) -> Self {
Self::new(v)
}
}

impl From<Srgba> for Background {
fn from(v: Srgba) -> Self {
Self::new(v)
}
}

pub enum Spacing {
Small,
Medium,
Expand Down
10 changes: 5 additions & 5 deletions violet-core/src/widget/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
SizeExt, StyleExt, WidgetSize,
},
unit::Unit,
Frame, Scope, Widget, WidgetCollection,
Scope, Widget, WidgetCollection,
};

/// Style for most container type widgets.
Expand Down Expand Up @@ -63,8 +63,8 @@ impl<W> Stack<W> {
self
}

pub fn with_background(mut self, background: Background) -> Self {
self.style.background = Some(background);
pub fn with_background(mut self, background: impl Into<Background>) -> Self {
self.style.background = Some(background.into());
self
}

Expand Down Expand Up @@ -143,8 +143,8 @@ impl<W: WidgetCollection> List<W> {
self
}

pub fn with_background(mut self, background: Background) -> Self {
self.style.background = Some(background);
pub fn with_background(mut self, background: impl Into<Background>) -> Self {
self.style.background = Some(background.into());
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion violet-core/src/widget/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
components::{min_size, offset, padding, rect, transform},
input::{focusable, on_scroll},
state::{StateMut, StateStream},
style::{accent_background, interactive_active, Background, SizeExt},
style::{interactive_active, SizeExt},
to_owned,
unit::Unit,
utils::zip_latest,
Expand Down
60 changes: 60 additions & 0 deletions violet-demo/src/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use futures::StreamExt;
use glam::Vec2;
use violet::{
core::{
state::{State, StateStream},
style::{danger_background, Background, SizeExt},
unit::Unit,
widget::{
card, col, label, pill, row, Rectangle, SliderWithLabel, StreamWidget, Text, TextInput,
},
Widget,
},
futures_signals::signal::Mutable,
palette::Srgba,
};

pub fn app() -> impl Widget {
let name = Mutable::new("".to_string());
let quest = Mutable::new("".to_string());
let color = Mutable::new(Srgba::new(0.0, 0.61, 0.388, 1.0));

// Map a `Mutable<Srgba>` into a `StateDuplex<f32>` for each field
let r = color.clone().map_ref(|v| &v.red, |v| &mut v.red);
let g = color.clone().map_ref(|v| &v.green, |v| &mut v.green);
let b = color.clone().map_ref(|v| &v.blue, |v| &mut v.blue);

let speed = Mutable::new(None as Option<f32>);

col((
card(row((label("What is your name?"), TextInput::new(name)))),
card(row((label("What is your quest?"), TextInput::new(quest)))),
card(col((
label("What is your favorite colour?"),
SliderWithLabel::new(r, 0.0, 1.0).round(0.01),
SliderWithLabel::new(g, 0.0, 1.0).round(0.01),
SliderWithLabel::new(b, 0.0, 1.0).round(0.01),
StreamWidget(color.stream().map(|v| {
Rectangle::new(v)
.with_maximize(Vec2::X)
.with_min_size(Unit::px2(100.0, 100.0))
})),
))),
card(row((
label("What is the airspeed velocity of an unladen swallow?"),
// Fallibly parse and fill in the None at the same time using the `State` trait
// combinators
TextInput::new(speed.clone().prevent_feedback().filter_map(
|v| v.map(|v| v.to_string()),
|v| Some(v.parse::<f32>().ok()),
)),
StreamWidget(speed.stream().map(|v| {
match v {
Some(v) => pill(Text::new(format!("{v} m/s"))),
None => pill(Text::new("×".to_string()))
.with_background(Background::new(danger_background())),
}
})),
))),
))
}
Loading

0 comments on commit 90fba29

Please sign in to comment.