From 3d3684c2483fdd18214b46e3f609cb0c354e72f8 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Wed, 3 Jul 2024 12:08:46 +0400 Subject: [PATCH] Rework locale handler and fix remaining RTL issues window handler was being duplicated, wouldn't apply for system default language nor on initial installation, winforms progress bar not following RTL changes --- Bloxstrap/App.xaml.cs | 5 +- Bloxstrap/Locale.cs | 60 ++++++++++--------- .../Bootstrapper/Base/WinFormsDialogBase.cs | 5 -- .../Elements/Bootstrapper/LegacyDialog2008.cs | 3 + .../Elements/Bootstrapper/LegacyDialog2011.cs | 3 + .../Elements/Bootstrapper/ProgressDialog.cs | 3 + .../Dialogs/LanguageSelectorViewModel.cs | 6 +- 7 files changed, 47 insertions(+), 38 deletions(-) diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index 3c036db..30c6cb6 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -111,7 +111,7 @@ namespace Bloxstrap { const string LOG_IDENT = "App::OnStartup"; - Locale.CurrentCulture = Thread.CurrentThread.CurrentUICulture; + Locale.Initialize(); base.OnStartup(e); @@ -144,7 +144,8 @@ namespace Bloxstrap Settings.Load(); State.Load(); FastFlags.Load(); - Locale.Set(); + + Locale.Set(Settings.Prop.Locale); } LaunchSettings.ParseRoblox(); diff --git a/Bloxstrap/Locale.cs b/Bloxstrap/Locale.cs index d3ed71b..3b62873 100644 --- a/Bloxstrap/Locale.cs +++ b/Bloxstrap/Locale.cs @@ -6,7 +6,7 @@ namespace Bloxstrap { internal static class Locale { - public static CultureInfo CurrentCulture = CultureInfo.InvariantCulture; + public static CultureInfo CurrentCulture { get; private set; } = CultureInfo.InvariantCulture; public static bool RightToLeft { get; private set; } = false; @@ -67,45 +67,47 @@ namespace Bloxstrap return languages; } - public static void Set() + public static void Set(string identifier) { - string identifier = App.Settings.Prop.Locale; - if (!SupportedLocales.ContainsKey(identifier)) identifier = "nil"; if (identifier == "nil") - return; - - CurrentCulture = new CultureInfo(identifier); - - CultureInfo.DefaultThreadCurrentUICulture = CurrentCulture; - Thread.CurrentThread.CurrentUICulture = CurrentCulture; - - RoutedEventHandler? handler = null; - - if (identifier == "ar" || identifier == "he") { - RightToLeft = true; + CurrentCulture = Thread.CurrentThread.CurrentUICulture; + } + else + { + CurrentCulture = new CultureInfo(identifier); - handler = new((sender, _) => - { - var window = (Window)sender; - + CultureInfo.DefaultThreadCurrentUICulture = CurrentCulture; + Thread.CurrentThread.CurrentUICulture = CurrentCulture; + } + + RightToLeft = CurrentCulture.Name.StartsWith("ar") || CurrentCulture.Name.StartsWith("he"); + } + + public static void Initialize() + { + Set("nil"); + + // https://supportcenter.devexpress.com/ticket/details/t905790/is-there-a-way-to-set-right-to-left-mode-in-wpf-for-the-whole-application + EventManager.RegisterClassHandler(typeof(Window), FrameworkElement.LoadedEvent, new RoutedEventHandler((sender, _) => + { + var window = (Window)sender; + + if (RightToLeft) + { window.FlowDirection = FlowDirection.RightToLeft; if (window.ContextMenu is not null) window.ContextMenu.FlowDirection = FlowDirection.RightToLeft; - }); - } - else if (identifier == "th") - { - handler = new((window, _) => ((Window)window).FontFamily = new System.Windows.Media.FontFamily(new Uri("pack://application:,,,/Resources/Fonts/"), "./#Noto Sans Thai")); - } - - // https://supportcenter.devexpress.com/ticket/details/t905790/is-there-a-way-to-set-right-to-left-mode-in-wpf-for-the-whole-application - if (handler is not null) - EventManager.RegisterClassHandler(typeof(Window), FrameworkElement.LoadedEvent, handler); + } + else if (CurrentCulture.Name.StartsWith("th")) + { + window.FontFamily = new System.Windows.Media.FontFamily(new Uri("pack://application:,,,/Resources/Fonts/"), "./#Noto Sans Thai"); + } + })); } } } diff --git a/Bloxstrap/UI/Elements/Bootstrapper/Base/WinFormsDialogBase.cs b/Bloxstrap/UI/Elements/Bootstrapper/Base/WinFormsDialogBase.cs index a66352f..588d950 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/Base/WinFormsDialogBase.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/Base/WinFormsDialogBase.cs @@ -100,11 +100,6 @@ namespace Bloxstrap.UI.Elements.Bootstrapper.Base this.RightToLeft = RightToLeft.Yes; this.RightToLeftLayout = true; } - else - { - this.RightToLeft = RightToLeft.No; - this.RightToLeftLayout = false; - } } #region WinForms event handlers diff --git a/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2008.cs b/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2008.cs index b851f2b..5543902 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2008.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2008.cs @@ -47,6 +47,9 @@ namespace Bloxstrap.UI.Elements.Bootstrapper ScaleWindow(); SetupDialog(); + + this.ProgressBar.RightToLeft = this.RightToLeft; + this.ProgressBar.RightToLeftLayout = this.RightToLeftLayout; } private void LegacyDialog2008_Load(object sender, EventArgs e) diff --git a/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2011.cs b/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2011.cs index 886c5f5..2349eda 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2011.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/LegacyDialog2011.cs @@ -47,6 +47,9 @@ namespace Bloxstrap.UI.Elements.Bootstrapper ScaleWindow(); SetupDialog(); + + this.ProgressBar.RightToLeft = this.RightToLeft; + this.ProgressBar.RightToLeftLayout = this.RightToLeftLayout; } private void LegacyDialog2011_Load(object sender, EventArgs e) diff --git a/Bloxstrap/UI/Elements/Bootstrapper/ProgressDialog.cs b/Bloxstrap/UI/Elements/Bootstrapper/ProgressDialog.cs index 001b31a..fc2974a 100644 --- a/Bloxstrap/UI/Elements/Bootstrapper/ProgressDialog.cs +++ b/Bloxstrap/UI/Elements/Bootstrapper/ProgressDialog.cs @@ -57,6 +57,9 @@ namespace Bloxstrap.UI.Elements.Bootstrapper this.IconBox.BackgroundImage = App.Settings.Prop.BootstrapperIcon.GetIcon().GetSized(128, 128).ToBitmap(); SetupDialog(); + + this.ProgressBar.RightToLeft = this.RightToLeft; + this.ProgressBar.RightToLeftLayout = this.RightToLeftLayout; } private void ButtonCancel_MouseEnter(object sender, EventArgs e) diff --git a/Bloxstrap/UI/ViewModels/Dialogs/LanguageSelectorViewModel.cs b/Bloxstrap/UI/ViewModels/Dialogs/LanguageSelectorViewModel.cs index 7754dcf..ff18451 100644 --- a/Bloxstrap/UI/ViewModels/Dialogs/LanguageSelectorViewModel.cs +++ b/Bloxstrap/UI/ViewModels/Dialogs/LanguageSelectorViewModel.cs @@ -21,8 +21,10 @@ namespace Bloxstrap.UI.ViewModels.Dialogs private void SetLocale() { - App.Settings.Prop.Locale = Locale.GetIdentifierFromName(SelectedLanguage); - Locale.Set(); + string identifier = Locale.GetIdentifierFromName(SelectedLanguage); + + Locale.Set(identifier); + App.Settings.Prop.Locale = identifier; CloseRequestEvent?.Invoke(this, new()); }