|
@@ -6,11 +6,15 @@ use dioxus_native_core::{
|
|
utils::{ElementProduced, PersistantElementIter},
|
|
utils::{ElementProduced, PersistantElementIter},
|
|
Dependancy, NodeId, RealDom, SendAnyMap, State,
|
|
Dependancy, NodeId, RealDom, SendAnyMap, State,
|
|
};
|
|
};
|
|
|
|
+use dioxus_native_core_macro::partial_derive_state;
|
|
|
|
+use shipyard::Component;
|
|
|
|
+use shipyard::{Get, ViewMut};
|
|
|
|
|
|
use std::{cmp::Ordering, num::NonZeroU16};
|
|
use std::{cmp::Ordering, num::NonZeroU16};
|
|
|
|
|
|
use dioxus_native_core::node_ref::NodeView;
|
|
use dioxus_native_core::node_ref::NodeView;
|
|
|
|
|
|
|
|
+#[derive(Component)]
|
|
pub struct Focused(pub bool);
|
|
pub struct Focused(pub bool);
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
@@ -58,11 +62,12 @@ impl Default for FocusLevel {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-#[derive(Clone, PartialEq, Debug, Default)]
|
|
|
|
|
|
+#[derive(Clone, PartialEq, Debug, Default, Component)]
|
|
pub(crate) struct Focus {
|
|
pub(crate) struct Focus {
|
|
pub level: FocusLevel,
|
|
pub level: FocusLevel,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#[partial_derive_state]
|
|
impl State for Focus {
|
|
impl State for Focus {
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new()
|
|
const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new()
|
|
.with_attrs(AttributeMaskBuilder::Some(FOCUS_ATTRIBUTES))
|
|
.with_attrs(AttributeMaskBuilder::Some(FOCUS_ATTRIBUTES))
|
|
@@ -77,7 +82,7 @@ impl State for Focus {
|
|
node_view: NodeView,
|
|
node_view: NodeView,
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
_: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
|
_: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
|
- _: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
|
|
|
|
|
|
+ _: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
|
_: &SendAnyMap,
|
|
_: &SendAnyMap,
|
|
) -> bool {
|
|
) -> bool {
|
|
let new = Focus {
|
|
let new = Focus {
|
|
@@ -126,7 +131,7 @@ impl State for Focus {
|
|
node_view: NodeView<()>,
|
|
node_view: NodeView<()>,
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
|
parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
|
|
- children: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
|
|
|
|
|
|
+ children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
|
|
context: &SendAnyMap,
|
|
context: &SendAnyMap,
|
|
) -> Self {
|
|
) -> Self {
|
|
let mut myself = Self::default();
|
|
let mut myself = Self::default();
|
|
@@ -159,7 +164,7 @@ impl FocusState {
|
|
/// Returns true if the focus has changed.
|
|
/// Returns true if the focus has changed.
|
|
pub fn progress(&mut self, rdom: &mut RealDom, forward: bool) -> bool {
|
|
pub fn progress(&mut self, rdom: &mut RealDom, forward: bool) -> bool {
|
|
if let Some(last) = self.last_focused_id {
|
|
if let Some(last) = self.last_focused_id {
|
|
- if rdom.get(last).unwrap().get::<PreventDefault>().copied()
|
|
|
|
|
|
+ if rdom.get(last).unwrap().get::<PreventDefault>().map(|p| *p)
|
|
== Some(PreventDefault::KeyDown)
|
|
== Some(PreventDefault::KeyDown)
|
|
{
|
|
{
|
|
return false;
|
|
return false;
|
|
@@ -257,7 +262,8 @@ impl FocusState {
|
|
}
|
|
}
|
|
node.insert(Focused(true));
|
|
node.insert(Focused(true));
|
|
if let Some(old) = self.last_focused_id.replace(id) {
|
|
if let Some(old) = self.last_focused_id.replace(id) {
|
|
- let focused = rdom.get_state_mut_raw::<Focused>(old).unwrap();
|
|
|
|
|
|
+ let mut focused_borrow: ViewMut<Focused> = rdom.raw_world().borrow().unwrap();
|
|
|
|
+ let focused = (&mut focused_borrow).get(old).unwrap();
|
|
focused.0 = false;
|
|
focused.0 = false;
|
|
}
|
|
}
|
|
// reset the position to the currently focused element
|
|
// reset the position to the currently focused element
|