Skip to content
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

Scoping Feature #55

Merged
merged 45 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
6c5071e
scope improvements
cyypherus Oct 1, 2024
da780e8
scope test
cyypherus Oct 3, 2024
c8cdb11
scope test
cyypherus Oct 3, 2024
e627490
yapping
cyypherus Oct 4, 2024
ff22cd2
yapping
cyypherus Oct 4, 2024
03d1e30
wip multiple generics
cyypherus Oct 8, 2024
ea41412
working?
cyypherus Oct 8, 2024
d8cc455
fixes
cyypherus Oct 8, 2024
22b3a83
doc
cyypherus Oct 8, 2024
56db748
test
cyypherus Oct 8, 2024
a290598
public api workaround
cyypherus Oct 14, 2024
9a1460d
working?
cyypherus Oct 14, 2024
95c77bb
polish, working!!!!!!!
cyypherus Oct 14, 2024
6c6a1c0
polish, working!!!!!!!
cyypherus Oct 14, 2024
f549374
swap &mut T for T: Copy for max flexibility
cyypherus Oct 14, 2024
87d8667
swap &mut T for T: Copy for max flexibility
cyypherus Oct 14, 2024
db4c8ff
checkpoint
cyypherus Oct 14, 2024
5ee5b84
copy > &mut
cyypherus Oct 15, 2024
7ba7be2
associated type
cyypherus Oct 15, 2024
7a1307e
cleanup
cyypherus Oct 15, 2024
cba54a8
merge & cleanup
cyypherus Oct 15, 2024
05c1d57
rename generics
cyypherus Oct 15, 2024
c35c241
doc examples
cyypherus Oct 15, 2024
6c3555e
scope test
cyypherus Oct 15, 2024
1f5fc04
warnings
cyypherus Oct 15, 2024
6349436
remove anynode, nice
cyypherus Oct 15, 2024
eb55807
cleanup
cyypherus Oct 15, 2024
0437e83
cleanup
cyypherus Oct 15, 2024
d7424e2
fixup macroquad example
cyypherus Oct 15, 2024
a156412
fixup egui example
cyypherus Oct 15, 2024
b58b129
fixup egui case study example
cyypherus Oct 15, 2024
3e069df
fixup demo site
cyypherus Oct 15, 2024
cacbceb
associated type -> generic for multiple scope paths
cyypherus Oct 15, 2024
7277b14
associated type -> generic for multiple scope paths
cyypherus Oct 15, 2024
97d607f
if let scope test wip
cyypherus Oct 15, 2024
7811fdc
if let scoping, cleanup
cyypherus Oct 15, 2024
3368a08
rename generics
cyypherus Oct 15, 2024
cc3194c
organizing
cyypherus Oct 16, 2024
0b2a9a6
organizing
cyypherus Oct 16, 2024
f015679
remove public-api workaround
cyypherus Oct 16, 2024
440853e
make scopable independent of state bc orphan rules
cyypherus Oct 16, 2024
52044b1
nicer method signature
cyypherus Oct 16, 2024
ec42776
update examples
cyypherus Oct 16, 2024
3c8f478
docs
cyypherus Oct 16, 2024
470ee86
docs
cyypherus Oct 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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