This commit is contained in:
Antonio Scandurra
2023-10-24 13:10:37 +02:00
parent 953857f8e3
commit 9a53da28bc
9 changed files with 413 additions and 377 deletions
+3 -8
View File
@@ -476,19 +476,14 @@ impl AppContext {
pub fn spawn_on_main<F, R>(
&self,
f: impl FnOnce(&mut MainThread<AppContext>) -> F + Send + 'static,
f: impl FnOnce(MainThread<AsyncAppContext>) -> F + Send + 'static,
) -> Task<R>
where
F: Future<Output = R> + 'static,
R: Send + 'static,
{
let this = self.this.upgrade().unwrap();
self.executor.spawn_on_main(move || {
let cx = &mut *this.lock();
cx.update(|cx| {
f(unsafe { mem::transmute::<&mut AppContext, &mut MainThread<AppContext>>(cx) })
})
})
let cx = self.to_async();
self.executor.spawn_on_main(move || f(MainThread(cx)))
}
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
+32 -3
View File
@@ -1,10 +1,15 @@
use crate::{
AppContext, AsyncAppContext, Context, Effect, EntityId, EventEmitter, Handle, Reference,
Subscription, Task, WeakHandle,
AppContext, AsyncAppContext, Context, Effect, EntityId, EventEmitter, Handle, MainThread,
Reference, Subscription, Task, WeakHandle,
};
use derive_more::{Deref, DerefMut};
use futures::FutureExt;
use std::{any::TypeId, future::Future, marker::PhantomData};
use std::{
any::TypeId,
borrow::{Borrow, BorrowMut},
future::Future,
marker::PhantomData,
};
#[derive(Deref, DerefMut)]
pub struct ModelContext<'a, T> {
@@ -174,6 +179,18 @@ impl<'a, T: Send + Sync + 'static> ModelContext<'a, T> {
let this = self.weak_handle();
self.app.spawn(|cx| f(this, cx))
}
pub fn spawn_on_main<Fut, R>(
&self,
f: impl FnOnce(WeakHandle<T>, MainThread<AsyncAppContext>) -> Fut + Send + 'static,
) -> Task<R>
where
Fut: Future<Output = R> + 'static,
R: Send + 'static,
{
let this = self.weak_handle();
self.app.spawn_on_main(|cx| f(this, cx))
}
}
impl<'a, T: EventEmitter + Send + Sync + 'static> ModelContext<'a, T> {
@@ -204,3 +221,15 @@ impl<'a, T: 'static> Context for ModelContext<'a, T> {
self.app.update_entity(handle, update)
}
}
impl<T> Borrow<AppContext> for ModelContext<'_, T> {
fn borrow(&self) -> &AppContext {
&self.app
}
}
impl<T> BorrowMut<AppContext> for ModelContext<'_, T> {
fn borrow_mut(&mut self) -> &mut AppContext {
&mut self.app
}
}
+10
View File
@@ -203,6 +203,16 @@ impl Hsla {
}
}
// impl From<Hsla> for Rgba {
// fn from(value: Hsla) -> Self {
// let h = value.h;
// let s = value.s;
// let l = value.l;
// let c = (1 - |2L - 1|) X s
// }
// }
impl From<Rgba> for Hsla {
fn from(color: Rgba) -> Self {
let r = color.r;
+44 -3
View File
@@ -1,5 +1,5 @@
use core::fmt::Debug;
use derive_more::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use derive_more::{Add, AddAssign, Div, DivAssign, Mul, Neg, Sub, SubAssign};
use refineable::Refineable;
use serde_derive::{Deserialize, Serialize};
use std::{
@@ -131,7 +131,7 @@ impl<T: Clone + Default + Debug> Clone for Point<T> {
}
}
#[derive(Refineable, Default, Clone, Copy, PartialEq, Div, Hash)]
#[derive(Refineable, Default, Clone, Copy, PartialEq, Div, Hash, Serialize, Deserialize)]
#[refineable(debug)]
#[repr(C)]
pub struct Size<T: Clone + Default + Debug> {
@@ -660,8 +660,9 @@ impl<T> Copy for Corners<T> where T: Copy + Clone + Default + Debug {}
AddAssign,
Sub,
SubAssign,
Div,
Neg,
Div,
DivAssign,
PartialEq,
PartialOrd,
Serialize,
@@ -670,6 +671,34 @@ impl<T> Copy for Corners<T> where T: Copy + Clone + Default + Debug {}
#[repr(transparent)]
pub struct Pixels(pub(crate) f32);
impl std::ops::Div for Pixels {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}
impl std::ops::DivAssign for Pixels {
fn div_assign(&mut self, rhs: Self) {
self.0 /= rhs.0;
}
}
impl std::ops::RemAssign for Pixels {
fn rem_assign(&mut self, rhs: Self) {
self.0 %= rhs.0;
}
}
impl std::ops::Rem for Pixels {
type Output = Self;
fn rem(self, rhs: Self) -> Self {
Self(self.0 % rhs.0)
}
}
impl Mul<f32> for Pixels {
type Output = Pixels;
@@ -703,6 +732,14 @@ impl MulAssign<f32> for Pixels {
impl Pixels {
pub const MAX: Pixels = Pixels(f32::MAX);
pub fn as_usize(&self) -> usize {
self.0 as usize
}
pub fn as_isize(&self) -> isize {
self.0 as isize
}
pub fn floor(&self) -> Self {
Self(self.0.floor())
}
@@ -714,6 +751,10 @@ impl Pixels {
pub fn scale(&self, factor: f32) -> ScaledPixels {
ScaledPixels(self.0 * factor)
}
pub fn pow(&self, exponent: f32) -> Self {
Self(self.0.powf(exponent))
}
}
impl Mul<Pixels> for Pixels {