Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow additional l10n resource in AppLauncher #1528

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ You can find its changes [documented below](#070---2021-01-01).
- WindowCloseRequested/WindowDisconnected event when a window is closing ([#1254] by [@rjwittams])
- RichTextBuilder ([#1520] by [@Maan2003])
- `get_external_handle` on `DelegateCtx` ([#1526] by [@Maan2003])
- `AppLauncher::localization_resources` to use custom l10n resources. ([#1528] by [@edwin0cheng])

### Changed

Expand Down
21 changes: 20 additions & 1 deletion druid/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type EnvSetupFn<T> = dyn FnOnce(&mut Env, &T);
pub struct AppLauncher<T> {
windows: Vec<WindowDesc<T>>,
env_setup: Option<Box<EnvSetupFn<T>>>,
l10n_resources: Option<(Vec<String>, String)>,
delegate: Option<Box<dyn AppDelegate<T>>>,
ext_event_host: ExtEventHost,
}
Expand Down Expand Up @@ -107,6 +108,7 @@ impl<T: Data> AppLauncher<T> {
AppLauncher {
windows: vec![window],
env_setup: None,
l10n_resources: None,
delegate: None,
ext_event_host: ExtEventHost::new(),
}
Expand Down Expand Up @@ -146,6 +148,19 @@ impl<T: Data> AppLauncher<T> {
self
}

/// Use custom localization resource
///
/// `resources` is a list of file names that contain strings. `base_dir`
/// is a path to a directory that includes per-locale subdirectories.
///
/// This directory should be of the structure `base_dir/{locale}/{resource}`,
/// where '{locale}' is a valid BCP47 language tag, and {resource} is a `.ftl`
/// included in `resources`.
pub fn localization_resources(mut self, resources: Vec<String>, base_dir: String) -> Self {
self.l10n_resources = Some((resources, base_dir));
self
}

/// Returns an [`ExtEventSink`] that can be moved between threads,
/// and can be used to submit commands back to the application.
///
Expand All @@ -161,7 +176,11 @@ impl<T: Data> AppLauncher<T> {
pub fn launch(mut self, data: T) -> Result<(), PlatformError> {
let app = Application::new()?;

let mut env = Env::default();
let mut env = self
.l10n_resources
.map(|it| Env::with_i10n(it.0, &it.1))
.unwrap_or_default();

if let Some(f) = self.env_setup.take() {
f(&mut env, &data);
}
Expand Down
56 changes: 31 additions & 25 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,37 +467,43 @@ impl Data for EnvImpl {
}
}

// Colors are from https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
// They're picked for visual distinction and accessbility (99 percent)
const DEBUG_COLOR: &[Color] = &[
Color::rgb8(230, 25, 75),
Color::rgb8(60, 180, 75),
Color::rgb8(255, 225, 25),
Color::rgb8(0, 130, 200),
Color::rgb8(245, 130, 48),
Color::rgb8(70, 240, 240),
Color::rgb8(240, 50, 230),
Color::rgb8(250, 190, 190),
Color::rgb8(0, 128, 128),
Color::rgb8(230, 190, 255),
Color::rgb8(170, 110, 40),
Color::rgb8(255, 250, 200),
Color::rgb8(128, 0, 0),
Color::rgb8(170, 255, 195),
Color::rgb8(0, 0, 128),
Color::rgb8(128, 128, 128),
Color::rgb8(255, 255, 255),
Color::rgb8(0, 0, 0),
];

impl Default for Env {
fn default() -> Self {
let l10n = L10nManager::new(vec!["builtin.ftl".into()], "./resources/i18n/");

// Colors are from https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
// They're picked for visual distinction and accessbility (99 percent)
let debug_colors = vec![
Color::rgb8(230, 25, 75),
Color::rgb8(60, 180, 75),
Color::rgb8(255, 225, 25),
Color::rgb8(0, 130, 200),
Color::rgb8(245, 130, 48),
Color::rgb8(70, 240, 240),
Color::rgb8(240, 50, 230),
Color::rgb8(250, 190, 190),
Color::rgb8(0, 128, 128),
Color::rgb8(230, 190, 255),
Color::rgb8(170, 110, 40),
Color::rgb8(255, 250, 200),
Color::rgb8(128, 0, 0),
Color::rgb8(170, 255, 195),
Color::rgb8(0, 0, 128),
Color::rgb8(128, 128, 128),
Color::rgb8(255, 255, 255),
Color::rgb8(0, 0, 0),
];
Env::with_i10n(vec!["builtin.ftl".into()], "./resources/i18n/")
}
}

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

let inner = EnvImpl {
l10n: Arc::new(l10n),
map: HashMap::new(),
debug_colors,
debug_colors: DEBUG_COLOR.into(),
};

let env = Env(Arc::new(inner))
Expand Down