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.foreground_executor().block_on(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 project::LocalProjectFlags {
96 init_worktree_trust: false,
97 ..Default::default()
98 },
99 cx,
100 );
101
102 let workspace = cx.new(|cx| {
103 Workspace::new(
104 Default::default(),
105 project.clone(),
106 app_state.clone(),
107 window,
108 cx,
109 )
110 });
111
112 workspace.update(cx, |workspace, cx| {
113 let weak_workspace = cx.entity().downgrade();
114 let language_registry = app_state.languages.clone();
115 let user_store = app_state.user_store.clone();
116
117 let component_preview = cx.new(|cx| {
118 ComponentPreview::new(
119 weak_workspace,
120 project,
121 language_registry,
122 user_store,
123 None,
124 None,
125 window,
126 cx,
127 )
128 .expect("Failed to create component preview")
129 });
130
131 workspace.add_item_to_active_pane(
132 Box::new(component_preview),
133 None,
134 true,
135 window,
136 cx,
137 );
138 });
139
140 workspace
141 }
142 },
143 )
144 .expect("Failed to open component preview window");
145
146 cx.activate(true);
147 });
148}