component_preview_example.rs

  1/// Run the component preview application.
  2///
  3/// This initializes the application with minimal required infrastructure
  4/// and opens a workspace with the ComponentPreview item.
  5#[cfg(feature = "preview")]
  6pub fn run_component_preview() {
  7    use fs::RealFs;
  8    use gpui::{
  9        AppContext as _, Application, Bounds, KeyBinding, WindowBounds, WindowOptions, actions,
 10        size,
 11    };
 12
 13    use client::{Client, UserStore};
 14    use language::LanguageRegistry;
 15    use node_runtime::NodeRuntime;
 16    use project::Project;
 17    use reqwest_client::ReqwestClient;
 18    use session::{AppSession, Session};
 19    use std::sync::Arc;
 20    use ui::{App, px};
 21    use workspace::{AppState, Workspace, WorkspaceStore};
 22
 23    use crate::{ComponentPreview, init};
 24
 25    actions!(zed, [Quit]);
 26
 27    fn quit(_: &Quit, cx: &mut App) {
 28        cx.quit();
 29    }
 30
 31    Application::new().run(|cx| {
 32        component::init();
 33
 34        cx.on_action(quit);
 35        cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
 36        let version = release_channel::AppVersion::load(env!("CARGO_PKG_VERSION"), None, None);
 37        release_channel::init(version, cx);
 38
 39        let http_client =
 40            ReqwestClient::user_agent("component_preview").expect("Failed to create HTTP client");
 41        cx.set_http_client(Arc::new(http_client));
 42
 43        let fs = Arc::new(RealFs::new(None, cx.background_executor().clone()));
 44        <dyn fs::Fs>::set_global(fs.clone(), cx);
 45
 46        settings::init(cx);
 47        theme::init(theme::LoadThemes::JustBase, cx);
 48
 49        let languages = Arc::new(LanguageRegistry::new(cx.background_executor().clone()));
 50        let client = Client::production(cx);
 51        client::init(&client, cx);
 52
 53        let user_store = cx.new(|cx| UserStore::new(client.clone(), cx));
 54        let workspace_store = cx.new(|cx| WorkspaceStore::new(client.clone(), cx));
 55        let session_id = uuid::Uuid::new_v4().to_string();
 56        let session = cx.background_executor().block(Session::new(session_id));
 57        let session = cx.new(|cx| AppSession::new(session, cx));
 58        let node_runtime = NodeRuntime::unavailable();
 59
 60        let app_state = Arc::new(AppState {
 61            languages,
 62            client,
 63            user_store,
 64            workspace_store,
 65            fs,
 66            build_window_options: |_, _| Default::default(),
 67            node_runtime,
 68            session,
 69        });
 70        AppState::set_global(Arc::downgrade(&app_state), cx);
 71
 72        workspace::init(app_state.clone(), cx);
 73        init(app_state.clone(), cx);
 74
 75        let size = size(px(1200.), px(800.));
 76        let bounds = Bounds::centered(None, size, cx);
 77
 78        cx.open_window(
 79            WindowOptions {
 80                window_bounds: Some(WindowBounds::Windowed(bounds)),
 81                ..Default::default()
 82            },
 83            {
 84                move |window, cx| {
 85                    let app_state = app_state;
 86                    theme::setup_ui_font(window, cx);
 87
 88                    let project = Project::local(
 89                        app_state.client.clone(),
 90                        app_state.node_runtime.clone(),
 91                        app_state.user_store.clone(),
 92                        app_state.languages.clone(),
 93                        app_state.fs.clone(),
 94                        None,
 95                        false,
 96                        cx,
 97                    );
 98
 99                    let workspace = cx.new(|cx| {
100                        Workspace::new(
101                            Default::default(),
102                            project.clone(),
103                            app_state.clone(),
104                            window,
105                            cx,
106                        )
107                    });
108
109                    workspace.update(cx, |workspace, cx| {
110                        let weak_workspace = cx.entity().downgrade();
111                        let language_registry = app_state.languages.clone();
112                        let user_store = app_state.user_store.clone();
113
114                        let component_preview = cx.new(|cx| {
115                            ComponentPreview::new(
116                                weak_workspace,
117                                project,
118                                language_registry,
119                                user_store,
120                                None,
121                                None,
122                                window,
123                                cx,
124                            )
125                            .expect("Failed to create component preview")
126                        });
127
128                        workspace.add_item_to_active_pane(
129                            Box::new(component_preview),
130                            None,
131                            true,
132                            window,
133                            cx,
134                        );
135                    });
136
137                    workspace
138                }
139            },
140        )
141        .expect("Failed to open component preview window");
142
143        cx.activate(true);
144    });
145}