Introduce Refinement trait and derive macro

This commit is contained in:
Nathan Sobo 2023-08-18 01:03:46 -06:00
parent 19ccb19c96
commit 9b74dc196e
22 changed files with 6164 additions and 244 deletions

View file

@ -1648,6 +1648,9 @@ impl AppContext {
subscription_id,
callback,
),
Effect::RepaintWindow { window } => {
self.handle_repaint_window_effect(window)
}
}
self.pending_notifications.clear();
} else {
@ -1885,6 +1888,14 @@ impl AppContext {
});
}
fn handle_repaint_window_effect(&mut self, window: AnyWindowHandle) {
self.update_window(window, |cx| {
if let Some(scene) = cx.paint().log_err() {
cx.window.platform_window.present_scene(scene);
}
});
}
fn handle_window_activation_effect(&mut self, window: AnyWindowHandle, active: bool) -> bool {
self.update_window(window, |cx| {
if cx.window.is_active == active {
@ -2244,6 +2255,9 @@ pub enum Effect {
window: AnyWindowHandle,
is_active: bool,
},
RepaintWindow {
window: AnyWindowHandle,
},
WindowActivationObservation {
window: AnyWindowHandle,
subscription_id: usize,
@ -2437,6 +2451,10 @@ impl Debug for Effect {
.debug_struct("Effect::ActiveLabeledTasksObservation")
.field("subscription_id", subscription_id)
.finish(),
Effect::RepaintWindow { window } => f
.debug_struct("Effect::RepaintWindow")
.field("window_id", &window.id())
.finish(),
}
}
}
@ -3617,7 +3635,7 @@ pub struct EventContext<'a, 'b, 'c, V> {
pub(crate) handled: bool,
}
impl<'a, 'b, 'c, V> EventContext<'a, 'b, 'c, V> {
impl<'a, 'b, 'c, V: 'static> EventContext<'a, 'b, 'c, V> {
pub fn new(view_context: &'c mut ViewContext<'a, 'b, V>) -> Self {
EventContext {
view_context,
@ -3628,6 +3646,12 @@ impl<'a, 'b, 'c, V> EventContext<'a, 'b, 'c, V> {
pub fn propagate_event(&mut self) {
self.handled = false;
}
pub fn repaint(&mut self) {
let window = self.window();
self.pending_effects
.push_back(Effect::RepaintWindow { window });
}
}
impl<'a, 'b, 'c, V> Deref for EventContext<'a, 'b, 'c, V> {

View file

@ -252,6 +252,10 @@ impl<'a> WindowContext<'a> {
self.window.platform_window.content_size()
}
pub fn mouse_position(&self) -> Vector2F {
self.window.mouse_position
}
pub fn text_layout_cache(&self) -> &TextLayoutCache {
&self.window.text_layout_cache
}
@ -892,7 +896,7 @@ impl<'a> WindowContext<'a> {
mouse_event,
window_cx,
region.view_id,
)
);
});
}
}
@ -1347,6 +1351,7 @@ pub struct MeasureParams {
pub available_space: Size<AvailableSpace>,
}
#[derive(Clone)]
pub enum AvailableSpace {
/// The amount of space available is the specified number of pixels
Pixels(f32),

View file

@ -11,6 +11,7 @@ pub use font_kit::{
properties::{Properties, Stretch, Style, Weight},
};
use ordered_float::OrderedFloat;
use refineable::Refineable;
use schemars::JsonSchema;
use serde::{de, Deserialize, Serialize};
use serde_json::Value;
@ -59,7 +60,7 @@ pub struct Features {
pub zero: Option<bool>,
}
#[derive(Clone, Debug, JsonSchema)]
#[derive(Clone, Debug, JsonSchema, Refineable)]
pub struct TextStyle {
pub color: Color,
pub font_family_name: Arc<str>,
@ -72,18 +73,6 @@ pub struct TextStyle {
pub soft_wrap: bool,
}
#[derive(Clone, Debug)]
pub struct TextStyleRefinement {
pub color: Option<Color>,
pub font_family_name: Option<Arc<str>>,
pub font_family_id: Option<FamilyId>,
pub font_id: Option<FontId>,
pub font_size: Option<f32>,
pub font_properties: Option<Properties>,
pub underline: Option<Underline>,
pub soft_wrap: Option<bool>,
}
impl TextStyle {
pub fn refine(self, refinement: TextStyleRefinement) -> TextStyle {
TextStyle {

View file

@ -2,6 +2,7 @@ use super::scene::{Path, PathVertex};
use crate::{color::Color, json::ToJson};
pub use pathfinder_geometry::*;
use rect::RectF;
use refineable::Refineable;
use serde::{Deserialize, Deserializer};
use serde_json::json;
use vector::{vec2f, Vector2F};
@ -132,13 +133,22 @@ impl ToJson for RectF {
}
}
#[derive(Clone)]
pub struct Point<T> {
#[derive(Refineable)]
pub struct Point<T: Clone> {
pub x: T,
pub y: T,
}
impl<T> Into<taffy::geometry::Point<T>> for Point<T> {
impl<T: Clone> Clone for Point<T> {
fn clone(&self) -> Self {
Self {
x: self.x.clone(),
y: self.y.clone(),
}
}
}
impl<T: Clone> Into<taffy::geometry::Point<T>> for Point<T> {
fn into(self) -> taffy::geometry::Point<T> {
taffy::geometry::Point {
x: self.x,
@ -147,13 +157,13 @@ impl<T> Into<taffy::geometry::Point<T>> for Point<T> {
}
}
#[derive(Clone)]
pub struct Size<T> {
#[derive(Clone, Refineable)]
pub struct Size<T: Clone> {
pub width: T,
pub height: T,
}
impl<S, T> From<taffy::geometry::Size<S>> for Size<T>
impl<S, T: Clone> From<taffy::geometry::Size<S>> for Size<T>
where
S: Into<T>,
{
@ -165,7 +175,7 @@ where
}
}
impl<S, T> Into<taffy::geometry::Size<S>> for Size<T>
impl<S, T: Clone> Into<taffy::geometry::Size<S>> for Size<T>
where
T: Into<S>,
{
@ -212,8 +222,8 @@ impl Size<Length> {
}
}
#[derive(Clone)]
pub struct Edges<T> {
#[derive(Clone, Default, Refineable)]
pub struct Edges<T: Clone> {
pub top: T,
pub right: T,
pub bottom: T,
@ -292,6 +302,12 @@ impl DefinedLength {
}
}
impl Default for DefinedLength {
fn default() -> Self {
Self::Pixels(0.)
}
}
/// A length that can be defined in pixels, rems, percent of parent, or auto.
#[derive(Clone, Copy)]
pub enum Length {
@ -329,3 +345,9 @@ impl From<DefinedLength> for Length {
Length::Defined(value)
}
}
impl Default for Length {
fn default() -> Self {
Self::Defined(DefinedLength::default())
}
}