This commit is contained in:
Nathan Sobo
2023-04-11 21:56:37 -06:00
parent e6cc132b19
commit d9e4136b02
14 changed files with 235 additions and 150 deletions
+36
View File
@@ -802,6 +802,14 @@ impl AppContext {
.is_some()
}
pub fn window_is_active(&self, window_id: usize) -> bool {
self.windows.get(&window_id).map_or(false, |w| w.is_active)
}
pub fn root_view(&self, window_id: usize) -> Option<&AnyViewHandle> {
self.windows.get(&window_id).map(|w| w.root_view())
}
pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
self.windows.keys().copied()
}
@@ -1648,6 +1656,18 @@ impl AppContext {
window
}
pub fn replace_root_view<V, F>(
&mut self,
window_id: usize,
build_root_view: F,
) -> Option<ViewHandle<V>>
where
V: View,
F: FnOnce(&mut ViewContext<V>) -> V,
{
self.update_window(window_id, |cx| cx.replace_root_view(build_root_view))
}
pub fn add_view<S, F>(&mut self, parent: &AnyViewHandle, build_view: F) -> ViewHandle<S>
where
S: View,
@@ -3326,6 +3346,22 @@ impl<'a, 'b, 'c, V: View> ViewContext<'a, 'b, 'c, V> {
self.window.focused_view_id == Some(self.view_id)
}
pub fn is_parent_view_focused(&self) -> bool {
if let Some(parent_view_id) = self.ancestors(self.window_id, self.view_id).next().clone() {
self.focused_view_id() == Some(parent_view_id)
} else {
false
}
}
pub fn focus_parent_view(&mut self) {
let next = self.ancestors(self.window_id, self.view_id).next().clone();
if let Some(parent_view_id) = next {
let window_id = self.window_id;
self.window_context.focus(window_id, Some(parent_view_id));
}
}
pub fn is_child(&self, view: impl Into<AnyViewHandle>) -> bool {
let view = view.into();
if self.window_id != view.window_id {
+40 -16
View File
@@ -12,8 +12,9 @@ use crate::{
},
text_layout::TextLayoutCache,
util::post_inc,
AnyView, AnyViewHandle, AppContext, Element, ElementBox, MouseRegion, MouseRegionId, ParentId,
RenderParams, SceneBuilder, View, ViewContext, ViewHandle, WindowInvalidation,
AnyView, AnyViewHandle, AppContext, Element, ElementBox, Entity, ModelContext, ModelHandle,
MouseRegion, MouseRegionId, ParentId, ReadView, RenderParams, SceneBuilder, UpdateModel, View,
ViewContext, ViewHandle, WindowInvalidation,
};
use anyhow::bail;
use collections::{HashMap, HashSet};
@@ -119,6 +120,22 @@ impl DerefMut for WindowContext<'_, '_> {
}
}
impl UpdateModel for WindowContext<'_, '_> {
fn update_model<M: Entity, R>(
&mut self,
handle: &ModelHandle<M>,
update: &mut dyn FnMut(&mut M, &mut ModelContext<M>) -> R,
) -> R {
self.app_context.update_model(handle, update)
}
}
impl ReadView for WindowContext<'_, '_> {
fn read_view<W: View>(&self, handle: &crate::ViewHandle<W>) -> &W {
self.app_context.read_view(handle)
}
}
impl<'a: 'b, 'b> WindowContext<'a, 'b> {
pub fn new(app_context: &'a mut AppContext, window: &'b mut Window, window_id: usize) -> Self {
Self {
@@ -133,6 +150,14 @@ impl<'a: 'b, 'b> WindowContext<'a, 'b> {
self.window_id
}
pub fn app_context(&mut self) -> &mut AppContext {
self.app_context
}
pub fn root_view(&self) -> &AnyViewHandle {
self.window.root_view()
}
pub fn window_size(&self) -> Vector2F {
self.window.platform_window.content_size()
}
@@ -701,23 +726,22 @@ impl<'a: 'b, 'b> WindowContext<'a, 'b> {
}
pub fn rect_for_text_range(&self, range_utf16: Range<usize>) -> Option<RectF> {
todo!()
let root_view_id = self.window.root_view().id();
self.window
.rendered_views
.get(&root_view_id)?
.rect_for_text_range(range_utf16, self, root_view_id)
}
pub fn debug_elements(&self) -> Option<json::Value> {
todo!()
// let view = self.root_view()?;
// Some(json!({
// "root_view": view.debug_json(self),
// "root_element": self.window.rendered_views.get(&view.id())
// .map(|root_element| {
// root_element.debug(&DebugContext {
// rendered_views: &self.window.rendered_views,
// font_cache: &self.window.font_cache,
// app: self,
// })
// })
// }))
let view = self.window.root_view();
Some(json!({
"root_view": view.debug_json(self),
"root_element": self.window.rendered_views.get(&view.id())
.map(|root_element| {
root_element.debug(self, view.id())
})
}))
}
pub fn set_window_title(&mut self, title: &str) {
+7 -1
View File
@@ -11,7 +11,7 @@ use collections::HashMap;
use pathfinder_geometry::rect::RectF;
use smallvec::SmallVec;
use crate::{platform::MouseButton, window::WindowContext, View, ViewContext};
use crate::{platform::MouseButton, window::WindowContext, ReadView, View, ViewContext};
use super::{
mouse_event::{
@@ -234,6 +234,12 @@ impl<V: View> DerefMut for EventContext<'_, '_, '_, '_, V> {
}
}
impl<V: View> ReadView for EventContext<'_, '_, '_, '_, V> {
fn read_view<W: View>(&self, handle: &crate::ViewHandle<W>) -> &W {
self.view_context.read_view(handle)
}
}
pub type HandlerCallback = Rc<dyn Fn(MouseEvent, &mut dyn Any, &mut WindowContext, usize) -> bool>;
#[derive(Clone, PartialEq, Eq, Hash)]