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