Merge remote-tracking branch 'origin' into divs
This commit is contained in:
commit
81957c49d5
177 changed files with 11085 additions and 3836 deletions
|
@ -3299,15 +3299,15 @@ impl<'a, 'b, V: 'static> ViewContext<'a, 'b, V> {
|
|||
let region_id = MouseRegionId::new(tag, self.view_id, region_id);
|
||||
MouseState {
|
||||
hovered: self.window.hovered_region_ids.contains(®ion_id),
|
||||
clicked: if let Some((clicked_region_id, button)) = self.window.clicked_region {
|
||||
if region_id == clicked_region_id {
|
||||
Some(button)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
},
|
||||
mouse_down: !self.window.clicked_region_ids.is_empty(),
|
||||
clicked: self
|
||||
.window
|
||||
.clicked_region_ids
|
||||
.iter()
|
||||
.find(|click_region_id| **click_region_id == region_id)
|
||||
// If we've gotten here, there should always be a clicked region.
|
||||
// But let's be defensive and return None if there isn't.
|
||||
.and_then(|_| self.window.clicked_region.map(|(_, button)| button)),
|
||||
accessed_hovered: false,
|
||||
accessed_clicked: false,
|
||||
}
|
||||
|
@ -3798,14 +3798,20 @@ impl<'a, T> DerefMut for Reference<'a, T> {
|
|||
pub struct MouseState {
|
||||
pub(crate) hovered: bool,
|
||||
pub(crate) clicked: Option<MouseButton>,
|
||||
pub(crate) mouse_down: bool,
|
||||
pub(crate) accessed_hovered: bool,
|
||||
pub(crate) accessed_clicked: bool,
|
||||
}
|
||||
|
||||
impl MouseState {
|
||||
pub fn dragging(&mut self) -> bool {
|
||||
self.accessed_hovered = true;
|
||||
self.hovered && self.mouse_down
|
||||
}
|
||||
|
||||
pub fn hovered(&mut self) -> bool {
|
||||
self.accessed_hovered = true;
|
||||
self.hovered
|
||||
self.hovered && (!self.mouse_down || self.clicked.is_some())
|
||||
}
|
||||
|
||||
pub fn clicked(&mut self) -> Option<MouseButton> {
|
||||
|
@ -4656,12 +4662,13 @@ impl AnyWeakModelHandle {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Copy)]
|
||||
pub struct WeakViewHandle<T> {
|
||||
any_handle: AnyWeakViewHandle,
|
||||
view_type: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T> Copy for WeakViewHandle<T> {}
|
||||
|
||||
impl<T> Debug for WeakViewHandle<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct(&format!("WeakViewHandle<{}>", type_name::<T>()))
|
||||
|
|
|
@ -624,10 +624,11 @@ impl<'a> WindowContext<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
if self
|
||||
.window
|
||||
.platform_window
|
||||
.is_topmost_for_position(*position)
|
||||
if pressed_button.is_none()
|
||||
&& self
|
||||
.window
|
||||
.platform_window
|
||||
.is_topmost_for_position(*position)
|
||||
{
|
||||
self.platform().set_cursor_style(style_to_assign);
|
||||
}
|
||||
|
@ -791,6 +792,11 @@ impl<'a> WindowContext<'a> {
|
|||
if clicked_region_ids.contains(&mouse_region.id()) {
|
||||
if mouse_region.bounds.contains_point(self.mouse_position()) {
|
||||
valid_regions.push(mouse_region.clone());
|
||||
} else {
|
||||
// Let the view know that it hasn't been clicked anymore
|
||||
if mouse_region.notify_on_click {
|
||||
notified_views.insert(mouse_region.id().view_id());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -234,6 +234,27 @@ pub trait Element<V: 'static>: 'static {
|
|||
{
|
||||
MouseEventHandler::for_child::<Tag>(self.into_any(), region_id)
|
||||
}
|
||||
|
||||
fn component(self) -> StatelessElementAdapter
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
StatelessElementAdapter::new(self.into_any())
|
||||
}
|
||||
|
||||
fn stateful_component(self) -> StatefulElementAdapter<V>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
StatefulElementAdapter::new(self.into_any())
|
||||
}
|
||||
|
||||
fn styleable_component(self) -> StylableAdapter<StatelessElementAdapter>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
StatelessElementAdapter::new(self.into_any()).stylable()
|
||||
}
|
||||
}
|
||||
|
||||
trait AnyElementState<V> {
|
||||
|
|
|
@ -1,79 +1,81 @@
|
|||
use std::marker::PhantomData;
|
||||
use std::{any::Any, marker::PhantomData};
|
||||
|
||||
use pathfinder_geometry::{rect::RectF, vector::Vector2F};
|
||||
|
||||
use crate::{
|
||||
AnyElement, Element, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, View,
|
||||
ViewContext,
|
||||
AnyElement, Element, LayoutContext, PaintContext, SceneBuilder, SizeConstraint, ViewContext,
|
||||
};
|
||||
|
||||
use super::Empty;
|
||||
|
||||
pub trait GeneralComponent {
|
||||
fn render<V: View>(self, v: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;
|
||||
fn element<V: View>(self) -> ComponentAdapter<V, Self>
|
||||
/// The core stateless component trait, simply rendering an element tree
|
||||
pub trait Component {
|
||||
fn render<V: 'static>(self, cx: &mut ViewContext<V>) -> AnyElement<V>;
|
||||
|
||||
fn element<V: 'static>(self) -> ComponentAdapter<V, Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
ComponentAdapter::new(self)
|
||||
}
|
||||
|
||||
fn stylable(self) -> StylableAdapter<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
StylableAdapter::new(self)
|
||||
}
|
||||
|
||||
fn stateful<V: 'static>(self) -> StatefulAdapter<Self, V>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
StatefulAdapter::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait StyleableComponent {
|
||||
/// Allows a a component's styles to be rebound in a simple way.
|
||||
pub trait Stylable: Component {
|
||||
type Style: Clone;
|
||||
type Output: GeneralComponent;
|
||||
|
||||
fn with_style(self, style: Self::Style) -> Self;
|
||||
}
|
||||
|
||||
/// This trait models the typestate pattern for a component's style,
|
||||
/// enforcing at compile time that a component is only usable after
|
||||
/// it has been styled while still allowing for late binding of the
|
||||
/// styling information
|
||||
pub trait SafeStylable {
|
||||
type Style: Clone;
|
||||
type Output: Component;
|
||||
|
||||
fn with_style(self, style: Self::Style) -> Self::Output;
|
||||
}
|
||||
|
||||
impl GeneralComponent for () {
|
||||
fn render<V: View>(self, _: &mut V, _: &mut ViewContext<V>) -> AnyElement<V> {
|
||||
Empty::new().into_any()
|
||||
/// All stylable components can trivially implement SafeStylable
|
||||
impl<C: Stylable> SafeStylable for C {
|
||||
type Style = C::Style;
|
||||
|
||||
type Output = C;
|
||||
|
||||
fn with_style(self, style: Self::Style) -> Self::Output {
|
||||
self.with_style(style)
|
||||
}
|
||||
}
|
||||
|
||||
impl StyleableComponent for () {
|
||||
type Style = ();
|
||||
type Output = ();
|
||||
|
||||
fn with_style(self, _: Self::Style) -> Self::Output {
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Component<V: View> {
|
||||
fn render(self, v: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;
|
||||
|
||||
fn element(self) -> ComponentAdapter<V, Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
ComponentAdapter::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: View, C: GeneralComponent> Component<V> for C {
|
||||
fn render(self, v: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V> {
|
||||
self.render(v, cx)
|
||||
}
|
||||
}
|
||||
|
||||
// StylableComponent -> GeneralComponent
|
||||
pub struct StylableComponentAdapter<C: Component<V>, V: View> {
|
||||
/// Allows converting an unstylable component into a stylable one
|
||||
/// by using `()` as the style type
|
||||
pub struct StylableAdapter<C: Component> {
|
||||
component: C,
|
||||
phantom: std::marker::PhantomData<V>,
|
||||
}
|
||||
|
||||
impl<C: Component<V>, V: View> StylableComponentAdapter<C, V> {
|
||||
impl<C: Component> StylableAdapter<C> {
|
||||
pub fn new(component: C) -> Self {
|
||||
Self {
|
||||
component,
|
||||
phantom: std::marker::PhantomData,
|
||||
}
|
||||
Self { component }
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: GeneralComponent, V: View> StyleableComponent for StylableComponentAdapter<C, V> {
|
||||
impl<C: Component> SafeStylable for StylableAdapter<C> {
|
||||
type Style = ();
|
||||
|
||||
type Output = C;
|
||||
|
@ -83,13 +85,150 @@ impl<C: GeneralComponent, V: View> StyleableComponent for StylableComponentAdapt
|
|||
}
|
||||
}
|
||||
|
||||
// Element -> Component
|
||||
pub struct ElementAdapter<V: View> {
|
||||
/// This is a secondary trait for components that can be styled
|
||||
/// which rely on their view's state. This is useful for components that, for example,
|
||||
/// want to take click handler callbacks Unfortunately, the generic bound on the
|
||||
/// Component trait makes it incompatible with the stateless components above.
|
||||
// So let's just replicate them for now
|
||||
pub trait StatefulComponent<V: 'static> {
|
||||
fn render(self, v: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V>;
|
||||
|
||||
fn element(self) -> ComponentAdapter<V, Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
ComponentAdapter::new(self)
|
||||
}
|
||||
|
||||
fn styleable(self) -> StatefulStylableAdapter<Self, V>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
StatefulStylableAdapter::new(self)
|
||||
}
|
||||
|
||||
fn stateless(self) -> StatelessElementAdapter
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
StatelessElementAdapter::new(self.element().into_any())
|
||||
}
|
||||
}
|
||||
|
||||
/// It is trivial to convert stateless components to stateful components, so lets
|
||||
/// do so en masse. Note that the reverse is impossible without a helper.
|
||||
impl<V: 'static, C: Component> StatefulComponent<V> for C {
|
||||
fn render(self, _: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V> {
|
||||
self.render(cx)
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as stylable, but generic over a view type
|
||||
pub trait StatefulStylable<V: 'static>: StatefulComponent<V> {
|
||||
type Style: Clone;
|
||||
|
||||
fn with_style(self, style: Self::Style) -> Self;
|
||||
}
|
||||
|
||||
/// Same as SafeStylable, but generic over a view type
|
||||
pub trait StatefulSafeStylable<V: 'static> {
|
||||
type Style: Clone;
|
||||
type Output: StatefulComponent<V>;
|
||||
|
||||
fn with_style(self, style: Self::Style) -> Self::Output;
|
||||
}
|
||||
|
||||
/// Converting from stateless to stateful
|
||||
impl<V: 'static, C: SafeStylable> StatefulSafeStylable<V> for C {
|
||||
type Style = C::Style;
|
||||
|
||||
type Output = C::Output;
|
||||
|
||||
fn with_style(self, style: Self::Style) -> Self::Output {
|
||||
self.with_style(style)
|
||||
}
|
||||
}
|
||||
|
||||
// A helper for converting stateless components into stateful ones
|
||||
pub struct StatefulAdapter<C, V> {
|
||||
component: C,
|
||||
phantom: std::marker::PhantomData<V>,
|
||||
}
|
||||
|
||||
impl<C: Component, V: 'static> StatefulAdapter<C, V> {
|
||||
pub fn new(component: C) -> Self {
|
||||
Self {
|
||||
component,
|
||||
phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Component, V: 'static> StatefulComponent<V> for StatefulAdapter<C, V> {
|
||||
fn render(self, _: &mut V, cx: &mut ViewContext<V>) -> AnyElement<V> {
|
||||
self.component.render(cx)
|
||||
}
|
||||
}
|
||||
|
||||
// A helper for converting stateful but style-less components into stylable ones
|
||||
// by using `()` as the style type
|
||||
pub struct StatefulStylableAdapter<C: StatefulComponent<V>, V: 'static> {
|
||||
component: C,
|
||||
phantom: std::marker::PhantomData<V>,
|
||||
}
|
||||
|
||||
impl<C: StatefulComponent<V>, V: 'static> StatefulStylableAdapter<C, V> {
|
||||
pub fn new(component: C) -> Self {
|
||||
Self {
|
||||
component,
|
||||
phantom: std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: StatefulComponent<V>, V: 'static> StatefulSafeStylable<V>
|
||||
for StatefulStylableAdapter<C, V>
|
||||
{
|
||||
type Style = ();
|
||||
|
||||
type Output = C;
|
||||
|
||||
fn with_style(self, _: Self::Style) -> Self::Output {
|
||||
self.component
|
||||
}
|
||||
}
|
||||
|
||||
/// A way of erasing the view generic from an element, useful
|
||||
/// for wrapping up an explicit element tree into stateless
|
||||
/// components
|
||||
pub struct StatelessElementAdapter {
|
||||
element: Box<dyn Any>,
|
||||
}
|
||||
|
||||
impl StatelessElementAdapter {
|
||||
pub fn new<V: 'static>(element: AnyElement<V>) -> Self {
|
||||
StatelessElementAdapter {
|
||||
element: Box::new(element) as Box<dyn Any>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for StatelessElementAdapter {
|
||||
fn render<V: 'static>(self, _: &mut ViewContext<V>) -> AnyElement<V> {
|
||||
*self
|
||||
.element
|
||||
.downcast::<AnyElement<V>>()
|
||||
.expect("Don't move elements out of their view :(")
|
||||
}
|
||||
}
|
||||
|
||||
// For converting elements into stateful components
|
||||
pub struct StatefulElementAdapter<V: 'static> {
|
||||
element: AnyElement<V>,
|
||||
_phantom: std::marker::PhantomData<V>,
|
||||
}
|
||||
|
||||
impl<V: View> ElementAdapter<V> {
|
||||
impl<V: 'static> StatefulElementAdapter<V> {
|
||||
pub fn new(element: AnyElement<V>) -> Self {
|
||||
Self {
|
||||
element,
|
||||
|
@ -98,20 +237,35 @@ impl<V: View> ElementAdapter<V> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<V: View> Component<V> for ElementAdapter<V> {
|
||||
impl<V: 'static> StatefulComponent<V> for StatefulElementAdapter<V> {
|
||||
fn render(self, _: &mut V, _: &mut ViewContext<V>) -> AnyElement<V> {
|
||||
self.element
|
||||
}
|
||||
}
|
||||
|
||||
// Component -> Element
|
||||
pub struct ComponentAdapter<V: View, E> {
|
||||
/// A convenient shorthand for creating an empty component.
|
||||
impl Component for () {
|
||||
fn render<V: 'static>(self, _: &mut ViewContext<V>) -> AnyElement<V> {
|
||||
Empty::new().into_any()
|
||||
}
|
||||
}
|
||||
|
||||
impl Stylable for () {
|
||||
type Style = ();
|
||||
|
||||
fn with_style(self, _: Self::Style) -> Self {
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
// For converting components back into Elements
|
||||
pub struct ComponentAdapter<V: 'static, E> {
|
||||
component: Option<E>,
|
||||
element: Option<AnyElement<V>>,
|
||||
phantom: PhantomData<V>,
|
||||
}
|
||||
|
||||
impl<E, V: View> ComponentAdapter<V, E> {
|
||||
impl<E, V: 'static> ComponentAdapter<V, E> {
|
||||
pub fn new(e: E) -> Self {
|
||||
Self {
|
||||
component: Some(e),
|
||||
|
@ -121,7 +275,7 @@ impl<E, V: View> ComponentAdapter<V, E> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<V: View, C: Component<V> + 'static> Element<V> for ComponentAdapter<V, C> {
|
||||
impl<V: 'static, C: StatefulComponent<V> + 'static> Element<V> for ComponentAdapter<V, C> {
|
||||
type LayoutState = ();
|
||||
|
||||
type PaintState = ();
|
||||
|
@ -184,6 +338,7 @@ impl<V: View, C: Component<V> + 'static> Element<V> for ComponentAdapter<V, C> {
|
|||
) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"type": "ComponentAdapter",
|
||||
"component": std::any::type_name::<C>(),
|
||||
"child": self.element.as_ref().map(|el| el.debug(view, cx)),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -44,6 +44,14 @@ impl ContainerStyle {
|
|||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn additional_length(&self) -> f32 {
|
||||
self.padding.left
|
||||
+ self.padding.right
|
||||
+ self.border.width * 2.
|
||||
+ self.margin.left
|
||||
+ self.margin.right
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Container<V> {
|
||||
|
|
|
@ -22,6 +22,7 @@ pub struct Flex<V> {
|
|||
children: Vec<AnyElement<V>>,
|
||||
scroll_state: Option<(ElementStateHandle<Rc<ScrollState>>, usize)>,
|
||||
child_alignment: f32,
|
||||
spacing: f32,
|
||||
}
|
||||
|
||||
impl<V: 'static> Flex<V> {
|
||||
|
@ -31,6 +32,7 @@ impl<V: 'static> Flex<V> {
|
|||
children: Default::default(),
|
||||
scroll_state: None,
|
||||
child_alignment: -1.,
|
||||
spacing: 0.,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -51,6 +53,11 @@ impl<V: 'static> Flex<V> {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn with_spacing(mut self, spacing: f32) -> Self {
|
||||
self.spacing = spacing;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn scrollable<Tag>(
|
||||
mut self,
|
||||
element_id: usize,
|
||||
|
@ -81,7 +88,7 @@ impl<V: 'static> Flex<V> {
|
|||
cx: &mut LayoutContext<V>,
|
||||
) {
|
||||
let cross_axis = self.axis.invert();
|
||||
for child in &mut self.children {
|
||||
for child in self.children.iter_mut() {
|
||||
if let Some(metadata) = child.metadata::<FlexParentData>() {
|
||||
if let Some((flex, expanded)) = metadata.flex {
|
||||
if expanded != layout_expanded {
|
||||
|
@ -132,12 +139,12 @@ impl<V: 'static> Element<V> for Flex<V> {
|
|||
cx: &mut LayoutContext<V>,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let mut total_flex = None;
|
||||
let mut fixed_space = 0.0;
|
||||
let mut fixed_space = self.children.len().saturating_sub(1) as f32 * self.spacing;
|
||||
let mut contains_float = false;
|
||||
|
||||
let cross_axis = self.axis.invert();
|
||||
let mut cross_axis_max: f32 = 0.0;
|
||||
for child in &mut self.children {
|
||||
for child in self.children.iter_mut() {
|
||||
let metadata = child.metadata::<FlexParentData>();
|
||||
contains_float |= metadata.map_or(false, |metadata| metadata.float);
|
||||
|
||||
|
@ -315,7 +322,7 @@ impl<V: 'static> Element<V> for Flex<V> {
|
|||
}
|
||||
}
|
||||
|
||||
for child in &mut self.children {
|
||||
for child in self.children.iter_mut() {
|
||||
if remaining_space > 0. {
|
||||
if let Some(metadata) = child.metadata::<FlexParentData>() {
|
||||
if metadata.float {
|
||||
|
@ -354,8 +361,8 @@ impl<V: 'static> Element<V> for Flex<V> {
|
|||
child.paint(scene, aligned_child_origin, visible_bounds, view, cx);
|
||||
|
||||
match self.axis {
|
||||
Axis::Horizontal => child_origin += vec2f(child.size().x(), 0.0),
|
||||
Axis::Vertical => child_origin += vec2f(0.0, child.size().y()),
|
||||
Axis::Horizontal => child_origin += vec2f(child.size().x() + self.spacing, 0.0),
|
||||
Axis::Vertical => child_origin += vec2f(0.0, child.size().y() + self.spacing),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -67,7 +67,9 @@ impl KeymapContextPredicate {
|
|||
}
|
||||
|
||||
pub fn eval(&self, contexts: &[KeymapContext]) -> bool {
|
||||
let Some(context) = contexts.first() else { return false };
|
||||
let Some(context) = contexts.first() else {
|
||||
return false;
|
||||
};
|
||||
match self {
|
||||
Self::Identifier(name) => (&context.set).contains(name.as_str()),
|
||||
Self::Equal(left, right) => context
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue