Rename PlatformLifecycle to MainThreadPlatform

Nathan Sobo and Max Brunsfeld created

Don't love it, but at least it's accurate.

Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>

Change summary

gpui/src/app.rs                   | 34 ++++++++++++++--------------
gpui/src/platform.rs              |  2 
gpui/src/platform/mac.rs          |  6 ++--
gpui/src/platform/mac/platform.rs | 40 ++++++++++++++++----------------
gpui/src/platform/test.rs         | 10 ++++----
5 files changed, 46 insertions(+), 46 deletions(-)

Detailed changes

gpui/src/app.rs 🔗

@@ -112,13 +112,13 @@ impl App {
         asset_source: A,
         f: F,
     ) -> T {
-        let lifecycle = platform::test::lifecycle();
+        let main_thread_platform = platform::test::main_thread_platform();
         let platform = platform::test::platform();
         let foreground = Rc::new(executor::Foreground::test());
         let cx = Rc::new(RefCell::new(MutableAppContext::new(
             foreground,
-            Rc::new(lifecycle),
             Rc::new(platform),
+            Rc::new(main_thread_platform),
             asset_source,
         )));
         cx.borrow_mut().weak_self = Some(Rc::downgrade(&cx));
@@ -131,14 +131,14 @@ impl App {
         Fn: FnOnce(TestAppContext) -> F,
         F: Future<Output = T>,
     {
-        let lifecycle = Rc::new(platform::test::lifecycle());
         let platform = Rc::new(platform::test::platform());
+        let main_thread_platform = Rc::new(platform::test::main_thread_platform());
         let foreground = Rc::new(executor::Foreground::test());
         let cx = TestAppContext(
             Rc::new(RefCell::new(MutableAppContext::new(
                 foreground.clone(),
-                lifecycle,
                 platform.clone(),
+                main_thread_platform,
                 asset_source,
             ))),
             platform,
@@ -150,18 +150,18 @@ impl App {
     }
 
     pub fn new(asset_source: impl AssetSource) -> Result<Self> {
-        let lifecycle = platform::current::lifecycle();
         let platform = platform::current::platform();
+        let main_thread_platform = platform::current::main_thread_platform();
         let foreground = Rc::new(executor::Foreground::platform(platform.dispatcher())?);
         let app = Self(Rc::new(RefCell::new(MutableAppContext::new(
             foreground,
-            lifecycle.clone(),
             platform.clone(),
+            main_thread_platform.clone(),
             asset_source,
         ))));
 
         let cx = app.0.clone();
-        lifecycle.on_menu_command(Box::new(move |command, arg| {
+        main_thread_platform.on_menu_command(Box::new(move |command, arg| {
             let mut cx = cx.borrow_mut();
             if let Some(key_window_id) = cx.platform.key_window_id() {
                 if let Some((presenter, _)) = cx.presenters_and_platform_windows.get(&key_window_id)
@@ -188,7 +188,7 @@ impl App {
         let cx = self.0.clone();
         self.0
             .borrow_mut()
-            .lifecycle
+            .main_thread_platform
             .on_become_active(Box::new(move || callback(&mut *cx.borrow_mut())));
         self
     }
@@ -200,7 +200,7 @@ impl App {
         let cx = self.0.clone();
         self.0
             .borrow_mut()
-            .lifecycle
+            .main_thread_platform
             .on_resign_active(Box::new(move || callback(&mut *cx.borrow_mut())));
         self
     }
@@ -212,7 +212,7 @@ impl App {
         let cx = self.0.clone();
         self.0
             .borrow_mut()
-            .lifecycle
+            .main_thread_platform
             .on_event(Box::new(move |event| {
                 callback(event, &mut *cx.borrow_mut())
             }));
@@ -226,7 +226,7 @@ impl App {
         let cx = self.0.clone();
         self.0
             .borrow_mut()
-            .lifecycle
+            .main_thread_platform
             .on_open_files(Box::new(move |paths| {
                 callback(paths, &mut *cx.borrow_mut())
             }));
@@ -237,8 +237,8 @@ impl App {
     where
         F: 'static + FnOnce(&mut MutableAppContext),
     {
-        let lifecycle = self.0.borrow().lifecycle.clone();
-        lifecycle.run(Box::new(move || {
+        let platform = self.0.borrow().main_thread_platform.clone();
+        platform.run(Box::new(move || {
             let mut cx = self.0.borrow_mut();
             on_finish_launching(&mut *cx);
         }))
@@ -525,7 +525,7 @@ type GlobalActionCallback = dyn FnMut(&dyn Any, &mut MutableAppContext);
 
 pub struct MutableAppContext {
     weak_self: Option<rc::Weak<RefCell<Self>>>,
-    lifecycle: Rc<dyn platform::Lifecycle>,
+    main_thread_platform: Rc<dyn platform::MainThreadPlatform>,
     platform: Rc<dyn platform::Platform>,
     assets: Arc<AssetCache>,
     cx: AppContext,
@@ -549,14 +549,14 @@ pub struct MutableAppContext {
 impl MutableAppContext {
     fn new(
         foreground: Rc<executor::Foreground>,
-        lifecycle: Rc<dyn platform::Lifecycle>,
         platform: Rc<dyn platform::Platform>,
+        main_thread_platform: Rc<dyn platform::MainThreadPlatform>,
         asset_source: impl AssetSource,
     ) -> Self {
         let fonts = platform.fonts();
         Self {
             weak_self: None,
-            lifecycle,
+            main_thread_platform,
             platform,
             assets: Arc::new(AssetCache::new(asset_source)),
             cx: AppContext {
@@ -717,7 +717,7 @@ impl MutableAppContext {
     }
 
     pub fn set_menus(&mut self, menus: Vec<Menu>) {
-        self.lifecycle.set_menus(menus);
+        self.main_thread_platform.set_menus(menus);
     }
 
     fn prompt<F>(

gpui/src/platform.rs 🔗

@@ -27,7 +27,7 @@ use std::{
     sync::Arc,
 };
 
-pub(crate) trait Lifecycle {
+pub(crate) trait MainThreadPlatform {
     fn on_menu_command(&self, callback: Box<dyn FnMut(&str, Option<&dyn Any>)>);
     fn on_become_active(&self, callback: Box<dyn FnMut()>);
     fn on_resign_active(&self, callback: Box<dyn FnMut()>);

gpui/src/platform/mac.rs 🔗

@@ -11,12 +11,12 @@ mod window;
 use cocoa::base::{BOOL, NO, YES};
 pub use dispatcher::Dispatcher;
 pub use fonts::FontSystem;
-use platform::{MacLifecycle, MacPlatform};
+use platform::{MacMainThreadPlatform, MacPlatform};
 use std::rc::Rc;
 use window::Window;
 
-pub(crate) fn lifecycle() -> Rc<dyn super::Lifecycle> {
-    Rc::new(MacLifecycle::default())
+pub(crate) fn main_thread_platform() -> Rc<dyn super::MainThreadPlatform> {
+    Rc::new(MacMainThreadPlatform::default())
 }
 
 pub(crate) fn platform() -> Rc<dyn super::Platform> {

gpui/src/platform/mac/platform.rs 🔗

@@ -32,7 +32,7 @@ use std::{
     sync::Arc,
 };
 
-const MAC_LIFECYCLE_IVAR: &'static str = "lifecycle";
+const MAC_PLATFORM_IVAR: &'static str = "platform";
 static mut APP_CLASS: *const Class = ptr::null();
 static mut APP_DELEGATE_CLASS: *const Class = ptr::null();
 
@@ -40,7 +40,7 @@ static mut APP_DELEGATE_CLASS: *const Class = ptr::null();
 unsafe fn build_classes() {
     APP_CLASS = {
         let mut decl = ClassDecl::new("GPUIApplication", class!(NSApplication)).unwrap();
-        decl.add_ivar::<*mut c_void>(MAC_LIFECYCLE_IVAR);
+        decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR);
         decl.add_method(
             sel!(sendEvent:),
             send_event as extern "C" fn(&mut Object, Sel, id),
@@ -50,7 +50,7 @@ unsafe fn build_classes() {
 
     APP_DELEGATE_CLASS = {
         let mut decl = ClassDecl::new("GPUIApplicationDelegate", class!(NSResponder)).unwrap();
-        decl.add_ivar::<*mut c_void>(MAC_LIFECYCLE_IVAR);
+        decl.add_ivar::<*mut c_void>(MAC_PLATFORM_IVAR);
         decl.add_method(
             sel!(applicationDidFinishLaunching:),
             did_finish_launching as extern "C" fn(&mut Object, Sel, id),
@@ -76,10 +76,10 @@ unsafe fn build_classes() {
 }
 
 #[derive(Default)]
-pub struct MacLifecycle(RefCell<MacLifecycleState>);
+pub struct MacMainThreadPlatform(RefCell<MacMainThreadPlatformState>);
 
 #[derive(Default)]
-pub struct MacLifecycleState {
+pub struct MacMainThreadPlatformState {
     become_active: Option<Box<dyn FnMut()>>,
     resign_active: Option<Box<dyn FnMut()>>,
     event: Option<Box<dyn FnMut(crate::Event) -> bool>>,
@@ -89,7 +89,7 @@ pub struct MacLifecycleState {
     menu_actions: Vec<(String, Option<Box<dyn Any>>)>,
 }
 
-impl MacLifecycle {
+impl MacMainThreadPlatform {
     unsafe fn create_menu_bar(&self, menus: Vec<Menu>) -> id {
         let menu_bar = NSMenu::new(nil).autorelease();
         let mut state = self.0.borrow_mut();
@@ -170,7 +170,7 @@ impl MacLifecycle {
     }
 }
 
-impl platform::Lifecycle for MacLifecycle {
+impl platform::MainThreadPlatform for MacMainThreadPlatform {
     fn on_become_active(&self, callback: Box<dyn FnMut()>) {
         self.0.borrow_mut().become_active = Some(callback);
     }
@@ -207,15 +207,15 @@ impl platform::Lifecycle for MacLifecycle {
             app.setDelegate_(app_delegate);
 
             let self_ptr = self as *const Self as *const c_void;
-            (*app).set_ivar(MAC_LIFECYCLE_IVAR, self_ptr);
-            (*app_delegate).set_ivar(MAC_LIFECYCLE_IVAR, self_ptr);
+            (*app).set_ivar(MAC_PLATFORM_IVAR, self_ptr);
+            (*app_delegate).set_ivar(MAC_PLATFORM_IVAR, self_ptr);
 
             let pool = NSAutoreleasePool::new(nil);
             app.run();
             pool.drain();
 
-            (*app).set_ivar(MAC_LIFECYCLE_IVAR, null_mut::<c_void>());
-            (*app.delegate()).set_ivar(MAC_LIFECYCLE_IVAR, null_mut::<c_void>());
+            (*app).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>());
+            (*app.delegate()).set_ivar(MAC_PLATFORM_IVAR, null_mut::<c_void>());
         }
     }
 }
@@ -448,16 +448,16 @@ impl platform::Platform for MacPlatform {
     }
 }
 
-unsafe fn get_lifecycle(object: &mut Object) -> &MacLifecycle {
-    let platform_ptr: *mut c_void = *object.get_ivar(MAC_LIFECYCLE_IVAR);
+unsafe fn get_main_thread_platform(object: &mut Object) -> &MacMainThreadPlatform {
+    let platform_ptr: *mut c_void = *object.get_ivar(MAC_PLATFORM_IVAR);
     assert!(!platform_ptr.is_null());
-    &*(platform_ptr as *const MacLifecycle)
+    &*(platform_ptr as *const MacMainThreadPlatform)
 }
 
 extern "C" fn send_event(this: &mut Object, _sel: Sel, native_event: id) {
     unsafe {
         if let Some(event) = Event::from_native(native_event, None) {
-            let platform = get_lifecycle(this);
+            let platform = get_main_thread_platform(this);
             if let Some(callback) = platform.0.borrow_mut().event.as_mut() {
                 if callback(event) {
                     return;
@@ -474,7 +474,7 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) {
         let app: id = msg_send![APP_CLASS, sharedApplication];
         app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
 
-        let platform = get_lifecycle(this);
+        let platform = get_main_thread_platform(this);
         let callback = platform.0.borrow_mut().finish_launching.take();
         if let Some(callback) = callback {
             callback();
@@ -483,14 +483,14 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) {
 }
 
 extern "C" fn did_become_active(this: &mut Object, _: Sel, _: id) {
-    let platform = unsafe { get_lifecycle(this) };
+    let platform = unsafe { get_main_thread_platform(this) };
     if let Some(callback) = platform.0.borrow_mut().become_active.as_mut() {
         callback();
     }
 }
 
 extern "C" fn did_resign_active(this: &mut Object, _: Sel, _: id) {
-    let platform = unsafe { get_lifecycle(this) };
+    let platform = unsafe { get_main_thread_platform(this) };
     if let Some(callback) = platform.0.borrow_mut().resign_active.as_mut() {
         callback();
     }
@@ -512,7 +512,7 @@ extern "C" fn open_files(this: &mut Object, _: Sel, _: id, paths: id) {
             })
             .collect::<Vec<_>>()
     };
-    let platform = unsafe { get_lifecycle(this) };
+    let platform = unsafe { get_main_thread_platform(this) };
     if let Some(callback) = platform.0.borrow_mut().open_files.as_mut() {
         callback(paths);
     }
@@ -520,7 +520,7 @@ extern "C" fn open_files(this: &mut Object, _: Sel, _: id, paths: id) {
 
 extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
     unsafe {
-        let platform = get_lifecycle(this);
+        let platform = get_main_thread_platform(this);
         if let Some(callback) = platform.0.borrow_mut().menu_command.as_mut() {
             let tag: NSInteger = msg_send![item, tag];
             let index = tag as usize;

gpui/src/platform/test.rs 🔗

@@ -8,8 +8,6 @@ use std::{
     sync::Arc,
 };
 
-pub(crate) struct Lifecycle;
-
 pub(crate) struct Platform {
     dispatcher: Arc<dyn super::Dispatcher>,
     fonts: Arc<dyn super::FontSystem>,
@@ -17,6 +15,8 @@ pub(crate) struct Platform {
     last_prompt_for_new_path_args: RefCell<Option<(PathBuf, Box<dyn FnOnce(Option<PathBuf>)>)>>,
 }
 
+pub(crate) struct MainThreadPlatform;
+
 struct Dispatcher;
 
 pub struct Window {
@@ -29,7 +29,7 @@ pub struct Window {
     pub(crate) last_prompt: RefCell<Option<Box<dyn FnOnce(usize)>>>,
 }
 
-impl super::Lifecycle for Lifecycle {
+impl super::MainThreadPlatform for MainThreadPlatform {
     fn on_menu_command(&self, _: Box<dyn FnMut(&str, Option<&dyn Any>)>) {}
 
     fn on_become_active(&self, _: Box<dyn FnMut()>) {}
@@ -179,8 +179,8 @@ impl super::Window for Window {
     }
 }
 
-pub(crate) fn lifecycle() -> Lifecycle {
-    Lifecycle
+pub(crate) fn main_thread_platform() -> MainThreadPlatform {
+    MainThreadPlatform
 }
 
 pub(crate) fn platform() -> Platform {