Skip to content

Commit

Permalink
feat: semantic value reference system
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Mar 11, 2024
1 parent d35afad commit 36f1412
Show file tree
Hide file tree
Showing 30 changed files with 603 additions and 497 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = [ "violet-wgpu", "violet-core", "violet-web-example" ]
members = [ "violet-wgpu", "violet-core", "violet-demo" ]

[workspace.package]
version = "0.0.1"
Expand Down Expand Up @@ -42,7 +42,7 @@ once_cell = "1.18"
slab = "0.4"
tynm ="0.1"
tokio = { version = "1.0", default-features = false, features = ["macros", "rt"] }
either = "1.10"
arrayvec = "0.7"

bytemuck = { version = "1.13", features = ["derive"] }
winit = "0.29"
Expand Down
109 changes: 38 additions & 71 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use flax::{components::name, FetchExt, Query};
use futures_signals::signal::Mutable;
use glam::{vec2, Vec2};
use itertools::Itertools;
use palette::{Hsva, IntoColor, Srgba};
use palette::{Hsva, IntoColor};
use std::time::Duration;
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, registry, util::SubscriberInitExt, EnvFilter,
Expand All @@ -19,35 +19,16 @@ use violet::core::{
Scope, StreamEffect, Widget,
};
use violet_core::{
style::Background,
style::{
colors::{DARK_CYAN_DEFAULT, JADE_DEFAULT, LION_DEFAULT},
danger_item, primary_background, secondary_background, spacing_medium, spacing_small,
Background, SizeExt, ValueOrRef,
},
widget::{BoxSized, ContainerStyle},
Edges,
};

struct MainApp;

macro_rules! srgba {
($color:literal) => {{
let [r, g, b] = color_hex::color_from_hex!($color);

Srgba::new(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0)
}};
}

const MARGIN: Edges = Edges::even(10.0);
const MARGIN_SM: Edges = Edges::even(5.0);

pub const EERIE_BLACK: Srgba = srgba!("#222525");
pub const EERIE_BLACK_300: Srgba = srgba!("#151616");
pub const EERIE_BLACK_400: Srgba = srgba!("#1b1e1e");
pub const EERIE_BLACK_600: Srgba = srgba!("#4c5353");
pub const PLATINUM: Srgba = srgba!("#dddddf");
pub const VIOLET: Srgba = srgba!("#8000ff");
pub const TEAL: Srgba = srgba!("#247b7b");
pub const EMERALD: Srgba = srgba!("#50c878");
pub const BRONZE: Srgba = srgba!("#cd7f32");
pub const CHILI_RED: Srgba = srgba!("#d34131");

impl Widget for MainApp {
fn mount(self, scope: &mut Scope) {
scope
Expand All @@ -73,16 +54,13 @@ impl Widget for MainApp {
.with_min_size(Unit::px(size))
.with_aspect_ratio(1.0),
)
.with_style(ContainerStyle {
margin: MARGIN,
..Default::default()
})
.with_margin(spacing_medium())
})
.collect_vec(),
)
.with_name("Images"),
Stack::new((Text::rich([
TextSegment::new("Violet").with_color(VIOLET),
TextSegment::new("Violet"),
TextSegment::new(" now has support for "),
TextSegment::new("rich ").with_style(Style::Italic),
TextSegment::new("text. I wanted to "),
Expand All @@ -95,22 +73,20 @@ impl Widget for MainApp {
TextSegment::new(" also show off the different font loadings: \n"),
TextSegment::new("Monospace:")
.with_family(FontFamily::named("JetBrainsMono Nerd Font"))
.with_color(TEAL),
.with_color(DARK_CYAN_DEFAULT),
TextSegment::new("\n\nfn main() { \n println!(")
.with_family(FontFamily::named("JetBrainsMono Nerd Font")),
TextSegment::new("\"Hello, world!\"")
.with_family(FontFamily::named("JetBrainsMono Nerd Font"))
.with_color(BRONZE)
.with_color(LION_DEFAULT)
.with_style(Style::Italic),
TextSegment::new("); \n}")
.with_family(FontFamily::named("JetBrainsMono Nerd Font")),
])
.with_font_size(18.0),))
.with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK)),
padding: MARGIN,
margin: MARGIN,
}),
.with_margin(spacing_small())
.with_margin(spacing_small())
.with_background(Background::new(primary_background())),
Stack::new(
Text::rich([
TextSegment::new("The quick brown fox 🦊 jumps over the lazy dog 🐕")
Expand All @@ -120,30 +96,25 @@ impl Widget for MainApp {
// .with_family("Inter")
.with_font_size(18.0),
)
.with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK)),
padding: MARGIN,
margin: MARGIN,
}),
.with_margin(spacing_small())
.with_padding(spacing_small())
.with_background(Background::new(primary_background())),
Stack::new((
BoxSized::new(Rectangle::new(CHILI_RED))
BoxSized::new(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(TEAL))
BoxSized::new(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),
))
.with_vertical_alignment(Alignment::Center)
.with_horizontal_alignment(Alignment::Center)
.with_background(Background::new(EERIE_BLACK_300))
.with_padding(MARGIN)
.with_margin(MARGIN),
.with_background(Background::new(secondary_background()))
.with_padding(spacing_small())
.with_margin(spacing_small()),
))
.with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK_600)),
..Default::default()
})
.with_background(Background::new(secondary_background()))
.contain_margins(true)
.with_direction(Direction::Vertical)
.mount(scope);
Expand Down Expand Up @@ -182,12 +153,12 @@ struct StackTest {}

impl Widget for StackTest {
fn mount(self, scope: &mut Scope<'_>) {
Stack::new((Text::new("This is an overlaid text").with_color(EMERALD),))
Stack::new((Text::new("This is an overlaid text").with_color(JADE_DEFAULT),))
.with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK_300)),
padding: MARGIN,
margin: MARGIN,
background: Some(Background::new(secondary_background())),
})
.with_margin(spacing_small())
.with_padding(spacing_small())
.mount(scope)
}
}
Expand All @@ -202,16 +173,13 @@ impl Widget for LayoutFlexTest {
let size = vec2(100.0, 20.0);

Stack::new(
BoxSized::new(Rectangle::new(
BoxSized::new(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_style(ContainerStyle {
margin: MARGIN,
..Default::default()
})
.with_margin(spacing_small())
})
.collect_vec(),
)
Expand All @@ -234,15 +202,14 @@ impl Widget for LayoutTest {
TextSegment::new("This is "),
TextSegment::new("sparta")
.with_style(Style::Italic)
.with_color(BRONZE),
.with_color(LION_DEFAULT),
])
.with_font_size(16.0)
.with_wrap(Wrap::None),
)
.with_margin(spacing_small())
.with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK)),
padding: MARGIN_SM,
margin: MARGIN_SM,
background: Some(Background::new(primary_background())),
}),
))
.on_press({
Expand Down Expand Up @@ -271,19 +238,19 @@ impl Widget for LayoutTest {
))
.contain_margins(self.contain_margins)
.with_cross_align(Alignment::Center)
.with_margin(spacing_small())
.with_padding(spacing_small())
.with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK)),
padding: MARGIN,
margin: MARGIN,
background: Some(Background::new(primary_background())),
});
// row_1.mount(scope);

List::new((row_1,))
.contain_margins(self.contain_margins)
.with_margin(spacing_small())
.with_padding(spacing_small())
.with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK_300)),
padding: MARGIN,
margin: MARGIN,
background: Some(Background::new(secondary_background())),
})
.mount(scope);
}
Expand Down
18 changes: 9 additions & 9 deletions examples/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use futures::StreamExt;
use futures_signals::signal::{Mutable, SignalExt};
use glam::Vec3;
use itertools::Itertools;
use palette::{num::Powi, FromColor, IntoColor, Lighten, Oklch, Srgb};
use tracing::info;
use palette::{FromColor, IntoColor, Oklch, Srgb};
use tracing_subscriber::{layer::SubscriberExt, registry, util::SubscriberInitExt, EnvFilter};
use tracing_tree::HierarchicalLayer;
use violet_core::{
state::{Map, MappedState, StateStream, StateStreamRef},
style::{colors::LION_500, SizeExt},
state::{Map, MapRef, StateStream, StateStreamRef},
style::{SizeExt, ValueOrRef},
unit::Unit,
utils::zip_latest,
widget::{
Expand Down Expand Up @@ -46,13 +45,13 @@ impl Widget for MainApp {
|v| Vec3::new(v.l, v.chroma, v.hue.into_positive_degrees()),
);

let lightness = MappedState::new(color.clone(), |v| &v.x, |v| &mut v.x);
let chroma = MappedState::new(color.clone(), |v| &v.y, |v| &mut v.y);
let hue = MappedState::new(color.clone(), |v| &v.z, |v| &mut v.z);
let lightness = MapRef::new(color.clone(), |v| &v.x, |v| &mut v.x);
let chroma = MapRef::new(color.clone(), |v| &v.y, |v| &mut v.y);
let hue = MapRef::new(color.clone(), |v| &v.z, |v| &mut v.z);

let color_rect = color.signal().map(|v| {
let color = Oklch::new(v.x, v.y, v.z).into_color();
Rectangle::new(color).with_min_size(Unit::px2(200.0, 100.0))
Rectangle::new(ValueOrRef::value(color)).with_min_size(Unit::px2(200.0, 100.0))
});

let falloff = Mutable::new(50.0);
Expand Down Expand Up @@ -131,7 +130,8 @@ impl Widget for Tints {
};

Stack::new(column((
Rectangle::new(color.into_color()).with_min_size(Unit::px2(60.0, 60.0)),
Rectangle::new(ValueOrRef::value(color.into_color()))
.with_min_size(Unit::px2(60.0, 60.0)),
Text::new(format!("{:.2}", f)),
)))
.with_margin(Edges::even(4.0))
Expand Down
19 changes: 6 additions & 13 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use tracing_tree::HierarchicalLayer;
use violet::core::{
components::size,
layout::Alignment,
style::StyleExt,
unit::Unit,
widget::{Button, List, SignalWidget, Stack, Text},
Scope, Widget,
};
use violet_core::{style::Background, widget::ContainerStyle, Edges};
use violet_core::style::{accent_item, secondary_background, spacing_small, Background, SizeExt};

macro_rules! srgba {
($color:literal) => {{
Expand All @@ -22,8 +21,6 @@ macro_rules! srgba {
}};
}

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

pub const EERIE_BLACK: Srgba = srgba!("#222525");
pub const EERIE_BLACK_300: Srgba = srgba!("#151616");
pub const EERIE_BLACK_400: Srgba = srgba!("#1b1e1e");
Expand All @@ -36,11 +33,10 @@ pub const BRONZE: Srgba = srgba!("#cd7f32");
pub const CHILI_RED: Srgba = srgba!("#d34131");

fn pill(widget: impl Widget) -> impl Widget {
Stack::new(widget).with_style(ContainerStyle {
background: Some(Background::new(EERIE_BLACK_300)),
padding: MARGIN_SM,
margin: MARGIN_SM,
})
Stack::new(widget)
.with_background(Background::new(secondary_background()))
.with_margin(spacing_small())
.with_padding(spacing_small())
}

struct MainApp;
Expand All @@ -64,10 +60,7 @@ impl Widget for MainApp {
"Please click the button to increment the counter",
)),
))
.with_style(ContainerStyle {
background: Some(Background::new(EMERALD)),
..Default::default()
})
.with_background(Background::new(accent_item()))
.with_cross_align(Alignment::Center)
.mount(scope);
}
Expand Down
Loading

0 comments on commit 36f1412

Please sign in to comment.