1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use crate::;
/// Marks a type whose value is stored once per app.
///
/// Implement it for a type, then store one value of that type with [`App::set_global`]. Read it with
/// [`App::global`], and change it with [`UpdateGlobal::update_global`]. Use a global for state that
/// exists once for the whole app, such as settings or a theme. Use an entity for state that can have
/// several instances, or that several parts of the app change independently.
///
/// The trait is a marker: it adds no methods of its own. Helper traits with blanket implementations,
/// such as [`ReadGlobal`] and [`UpdateGlobal`], add methods to every `Global` type.
///
/// # Restricting access to globals
///
/// In some situations you may need to store some global state, but want to
/// restrict access to reading it or writing to it.
///
/// In these cases, Rust's visibility system can be used to restrict access to
/// a global value. For example, you can create a private struct that implements
/// [`Global`] and holds the global state. Then create a newtype struct that wraps
/// the global type and create custom accessor methods to expose the desired subset
/// of operations.
///
/// # Examples
///
/// ```
/// # use gpui::{Global, TestApp};
/// struct Theme {
/// dark: bool,
/// }
///
/// impl Global for Theme {}
///
/// # let mut app = TestApp::new();
/// app.set_global(Theme { dark: false });
/// assert!(!app.read(|cx| cx.global::<Theme>().dark));
/// ```
/// Reads the global of the implementing type from an [`App`], as in `Theme::global(cx)`.
///
/// Every [`Global`] type implements this trait. Use it to name a global by type instead of calling
/// [`App::global`] with a turbofish.
///
/// # Examples
///
/// ```
/// # use gpui::{Global, ReadGlobal, TestApp};
/// struct Theme {
/// dark: bool,
/// }
///
/// impl Global for Theme {}
///
/// # let mut app = TestApp::new();
/// app.set_global(Theme { dark: true });
/// assert!(app.read(|cx| Theme::global(cx).dark));
/// ```
/// Changes or sets the global of the implementing type through a context.
///
/// Every [`Global`] type implements this trait. `Theme::update_global(cx, ...)` is the same as
/// `cx.update_global::<Theme, _>(...)`, and `Theme::set_global(cx, ...)` is the same as
/// `cx.set_global(...)`. Both accept any context that implements `BorrowAppContext`, such as
/// `&mut App`.
///
/// # Examples
///
/// ```
/// # use gpui::{Global, TestApp, UpdateGlobal};
/// struct Theme {
/// dark: bool,
/// }
///
/// impl Global for Theme {}
///
/// # let mut app = TestApp::new();
/// app.update(|cx| {
/// Theme::set_global(cx, Theme { dark: false });
/// Theme::update_global(cx, |theme, _cx| theme.dark = true);
/// });
/// assert!(app.read(|cx| cx.global::<Theme>().dark));
/// ```