Pass PaintContext to Element::paint

I want to use this on another branch, but it's a sweeping change,
so this prepares the ground for it. This can always be reverted if
it doesn't work out.
This commit is contained in:
Nathan Sobo 2023-07-25 17:32:31 -06:00
parent cc23360bab
commit 1b03c5d69c
32 changed files with 223 additions and 80 deletions

View file

@ -44,6 +44,7 @@ use window_input_handler::WindowInputHandler;
use crate::{
elements::{AnyElement, AnyRootElement, RootElement},
executor::{self, Task},
fonts::TextStyle,
json,
keymap_matcher::{self, Binding, KeymapContext, KeymapMatcher, Keystroke, MatchResult},
platform::{
@ -3363,6 +3364,7 @@ pub struct LayoutContext<'a, 'b, 'c, V: View> {
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]>>,
text_style_stack: Vec<Arc<TextStyle>>,
pub refreshing: bool,
}
@ -3377,6 +3379,7 @@ impl<'a, 'b, 'c, V: View> LayoutContext<'a, 'b, 'c, V> {
view_context,
new_parents,
views_to_notify_if_ancestors_change,
text_style_stack: Vec::new(),
refreshing,
}
}
@ -3428,6 +3431,24 @@ impl<'a, 'b, 'c, V: View> LayoutContext<'a, 'b, 'c, V> {
.or_default()
.push(self_view_id);
}
pub fn text_style(&self) -> Arc<TextStyle> {
self.text_style_stack
.last()
.cloned()
.unwrap_or(Default::default())
}
pub fn with_text_style<S, F, T>(&mut self, style: S, f: F) -> T
where
S: Into<Arc<TextStyle>>,
F: FnOnce(&mut Self) -> T,
{
self.text_style_stack.push(style.into());
let result = f(self);
self.text_style_stack.pop();
result
}
}
impl<'a, 'b, 'c, V: View> Deref for LayoutContext<'a, 'b, 'c, V> {
@ -3464,6 +3485,72 @@ impl<V: View> BorrowWindowContext for LayoutContext<'_, '_, '_, V> {
}
}
pub struct PaintContext<'a, 'b, 'c, V: View> {
view_context: &'c mut ViewContext<'a, 'b, V>,
text_style_stack: Vec<Arc<TextStyle>>,
}
impl<'a, 'b, 'c, V: View> PaintContext<'a, 'b, 'c, V> {
pub fn new(view_context: &'c mut ViewContext<'a, 'b, V>) -> Self {
Self {
view_context,
text_style_stack: Vec::new(),
}
}
pub fn text_style(&self) -> Arc<TextStyle> {
self.text_style_stack
.last()
.cloned()
.unwrap_or(Default::default())
}
pub fn with_text_style<S, F, T>(&mut self, style: S, f: F) -> T
where
S: Into<Arc<TextStyle>>,
F: FnOnce(&mut Self) -> T,
{
self.text_style_stack.push(style.into());
let result = f(self);
self.text_style_stack.pop();
result
}
}
impl<'a, 'b, 'c, V: View> Deref for PaintContext<'a, 'b, 'c, V> {
type Target = ViewContext<'a, 'b, V>;
fn deref(&self) -> &Self::Target {
&self.view_context
}
}
impl<V: View> DerefMut for PaintContext<'_, '_, '_, V> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.view_context
}
}
impl<V: View> BorrowAppContext for PaintContext<'_, '_, '_, V> {
fn read_with<T, F: FnOnce(&AppContext) -> T>(&self, f: F) -> T {
BorrowAppContext::read_with(&*self.view_context, f)
}
fn update<T, F: FnOnce(&mut AppContext) -> T>(&mut self, f: F) -> T {
BorrowAppContext::update(&mut *self.view_context, f)
}
}
impl<V: View> BorrowWindowContext for PaintContext<'_, '_, '_, V> {
fn read_with<T, F: FnOnce(&WindowContext) -> T>(&self, window_id: usize, f: F) -> T {
BorrowWindowContext::read_with(&*self.view_context, window_id, f)
}
fn update<T, F: FnOnce(&mut WindowContext) -> T>(&mut self, window_id: usize, f: F) -> T {
BorrowWindowContext::update(&mut *self.view_context, window_id, f)
}
}
pub struct EventContext<'a, 'b, 'c, V: View> {
view_context: &'c mut ViewContext<'a, 'b, V>,
pub(crate) handled: bool,