Skip to content

Commit 4eaffd2

Browse files
committed
Remove Env::default
This was a bit of a footgun; it would always configure localization resources, which doesn't make sense if we want to use an `Env` as just a bag of key/values in some contexts. Specifically, we may want to do this for #1658. The fix is a two-parter: - the `LocalizationManager` owned by the env is now optional, although it is expected to exist for the root env. The framework makes sure this is the case. - There is a new `Env::empty` method that creates a new empty environment, with no localization resources. This is suitable for the 'specifying overrides' case.
1 parent dc17683 commit 4eaffd2

File tree

7 files changed

+32
-24
lines changed

7 files changed

+32
-24
lines changed

druid/src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ impl<T: Data> AppLauncher<T> {
242242
let mut env = self
243243
.l10n_resources
244244
.map(|it| Env::with_i10n(it.0, &it.1))
245-
.unwrap_or_default();
245+
.unwrap_or_else(Env::with_default_i10n);
246246

247247
if let Some(f) = self.env_setup.take() {
248248
f(&mut env, &data);

druid/src/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ mod tests {
14111411
state: &mut state,
14121412
};
14131413

1414-
let env = Env::default();
1414+
let env = Env::with_default_i10n();
14151415

14161416
widget.lifecycle(&mut ctx, &LifeCycle::WidgetAdded, &1, &env);
14171417
assert!(ctx.widget_state.children.may_contain(&ID_1));

druid/src/env.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct Env(Arc<EnvImpl>);
5454
#[derive(Clone)]
5555
struct EnvImpl {
5656
map: HashMap<ArcStr, Value>,
57-
l10n: Arc<L10nManager>,
57+
l10n: Option<Arc<L10nManager>>,
5858
}
5959

6060
/// A typed [`Env`] key.
@@ -228,7 +228,7 @@ impl Env {
228228
///
229229
/// ```no_run
230230
/// # use druid::Env;
231-
/// # let env = Env::default();
231+
/// # let env = Env::empty();
232232
/// # let widget_id = 0;
233233
/// # let my_rect = druid::Rect::ZERO;
234234
/// if env.get(Env::DEBUG_WIDGET) {
@@ -359,9 +359,11 @@ impl Env {
359359
/// Returns a reference to the [`L10nManager`], which handles localization
360360
/// resources.
361361
///
362+
/// This always exists on the base `Env` configured by druid.
363+
///
362364
/// [`L10nManager`]: struct.L10nManager.html
363-
pub(crate) fn localization_manager(&self) -> &L10nManager {
364-
&self.0.l10n
365+
pub(crate) fn localization_manager(&self) -> Option<&L10nManager> {
366+
self.0.l10n.as_deref()
365367
}
366368

367369
/// Given an id, returns one of 18 distinct colors
@@ -504,18 +506,26 @@ static DEBUG_COLOR: &[Color] = &[
504506
Color::rgb8(0, 0, 0),
505507
];
506508

507-
impl Default for Env {
508-
fn default() -> Self {
509+
impl Env {
510+
/// Returns an empty `Env`.
511+
///
512+
/// This is useful for creating a set of overrides.
513+
pub fn empty() -> Self {
514+
Env(Arc::new(EnvImpl {
515+
l10n: None,
516+
map: HashMap::new(),
517+
}))
518+
}
519+
520+
pub(crate) fn with_default_i10n() -> Self {
509521
Env::with_i10n(vec!["builtin.ftl".into()], "./resources/i18n/")
510522
}
511-
}
512523

513-
impl Env {
514524
pub(crate) fn with_i10n(resources: Vec<String>, base_dir: &str) -> Self {
515525
let l10n = L10nManager::new(resources, base_dir);
516526

517527
let inner = EnvImpl {
518-
l10n: Arc::new(l10n),
528+
l10n: Some(Arc::new(l10n)),
519529
map: HashMap::new(),
520530
};
521531

@@ -647,7 +657,7 @@ mod tests {
647657
#[test]
648658
fn string_key_or_value() {
649659
const MY_KEY: Key<ArcStr> = Key::new("org.linebender.test.my-string-key");
650-
let env = Env::default().adding(MY_KEY, "Owned");
660+
let env = Env::empty().adding(MY_KEY, "Owned");
651661
assert_eq!(env.get(MY_KEY).as_ref(), "Owned");
652662

653663
let key: KeyOrValue<ArcStr> = MY_KEY.into();

druid/src/localization.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,19 @@ impl<T> LocalizedString<T> {
338338
//TODO: this recomputes the string if either the language has changed,
339339
//or *anytime* we have arguments. Ideally we would be using a lens
340340
//to only recompute when our actual data has changed.
341-
if self.args.is_some()
342-
|| self.resolved_lang.as_ref() != Some(&env.localization_manager().current_locale)
343-
{
341+
let manager = match env.localization_manager() {
342+
Some(manager) => manager,
343+
None => return false,
344+
};
345+
346+
if self.args.is_some() || self.resolved_lang.as_ref() != Some(&manager.current_locale) {
344347
let args: Option<FluentArgs> = self
345348
.args
346349
.as_ref()
347350
.map(|a| a.iter().map(|(k, v)| (*k, (v.0)(data, env))).collect());
348351

349-
self.resolved_lang = Some(env.localization_manager().current_locale.clone());
350-
let next = env.localization_manager().localize(self.key, args.as_ref());
352+
self.resolved_lang = Some(manager.current_locale.clone());
353+
let next = manager.localize(self.key, args.as_ref());
351354
let result = next != self.resolved;
352355
self.resolved = next;
353356
result

druid/src/scroll_component.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ mod tests {
748748
}
749749

750750
fn test_env() -> Env {
751-
Env::default()
751+
Env::empty()
752752
.adding(theme::SCROLLBAR_WIDTH, TEST_SCROLLBAR_WIDTH)
753753
.adding(theme::SCROLLBAR_PAD, TEST_SCROLLBAR_PAD)
754754
.adding(theme::SCROLLBAR_MIN_SIZE, TEST_SCROLLBAR_MIN_SIZE)

druid/src/tests/harness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<T: Data> Harness<'_, T> {
154154

155155
let inner = Inner {
156156
data,
157-
env: Env::default(),
157+
env: Env::with_default_i10n(),
158158
window,
159159
cmds: Default::default(),
160160
};

druid/src/theme.rs

-5
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,3 @@ pub(crate) fn add_to_env(env: Env) -> Env {
183183
.with_size(15.0),
184184
)
185185
}
186-
187-
#[deprecated(since = "0.7.0", note = "use Env::default() instead")]
188-
pub fn init() -> Env {
189-
Env::default()
190-
}

0 commit comments

Comments
 (0)