Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions crates/components/src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ mod test {
use freya_testing::prelude::*;

#[tokio::test]
pub async fn button() {
fn button_app() -> Element {
pub async fn switch() {
fn switch_app() -> Element {
let mut enabled = use_signal(|| false);

rsx!(
Expand All @@ -202,7 +202,7 @@ mod test {
)
}

let mut utils = launch_test(button_app);
let mut utils = launch_test(switch_app);
let root = utils.root();
let label = root.get(1);
utils.wait_for_update().await;
Expand Down
85 changes: 49 additions & 36 deletions crates/state/src/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ use freya_native_core::{
};
use freya_native_core_macro::partial_derive_state;

use crate::CustomAttributeValues;
use crate::{
CustomAttributeValues,
ParseAttribute,
ParseError,
};

#[derive(Clone, Debug, PartialEq, Eq, Default, Component)]
pub struct AccessibilityNodeState {
Expand All @@ -28,6 +32,49 @@ pub struct AccessibilityNodeState {
pub focusable: bool,
}

impl ParseAttribute for AccessibilityNodeState {
fn parse_attribute(
&mut self,
attr: freya_native_core::prelude::OwnedAttributeView<CustomAttributeValues>,
) -> Result<(), crate::ParseError> {
match attr.attribute {
AttributeName::FocusId => {
if let OwnedAttributeValue::Custom(CustomAttributeValues::AccessibilityId(id)) =
attr.value
{
self.accessibility_id = Some(*id);
}
}
AttributeName::Role => {
if let OwnedAttributeValue::Text(attr) = attr.value {
self.role = Some(
serde_json::from_str::<Role>(&format!("\"{attr}\""))
.map_err(|_| ParseError)?,
)
}
}
AttributeName::Alt => {
if let OwnedAttributeValue::Text(attr) = attr.value {
self.alt = Some(attr.to_owned())
}
}
AttributeName::Name => {
if let OwnedAttributeValue::Text(attr) = attr.value {
self.name = Some(attr.to_owned())
}
}
AttributeName::Focusable => {
if let OwnedAttributeValue::Text(attr) = attr.value {
self.focusable = attr.parse().unwrap_or_default()
}
}
_ => {}
}

Ok(())
}
}

#[partial_derive_state]
impl State<CustomAttributeValues> for AccessibilityNodeState {
type ParentDependencies = ();
Expand Down Expand Up @@ -57,41 +104,7 @@ impl State<CustomAttributeValues> for AccessibilityNodeState {

if let Some(attributes) = node_view.attributes() {
for attr in attributes {
match attr.attribute {
AttributeName::FocusId => {
if let OwnedAttributeValue::Custom(
CustomAttributeValues::AccessibilityId(id),
) = attr.value
{
accessibility.accessibility_id = Some(*id);
}
}
AttributeName::Role => {
if let OwnedAttributeValue::Text(attr) = attr.value {
if let Ok(new_role) =
serde_json::from_str::<Role>(&format!("\"{attr}\""))
{
accessibility.role = Some(new_role)
}
}
}
AttributeName::Alt => {
if let OwnedAttributeValue::Text(attr) = attr.value {
accessibility.alt = Some(attr.to_owned())
}
}
AttributeName::Name => {
if let OwnedAttributeValue::Text(attr) = attr.value {
accessibility.name = Some(attr.to_owned())
}
}
AttributeName::Focusable => {
if let OwnedAttributeValue::Text(attr) = attr.value {
accessibility.focusable = attr.parse().unwrap_or_default()
}
}
_ => {}
}
accessibility.parse_safe(attr);
}
}
let changed = &accessibility != self;
Expand Down
124 changes: 64 additions & 60 deletions crates/state/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use freya_native_core::{
AttributeMaskBuilder,
Dependancy,
NodeMaskBuilder,
OwnedAttributeView,
State,
},
tags::TagName,
Expand All @@ -22,6 +23,8 @@ use crate::{
CustomAttributeValues,
HighlightMode,
Parse,
ParseAttribute,
ParseError,
};

#[derive(Clone, Debug, PartialEq, Component)]
Expand Down Expand Up @@ -51,6 +54,66 @@ impl Default for CursorState {
}
}

impl ParseAttribute for CursorState {
fn parse_attribute(
&mut self,
attr: OwnedAttributeView<CustomAttributeValues>,
) -> Result<(), crate::ParseError> {
match attr.attribute {
AttributeName::CursorIndex => {
if let Some(value) = attr.value.as_text() {
if value != "none" {
self.position = Some(value.parse().map_err(|_| ParseError)?);
}
}
}
AttributeName::CursorColor => {
if let Some(value) = attr.value.as_text() {
self.color = Color::parse(value)?;
}
}
AttributeName::CursorMode => {
if let Some(value) = attr.value.as_text() {
self.mode = CursorMode::parse(value)?;
}
}
AttributeName::CursorId => {
if let Some(value) = attr.value.as_text() {
self.cursor_id = Some(value.parse().map_err(|_| ParseError)?);
}
}
AttributeName::Highlights => {
if let Some(CustomAttributeValues::TextHighlights(highlights)) =
attr.value.as_custom()
{
self.highlights = Some(highlights.clone());
}
}
AttributeName::HighlightColor => {
if let Some(value) = attr.value.as_text() {
self.highlight_color = Color::parse(value)?;
}
}
AttributeName::HighlightMode => {
if let Some(value) = attr.value.as_text() {
self.highlight_mode = HighlightMode::parse(value)?;
}
}
AttributeName::CursorReference => {
if let OwnedAttributeValue::Custom(CustomAttributeValues::CursorReference(
reference,
)) = attr.value
{
self.cursor_ref = Some(reference.clone());
}
}
_ => {}
}

Ok(())
}
}

#[partial_derive_state]
impl State<CustomAttributeValues> for CursorState {
type ParentDependencies = (Self,);
Expand Down Expand Up @@ -85,66 +148,7 @@ impl State<CustomAttributeValues> for CursorState {

if let Some(attributes) = node_view.attributes() {
for attr in attributes {
match attr.attribute {
AttributeName::CursorIndex => {
let value = attr.value.as_text().unwrap();
if value != "none" {
let new_cursor_index = value.parse().unwrap();
cursor.position = Some(new_cursor_index);
}
}
AttributeName::CursorColor => {
if let Some(value) = attr.value.as_text() {
if let Ok(color) = Color::parse(value) {
cursor.color = color;
}
}
}
AttributeName::CursorMode => {
if let Some(value) = attr.value.as_text() {
if let Ok(mode) = CursorMode::parse(value) {
cursor.mode = mode;
}
}
}
AttributeName::CursorId => {
if let Some(value) = attr.value.as_text() {
if let Ok(id) = value.parse() {
cursor.cursor_id = Some(id);
}
}
}
AttributeName::Highlights => {
if let Some(CustomAttributeValues::TextHighlights(highlights)) =
attr.value.as_custom()
{
cursor.highlights = Some(highlights.clone());
}
}
AttributeName::HighlightColor => {
if let Some(value) = attr.value.as_text() {
if let Ok(highlight_color) = Color::parse(value) {
cursor.highlight_color = highlight_color;
}
}
}
AttributeName::HighlightMode => {
if let Some(value) = attr.value.as_text() {
if let Ok(highlight_mode) = HighlightMode::parse(value) {
cursor.highlight_mode = highlight_mode;
}
}
}
AttributeName::CursorReference => {
if let OwnedAttributeValue::Custom(
CustomAttributeValues::CursorReference(reference),
) = attr.value
{
cursor.cursor_ref = Some(reference.clone());
}
}
_ => {}
}
cursor.parse_safe(attr);
}
}
let changed = &cursor != self;
Expand Down
Loading