From ad0f75f3869f81ec60edb77769d5ca0be8eec85b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 25 Aug 2022 13:48:10 -0600 Subject: [PATCH] Add capture example to GPUI Added a linker arg to the GPUI build script. Not sure if we'll want to bake this into GPUI or do it via another crate, but this is convenient for exploration for now. --- crates/gpui/Cargo.toml | 6 +++ crates/gpui/build.rs | 3 ++ crates/gpui/examples/capture.rs | 78 +++++++++++++++++++++++++++++++++ crates/gpui/script/capture | 4 ++ 4 files changed, 91 insertions(+) create mode 100644 crates/gpui/examples/capture.rs create mode 100755 crates/gpui/script/capture diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 72c65a4c337ff11fb828f324b5cef5995a6f5e72..255e85a5272c1fae46e4bc81f7e0e169ff065bd1 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -3,6 +3,7 @@ authors = ["Nathan Sobo "] edition = "2021" name = "gpui" version = "0.1.0" +description = "A GPU-accelerated UI framework" [lib] path = "src/gpui.rs" @@ -11,6 +12,11 @@ doctest = false [features] test-support = ["backtrace", "dhat", "env_logger", "collections/test-support"] +[package.metadata.bundle.example.capture] +name = "Capture" +identifier = "rs.gpui.examples.Capture" +description = "An example of screen capture" + [dependencies] collections = { path = "../collections" } gpui_macros = { path = "../gpui_macros" } diff --git a/crates/gpui/build.rs b/crates/gpui/build.rs index 836d586c26bea742d7b31832d0b17b64d0295a05..1c6cc11de0057bf9d12d40bb4f900e2843e493ee 100644 --- a/crates/gpui/build.rs +++ b/crates/gpui/build.rs @@ -9,6 +9,9 @@ fn main() { compile_context_predicate_parser(); compile_metal_shaders(); generate_shader_bindings(); + + // Support screen capture + println!("cargo:rustc-link-lib=framework=ScreenCaptureKit"); } fn generate_dispatch_bindings() { diff --git a/crates/gpui/examples/capture.rs b/crates/gpui/examples/capture.rs new file mode 100644 index 0000000000000000000000000000000000000000..27bb843ab983790cfb2e008b730a5be281043526 --- /dev/null +++ b/crates/gpui/examples/capture.rs @@ -0,0 +1,78 @@ +use std::{slice, str}; + +use block::ConcreteBlock; +use cocoa::{ + base::id, + foundation::{NSString, NSUInteger}, +}; +use gpui::{actions, elements::*, keymap::Binding, Menu, MenuItem}; +use log::LevelFilter; +use objc::{class, msg_send, sel, sel_impl}; +use simplelog::SimpleLogger; + +#[allow(non_upper_case_globals)] +const NSUTF8StringEncoding: NSUInteger = 4; + +actions!(capture, [Quit]); + +fn main() { + SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); + + gpui::App::new(()).unwrap().run(|cx| { + cx.platform().activate(true); + cx.add_global_action(quit); + + cx.add_bindings([Binding::new("cmd-q", Quit, None)]); + cx.set_menus(vec![Menu { + name: "Zed", + items: vec![MenuItem::Action { + name: "Quit", + action: Box::new(Quit), + }], + }]); + + unsafe { + let block = ConcreteBlock::new(move |content: id, error: id| { + println!("got response with shareable content"); + dbg!(content); + dbg!(error); + dbg!(string_from_objc(msg_send![error, localizedDescription])); + }); + + let _: id = msg_send![ + class!(SCShareableContent), + getShareableContentWithCompletionHandler: block + ]; + } + + // cx.add_window(Default::default(), |_| ScreenCaptureView); + }); +} + +struct ScreenCaptureView; + +impl gpui::Entity for ScreenCaptureView { + type Event = (); +} + +impl gpui::View for ScreenCaptureView { + fn ui_name() -> &'static str { + "View" + } + + fn render(&mut self, _: &mut gpui::RenderContext) -> gpui::ElementBox { + Empty::new().boxed() + } +} + +pub unsafe fn string_from_objc(string: id) -> String { + let len = msg_send![string, lengthOfBytesUsingEncoding: NSUTF8StringEncoding]; + let bytes = string.UTF8String() as *const u8; + str::from_utf8(slice::from_raw_parts(bytes, len)) + .unwrap() + .to_string() +} + +fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) { + cx.platform().quit(); +} diff --git a/crates/gpui/script/capture b/crates/gpui/script/capture new file mode 100755 index 0000000000000000000000000000000000000000..753e5108e7f91093686c0b0e481b9b5380dcec4e --- /dev/null +++ b/crates/gpui/script/capture @@ -0,0 +1,4 @@ +#!/bin/bash + +cargo bundle --example capture +open ../../target/debug/examples/bundle/osx/Capture.app