Skip to content

Commit

Permalink
Attempt to fix the dynamic labels using DynamicDisplay::generation.
Browse files Browse the repository at this point in the history
The labels change dynamically, but sometimes they are clipped/cropped.
  • Loading branch information
hydra committed Nov 28, 2024
1 parent d632f89 commit 571b93b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
3 changes: 2 additions & 1 deletion examples/localized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ fn localized() -> impl MakeWidget {
*counter.lock() -= 1;
}
})))
.into_columns();
.into_columns()
.localized(dynamic_locale.map_each(LanguageChoices::to_locale));

element_in_default_locale
.and(elements_in_specific_locale)
Expand Down
16 changes: 9 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::styles::components::{
};
use crate::styles::{ComponentDefinition, FontFamilyList, Styles, Theme, ThemePair};
use crate::tree::Tree;
use crate::value::{IntoValue, Source, Value};
use crate::value::{Dynamic, IntoValue, Source, Value};
use crate::widget::{EventHandling, MountedWidget, RootBehavior, WidgetId, WidgetInstance};
use crate::window::{
CursorState, DeviceId, KeyEvent, PlatformWindow, ThemeMode, WidgetCursorState,
Expand Down Expand Up @@ -933,7 +933,7 @@ pub struct WidgetContext<'context> {
font_state: &'context mut FontState,
effective_styles: Styles,
cache: WidgetCacheKey,
locale: LanguageIdentifier,
locale: Value<LanguageIdentifier>,
translations: &'context mut TranslationState,
}

Expand Down Expand Up @@ -978,7 +978,7 @@ impl<'context> WidgetContext<'context> {
font_state,
theme: Cow::Borrowed(theme),
window,
locale: translations.fallback_locale.clone(),
locale: Dynamic::new(translations.fallback_locale.clone()).into_value(),
translations
};

Expand All @@ -989,8 +989,10 @@ impl<'context> WidgetContext<'context> {
context.cache.theme_mode = mode.get_tracking_redraw(&context);
}
if let Some(locale) = overridden_locale {
context.locale = locale.get_tracking_redraw(&context);
context.locale = locale
}
context.locale.invalidate_when_changed(&context);
context.locale.redraw_when_changed(&context);

context
}
Expand Down Expand Up @@ -1035,7 +1037,7 @@ impl<'context> WidgetContext<'context> {
};
let locale = current_node.overridden_locale();
let locale = if let Some(locale) = locale {
locale.get_tracking_redraw(self)
locale
} else {
self.locale.clone()
};
Expand Down Expand Up @@ -1282,15 +1284,15 @@ impl<'context> WidgetContext<'context> {

/// Returns the locale for this widget.
#[must_use]
pub fn locale(&self) -> &LanguageIdentifier {
pub fn locale(&self) -> &Value<LanguageIdentifier> {
&self.locale
}

/// Returns the current translation bundle for this widget.
pub fn translation(
&self
) -> &FluentBundle<FluentResource> {
if let Some(bundle) = self.translations.loaded_translations.get(&self.locale) {
if let Some(bundle) = self.translations.loaded_translations.get(&self.locale.get()) {
bundle
} else {
self.translations.loaded_translations.get(&self.translations.fallback_locale).unwrap()
Expand Down
10 changes: 7 additions & 3 deletions src/localization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use fluent_bundle::{FluentArgs, FluentBundle, FluentResource, FluentValue};
use unic_langid::LanguageIdentifier;
use cushy::widgets::Label;
use crate::context::WidgetContext;
use crate::value::{Dynamic, IntoValue};
use crate::value::{Dynamic, Generation, IntoValue};
use crate::widgets::label::{DynamicDisplay};

pub struct Localize<'args> {
Expand Down Expand Up @@ -53,8 +53,12 @@ impl Localize<'static> {
}

impl DynamicDisplay for Localize<'static> {
fn fmt(&self, context: &WidgetContext<'_>, f: &mut Formatter<'_>) -> std::fmt::Result {
let locale = context.locale();
fn generation(&self, context: &WidgetContext<'_>) -> Option<Generation> {
context.locale().generation()
}

fn fmt(&self, context: &WidgetContext<'_>, f: &mut Formatter<'_>) -> fmt::Result {
let locale = context.locale().get();
println!("{:?}", locale);

let bundle = context.translation();
Expand Down

0 comments on commit 571b93b

Please sign in to comment.