Skip to content

Commit

Permalink
feat: todo.rs example and some fixes (#1108)
Browse files Browse the repository at this point in the history
* feat: `todo.rs` example and some fixes

* feat: Close popup by clicking on its background

* dark theme for the todo example
  • Loading branch information
marc2332 authored Feb 13, 2025
1 parent 8d0dd30 commit be6428b
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 62 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
42 changes: 26 additions & 16 deletions crates/components/src/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use freya_elements::{
Key,
KeyboardEvent,
},
MouseEvent,
};
use freya_hooks::{
theme_with,
Expand All @@ -26,18 +27,28 @@ use crate::{
/// The background of the [`Popup`] component.
#[allow(non_snake_case)]
#[component]
pub fn PopupBackground(children: Element) -> Element {
pub fn PopupBackground(children: Element, onclick: EventHandler<MouseEvent>) -> Element {
rsx!(rect {
height: "100v",
width: "100v",
background: "rgb(0, 0, 0, 150)",
position: "absolute",
position_top: "0",
position_left: "0",
layer: "-2000",
main_align: "center",
cross_align: "center",
{children}
rect {
onclick,
height: "100v",
width: "100v",
background: "rgb(0, 0, 0, 150)",
position: "global",
position_top: "0",
position_left: "0",
}
rect {
height: "100v",
width: "100v",
position: "global",
position_top: "0",
position_left: "0",
main_align: "center",
cross_align: "center",
{children}
}
})
}

Expand Down Expand Up @@ -133,10 +144,9 @@ pub fn Popup(
}
};

let onpress = move |_| request_to_close();

rsx!(
PopupBackground {
onclick: move |_| request_to_close(),
rect {
scale: "{scale.read()} {scale.read()}",
margin: "{margin.read()} 0 0 0",
Expand All @@ -163,10 +173,10 @@ pub fn Popup(
corner_radius: "999".into(),
shadow: "none".into()
}),
onpress,
onpress: move |_| request_to_close(),
CrossIcon {
fill: cross_fill
}
}
}
}
}
Expand Down Expand Up @@ -254,7 +264,7 @@ mod test {
utils.wait_for_update().await;

// Check the popup is opened
assert_eq!(utils.sdom().get().layout().size(), 10);
assert_eq!(utils.sdom().get().layout().size(), 12);

utils.click_cursor((395., 180.)).await;

Expand All @@ -273,7 +283,7 @@ mod test {
});
utils.wait_for_update().await;
// Check the popup is still open
assert_eq!(utils.sdom().get().layout().size(), 10);
assert_eq!(utils.sdom().get().layout().size(), 12);

// Send a ESC globalkeydown event
utils.push_event(TestEvent::Keyboard {
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 be6428b

Please sign in to comment.