gpui_component/
lib.rs

1use gpui::{App, SharedString};
2use std::ops::Deref;
3
4mod event;
5mod geometry;
6mod global_state;
7mod icon;
8mod index_path;
9#[cfg(any(feature = "inspector", debug_assertions))]
10mod inspector;
11mod root;
12mod styled;
13mod time;
14mod title_bar;
15mod virtual_list;
16mod window_border;
17
18pub(crate) mod actions;
19
20pub mod accordion;
21pub mod alert;
22pub mod animation;
23pub mod avatar;
24pub mod badge;
25pub mod breadcrumb;
26pub mod button;
27pub mod chart;
28pub mod checkbox;
29pub mod clipboard;
30pub mod collapsible;
31pub mod color_picker;
32pub mod description_list;
33pub mod dialog;
34pub mod divider;
35pub mod dock;
36pub mod form;
37pub mod group_box;
38pub mod highlighter;
39pub mod history;
40pub mod input;
41pub mod kbd;
42pub mod label;
43pub mod link;
44pub mod list;
45pub mod menu;
46pub mod notification;
47pub mod plot;
48pub mod popover;
49pub mod progress;
50pub mod radio;
51pub mod resizable;
52pub mod scroll;
53pub mod select;
54pub mod setting;
55pub mod sheet;
56pub mod sidebar;
57pub mod skeleton;
58pub mod slider;
59pub mod spinner;
60pub mod switch;
61pub mod tab;
62pub mod table;
63pub mod tag;
64pub mod text;
65pub mod theme;
66pub mod tooltip;
67pub mod tree;
68pub use time::{calendar, date_picker};
69
70#[cfg(feature = "webview")]
71pub mod webview;
72
73// re-export
74#[cfg(feature = "webview")]
75pub use wry;
76
77pub use crate::Disableable;
78pub use event::InteractiveElementExt;
79pub use geometry::*;
80pub use icon::*;
81pub use index_path::IndexPath;
82pub use input::{Rope, RopeExt, RopeLines};
83#[cfg(any(feature = "inspector", debug_assertions))]
84pub use inspector::*;
85pub use root::{Root, WindowExt};
86pub use styled::*;
87pub use theme::*;
88pub use title_bar::*;
89pub use virtual_list::{VirtualList, VirtualListScrollHandle, h_virtual_list, v_virtual_list};
90pub use window_border::{WindowBorder, window_border, window_paddings};
91
92rust_i18n::i18n!("locales", fallback = "en");
93
94/// Initialize the components.
95///
96/// You must initialize the components at your application's entry point.
97pub fn init(cx: &mut App) {
98    theme::init(cx);
99    global_state::init(cx);
100    #[cfg(any(feature = "inspector", debug_assertions))]
101    inspector::init(cx);
102    root::init(cx);
103    date_picker::init(cx);
104    color_picker::init(cx);
105    dock::init(cx);
106    sheet::init(cx);
107    select::init(cx);
108    input::init(cx);
109    list::init(cx);
110    dialog::init(cx);
111    popover::init(cx);
112    menu::init(cx);
113    table::init(cx);
114    text::init(cx);
115    tree::init(cx);
116}
117
118#[inline]
119pub fn locale() -> impl Deref<Target = str> {
120    rust_i18n::locale()
121}
122
123#[inline]
124pub fn set_locale(locale: &str) {
125    rust_i18n::set_locale(locale)
126}
127
128#[inline]
129pub(crate) fn measure_enable() -> bool {
130    std::env::var("ZED_MEASUREMENTS").is_ok() || std::env::var("GPUI_MEASUREMENTS").is_ok()
131}
132
133/// Measures the execution time of a function and logs it if `if_` is true.
134///
135/// And need env `GPUI_MEASUREMENTS=1`
136#[inline]
137#[track_caller]
138pub fn measure_if(name: impl Into<SharedString>, if_: bool, f: impl FnOnce()) {
139    if if_ && measure_enable() {
140        let measure = Measure::new(name);
141        f();
142        measure.end();
143    } else {
144        f();
145    }
146}
147
148/// Measures the execution time.
149#[inline]
150#[track_caller]
151pub fn measure(name: impl Into<SharedString>, f: impl FnOnce()) {
152    measure_if(name, true, f);
153}
154
155pub struct Measure {
156    name: SharedString,
157    start: std::time::Instant,
158}
159
160impl Measure {
161    #[track_caller]
162    pub fn new(name: impl Into<SharedString>) -> Self {
163        Self {
164            name: name.into(),
165            start: std::time::Instant::now(),
166        }
167    }
168
169    #[track_caller]
170    pub fn end(self) {
171        let duration = self.start.elapsed();
172        tracing::trace!("{} in {:?}", self.name, duration);
173    }
174}