From 3e24f210d2e3dd94b315ad700af43c85126b4e3d Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Thu, 2 Apr 2020 10:27:47 +0200 Subject: [PATCH] Add live translations on Language view Now when scrolling picke the title and button label should change accordingly. --- Oobe/OobeCommon/Controls/CarouselPicker.cs | 14 ++++++++++++-- Oobe/OobeCommon/Controls/ScrollableBase.cs | 16 +++++++++++++--- Oobe/OobeLanguage/LanguageStep.cs | 29 +++++++++++++++++++++++------ 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Oobe/OobeCommon/Controls/CarouselPicker.cs b/Oobe/OobeCommon/Controls/CarouselPicker.cs index 203185f..1e2c095 100644 --- a/Oobe/OobeCommon/Controls/CarouselPicker.cs +++ b/Oobe/OobeCommon/Controls/CarouselPicker.cs @@ -48,18 +48,20 @@ namespace Oobe.Common.Controls itemsListView.Children.RemoveAt(index); } + private int selectedItemIndex = 0; public int SelectedItemIndex { get { - return scrollableBase.CurrentPage; + return selectedItemIndex; } set { - if (scrollableBase.CurrentPage != value) + if (selectedItemIndex != value) { scrollableBase.ScrollToIndex(value); SelectedItemChanged?.Invoke(this, null); + selectedItemIndex = value; } } } @@ -125,6 +127,14 @@ namespace Oobe.Common.Controls { UpdateItems(); }; + scrollableBase.ScrollAnimationEndEvent += (sender, args) => + { + if (selectedItemIndex != scrollableBase.CurrentPage) + { + selectedItemIndex = scrollableBase.CurrentPage; + SelectedItemChanged?.Invoke(this, null); + } + }; if (upperLine != null) { diff --git a/Oobe/OobeCommon/Controls/ScrollableBase.cs b/Oobe/OobeCommon/Controls/ScrollableBase.cs index d3ffe3f..0e73f7e 100755 --- a/Oobe/OobeCommon/Controls/ScrollableBase.cs +++ b/Oobe/OobeCommon/Controls/ScrollableBase.cs @@ -122,6 +122,7 @@ namespace Oobe.Common.Controls protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom) { + ScrollableBase scrollableBase = this.Owner as ScrollableBase; foreach( LayoutItem childLayout in LayoutChildren ) { if( childLayout != null ) @@ -139,6 +140,9 @@ namespace Oobe.Common.Controls childLayout.Layout( childLeft, childTop, childLeft + childWidth, childTop + childHeight ); } } + // workaround issue with ScrollableBase not properly scrolling + // to index when ScrollToIndex is used before layouting + scrollableBase.ScrollToIndex(scrollableBase.CurrentPage); } } // ScrollableBaseCustomLayout @@ -457,10 +461,16 @@ namespace Oobe.Common.Controls CurrentPage = index; } - maxScrollDistance = CalculateMaximumScrollDistance(); + float destinationX; + if (ScrollingDirection == Direction.Horizontal) + { + destinationX = -(mScrollingChild.Children[CurrentPage].Position.X + mScrollingChild.Children[CurrentPage].CurrentSize.Width/2 - CurrentSize.Width/2); // set to middle of current page + } else + { + destinationX = -(mScrollingChild.Children[CurrentPage].Position.Y + mScrollingChild.Children[CurrentPage].CurrentSize.Height/2 - CurrentSize.Height/2); // set to middle of current page + } - float targetPosition = Math.Min(ScrollingDirection == Direction.Vertical ? mScrollingChild.Children[index].Position.Y : mScrollingChild.Children[index].Position.X, maxScrollDistance); - AnimateChildTo(ScrollDuration, -targetPosition); + AnimateChildTo(ScrollDuration, destinationX); } private void OnScrollDragStart() diff --git a/Oobe/OobeLanguage/LanguageStep.cs b/Oobe/OobeLanguage/LanguageStep.cs index 5ad340c..4fdfaf9 100644 --- a/Oobe/OobeLanguage/LanguageStep.cs +++ b/Oobe/OobeLanguage/LanguageStep.cs @@ -7,11 +7,14 @@ using Oobe.Language.Model; using Oobe.Common.Controls; using Oobe.Common.Utils; using OobeCommon.Resources; +using System.Linq; +using System.Globalization; +using System; namespace Oobe.Language { - public class LanguageStep : ProcessStep - { + public class LanguageStep : ProcessStep + { private LanguageManger manager; public LanguageStep() : base() @@ -31,7 +34,7 @@ namespace Oobe.Language title.TranslatableText = "CHOOSE_LANGUAGE"; title.Position2D = new Position2D(410, 160); title.Size2D = new Size2D(364, 58); - title.TextColor = new Color(0, 20.0f/255.0f, 71.0f/255.0f, 1.0f); + title.TextColor = new Color(0, 20.0f / 255.0f, 71.0f / 255.0f, 1.0f); title.HorizontalAlignment = HorizontalAlignment.Center; title.Ellipsis = false; title.PixelSize = 48.0f; @@ -48,10 +51,13 @@ namespace Oobe.Language item.Text = info.LocalName; carousel.AddItem(item); } + int currentIndex = manager.Languages.FindIndex(x => x == manager.CurrentLanguage); + carousel.SelectedItemIndex = currentIndex; Button btn = new Button(ButtonStyles.Next); btn.Position2D = new Position2D(888, 512); - btn.ClickEvent += (obj, args) => { + btn.ClickEvent += (obj, args) => + { if (carousel.SelectedItemIndex >= 0 && carousel.SelectedItemIndex < manager.Languages.Count) { var lang = manager.Languages[carousel.SelectedItemIndex]; @@ -60,11 +66,22 @@ namespace Oobe.Language nav.Next(); }; + carousel.SelectedItemChanged += (sender, args) => + { + if (carousel.SelectedItemIndex >= 0 && carousel.SelectedItemIndex < manager.Languages.Count) + { + string language = manager.Languages[carousel.SelectedItemIndex].Code.Replace("_", "-"); + var culture = CultureInfo.CreateSpecificCulture(language); + title.Text = Translations.ResourceManager.GetString("CHOOSE_LANGUAGE", culture); + btn.Text = Translations.ResourceManager.GetString("CONTINUE", culture); + } + }; + container.Add(title); container.Add(btn); container.Add(carousel); return container; - } - } + } + } } -- 2.7.4