Skip to content

Commit

Permalink
Split up form classes
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Jun 12, 2024
1 parent ad0abbd commit 2f8e755
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 98 deletions.
42 changes: 21 additions & 21 deletions Launcher/Extensions/FormEntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ public static class FormEntryExtensions
{
public static UserControl ToTemplatedControl(this FormEntry formEntry)
{
switch (formEntry.Type)
switch (formEntry)
{
case FormEntryType.TextInput:
return new TextInput(formEntry);
case FormEntryType.TextBox:
return new TextBox(formEntry);
case FormEntryType.Toggle:
return new Toggle(formEntry);
case FormEntryType.FilePicker:
case FormEntryType.FolderPicker:
case TextInputElement textInputElement:
return new TextInput(textInputElement);
case TextBoxElement textBoxElement:
return new TextBox(textBoxElement);
case ToggleElement toggleElement:
return new Toggle(toggleElement);
case FilePickerElement:
case FolderPickerElement:
return new FilePicker(formEntry);
case FormEntryType.ClickableLinkBox:
return new ClickableLinkBox(formEntry);
case FormEntryType.ButtonList:
return new ButtonList(formEntry);
case FormEntryType.Dropdown:
return new Dropdown(formEntry);
case FormEntryType.Separator:
return new Separator(formEntry);
case FormEntryType.Image:
return new ImageView(formEntry);
case FormEntryType.HorizontalPanel:
return new HorizontalPanel(formEntry);
case ClickableLinkBoxElement clickableLinkBoxElement:
return new ClickableLinkBox(clickableLinkBoxElement);
case ButtonListElement buttonListElement:
return new ButtonList(buttonListElement);
case DropdownElement dropdownElement:
return new Dropdown(dropdownElement);
case SeperatorElement separatorElement:
return new Separator(separatorElement);
case ImageElement imageElement:
return new ImageView(imageElement);
case HorizontalPanelElement horizontalPanelElement:
return new HorizontalPanel(horizontalPanelElement);
default:
throw new NotImplementedException();
}
Expand Down
6 changes: 3 additions & 3 deletions Launcher/Forms/FormTemplates/ButtonList.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ namespace Launcher.Forms.FormTemplates;

public partial class ButtonList : UserControl
{
private FormEntry _formEntry;
private ButtonListElement _formEntry;

public ButtonList()
{
InitializeComponent();
}

public ButtonList(FormEntry formEntry) : this()
public ButtonList(ButtonListElement formEntry) : this()
{
_formEntry = formEntry;

foreach (var x in _formEntry.ButtonList)
foreach (var x in _formEntry.Buttons)
{
Button b = new();
b.Content = x.Name;
Expand Down
4 changes: 2 additions & 2 deletions Launcher/Forms/FormTemplates/ClickableLinkBox.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace Launcher.Forms.FormTemplates;

public partial class ClickableLinkBox : UserControl
{
private FormEntry _formEntry;
private ClickableLinkBoxElement _formEntry;

public ClickableLinkBox()
{
InitializeComponent();
}

public ClickableLinkBox(FormEntry formEntry) : this()
public ClickableLinkBox(ClickableLinkBoxElement formEntry) : this()
{
_formEntry = formEntry;
Button.Content = formEntry.Name;
Expand Down
2 changes: 1 addition & 1 deletion Launcher/Forms/FormTemplates/Dropdown.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Dropdown()
InitializeComponent();
}

public Dropdown(FormEntry formEntry) : this()
public Dropdown(DropdownElement formEntry) : this()
{
ComboBox.ItemsSource = formEntry.DropdownOptions;
Label.Content = formEntry.Name;
Expand Down
2 changes: 1 addition & 1 deletion Launcher/Forms/FormTemplates/FilePicker.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private void Update()

public async void OnBrowse()
{
if (_formEntry.Type == FormEntryType.FilePicker)
if (_formEntry is FilePickerElement)
{
OpenFileDialog dialog = new();
dialog.AllowMultiple = false;
Expand Down
4 changes: 2 additions & 2 deletions Launcher/Forms/FormTemplates/HorizontalPanel.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public HorizontalPanel()
InitializeComponent();
}

public HorizontalPanel(FormEntry formEntry) : this()
public HorizontalPanel(HorizontalPanelElement formEntry) : this()
{
Core.HorizontalAlignment = formEntry.Alignment.ToAvaloniaAlignment();
StackPanel.Spacing = int.Parse(formEntry.Value);

foreach (var entry in formEntry.HorizontalPanel)
foreach (var entry in formEntry.Entries)
{
StackPanel.Children.Add(entry.ToTemplatedControl());
}
Expand Down
8 changes: 4 additions & 4 deletions Launcher/Forms/FormTemplates/ImageView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace Launcher.Forms.FormTemplates;

public partial class ImageView : UserControl
{
private FormEntry _formEntry;
private ImageElement _formEntry;

public ImageView()
{
InitializeComponent();
}

public ImageView(FormEntry formEntry) : this()
public ImageView(ImageElement formEntry) : this()
{
_formEntry = formEntry;

Expand All @@ -28,8 +28,8 @@ public ImageView(FormEntry formEntry) : this()
else
Label.Content = formEntry.Name;

if (formEntry.LinkClick != null)
StackPanel.PointerPressed += (sender, args) => formEntry.LinkClick(formEntry);
if (formEntry.Click != null)
StackPanel.PointerPressed += (sender, args) => formEntry.Click(formEntry);

SetImage();
}
Expand Down
21 changes: 10 additions & 11 deletions Launcher/Launcher/CustomBootProfileGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ public void CreateProfileForm(string warnMessage = "")

List<FormEntry> entries = new()
{
new(FormEntryType.TextBox, $"{createOrEdit} a custom app wrapper", "Bold", alignment: FormAlignment.Center),
new(FormEntryType.TextInput, "Name:", _profile.Name),
new(FormEntryType.TextInput, "Executable:", _profile.Executable),
new(FormEntryType.TextInput, "Args:", _profile.Args),
new (FormEntryType.TextBox, "Template replaces:\n- {EXEC}: Gets replaced with the executable\n- {ARGS}: Gets replaced with the arguments passed to the executable\n- {WORKDIR}: Gets replaced with the working directory of the executable"),
new(FormEntryType.TextInput, "Environment:", _profile.EnviromentVariables),
new(FormEntryType.Dropdown, "Target Executable:",
_profile.CompatibleExecutable == Platform.Windows ? "Windows" : "Linux",
dropdownOptions: new() {"Windows", "Linux"}),
new (FormEntryType.Toggle, "Escape special characters (Linux only)", _profile.EscapeReplaceables ? "1" : "0"),
Form.TextBox($"{createOrEdit} a custom app wrapper", FormAlignment.Center, "Bold"),
Form.TextInput("Name:", _profile.Name),
Form.TextInput("Executable:", _profile.Executable),
Form.TextInput("Args:", _profile.Args),
Form.TextBox("Template replaces:\n- {EXEC}: Gets replaced with the executable\n- {ARGS}: Gets replaced with the arguments passed to the executable\n- {WORKDIR}: Gets replaced with the working directory of the executable"),
Form.TextInput("Environment:", _profile.EnviromentVariables),
Form.Dropdown("Target Executable:",
dropdownOptions: new() {"Windows", "Linux"}, _profile.CompatibleExecutable == Platform.Windows ? "Windows" : "Linux"),
Form.Toggle("Escape special characters (Linux only)", _profile.EscapeReplaceables),
Form.Button("Back", _ => _app.HideForm(), "Save", x =>
{
_app.HideForm();
Expand All @@ -47,7 +46,7 @@ public void CreateProfileForm(string warnMessage = "")
};

if (warnMessage != "")
entries.Add(new(FormEntryType.TextBox, warnMessage, "Bold", alignment: FormAlignment.Center));
entries.Add(Form.TextBox(warnMessage, alignment: FormAlignment.Center, "Bold"));

_app.ShowForm(new(entries));
}
Expand Down
23 changes: 12 additions & 11 deletions LauncherGamePlugin/Forms/Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,31 @@ public static Form CreateDismissibleTextPrompt(string text, IApp app)
});
}

public static FormEntry TextInput(string label, string value = "") => new(FormEntryType.TextInput, label, value);
public static FormEntry TextInput(string label, string value = "")
=> new TextInputElement(label, value);

public static FormEntry TextBox(string text, FormAlignment alignment = FormAlignment.Default,
string fontWeight = "")
=> new(FormEntryType.TextBox, text, fontWeight, alignment: alignment);
=> new TextBoxElement(text, fontWeight, alignment: alignment);

public static FormEntry ClickableLinkBox(string text, Action<Form> action,
FormAlignment alignment = FormAlignment.Default, string fontWeight = "")
=> new(FormEntryType.ClickableLinkBox, text, alignment: alignment, linkClick: x => action(x.ContainingForm), value: fontWeight);
=> new ClickableLinkBoxElement(text, alignment: alignment, linkClick: x => action(x.ContainingForm), value: fontWeight);

public static FormEntry Toggle(string label, bool value, FormAlignment alignment = FormAlignment.Default, bool enabled = true)
=> new(FormEntryType.Toggle, label, value ? "1" : "0", alignment: alignment, enabled: enabled);
=> new ToggleElement(label, value ? "1" : "0", alignment: alignment, enabled: enabled);

public static FormEntry FilePicker(string label, string value = "")
=> new(FormEntryType.FilePicker, label, value);
=> new FilePickerElement(label, value);

public static FormEntry FolderPicker(string label, string value = "")
=> new(FormEntryType.FolderPicker, label, value);
=> new FolderPickerElement(label, value);

public static FormEntry Dropdown(string label, List<string> dropdownOptions, string value = "")
=> new(FormEntryType.Dropdown, label, value, dropdownOptions: dropdownOptions);
=> new DropdownElement(label, value, dropdownOptions: dropdownOptions);

public static FormEntry ButtonList(List<ButtonEntry> buttons, FormAlignment alignment = FormAlignment.Default)
=> new(FormEntryType.ButtonList, buttonList: buttons, alignment: alignment);
=> new ButtonListElement(buttons: buttons, alignment: alignment);

public static FormEntry Button(string label, Action<Form> action, FormAlignment alignment = FormAlignment.Default)
=> ButtonList(new() {new(label, action)}, alignment);
Expand All @@ -80,13 +81,13 @@ public static FormEntry Button(string label1, Action<Form> action1, string label
=> ButtonList(new() {new(label1, action1), new(label2, action2), new(label3, action3)}, alignment);

public static FormEntry Image(string label, Func<Task<byte[]?>> image, Action<Form>? onClick = null, FormAlignment alignment = FormAlignment.Default)
=> new(FormEntryType.Image, label, image: image, alignment: alignment, linkClick: x => onClick?.Invoke(x.ContainingForm));
=> new ImageElement(label, getImage: image, alignment: alignment, click: x => onClick?.Invoke(x.ContainingForm));

public static FormEntry Horizontal(List<FormEntry> entries, int spacing = 5, FormAlignment alignment = FormAlignment.Default)
=> new(FormEntryType.HorizontalPanel, horizontalPanel: entries, value: spacing.ToString(), alignment: alignment);
=> new HorizontalPanelElement(entries: entries, value: spacing.ToString(), alignment: alignment);

public static FormEntry Separator(int height = 1)
=> new(FormEntryType.Separator, value: height.ToString());
=> new SeperatorElement(value: height.ToString());
}

public static class FormExtensions
Expand Down
Loading

0 comments on commit 2f8e755

Please sign in to comment.