From a64ea37a00719a3869b99b9307a8400660f28b95 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 17 Sep 2022 12:31:12 +0200 Subject: [PATCH] Show the DeepL usage as part of the settings view --- .../UI WinForms/Components/Setting.cs | 87 ++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/I18N Commander/UI WinForms/Components/Setting.cs b/I18N Commander/UI WinForms/Components/Setting.cs index 639487b..f9d7e2a 100644 --- a/I18N Commander/UI WinForms/Components/Setting.cs +++ b/I18N Commander/UI WinForms/Components/Setting.cs @@ -6,6 +6,8 @@ namespace UI_WinForms.Components; public sealed partial class Setting : UserControl { + private SettingUIData data; + public Setting() { this.InitializeComponent(); @@ -15,6 +17,7 @@ public sealed partial class Setting : UserControl { this.InitializeComponent(); this.Dock = DockStyle.Top; + this.data = settingMetaData; this.labelIcon.Image = settingMetaData.Icon; this.labelSettingName.Text = settingMetaData.SettingName(); this.labelExplanation.Text = settingMetaData.SettingExplanation(); @@ -38,6 +41,8 @@ public sealed partial class Setting : UserControl dataControl.Margin = new Padding(0, (int) margin, 0, (int)margin); }; } + + private void UpdateExplanation() => this.labelExplanation.Text = this.data.SettingExplanation(); private readonly record struct SettingUIData( Bitmap Icon, @@ -109,6 +114,85 @@ public sealed partial class Setting : UserControl return new Setting(settingData); } + private static async Task ShowDeepLUsageSettingAsync() + { + var currentUsage = await Processor.DeepL.GetUsage(); + var percent = currentUsage.Enabled ? currentUsage.CharacterCount / (float)currentUsage.CharacterLimit : 0; + + // Local function to show & update the explanation text: + string GetUsageText() => currentUsage.Enabled ? + $"You used {currentUsage.CharacterCount:###,###,###,##0} characters out of {currentUsage.CharacterLimit:###,###,###,##0} ({percent:P2})." : currentUsage.AuthIssue ? + "Was not able to authorize with DeepL. Please check your API key." : + "DeepL is disabled or the API key is not set."; + + var settingData = new SettingUIData( + Icon: Icons.icons8_increase_512, + SettingName: () => "DeepL Usage", + SettingExplanation: GetUsageText, + SetupDataControl: () => + { + var progressbar = new ProgressBar(); + progressbar.Maximum = 100; + progressbar.Margin = new Padding(0, 16, 0, 16); + progressbar.Dock = DockStyle.Fill; + progressbar.Style = ProgressBarStyle.Continuous; + progressbar.Value = percent switch + { + < 0 => 0, + > 1 => 100, + + _ => (int)(percent * 100) + }; + + var reloadButton = new Button(); + reloadButton.Text = string.Empty; + reloadButton.Image = Icons.icons8_reload_512; + reloadButton.FlatStyle = FlatStyle.Flat; + reloadButton.FlatAppearance.BorderSize = 0; + reloadButton.BackColor = Color.Empty; + reloadButton.UseVisualStyleBackColor = true; + reloadButton.Size = new Size(60, 60); + reloadButton.Click += async (sender, args) => + { + var usage = await Processor.DeepL.GetUsage(); + + // Update the outer variables: + percent = usage.Enabled ? usage.CharacterCount / (float)usage.CharacterLimit : 0; + currentUsage = usage; + + // Update the progress bar: + progressbar.Value = percent switch + { + < 0 => 0, + > 1 => 100, + + _ => (int)(percent * 100) + }; + + // Update the explanation text. Therefore, we need to get the setting object through the chain of parents: + var setting = (Setting) ((Control) sender).Parent.Parent.Parent; + setting.UpdateExplanation(); + }; + + // Setup the layout: + var layout = new TableLayoutPanel(); + layout.ColumnCount = 2; + layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100)); + layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 66)); + layout.RowCount = 1; + layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100)); + layout.Controls.Add(progressbar, 0, 0); + layout.Controls.Add(reloadButton, 1, 0); + layout.Dock = DockStyle.Fill; + + return layout; + } + ); + + var setting = new Setting(settingData); + return setting; + } + private static async Task ShowDeepLActionSettingAsync() { var currentSetting = await AppSettings.GetDeepLAction(); @@ -264,7 +348,8 @@ public sealed partial class Setting : UserControl { yield return setting; } - + + yield return ShowDeepLUsageSettingAsync(); yield return ShowDeepLActionSettingAsync(); yield return ShowDeepLAPIKeySettingAsync(); yield return ShowDeepLModeSettingAsync();