From cd839b62b761c456b01eabda285e86823ea275bc Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 27 Jan 2025 03:43:49 +0800 Subject: [PATCH] fix Root. --- crates/story/src/main.rs | 2 +- crates/ui/src/input/input.rs | 2 -- crates/ui/src/root.rs | 63 ++++++++++++------------------------ 3 files changed, 21 insertions(+), 46 deletions(-) diff --git a/crates/story/src/main.rs b/crates/story/src/main.rs index ce5de5b2..537da2a1 100644 --- a/crates/story/src/main.rs +++ b/crates/story/src/main.rs @@ -395,7 +395,7 @@ impl StoryWorkspace { }; let window = cx.open_window(options, |window, cx| { - let story_view = cx.new(|cx| Self::new(window, cx)); + let story_view = cx.new(|cx| StoryWorkspace::new(window, cx)); cx.new(|cx| Root::new(story_view.into(), window, cx)) })?; diff --git a/crates/ui/src/input/input.rs b/crates/ui/src/input/input.rs index dd31c596..5b489e5b 100644 --- a/crates/ui/src/input/input.rs +++ b/crates/ui/src/input/input.rs @@ -1097,7 +1097,6 @@ impl TextInput { // Click in the this line but not in the text, move cursor to the end of the line. // The fallback index is saved in Err from `index_for_position` method. index += index_result.unwrap_err(); - println!("------------------ 2"); break; } else if line.len() == 0 { // empty line @@ -1110,7 +1109,6 @@ impl TextInput { break; } } else { - println!("------------------ 3 {}", line.len()); index += line.len(); } diff --git a/crates/ui/src/root.rs b/crates/ui/src/root.rs index 20ebca5f..6440267f 100644 --- a/crates/ui/src/root.rs +++ b/crates/ui/src/root.rs @@ -24,7 +24,7 @@ pub trait ContextModal: Sized { F: Fn(Drawer, &mut Window, &mut App) -> Drawer + 'static; /// Return true, if there is an active Drawer. - fn has_active_drawer(&self, cx: &mut App) -> bool; + fn has_active_drawer(&mut self, cx: &mut App) -> bool; /// Closes the active Drawer. fn close_drawer(&mut self, cx: &mut App); @@ -34,7 +34,7 @@ pub trait ContextModal: Sized { where F: Fn(Modal, &mut Window, &mut App) -> Modal + 'static; /// Return true, if there is an active Modal. - fn has_active_modal(&self, cx: &mut App) -> bool; + fn has_active_modal(&mut self, cx: &mut App) -> bool; /// Closes the last active Modal. fn close_modal(&mut self, cx: &mut App); @@ -46,7 +46,7 @@ pub trait ContextModal: Sized { fn push_notification(&mut self, note: impl Into, cx: &mut App); fn clear_notifications(&mut self, cx: &mut App); /// Returns number of notifications. - fn notifications(&self, cx: &mut App) -> Rc>>; + fn notifications(&mut self, cx: &mut App) -> Rc>>; } impl ContextModal for Window { @@ -78,8 +78,8 @@ impl ContextModal for Window { }) } - fn has_active_drawer(&self, cx: &mut App) -> bool { - Root::read(&self, cx).active_drawer.is_some() + fn has_active_drawer(&mut self, cx: &mut App) -> bool { + Root::read(self, cx).active_drawer.is_some() } fn close_drawer(&mut self, cx: &mut App) { @@ -112,8 +112,8 @@ impl ContextModal for Window { }) } - fn has_active_modal(&self, cx: &mut App) -> bool { - Root::read(&self, cx).active_modals.len() > 0 + fn has_active_modal(&mut self, cx: &mut App) -> bool { + Root::read(self, cx).active_modals.len() > 0 } fn close_modal(&mut self, cx: &mut App) { @@ -156,8 +156,8 @@ impl ContextModal for Window { }) } - fn notifications(&self, cx: &mut App) -> Rc>> { - let entity = Root::read(&self, cx).notification.clone(); + fn notifications(&mut self, cx: &mut App) -> Rc>> { + let entity = Root::read(self, cx).notification.clone(); Rc::new(entity.read(cx).notifications()) } } @@ -262,25 +262,17 @@ impl Root { where F: FnOnce(&mut Self, &mut Window, &mut Context) + 'static, { - let root = window - .window_handle() - .downcast::() - .expect("The window root view should be of type `ui::Root`.") - .root(cx) - .unwrap(); - - root.update(cx, |root, cx| f(root, window, cx)) + if let Some(Some(root)) = window.root_model::() { + root.update(cx, |root, cx| f(root, window, cx)); + } } - pub fn read<'a>(window: &'a Window, cx: &'a mut App) -> &'a Self { - let root = window - .window_handle() - .downcast::() + pub fn read<'a>(window: &'a mut Window, cx: &'a mut App) -> &'a Self { + &window + .root_model::() .expect("The window root view should be of type `ui::Root`.") - .root(cx) - .unwrap(); - - root.read(cx) + .unwrap() + .read(cx) } fn focus_back(&mut self, window: &mut Window, _: &mut App) { @@ -294,12 +286,7 @@ impl Root { window: &mut Window, cx: &mut App, ) -> Option { - let root = window - .window_handle() - .downcast::() - .expect("The window root view should be of type `ui::Root`.") - .root(cx) - .ok()?; + let root = window.root_model::()??; let active_drawer_placement = root.read(cx).active_drawer.clone().map(|d| d.placement); @@ -319,12 +306,7 @@ impl Root { /// Render the Drawer layer. pub fn render_drawer_layer(window: &mut Window, cx: &mut App) -> Option { - let root = window - .window_handle() - .downcast::() - .expect("The window root view should be of type `ui::Root`.") - .root(cx) - .ok()?; + let root = window.root_model::()??; if let Some(active_drawer) = root.read(cx).active_drawer.clone() { let mut drawer = Drawer::new(window, cx); @@ -351,12 +333,7 @@ impl Root { /// Render the Modal layer. pub fn render_modal_layer(window: &mut Window, cx: &mut App) -> Option { - let root = window - .window_handle() - .downcast::() - .expect("The window root view should be of type `ui::Root`.") - .root(cx) - .ok()?; + let root = window.root_model::()??; let active_modals = root.read(cx).active_modals.clone(); let mut has_overlay = false;