I18NCommander/I18N Commander/UI WinForms/Components/TextElements.cs

104 lines
3.3 KiB
C#

using DataModel.Database;
using Processor;
using UI_WinForms.Dialogs;
using UI_WinForms.Resources;
namespace UI_WinForms.Components;
public partial class TextElements : UserControl
{
private Section? currentSection;
public TextElements()
{
this.InitializeComponent();
// Check if we are in the designer:
if(Program.SERVICE_PROVIDER is null)
return;
// Create an image list from a resource:
var imgList = new ImageList();
imgList.ImageSize = new Size(45, 45);
imgList.ColorDepth = ColorDepth.Depth32Bit;
imgList.Images.Add(Icons.icons8_align_text_left_512);
// Set the image list to the list box:
this.listTextElements.SmallImageList = imgList;
// When the section is changed, update this component:
AppEvents.WhenSectionChanged += async (sender, section) =>
{
this.currentSection = section;
this.buttonAdd.Enabled = this.currentSection is not null;
// Update the path:
if (this.currentSection is null)
return;
this.labelSectionPath.Text = await SectionProcessor.GetSectionPath(this.currentSection.DataKey);
this.LoadTextElements();
};
}
// Loads all the text elements for the current section.
private async void LoadTextElements()
{
if (this.currentSection is null)
return;
// Load the text elements:
var textElements = await TextElementProcessor.GetTextElements(this.currentSection);
// Update the list:
this.listTextElements.Items.Clear();
foreach (var textElement in textElements)
this.listTextElements.Items.Add(new ListViewItem(textElement.Name, 0)
{
Tag = textElement.Code,
});
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
}
private async void buttonAdd_Click(object sender, EventArgs e)
{
if(this.DesignMode)
return;
var result = InputDialog.Show(new InputDialog.Options(
Message: "Please type the desired text element's name.",
Title: "Add a text element",
Placeholder: "My text element",
ShowQuestionCheckbox: false
));
if(result.DialogResult == DialogResult.Cancel)
return;
// Add the text element to the database into the current section:
var newTextElement = await TextElementProcessor.AddTextElement(this.currentSection?.DataKey, result.Text);
newTextElement.ProcessError();
if(!newTextElement.Successful)
return;
// Add the text element to the list:
this.listTextElements.Items.Add(new ListViewItem(newTextElement.Result!.Name, 0)
{
Tag = newTextElement.Result.Code,
});
this.column.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
}
private void buttonRemove_Click(object sender, EventArgs e)
{
}
private void buttonRename_Click(object sender, EventArgs e)
{
}
}