Skip to content

Commit

Permalink
Merge pull request #55 from ejjonny/dyn-trait-scoping-mut-multi
Browse files Browse the repository at this point in the history
Scoping Feature
  • Loading branch information
cyypherus authored Oct 16, 2024
2 parents 447961f + 470ee86 commit af25dad
Show file tree
Hide file tree
Showing 23 changed files with 813 additions and 379 deletions.
18 changes: 9 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ authors = ["ejjonny"]
[lib]
crate-type = ["lib"]

[[example]]
name = "macroquad-example"
path = "examples/macroquad-example/src/main.rs"
# [[example]]
# name = "macroquad-example"
# path = "examples/macroquad-example/src/main.rs"

[[example]]
name = "egui-example"
path = "examples/egui-example/src/main.rs"
# [[example]]
# name = "egui-example"
# path = "examples/egui-example/src/main.rs"

[[example]]
name = "egui-case-study"
path = "examples/egui-case-study/src/main.rs"
# [[example]]
# name = "egui-case-study"
# path = "examples/egui-case-study/src/main.rs"

[dev-dependencies]
macroquad = "0.4.13"
Expand Down
2 changes: 1 addition & 1 deletion examples/demo-site/Cargo.lock

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

14 changes: 10 additions & 4 deletions examples/macroquad-example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use backer::models::*;
use backer::nodes::*;
use backer::traits::Scopable;
use backer::Layout;
use backer::Node;
use macroquad::prelude::*;
Expand Down Expand Up @@ -40,14 +41,19 @@ async fn main() {

const BTN_SIZE: f32 = 50.;
fn layout_for_highlight(ctx: &mut State) -> Node<State> {
impl Scopable<HighlightedCase> for State {
fn scope<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut HighlightedCase) -> R,
{
f(&mut self.highlight)
}
}
let highlight = ctx.highlight;
row_spaced(
20.,
vec![
scope(
|state: &mut State| &mut state.highlight,
rel_abs_seq(ctx.highlight),
),
scope(|highlight| rel_abs_seq(*highlight)),
if highlight == HighlightedCase::AlignmentOffset || highlight == HighlightedCase::None {
column_spaced(
10.,
Expand Down
45 changes: 0 additions & 45 deletions src/anynode.rs

This file was deleted.

68 changes: 0 additions & 68 deletions src/clone.rs

This file was deleted.

69 changes: 36 additions & 33 deletions src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,24 @@ impl Constraint {
}
}

impl<State> NodeValue<State> {
impl<State, Ctx> NodeValue<State, Ctx> {
pub(crate) fn constraints(
&mut self,
available_area: Area,
state: &mut State,
ctx: &mut Ctx,
) -> SizeConstraints {
let contextual_aligns = self.contextual_aligns();
let allocations = self.allocate_area(
available_area,
contextual_aligns.0,
contextual_aligns.1,
state,
ctx,
);
match self {
NodeValue::Padding { amounts, element } => {
let child = element.constraints(allocations[0], state);
let child = element.constraints(allocations[0], state, ctx);
SizeConstraints {
width: Constraint {
lower: Some(
Expand Down Expand Up @@ -78,16 +80,16 @@ impl<State> NodeValue<State> {
if let Some(current) = current {
Some(SizeConstraints {
width: current.width.combine_adjacent_priority(
element.constraints(*allocated, state).width,
element.constraints(*allocated, state, ctx).width,
),
height: current.height.combine_sum(
element.constraints(*allocated, state).height,
element.constraints(*allocated, state, ctx).height,
*spacing,
),
aspect: None,
})
} else {
Some(element.constraints(*allocated, state))
Some(element.constraints(*allocated, state, ctx))
}
},
)
Expand All @@ -109,16 +111,16 @@ impl<State> NodeValue<State> {
if let Some(current) = current {
Some(SizeConstraints {
width: current.width.combine_sum(
element.constraints(*allocated, state).width,
element.constraints(*allocated, state, ctx).width,
*spacing,
),
height: current.height.combine_adjacent_priority(
element.constraints(*allocated, state).height,
element.constraints(*allocated, state, ctx).height,
),
aspect: None,
})
} else {
Some(element.constraints(*allocated, state))
Some(element.constraints(*allocated, state, ctx))
}
},
)
Expand All @@ -127,41 +129,42 @@ impl<State> NodeValue<State> {
height: Constraint::none(),
aspect: None,
}),
NodeValue::Stack(elements) => {
elements
.iter_mut()
.fold(Option::<SizeConstraints>::None, |current, element| {
if let Some(current) = current {
Some(current.combine_adjacent_priority(
element.constraints(allocations[0], state),
))
} else {
Some(element.constraints(allocations[0], state))
}
})
.unwrap_or(SizeConstraints {
width: Constraint::none(),
height: Constraint::none(),
aspect: None,
})
}
NodeValue::Stack(elements) => elements
.iter_mut()
.fold(Option::<SizeConstraints>::None, |current, element| {
if let Some(current) = current {
Some(current.combine_adjacent_priority(element.constraints(
allocations[0],
state,
ctx,
)))
} else {
Some(element.constraints(allocations[0], state, ctx))
}
})
.unwrap_or(SizeConstraints {
width: Constraint::none(),
height: Constraint::none(),
aspect: None,
}),
NodeValue::Explicit { options, element } => element
.constraints(allocations[0], state)
.constraints(allocations[0], state, ctx)
.combine_equal_priority(SizeConstraints::from_size(
options.clone(),
allocations[0],
state,
ctx,
)),
NodeValue::Offset { element, .. } => element.constraints(allocations[0], state),
NodeValue::Scope { scoped, .. } => scoped.constraints(allocations[0], state),
NodeValue::Offset { element, .. } => element.constraints(allocations[0], state, ctx),
NodeValue::Scope { scoped } => scoped.constraints(allocations[0], state, ctx),
NodeValue::Draw(_) | NodeValue::Space | NodeValue::AreaReader { .. } => {
SizeConstraints {
width: Constraint::none(),
height: Constraint::none(),
aspect: None,
}
}
NodeValue::Coupled { element, .. } => element.constraints(allocations[0], state),
NodeValue::Coupled { element, .. } => element.constraints(allocations[0], state, ctx),
NodeValue::Empty | NodeValue::Group(_) => unreachable!(),
}
}
Expand Down Expand Up @@ -249,7 +252,7 @@ impl Constraint {
}

impl SizeConstraints {
pub(crate) fn from_size<U>(value: Size<U>, area: Area, state: &mut U) -> Self {
pub(crate) fn from_size<A, B>(value: Size<A, B>, area: Area, a: &mut A, b: &mut B) -> Self {
let mut initial = SizeConstraints {
width: if value.width_min.is_some() || value.width_max.is_some() {
Constraint {
Expand All @@ -276,12 +279,12 @@ impl SizeConstraints {
aspect: value.aspect,
};
if let Some(dynamic) = value.dynamic_height {
let result = Some(initial.height.clamp(dynamic(area.width, state)));
let result = Some(initial.height.clamp(dynamic(area.width, a, b)));
initial.height.lower = result;
initial.height.upper = result;
}
if let Some(dynamic) = value.dynamic_width {
let result = Some(initial.width.clamp(dynamic(area.height, state)));
let result = Some(initial.width.clamp(dynamic(area.height, a, b)));
initial.width.lower = result;
initial.width.upper = result;
}
Expand Down
6 changes: 4 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::layout::NodeValue;
use std::fmt;

impl<State> fmt::Debug for NodeValue<State> {
impl<State, Ctx> fmt::Debug for NodeValue<State, Ctx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NodeValue::Padding { amounts, element } => f
Expand Down Expand Up @@ -54,7 +54,9 @@ impl<State> fmt::Debug for NodeValue<State> {
NodeValue::Space => write!(f, "Space"),
NodeValue::Empty => write!(f, "Empty"),
NodeValue::AreaReader { .. } => write!(f, "WidthReader"),
NodeValue::Scope { scoped } => f.debug_struct("Scope").field("scoped", scoped).finish(),
NodeValue::Scope { scoped } => {
f.debug_struct("Scope").field("scoped", &scoped).finish()
}
NodeValue::Coupled {
element,
coupled,
Expand Down
14 changes: 7 additions & 7 deletions src/drawable.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use crate::models::Area;
use std::{fmt, rc::Rc};

type DrawFn<State> = Rc<dyn Fn(Area, &'_ mut State)>;
type DrawFn<State, Ctx> = Rc<dyn Fn(Area, &'_ mut State, &'_ mut Ctx)>;

#[derive(Clone)]
pub(crate) struct Drawable<State> {
pub(crate) struct Drawable<State, Ctx> {
pub(crate) area: Area,
pub(crate) draw: DrawFn<State>,
pub(crate) draw: DrawFn<State, Ctx>,
}

impl<State> Drawable<State> {
pub(crate) fn draw(&self, area: Area, state: &mut State) {
impl<State, Ctx> Drawable<State, Ctx> {
pub(crate) fn draw(&self, area: Area, a: &mut State, b: &mut Ctx) {
if area.width > 0. && area.height > 0. {
(self.draw)(area, state);
(self.draw)(area, a, b);
}
}
}

impl<State> fmt::Debug for Drawable<State> {
impl<State, Ctx> fmt::Debug for Drawable<State, Ctx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Drawable")
.field("area", &self.area)
Expand Down
Loading

0 comments on commit af25dad

Please sign in to comment.