handle.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. use super::owned::UseStateOwned;
  2. use std::{
  3. cell::{Ref, RefMut},
  4. fmt::{Debug, Display},
  5. rc::Rc,
  6. };
  7. pub struct UseState<'a, T: 'static>(pub(crate) &'a UseStateOwned<T>);
  8. impl<T> Copy for UseState<'_, T> {}
  9. impl<'a, T: 'static> Clone for UseState<'a, T> {
  10. fn clone(&self) -> Self {
  11. UseState(self.0)
  12. }
  13. }
  14. impl<T: Debug> Debug for UseState<'_, T> {
  15. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  16. write!(f, "{:?}", self.0.current_val)
  17. }
  18. }
  19. impl<'a, T: 'static> UseState<'a, T> {
  20. /// Tell the Dioxus Scheduler that we need to be processed
  21. pub fn needs_update(&self) {
  22. if !self.0.update_scheuled.get() {
  23. self.0.update_scheuled.set(true);
  24. (self.0.update_callback)();
  25. }
  26. }
  27. pub fn set(&self, new_val: T) {
  28. *self.0.wip.borrow_mut() = Some(new_val);
  29. self.needs_update();
  30. }
  31. pub fn get(&self) -> &'a T {
  32. &self.0.current_val
  33. }
  34. pub fn get_rc(&self) -> &'a Rc<T> {
  35. &self.0.current_val
  36. }
  37. /// Get the current status of the work-in-progress data
  38. pub fn get_wip(&self) -> Ref<Option<T>> {
  39. self.0.wip.borrow()
  40. }
  41. /// Get the current status of the work-in-progress data
  42. pub fn get_wip_mut(&self) -> RefMut<Option<T>> {
  43. self.0.wip.borrow_mut()
  44. }
  45. pub fn classic(self) -> (&'a T, Rc<dyn Fn(T)>) {
  46. (&self.0.current_val, self.setter())
  47. }
  48. pub fn setter(&self) -> Rc<dyn Fn(T)> {
  49. let slot = self.0.wip.clone();
  50. let callback = self.0.update_callback.clone();
  51. Rc::new(move |new| {
  52. callback();
  53. *slot.borrow_mut() = Some(new)
  54. })
  55. }
  56. pub fn with(&self, f: impl FnOnce(&mut T)) {
  57. let mut val = self.0.wip.borrow_mut();
  58. if let Some(inner) = val.as_mut() {
  59. f(inner);
  60. }
  61. }
  62. pub fn for_async(&self) -> UseStateOwned<T> {
  63. let UseStateOwned {
  64. current_val,
  65. wip,
  66. update_callback,
  67. update_scheuled,
  68. } = self.0;
  69. UseStateOwned {
  70. current_val: current_val.clone(),
  71. wip: wip.clone(),
  72. update_callback: update_callback.clone(),
  73. update_scheuled: update_scheuled.clone(),
  74. }
  75. }
  76. }
  77. impl<'a, T: 'static + ToOwned<Owned = T>> UseState<'a, T> {
  78. /// Gain mutable access to the new value via [`RefMut`].
  79. ///
  80. /// If `modify` is called, then the component will re-render.
  81. ///
  82. /// This method is only available when the value is a `ToOwned` type.
  83. ///
  84. /// Mutable access is derived by calling "ToOwned" (IE cloning) on the current value.
  85. ///
  86. /// To get a reference to the current value, use `.get()`
  87. pub fn modify(self) -> RefMut<'a, T> {
  88. // make sure we get processed
  89. self.needs_update();
  90. // Bring out the new value, cloning if it we need to
  91. // "get_mut" is locked behind "ToOwned" to make it explicit that cloning occurs to use this
  92. RefMut::map(self.0.wip.borrow_mut(), |slot| {
  93. if slot.is_none() {
  94. *slot = Some(self.0.current_val.as_ref().to_owned());
  95. }
  96. slot.as_mut().unwrap()
  97. })
  98. }
  99. pub fn inner(self) -> T {
  100. self.0.current_val.as_ref().to_owned()
  101. }
  102. }
  103. impl<'a, T> std::ops::Deref for UseState<'a, T> {
  104. type Target = T;
  105. fn deref(&self) -> &Self::Target {
  106. self.get()
  107. }
  108. }
  109. // enable displaty for the handle
  110. impl<'a, T: 'static + Display> std::fmt::Display for UseState<'a, T> {
  111. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  112. write!(f, "{}", self.0.current_val)
  113. }
  114. }
  115. impl<'a, V, T: PartialEq<V>> PartialEq<V> for UseState<'a, T> {
  116. fn eq(&self, other: &V) -> bool {
  117. self.get() == other
  118. }
  119. }
  120. impl<'a, O, T: std::ops::Not<Output = O> + Copy> std::ops::Not for UseState<'a, T> {
  121. type Output = O;
  122. fn not(self) -> Self::Output {
  123. !*self.get()
  124. }
  125. }
  126. /*
  127. Convenience methods for UseState.
  128. Note!
  129. This is not comprehensive.
  130. This is *just* meant to make common operations easier.
  131. */
  132. use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
  133. impl<'a, T: Copy + Add<T, Output = T>> Add<T> for UseState<'a, T> {
  134. type Output = T;
  135. fn add(self, rhs: T) -> Self::Output {
  136. self.0.current_val.add(rhs)
  137. }
  138. }
  139. impl<'a, T: Copy + Add<T, Output = T>> AddAssign<T> for UseState<'a, T> {
  140. fn add_assign(&mut self, rhs: T) {
  141. self.set(self.0.current_val.add(rhs));
  142. }
  143. }
  144. /// Sub
  145. impl<'a, T: Copy + Sub<T, Output = T>> Sub<T> for UseState<'a, T> {
  146. type Output = T;
  147. fn sub(self, rhs: T) -> Self::Output {
  148. self.0.current_val.sub(rhs)
  149. }
  150. }
  151. impl<'a, T: Copy + Sub<T, Output = T>> SubAssign<T> for UseState<'a, T> {
  152. fn sub_assign(&mut self, rhs: T) {
  153. self.set(self.0.current_val.sub(rhs));
  154. }
  155. }
  156. /// MUL
  157. impl<'a, T: Copy + Mul<T, Output = T>> Mul<T> for UseState<'a, T> {
  158. type Output = T;
  159. fn mul(self, rhs: T) -> Self::Output {
  160. self.0.current_val.mul(rhs)
  161. }
  162. }
  163. impl<'a, T: Copy + Mul<T, Output = T>> MulAssign<T> for UseState<'a, T> {
  164. fn mul_assign(&mut self, rhs: T) {
  165. self.set(self.0.current_val.mul(rhs));
  166. }
  167. }
  168. /// DIV
  169. impl<'a, T: Copy + Div<T, Output = T>> Div<T> for UseState<'a, T> {
  170. type Output = T;
  171. fn div(self, rhs: T) -> Self::Output {
  172. self.0.current_val.div(rhs)
  173. }
  174. }
  175. impl<'a, T: Copy + Div<T, Output = T>> DivAssign<T> for UseState<'a, T> {
  176. fn div_assign(&mut self, rhs: T) {
  177. self.set(self.0.current_val.div(rhs));
  178. }
  179. }