Skip to content

Commit

Permalink
feat: maximize and some performance
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Mar 16, 2024
1 parent 40626c8 commit 59beb44
Show file tree
Hide file tree
Showing 30 changed files with 376 additions and 276 deletions.
Empty file added .gitmodules
Empty file.
6 changes: 4 additions & 2 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 @@ -82,6 +82,7 @@ palette.workspace = true
futures-signals.workspace = true
flax.workspace = true
lru.workspace = true
web-time.workspace = true

[dev-dependencies]
anyhow.workspace = true
Expand Down
16 changes: 8 additions & 8 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use violet_core::{
danger_item, primary_background, secondary_background, spacing_medium, spacing_small,
Background, SizeExt, ValueOrRef,
},
widget::{BoxSized, ContainerStyle},
widget::ContainerStyle,
};

struct MainApp;
Expand All @@ -50,7 +50,7 @@ impl Widget for MainApp {
.map(|i| {
let size = Vec2::splat(128.0 / i as f32);
Stack::new(
BoxSized::new(Image::new("./assets/images/statue.jpg"))
Image::new("./assets/images/statue.jpg")
.with_min_size(Unit::px(size))
.with_aspect_ratio(1.0),
)
Expand Down Expand Up @@ -100,10 +100,10 @@ impl Widget for MainApp {
.with_padding(spacing_small())
.with_background(Background::new(primary_background())),
Stack::new((
BoxSized::new(Rectangle::new(danger_item()))
Rectangle::new(danger_item())
.with_min_size(Unit::px(vec2(100.0, 30.0)))
.with_size(Unit::px(vec2(50.0, 30.0))),
BoxSized::new(Rectangle::new(danger_item()))
Rectangle::new(danger_item())
.with_min_size(Unit::px(vec2(200.0, 10.0)))
.with_size(Unit::px(vec2(50.0, 10.0))),
Text::new("This is some text").with_font_size(16.0),
Expand Down Expand Up @@ -170,14 +170,14 @@ impl Widget for LayoutFlexTest {
List::new(
(0..8)
.map(|i| {
let size = vec2(100.0, 20.0);
let size = vec2(50.0, 20.0);

Stack::new(
BoxSized::new(Rectangle::new(ValueOrRef::value(
Rectangle::new(ValueOrRef::value(
Hsva::new(i as f32 * 30.0, 1.0, 1.0, 1.0).into_color(),
)))
))
.with_min_size(Unit::px(size))
.with_size(Unit::px(size * vec2(i as f32, 1.0))),
.with_maximize(Vec2::X * i as f32),
)
.with_margin(spacing_small())
})
Expand Down
24 changes: 4 additions & 20 deletions examples/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,13 @@ use violet_core::{
style::{
self,
colors::{EERIE_BLACK_600, EERIE_BLACK_DEFAULT},
secondary_background, spacing_small, Background, SizeExt,
spacing_small, Background, SizeExt,
},
text::Wrap,
widget::{
card, column, row, BoxSized, Button, ButtonStyle, ContainerStyle, SliderWithLabel,
TextInput,
},
widget::{card, column, label, row, Button, ButtonStyle, SliderWithLabel, TextInput},
};
use violet_wgpu::renderer::RendererConfig;

fn label(text: impl Into<String>) -> Stack<Text> {
Stack::new(Text::new(text.into()))
.with_padding(spacing_small())
.with_margin(spacing_small())
}

fn pill(widget: impl Widget) -> impl Widget {
Stack::new(widget).with_style(ContainerStyle {
background: Some(Background::new(secondary_background())),
})
}

pub fn main() -> anyhow::Result<()> {
registry()
.with(
Expand Down Expand Up @@ -94,8 +79,7 @@ impl Widget for MainApp {
))
.with_stretch(true),
),
BoxSized::new(Rectangle::new(EERIE_BLACK_600))
.with_size(Unit::rel2(1.0, 0.0) + Unit::px2(0.0, 1.0)),
Rectangle::new(EERIE_BLACK_600).with_size(Unit::rel2(1.0, 0.0) + Unit::px2(0.0, 1.0)),
card(column((
column((
row((
Expand Down Expand Up @@ -147,7 +131,7 @@ impl Widget for Tints {
);

card(column((
BoxSized::new(Rectangle::new(color)).with_size(Unit::px2(100.0, 40.0)),
Rectangle::new(color).with_size(Unit::px2(100.0, 40.0)),
label(format!("{tint}")),
label(color_string),
)))
Expand Down
24 changes: 20 additions & 4 deletions examples/row.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::iter::repeat;

use glam::{vec2, Vec2};
use itertools::Itertools;
use palette::named::DARKCYAN;
use tracing_subscriber::{layer::SubscriberExt, registry, util::SubscriberInitExt, EnvFilter};
use tracing_tree::HierarchicalLayer;

Expand All @@ -15,10 +17,13 @@ use violet::core::{
};
use violet_core::{
style::{
colors::{JADE_400, JADE_DEFAULT, LION_DEFAULT, REDWOOD_100, ULTRA_VIOLET_DEFAULT},
colors::{
DARK_CYAN_DEFAULT, JADE_400, JADE_DEFAULT, LION_DEFAULT, REDWOOD_100,
ULTRA_VIOLET_DEFAULT,
},
spacing_medium, spacing_small, SizeExt,
},
widget::{card, column, label, row, Stack},
widget::{card, centered, column, label, row, Image, Stack},
};
use violet_wgpu::renderer::RendererConfig;

Expand Down Expand Up @@ -66,7 +71,17 @@ impl Widget for MainApp {
// .with_min_size(Unit::px2(100.0, 100.0))
// .with_size(Unit::px2(0.0, 100.0) + Unit::rel2(1.0, 0.0)),
// .with_margin(spacing_medium()),
row((0..16).map(|_| Stack::new(Item)).collect_vec()),
row((0..4)
.map(|_| Box::new(Stack::new(Item)) as Box<dyn Widget>)
.chain([Box::new(
centered((Rectangle::new(JADE_DEFAULT)
.with_maximize(vec2(1.0, 0.0))
.with_size(Unit::px2(0.0, 50.0))
.with_max_size(Unit::px2(1000.0, 100.0)),))
.with_maximize(Vec2::ONE),
) as Box<dyn Widget>])
.collect_vec())
.with_padding(spacing_small()),
))
// .with_padding(spacing_medium())
.contain_margins(true),
Expand All @@ -81,8 +96,9 @@ struct Item;

impl Widget for Item {
fn mount(self, scope: &mut Scope<'_>) {
Rectangle::new(ULTRA_VIOLET_DEFAULT)
Image::new("./assets/images/statue.jpg")
.with_size(Unit::px2(100.0, 100.0))
// .with_aspect_ratio(1.0)
.with_margin(spacing_small())
.mount(scope)
}
Expand Down
9 changes: 1 addition & 8 deletions examples/sizing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,12 @@ use violet_core::{
state::MapRef,
style::{colors::DARK_CYAN_DEFAULT, SizeExt},
text::Wrap,
widget::{card, centered, column, row, Slider},
widget::{card, centered, column, label, row, Slider},
};
use violet_wgpu::renderer::RendererConfig;

const MARGIN_SM: Edges = Edges::even(4.0);

fn label(text: impl Into<String>) -> Stack<Text> {
Stack::new(Text::new(text.into()))
.with_padding(MARGIN_SM)
.with_margin(MARGIN_SM)
.with_background(Background::new(EERIE_BLACK_400))
}

pub fn main() -> anyhow::Result<()> {
registry()
.with(
Expand Down
6 changes: 3 additions & 3 deletions recipes.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"check": {
"cmd": "cargo check --all-targets --all-features"
"cmd": "cargo check --all-targets --all-features --workspace"
},
"run demo": {
"cmd": "cargo run --package violet-demo"
Expand Down Expand Up @@ -56,10 +56,10 @@
},
"build web": {
"cmd": "wasm-pack build --target web",
"cwd": "./violet-web-example/"
"cwd": "./violet-demo/"
},
"host": {
"cmd": "python3 -m http.server 8080",
"cwd": "./violet-web-example/"
"cwd": "./violet-demo/"
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub use flax;
pub use futures_signals;
pub use glam;
pub use palette;
pub use web_time;
2 changes: 2 additions & 0 deletions violet-core/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ component! {
/// A margin is in essence a minimum allowed distance to another items bounds
pub margin: Edges => [ Debuggable ],

pub maximize: Vec2 => [ Debuggable ],

pub text: Vec<TextSegment> => [ ],
pub text_wrap: Wrap => [ Debuggable ],
pub font_size: f32 => [ Debuggable ],
Expand Down
39 changes: 39 additions & 0 deletions violet-core/src/effect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

pub use future::FutureEffect;
use pin_project::pin_project;
pub use stream::StreamEffect;

/// An asynchronous computation which has access to `Data` when polled
Expand All @@ -16,4 +17,42 @@ pub use stream::StreamEffect;
pub trait Effect<Data> {
/// Polls the effect
fn poll(self: Pin<&mut Self>, context: &mut Context<'_>, data: &mut Data) -> Poll<()>;
fn label(&self) -> Option<&str> {
None
}

fn with_label(self, label: impl Into<String>) -> EffectWithLabel<Self>
where
Self: Sized,
{
EffectWithLabel::new(self, Some(label.into()))
}
}

#[pin_project]
#[doc(hidden)]
pub struct EffectWithLabel<E> {
#[pin]
effect: E,
label: Option<String>,
}

impl<E> EffectWithLabel<E> {
pub fn new(effect: E, label: Option<String>) -> Self {
Self { effect, label }
}
}

impl<E, Data> Effect<Data> for EffectWithLabel<E>
where
E: Effect<Data>,
{
#[inline]
fn poll(self: Pin<&mut Self>, context: &mut Context<'_>, data: &mut Data) -> Poll<()> {
self.project().effect.poll(context, data)
}

fn label(&self) -> Option<&str> {
self.label.as_deref()
}
}
4 changes: 4 additions & 0 deletions violet-core/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ impl<Data> Executor<Data> {
}

pub fn tick(&mut self, data: &mut Data) {
puffin::profile_function!();
assert!(self.processing.is_empty());
loop {
puffin::profile_scope!("tick");
// Add new tasks
self.processing
.extend(self.incoming.borrow_mut().drain(..).map(|task| {
Expand All @@ -201,6 +203,8 @@ impl<Data> Executor<Data> {
// external waker
continue;
};

puffin::profile_scope!("process task", task.effect.label().unwrap_or_default());
let mut context = Context::from_waker(&*waker);
tracing::trace!(?id, "Polling task");

Expand Down
4 changes: 1 addition & 3 deletions violet-core/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::str::FromStr;

use flax::{
component, components::child_of, entity_ids, fetch::Satisfied, filter::All, Component, Entity,
EntityIds, EntityRef, Fetch, FetchExt, Mutable, Query, Topo, World,
EntityIds, EntityRef, Fetch, FetchExt, Query, Topo, World,
};
use glam::Vec2;

Expand Down
Loading

0 comments on commit 59beb44

Please sign in to comment.