• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6603144428

22 Oct 2023 10:10AM UTC coverage: 43.896% (-0.2%) from 44.094%
6603144428

Pull #54

github

web-flow
Merge 6b56375be into 6b38fe9a4
Pull Request #54: Feature/rework override default

765 of 2403 branches covered (0.0%)

Branch coverage included in aggregate %.

97 of 97 new or added lines in 10 files covered. (100.0%)

3902 of 8229 relevant lines covered (47.42%)

20421.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Key2Joy.Gui/ConfigForm.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Drawing;
4
using System.Reflection;
5
using System.Windows.Forms;
6
using CommonServiceLocator;
7
using Key2Joy.Config;
8
using Key2Joy.Contracts.Util;
9

10
namespace Key2Joy.Gui;
11

12
public partial class ConfigForm : Form
13
{
14
    private readonly ConfigState configState;
15

16
    public ConfigForm()
×
17
    {
18
        this.configState = ServiceLocator.Current
×
19
            .GetInstance<IConfigManager>()
×
20
            .GetConfigState();
×
21

22
        this.InitializeComponent();
×
23

24
        var configs = ConfigControlAttribute.GetAllProperties(typeof(ConfigState), new AttributeProvider());
×
25

26
        foreach (var kvp in configs)
×
27
        {
28
            var property = kvp.Key;
×
29
            var attribute = kvp.Value;
×
30

31
            Panel controlParent = new();
×
32
            var value = property.GetValue(this.configState);
×
33
            var control = this.MakeControl(attribute, value, controlParent);
×
34
            control.Tag = kvp;
×
35
            control.Dock = DockStyle.Bottom;
×
36

37
            controlParent.AutoSize = true;
×
38
            controlParent.Padding = new Padding(10, 10, 10, 0);
×
39
            controlParent.Controls.Add(control);
×
40

41
            this.pnlConfigurations.Controls.Add(controlParent);
×
42
            controlParent.Dock = DockStyle.Bottom;
×
43

44
            if (!string.IsNullOrWhiteSpace(attribute.Hint))
×
45
            {
46
                var wrapped = attribute.Hint.Wrap(80);
×
47

48
                Label label = new()
×
49
                {
×
50
                    AutoSize = true,
×
51
                    Dock = DockStyle.Bottom,
×
52
                    Text = wrapped,
×
53
                    ForeColor = Color.Gray,
×
54
                    Font = new Font(this.Font, FontStyle.Italic),
×
55
                };
×
56
                controlParent.Controls.Add(label);
×
57
            }
58
        }
59

60
        this.PerformLayout();
×
61
    }
×
62

63
    private void BtnSave_Click(object sender, EventArgs e)
64
    {
65
        foreach (Panel parents in this.pnlConfigurations.Controls)
×
66
        {
67
            foreach (Control control in parents.Controls)
×
68
            {
69
                if (control.Tag == null)
×
70
                {
71
                    continue;
72
                }
73

74
                var kvp = (KeyValuePair<PropertyInfo, ConfigControlAttribute>)control.Tag;
×
75
                var property = kvp.Key;
×
76
                var attribute = kvp.Value;
×
77
                var value = this.GetControlValue(attribute, control);
×
78

79
                value = Convert.ChangeType(value, property.PropertyType);
×
80

81
                property.SetValue(this.configState, value);
×
82
            }
83
        }
84

85
        MessageBox.Show("Configurations successfully saved!", "Configurations saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
×
86
        this.Close();
×
87
    }
×
88

89
    private Control MakeControl(ConfigControlAttribute attribute, object value, Panel controlParent)
90
    {
91
        void CreateLabel()
92
        {
93
            Label label = new()
×
94
            {
×
95
                AutoSize = true,
×
96
                Dock = DockStyle.Bottom,
×
97
                Text = $"{attribute.Text}: "
×
98
            };
×
99
            controlParent.Controls.Add(label);
×
100
        }
×
101

102
        switch (attribute)
103
        {
104
            case BooleanConfigControlAttribute booleanConfigControlAttribute:
105
            {
106
                CheckBox control = new()
×
107
                {
×
108
                    Text = booleanConfigControlAttribute.Text,
×
109
                    Checked = (bool)value
×
110
                };
×
111

112
                return control;
×
113
            }
114
            case NumericConfigControlAttribute numericConfigControlAttribute:
115
            {
116
                CreateLabel();
×
117
                NumericUpDown control = new()
×
118
                {
×
119
                    Minimum = (decimal)numericConfigControlAttribute.Minimum,
×
120
                    Maximum = (decimal)numericConfigControlAttribute.Maximum,
×
121
                    Value = (decimal)Convert.ChangeType(value, typeof(decimal))
×
122
                };
×
123

124
                return control;
×
125
            }
126
            case TextConfigControlAttribute textConfigControlAttribute:
127
            {
128
                CreateLabel();
×
129
                TextBox control = new()
×
130
                {
×
131
                    Text = value.ToString(),
×
132
                    MaxLength = textConfigControlAttribute.MaxLength
×
133
                };
×
134

135
                return control;
×
136
            }
137
            case EnumConfigControlAttribute enumConfigControlAttribute:
138
            {
139
                CreateLabel();
×
140
                var enumValues = Enum.GetValues(enumConfigControlAttribute.EnumType);
×
141
                var selected = Enum.Parse(enumConfigControlAttribute.EnumType, value.ToString());
×
142
                ComboBox control = new()
×
143
                {
×
144
                    DropDownStyle = ComboBoxStyle.DropDownList,
×
145
                };
×
146

147
                foreach (var enumValue in enumValues)
×
148
                {
149
                    control.Items.Add(enumValue);
×
150
                }
151

152
                control.SelectedIndex = Array.IndexOf(enumValues, selected);
×
153

154
                return control;
×
155
            }
156

157
            default:
158
                break;
159
        }
160

161
        throw new NotImplementedException("ConfigControlAttribute type not implemented: " + attribute.GetType().Name);
×
162
    }
163

164
    private object GetControlValue(ConfigControlAttribute attribute, Control control)
165
    {
166
        switch (attribute)
167
        {
168
            case BooleanConfigControlAttribute:
169
            {
170
                var checkbox = (CheckBox)control;
×
171
                return checkbox.Checked;
×
172
            }
173
            case NumericConfigControlAttribute:
174
            {
175
                var numeric = (NumericUpDown)control;
×
176
                return numeric.Value;
×
177
            }
178
            case TextConfigControlAttribute:
179
            {
180
                var textbox = (TextBox)control;
×
181
                return textbox.Text;
×
182
            }
183
            case EnumConfigControlAttribute:
184
            {
185
                var combobox = (ComboBox)control;
×
186
                return combobox.SelectedItem;
×
187
            }
188

189
            default:
190
                break;
191
        }
192

193
        throw new NotImplementedException("ConfigControlAttribute type not implemented: " + attribute.GetType().Name);
×
194
    }
195
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc