This commit is contained in:
Antonio Scandurra 2022-09-08 19:07:11 +02:00
parent c03300df29
commit 2b9fe0a2e6
8 changed files with 89 additions and 51 deletions

View file

@ -116,15 +116,25 @@ impl gpui::View for ScreenCaptureView {
fn render(&mut self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox { fn render(&mut self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox {
let image_buffer = self.image_buffer.clone(); 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() { if let Some(image_buffer) = image_buffer.clone() {
cx.scene.push_surface(Surface { cx.scene.push_surface(Surface {
bounds, bounds,
image_buffer, image_buffer,
}); });
} }
}) });
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() .boxed()
} else {
canvas.boxed()
}
} }
} }

View file

@ -1926,27 +1926,12 @@ impl MutableAppContext {
}) })
} }
// pub fn add_status_bar_item<I, M, F1, F2>( // pub fn add_status_bar_item<I, F>(&mut self, build_item: F)
// &mut self, // where
// build_item: F1,
// build_menu: F2,
// menu_bounds: Vector2F,
// ) where
// I: View, // I: View,
// M: View, // F: FnOnce(&mut ViewContext<I>) -> I,
// F1: FnOnce(&mut ViewContext<I>) -> I,
// F2: FnOnce(&mut ViewContext<M>) -> M,
// { // {
// self.add_window( // mem::forget(self.platform.add_status_item());
// WindowOptions {
// bounds: menu_bounds,
// titlebar: None,
// title: None,
// titlebar_appears_transparent: true,
// traffic_light_position: (),
// },
// build_root_view,
// )
// } // }
pub fn replace_root_view<T, F>(&mut self, window_id: usize, build_root_view: F) -> ViewHandle<T> pub fn replace_root_view<T, F>(&mut self, window_id: usize, build_root_view: F) -> ViewHandle<T>

View file

@ -39,6 +39,11 @@ pub trait Platform: Send + Sync {
fn fonts(&self) -> Arc<dyn FontSystem>; fn fonts(&self) -> Arc<dyn FontSystem>;
fn activate(&self, ignoring_other_apps: bool); 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( fn open_window(
&self, &self,
id: usize, id: usize,
@ -46,10 +51,8 @@ pub trait Platform: Send + Sync {
executor: Rc<executor::Foreground>, executor: Rc<executor::Foreground>,
) -> Box<dyn Window>; ) -> Box<dyn Window>;
fn key_window_id(&self) -> Option<usize>; fn key_window_id(&self) -> Option<usize>;
fn hide(&self);
fn hide_other_apps(&self); fn add_status_item(&self) -> Box<dyn StatusItem>;
fn unhide_other_apps(&self);
fn quit(&self);
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>;
@ -133,6 +136,8 @@ pub trait WindowContext {
fn present_scene(&mut self, scene: Scene); fn present_scene(&mut self, scene: Scene);
} }
pub trait StatusItem {}
#[derive(Debug)] #[derive(Debug)]
pub struct WindowOptions<'a> { pub struct WindowOptions<'a> {
pub bounds: WindowBounds, pub bounds: WindowBounds,

View file

@ -7,6 +7,7 @@ mod image_cache;
mod platform; mod platform;
mod renderer; mod renderer;
mod sprite_cache; mod sprite_cache;
mod status_item;
mod window; mod window;
use cocoa::base::{BOOL, NO, YES}; use cocoa::base::{BOOL, NO, YES};

View file

@ -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::{ use crate::{
executor, keymap, executor, keymap,
platform::{self, CursorStyle}, platform::{self, CursorStyle},
@ -439,23 +441,6 @@ impl platform::Platform for MacPlatform {
} }
} }
fn open_window(
&self,
id: usize,
options: platform::WindowOptions,
executor: Rc<executor::Foreground>,
) -> Box<dyn platform::Window> {
Box::new(Window::open(id, options, executor, self.fonts()))
}
fn key_window_id(&self) -> Option<usize> {
Window::key_window_id()
}
fn fonts(&self) -> Arc<dyn platform::FontSystem> {
self.fonts.clone()
}
fn hide(&self) { fn hide(&self) {
unsafe { unsafe {
let app = NSApplication::sharedApplication(nil); 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<executor::Foreground>,
) -> Box<dyn platform::Window> {
Box::new(Window::open(id, options, executor, self.fonts()))
}
fn key_window_id(&self) -> Option<usize> {
Window::key_window_id()
}
fn add_status_item(&self) -> Box<dyn platform::StatusItem> {
Box::new(StatusItem::add())
}
fn fonts(&self) -> Arc<dyn platform::FontSystem> {
self.fonts.clone()
}
fn write_to_clipboard(&self, item: ClipboardItem) { fn write_to_clipboard(&self, item: ClipboardItem) {
unsafe { unsafe {
self.pasteboard.clearContents(); self.pasteboard.clearContents();

View file

@ -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 {}

View file

@ -120,6 +120,14 @@ impl super::Platform for Platform {
fn activate(&self, _ignoring_other_apps: bool) {} 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( fn open_window(
&self, &self,
_: usize, _: usize,
@ -136,13 +144,9 @@ impl super::Platform for Platform {
None None
} }
fn hide(&self) {} fn add_status_item(&self) -> Box<dyn crate::StatusItem> {
todo!()
fn hide_other_apps(&self) {} }
fn unhide_other_apps(&self) {}
fn quit(&self) {}
fn write_to_clipboard(&self, item: ClipboardItem) { fn write_to_clipboard(&self, item: ClipboardItem) {
*self.current_clipboard_item.lock() = Some(item); *self.current_clipboard_item.lock() = Some(item);

View file

@ -87,6 +87,8 @@ fn main() {
}); });
app.run(move |cx| { app.run(move |cx| {
std::mem::forget(cx.platform().add_status_item());
let client = client::Client::new(http.clone()); let client = client::Client::new(http.clone());
let mut languages = LanguageRegistry::new(login_shell_env_loaded); let mut languages = LanguageRegistry::new(login_shell_env_loaded);
languages.set_language_server_download_dir(zed::paths::LANGUAGES_DIR.clone()); languages.set_language_server_download_dir(zed::paths::LANGUAGES_DIR.clone());