gpui_component/
lib.rs

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