From 2b9fe0a2e6d32f025a093ef142caabcf2baeb117 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 8 Sep 2022 19:07:11 +0200 Subject: [PATCH] WIP --- crates/capture/src/main.rs | 16 ++++++-- crates/gpui/src/app.rs | 23 ++--------- crates/gpui/src/platform.rs | 13 +++++-- crates/gpui/src/platform/mac.rs | 1 + crates/gpui/src/platform/mac/platform.rs | 42 ++++++++++++--------- crates/gpui/src/platform/mac/status_item.rs | 25 ++++++++++++ crates/gpui/src/platform/test.rs | 18 +++++---- crates/zed/src/main.rs | 2 + 8 files changed, 89 insertions(+), 51 deletions(-) create mode 100644 crates/gpui/src/platform/mac/status_item.rs diff --git a/crates/capture/src/main.rs b/crates/capture/src/main.rs index af2749ecd9622319358605a1ed6554b94d242a29..c34f451e41e14ae539259c231c05e2e6684e0408 100644 --- a/crates/capture/src/main.rs +++ b/crates/capture/src/main.rs @@ -116,15 +116,25 @@ impl gpui::View for ScreenCaptureView { fn render(&mut self, _: &mut gpui::RenderContext) -> gpui::ElementBox { let image_buffer = self.image_buffer.clone(); - Canvas::new(move |bounds, _, cx| { + let canvas = Canvas::new(move |bounds, _, cx| { if let Some(image_buffer) = image_buffer.clone() { cx.scene.push_surface(Surface { bounds, image_buffer, }); } - }) - .boxed() + }); + + if let Some(image_buffer) = self.image_buffer.as_ref() { + canvas + .constrained() + .with_width(image_buffer.width() as f32) + .with_height(image_buffer.height() as f32) + .aligned() + .boxed() + } else { + canvas.boxed() + } } } diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 9598540166e624a37a51a2318bd69cf7e8d300f4..4a749b551f4067f6bfd920222b1a17fdf26d4a8f 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -1926,27 +1926,12 @@ impl MutableAppContext { }) } - // pub fn add_status_bar_item( - // &mut self, - // build_item: F1, - // build_menu: F2, - // menu_bounds: Vector2F, - // ) where + // pub fn add_status_bar_item(&mut self, build_item: F) + // where // I: View, - // M: View, - // F1: FnOnce(&mut ViewContext) -> I, - // F2: FnOnce(&mut ViewContext) -> M, + // F: FnOnce(&mut ViewContext) -> I, // { - // self.add_window( - // WindowOptions { - // bounds: menu_bounds, - // titlebar: None, - // title: None, - // titlebar_appears_transparent: true, - // traffic_light_position: (), - // }, - // build_root_view, - // ) + // mem::forget(self.platform.add_status_item()); // } pub fn replace_root_view(&mut self, window_id: usize, build_root_view: F) -> ViewHandle diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index e31cc4621374b6f86d2dcb340e5f61429acf9fa6..ec11ee196a51ef5048f11d7cd77ef6c3c43f872f 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -39,6 +39,11 @@ pub trait Platform: Send + Sync { fn fonts(&self) -> Arc; fn activate(&self, ignoring_other_apps: bool); + fn hide(&self); + fn hide_other_apps(&self); + fn unhide_other_apps(&self); + fn quit(&self); + fn open_window( &self, id: usize, @@ -46,10 +51,8 @@ pub trait Platform: Send + Sync { executor: Rc, ) -> Box; fn key_window_id(&self) -> Option; - fn hide(&self); - fn hide_other_apps(&self); - fn unhide_other_apps(&self); - fn quit(&self); + + fn add_status_item(&self) -> Box; fn write_to_clipboard(&self, item: ClipboardItem); fn read_from_clipboard(&self) -> Option; @@ -133,6 +136,8 @@ pub trait WindowContext { fn present_scene(&mut self, scene: Scene); } +pub trait StatusItem {} + #[derive(Debug)] pub struct WindowOptions<'a> { pub bounds: WindowBounds, diff --git a/crates/gpui/src/platform/mac.rs b/crates/gpui/src/platform/mac.rs index c1a4d45561dae5307a0f628588c37b8977809a61..bd3c4eaf20c96750220a6d10376a79c34663e5af 100644 --- a/crates/gpui/src/platform/mac.rs +++ b/crates/gpui/src/platform/mac.rs @@ -7,6 +7,7 @@ mod image_cache; mod platform; mod renderer; mod sprite_cache; +mod status_item; mod window; use cocoa::base::{BOOL, NO, YES}; diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index 113653478a8a235e47ab597229360dc5952b60cc..cda6521a8a708f3813265d30686fe934d9fdf24f 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -1,4 +1,6 @@ -use super::{event::key_to_native, BoolExt as _, Dispatcher, FontSystem, Window}; +use super::{ + event::key_to_native, status_item::StatusItem, BoolExt as _, Dispatcher, FontSystem, Window, +}; use crate::{ executor, keymap, platform::{self, CursorStyle}, @@ -439,23 +441,6 @@ impl platform::Platform for MacPlatform { } } - fn open_window( - &self, - id: usize, - options: platform::WindowOptions, - executor: Rc, - ) -> Box { - Box::new(Window::open(id, options, executor, self.fonts())) - } - - fn key_window_id(&self) -> Option { - Window::key_window_id() - } - - fn fonts(&self) -> Arc { - self.fonts.clone() - } - fn hide(&self) { unsafe { let app = NSApplication::sharedApplication(nil); @@ -497,6 +482,27 @@ impl platform::Platform for MacPlatform { } } + fn open_window( + &self, + id: usize, + options: platform::WindowOptions, + executor: Rc, + ) -> Box { + Box::new(Window::open(id, options, executor, self.fonts())) + } + + fn key_window_id(&self) -> Option { + Window::key_window_id() + } + + fn add_status_item(&self) -> Box { + Box::new(StatusItem::add()) + } + + fn fonts(&self) -> Arc { + self.fonts.clone() + } + fn write_to_clipboard(&self, item: ClipboardItem) { unsafe { self.pasteboard.clearContents(); diff --git a/crates/gpui/src/platform/mac/status_item.rs b/crates/gpui/src/platform/mac/status_item.rs new file mode 100644 index 0000000000000000000000000000000000000000..6b32d5cb404422df54c9c13adccc00a7598e1e24 --- /dev/null +++ b/crates/gpui/src/platform/mac/status_item.rs @@ -0,0 +1,25 @@ +use cocoa::{ + appkit::{NSSquareStatusItemLength, NSStatusBar}, + base::{id, nil}, +}; +use core_foundation::base::TCFType; +use core_graphics::color::CGColor; +use objc::{msg_send, sel, sel_impl}; + +pub struct StatusItem(id); + +impl StatusItem { + pub fn add() -> Self { + unsafe { + let status_bar = NSStatusBar::systemStatusBar(nil); + let native_item: id = + msg_send![status_bar, statusItemWithLength: NSSquareStatusItemLength]; + let button: id = msg_send![native_item, button]; + let layer: id = msg_send![button, layer]; + let _: () = msg_send![layer, setBackgroundColor: CGColor::rgb(1., 0., 0., 1.).as_concrete_TypeRef()]; + StatusItem(native_item) + } + } +} + +impl crate::StatusItem for StatusItem {} diff --git a/crates/gpui/src/platform/test.rs b/crates/gpui/src/platform/test.rs index 519ba931d10ca59fc1721a26b2566c9a8897b8b2..7fe7aca669a3d4737d99d0307d9f1b036455fd06 100644 --- a/crates/gpui/src/platform/test.rs +++ b/crates/gpui/src/platform/test.rs @@ -120,6 +120,14 @@ impl super::Platform for Platform { fn activate(&self, _ignoring_other_apps: bool) {} + fn hide(&self) {} + + fn hide_other_apps(&self) {} + + fn unhide_other_apps(&self) {} + + fn quit(&self) {} + fn open_window( &self, _: usize, @@ -136,13 +144,9 @@ impl super::Platform for Platform { None } - fn hide(&self) {} - - fn hide_other_apps(&self) {} - - fn unhide_other_apps(&self) {} - - fn quit(&self) {} + fn add_status_item(&self) -> Box { + todo!() + } fn write_to_clipboard(&self, item: ClipboardItem) { *self.current_clipboard_item.lock() = Some(item); diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index fc3616d59287f184d79c4895fda4bd6ce7368ea7..fafc954d2c33d5b803a1c064a72c1191fd69d833 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -87,6 +87,8 @@ fn main() { }); app.run(move |cx| { + std::mem::forget(cx.platform().add_status_item()); + let client = client::Client::new(http.clone()); let mut languages = LanguageRegistry::new(login_shell_env_loaded); languages.set_language_server_download_dir(zed::paths::LANGUAGES_DIR.clone());