Skip to content

Commit

Permalink
feat: Add basic constraints (macOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
usadson committed Feb 7, 2024
1 parent 26cc5cf commit e416090
Show file tree
Hide file tree
Showing 7 changed files with 303 additions and 20 deletions.
13 changes: 13 additions & 0 deletions finestra/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub struct ViewTree<State> {

#[cfg(target_os = "macos")]
dispatcher: StatefulEventDispatcher<State>,

parent_id: Option<ViewId>,
}

impl<State: 'static> ViewTree<State> {
Expand All @@ -62,6 +64,7 @@ impl<State: 'static> ViewTree<State> {
Self {
id_generator: Default::default(),
registry,
parent_id: None,
}
}

Expand All @@ -71,6 +74,7 @@ impl<State: 'static> ViewTree<State> {
id_generator: Default::default(),
registry,
dispatcher,
parent_id: None,
}
}

Expand All @@ -80,6 +84,7 @@ impl<State: 'static> ViewTree<State> {
id
}

#[cfg(windows)]
pub(crate) fn put_event_handlers_with_id(&mut self, id: ViewId, map: EventHandlerMap<State>) -> ViewId {
self.registry.map.insert(id, map);
id
Expand All @@ -89,6 +94,14 @@ impl<State: 'static> ViewTree<State> {
pub(crate) fn create_dispatcher(&self) -> Box<dyn EventDispatcher> {
Box::new(self.dispatcher.clone())
}

pub(crate) fn parent_id(&self) -> Option<ViewId> {
self.parent_id
}

pub(crate) fn set_parent_id(&mut self, id: ViewId) {
self.parent_id = Some(id);
}
}

#[derive(Debug, Default)]
Expand Down
49 changes: 49 additions & 0 deletions finestra/src/layout/constraints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (C) 2024 Tristan Gerritsen <tristan@thewoosh.org>
// All Rights Reserved.

use crate::ViewId;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Constraint {
pub alignment: ConstraintAlignment,

pub reference_id: ViewId,
pub reference_alignment: ConstraintAlignment,
}

#[cfg(target_os = "macos")]
impl Constraint {
pub fn convert(
&self,
this: &impl crate::platform::macos::LayoutExt,
reference: &impl crate::platform::macos::LayoutExt,
) -> cacao::layout::LayoutConstraint {
_ = self.reference_alignment; // TODO

match self.alignment {
ConstraintAlignment::Top => {
this.layout_constraint_top().constraint_less_than_or_equal_to(reference.layout_constraint_top())
}

ConstraintAlignment::Bottom => {
this.layout_constraint_bottom().constraint_less_than_or_equal_to(reference.layout_constraint_bottom())
}

ConstraintAlignment::Left => {
this.layout_constraint_left().constraint_less_than_or_equal_to(reference.layout_constraint_left())
}

ConstraintAlignment::Right => {
this.layout_constraint_right().constraint_less_than_or_equal_to(reference.layout_constraint_right())
}
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ConstraintAlignment {
Left,
Right,
Top,
Bottom,
}
6 changes: 6 additions & 0 deletions finestra/src/layout/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (C) 2024 Tristan Gerritsen <tristan@thewoosh.org>
// All Rights Reserved.

mod constraints;

pub(crate) use self::constraints::*;
2 changes: 2 additions & 0 deletions finestra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use euclid;
mod app;
mod error;
mod event;
mod layout;
mod platform;
mod resources;
mod state;
Expand All @@ -19,6 +20,7 @@ mod window;

pub use self::app::*;
pub use self::error::*;
pub(crate) use self::layout::*;
pub use self::resources::*;
pub use self::state::*;
pub use self::views::*;
Expand Down
Loading

0 comments on commit e416090

Please sign in to comment.