Skip to content

Commit

Permalink
feat: todo.rs example and some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Feb 11, 2025
1 parent 6e25342 commit 1c88b19
Show file tree
Hide file tree
Showing 7 changed files with 412 additions and 47 deletions.
4 changes: 4 additions & 0 deletions crates/components/src/drag_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dioxus::prelude::*;
use freya_elements::{
self as dioxus_elements,
events::MouseEvent,
MouseButton,
};
use freya_hooks::use_node_signal;
use torin::prelude::CursorPoint;
Expand Down Expand Up @@ -64,6 +65,9 @@ pub fn DragZone<T: 'static + Clone + PartialEq>(
};

let onmousedown = move |e: MouseEvent| {
if e.data.trigger_button != Some(MouseButton::Left) {
return;
}
let size = size.read();
let coord = e.get_screen_coordinates();
pos.set(
Expand Down
2 changes: 1 addition & 1 deletion crates/components/src/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn PopupBackground(children: Element) -> Element {
height: "100v",
width: "100v",
background: "rgb(0, 0, 0, 150)",
position: "absolute",
position: "global",
position_top: "0",
position_left: "0",
layer: "-2000",
Expand Down
1 change: 1 addition & 0 deletions crates/core/src/elements/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ pub trait ElementUtils {
drawing_area = drawing_area.translate(-center.to_vector());
drawing_area = drawing_area.scale(*scale_x, *scale_y);
drawing_area = drawing_area.translate(center.to_vector());
drawing_area = drawing_area.inflate(1.0, 1.0);
}

if !transform_state.rotations.is_empty() {
Expand Down
4 changes: 1 addition & 3 deletions crates/core/src/states/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ impl ParseAttribute for LayoutState {
}
AttributeName::Position => {
if let Some(value) = attr.value.as_text() {
if self.position.is_empty() {
self.position = Position::parse(value)?;
}
self.position.swap_for(Position::parse(value)?);
}
}
AttributeName::PositionTop => {
Expand Down
8 changes: 4 additions & 4 deletions crates/torin/src/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ where
let Some(child_data) = self.dom_adapter.get_node(child_id) else {
return false;
};
let is_stacked = !child_data.position.is_absolute();
let is_stacked = child_data.position.is_stacked();
if is_stacked {
last_child = Some(**child_id);

Expand Down Expand Up @@ -358,7 +358,7 @@ where

// No need to consider this Node for a two-phasing
// measurements as it will float on its own.
if child_data.position.is_absolute() {
if !child_data.position.is_stacked() {
continue;
}

Expand Down Expand Up @@ -516,7 +516,7 @@ where
}

// Only the stacked children will be aligned
if parent_node.main_alignment.is_spaced() && !child_data.position.is_absolute() {
if parent_node.main_alignment.is_spaced() && child_data.position.is_stacked() {
// Align the Main axis if necessary
Self::align_position(
AlignmentDirection::Main,
Expand Down Expand Up @@ -561,7 +561,7 @@ where
child_areas.area.adjust_size(&child_data);

// Stack this child into the parent
if !child_data.position.is_absolute() {
if child_data.position.is_stacked() {
Self::stack_child(
available_area,
parent_node,
Expand Down
81 changes: 42 additions & 39 deletions crates/torin/src/values/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,26 @@ pub enum Position {
}

impl Position {
pub const fn is_empty(&self) -> bool {
match self {
Self::Absolute(absolute_position) => {
let PositionSides {
top,
right,
bottom,
left,
} = &**absolute_position;
top.is_some() && right.is_some() && bottom.is_some() && left.is_some()
pub fn swap_for(&mut self, mut other: Self) {
let old_positions = match self {
Self::Global(positions) | Self::Absolute(positions) => Some(positions.clone()),
Self::Stacked => None,
};

let new_positions = match &mut other {
Self::Absolute(new_positions) => {
*self = Self::new_absolute();
Some(new_positions)
}
Self::Global(global_position) => {
let PositionSides {
top,
right,
bottom,
left,
} = &**global_position;
top.is_some() && right.is_some() && bottom.is_some() && left.is_some()
Self::Global(new_positions) => {
*self = Self::new_global();
Some(new_positions)
}
Self::Stacked => true,
Self::Stacked => None,
};

if let Some((new_positions, old_positions)) = new_positions.zip(old_positions) {
*new_positions = old_positions;
}
}

Expand All @@ -67,6 +66,10 @@ impl Position {
}))
}

pub fn is_stacked(&self) -> bool {
matches!(self, Self::Stacked { .. })
}

pub fn is_absolute(&self) -> bool {
matches!(self, Self::Absolute { .. })
}
Expand All @@ -76,38 +79,38 @@ impl Position {
}

pub fn set_top(&mut self, value: f32) {
if !self.is_absolute() {
*self = Self::new_absolute();
}
if let Self::Absolute(absolute_position) = self {
absolute_position.top = Some(value);
match self {
Self::Absolute(position) | Self::Global(position) => {
position.top = Some(value);
}
Self::Stacked => {}
}
}

pub fn set_right(&mut self, value: f32) {
if !self.is_absolute() {
*self = Self::new_absolute();
}
if let Self::Absolute(absolute_position) = self {
absolute_position.right = Some(value);
match self {
Self::Absolute(position) | Self::Global(position) => {
position.right = Some(value);
}
Self::Stacked => {}
}
}

pub fn set_bottom(&mut self, value: f32) {
if !self.is_absolute() {
*self = Self::new_absolute();
}
if let Self::Absolute(absolute_position) = self {
absolute_position.bottom = Some(value);
match self {
Self::Absolute(position) | Self::Global(position) => {
position.bottom = Some(value);
}
Self::Stacked => {}
}
}

pub fn set_left(&mut self, value: f32) {
if !self.is_absolute() {
*self = Self::new_absolute();
}
if let Self::Absolute(absolute_position) = self {
absolute_position.left = Some(value);
match self {
Self::Absolute(position) | Self::Global(position) => {
position.left = Some(value);
}
Self::Stacked => {}
}
}

Expand Down
Loading

0 comments on commit 1c88b19

Please sign in to comment.