Track active window id in test platform

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2023-04-20 17:20:52 +02:00
parent d9bb37c649
commit 0d5eea8169
4 changed files with 30 additions and 13 deletions

View file

@ -1315,7 +1315,7 @@ impl AppContext {
{ {
self.update(|this| { self.update(|this| {
let window_id = post_inc(&mut this.next_window_id); let window_id = post_inc(&mut this.next_window_id);
let platform_window = this.platform.add_status_item(); let platform_window = this.platform.add_status_item(window_id);
let window = this.build_window(window_id, platform_window, build_root_view); let window = this.build_window(window_id, platform_window, build_root_view);
let root_view = window.root_view().clone().downcast::<V>().unwrap(); let root_view = window.root_view().clone().downcast::<V>().unwrap();

View file

@ -63,7 +63,7 @@ pub trait Platform: Send + Sync {
) -> Box<dyn Window>; ) -> Box<dyn Window>;
fn main_window_id(&self) -> Option<usize>; fn main_window_id(&self) -> Option<usize>;
fn add_status_item(&self) -> Box<dyn Window>; fn add_status_item(&self, id: usize) -> Box<dyn Window>;
fn write_to_clipboard(&self, item: ClipboardItem); fn write_to_clipboard(&self, item: ClipboardItem);
fn read_from_clipboard(&self) -> Option<ClipboardItem>; fn read_from_clipboard(&self) -> Option<ClipboardItem>;

View file

@ -601,7 +601,7 @@ impl platform::Platform for MacPlatform {
Window::main_window_id() Window::main_window_id()
} }
fn add_status_item(&self) -> Box<dyn platform::Window> { fn add_status_item(&self, _id: usize) -> Box<dyn platform::Window> {
Box::new(StatusItem::add(self.fonts())) Box::new(StatusItem::add(self.fonts()))
} }

View file

@ -102,6 +102,7 @@ pub struct Platform {
fonts: Arc<dyn super::FontSystem>, fonts: Arc<dyn super::FontSystem>,
current_clipboard_item: Mutex<Option<ClipboardItem>>, current_clipboard_item: Mutex<Option<ClipboardItem>>,
cursor: Mutex<CursorStyle>, cursor: Mutex<CursorStyle>,
active_window_id: Arc<Mutex<Option<usize>>>,
} }
impl Platform { impl Platform {
@ -111,6 +112,7 @@ impl Platform {
fonts: Arc::new(super::current::FontSystem::new()), fonts: Arc::new(super::current::FontSystem::new()),
current_clipboard_item: Default::default(), current_clipboard_item: Default::default(),
cursor: Mutex::new(CursorStyle::Arrow), cursor: Mutex::new(CursorStyle::Arrow),
active_window_id: Default::default(),
} }
} }
} }
@ -144,22 +146,31 @@ impl super::Platform for Platform {
fn open_window( fn open_window(
&self, &self,
_: usize, id: usize,
options: super::WindowOptions, options: super::WindowOptions,
_executor: Rc<super::executor::Foreground>, _executor: Rc<super::executor::Foreground>,
) -> Box<dyn super::Window> { ) -> Box<dyn super::Window> {
Box::new(Window::new(match options.bounds { *self.active_window_id.lock() = Some(id);
WindowBounds::Maximized | WindowBounds::Fullscreen => vec2f(1024., 768.), Box::new(Window::new(
WindowBounds::Fixed(rect) => rect.size(), id,
})) match options.bounds {
WindowBounds::Maximized | WindowBounds::Fullscreen => vec2f(1024., 768.),
WindowBounds::Fixed(rect) => rect.size(),
},
self.active_window_id.clone(),
))
} }
fn main_window_id(&self) -> Option<usize> { fn main_window_id(&self) -> Option<usize> {
None self.active_window_id.lock().clone()
} }
fn add_status_item(&self) -> Box<dyn crate::platform::Window> { fn add_status_item(&self, id: usize) -> Box<dyn crate::platform::Window> {
Box::new(Window::new(vec2f(24., 24.))) Box::new(Window::new(
id,
vec2f(24., 24.),
self.active_window_id.clone(),
))
} }
fn write_to_clipboard(&self, item: ClipboardItem) { fn write_to_clipboard(&self, item: ClipboardItem) {
@ -245,6 +256,7 @@ impl super::Screen for Screen {
} }
pub struct Window { pub struct Window {
id: usize,
pub(crate) size: Vector2F, pub(crate) size: Vector2F,
scale_factor: f32, scale_factor: f32,
current_scene: Option<crate::Scene>, current_scene: Option<crate::Scene>,
@ -258,11 +270,13 @@ pub struct Window {
pub(crate) title: Option<String>, pub(crate) title: Option<String>,
pub(crate) edited: bool, pub(crate) edited: bool,
pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>, pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,
active_window_id: Arc<Mutex<Option<usize>>>,
} }
impl Window { impl Window {
pub fn new(size: Vector2F) -> Self { pub fn new(id: usize, size: Vector2F, active_window_id: Arc<Mutex<Option<usize>>>) -> Self {
Self { Self {
id,
size, size,
event_handlers: Default::default(), event_handlers: Default::default(),
resize_handlers: Default::default(), resize_handlers: Default::default(),
@ -276,6 +290,7 @@ impl Window {
title: None, title: None,
edited: false, edited: false,
pending_prompts: Default::default(), pending_prompts: Default::default(),
active_window_id,
} }
} }
@ -326,7 +341,9 @@ impl super::Window for Window {
done_rx done_rx
} }
fn activate(&self) {} fn activate(&self) {
*self.active_window_id.lock() = Some(self.id);
}
fn set_title(&mut self, title: &str) { fn set_title(&mut self, title: &str) {
self.title = Some(title.to_string()) self.title = Some(title.to_string())