Show the DeepL usage as part of the settings view
This commit is contained in:
parent
86b1f8348e
commit
a64ea37a00
@ -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<Setting> 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<Setting> 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();
|
||||
|
Loading…
Reference in New Issue
Block a user