This commit is contained in:
Nathan Sobo 2023-08-01 20:53:15 -06:00
parent 6f6096238d
commit 1dfde8eab5
15 changed files with 647 additions and 106 deletions

View file

@ -42,7 +42,7 @@ pub use test_app_context::{ContextHandle, TestAppContext};
use window_input_handler::WindowInputHandler;
use crate::{
elements::{AnyElement, AnyRootElement, RootElement},
elements::{AnyElement, AnyRootElement, Empty, RootElement},
executor::{self, Task},
fonts::TextStyle,
json,
@ -53,7 +53,7 @@ use crate::{
},
util::post_inc,
window::{Window, WindowContext},
AssetCache, AssetSource, ClipboardItem, FontCache, MouseRegionId,
AssetCache, AssetSource, ClipboardItem, Element, FontCache, MouseRegionId,
};
use self::ref_counts::RefCounts;
@ -71,10 +71,12 @@ pub trait Entity: 'static {
}
pub trait View: Entity + Sized {
fn ui_name() -> &'static str;
fn render(&mut self, cx: &mut ViewContext<'_, '_, Self>) -> AnyElement<Self>;
fn focus_in(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {}
fn focus_out(&mut self, _: AnyViewHandle, _: &mut ViewContext<Self>) {}
fn ui_name() -> &'static str {
type_name::<Self>()
}
fn key_down(&mut self, _: &KeyDownEvent, _: &mut ViewContext<Self>) -> bool {
false
}
@ -125,6 +127,16 @@ pub trait View: Entity + Sized {
}
}
impl Entity for () {
type Event = ();
}
impl View for () {
fn render(&mut self, _: &mut ViewContext<'_, '_, Self>) -> AnyElement<Self> {
Empty::new().into_any()
}
}
pub trait BorrowAppContext {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T;
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T;
@ -3364,7 +3376,7 @@ impl<V> BorrowWindowContext for ViewContext<'_, '_, V> {
}
}
pub struct LayoutContext<'a, 'b, 'c, V: View> {
pub struct LayoutContext<'a, 'b, 'c, V> {
view_context: &'c mut ViewContext<'a, 'b, V>,
new_parents: &'c mut HashMap<usize, usize>,
views_to_notify_if_ancestors_change: &'c mut HashMap<usize, SmallVec<[usize; 2]>>,

View file

@ -161,6 +161,19 @@ impl TestAppContext {
(window_id, view)
}
pub fn add_window2<T, F>(&mut self, build_root_view: F) -> WindowHandle<T>
where
T: View,
F: FnOnce(&mut ViewContext<T>) -> T,
{
let (window_id, view) = self
.cx
.borrow_mut()
.add_window(Default::default(), build_root_view);
self.simulate_window_activation(Some(window_id));
(window_id, view)
}
pub fn add_view<T, F>(&mut self, window_id: usize, build_view: F) -> ViewHandle<T>
where
T: View,

View file

@ -141,6 +141,12 @@ impl<'de> Deserialize<'de> for Color {
}
}
impl From<u32> for Color {
fn from(value: u32) -> Self {
Self(ColorU::from_u32(value))
}
}
impl ToJson for Color {
fn to_json(&self) -> serde_json::Value {
json!(format!(

View file

@ -201,7 +201,7 @@ pub trait Element<V: View>: 'static {
}
}
trait AnyElementState<V: View> {
trait AnyElementState<V> {
fn layout(
&mut self,
constraint: SizeConstraint,