-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddButtons.xaml.cs
181 lines (147 loc) · 5.47 KB
/
AddButtons.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using Newtonsoft.Json;
using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
namespace Discord_Custom_RPC
{
/// <summary>
/// Логика взаимодействия для AddButtons.xaml
/// </summary>
public partial class AddButtons : Window
{
private readonly MainWindow? mainWindow;
private static readonly string directoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Discord Custom RPC");
private static readonly string jsonFilePath = Path.Combine(directoryPath, "lastConfig.json");
#region -- Параметры окна --
public AddButtons(MainWindow main)
{
InitializeComponent();
ReadJsonButtonsData();
mainWindow = main;
}
private void TitleBarButtons_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
#endregion
#region -- JSON --
private void ReadJsonButtonsData()
{
TextBoxButton1Text.Text = ReadJsonValue("ButtonFText");
TextBoxButton1Link.Text = ReadJsonValue("ButtonFLink");
TextBoxButton2Text.Text = ReadJsonValue("ButtonSText");
TextBoxButton2Link.Text = ReadJsonValue("ButtonSLink");
}
private static string ReadJsonValue(string propertyName)
{
if (File.Exists(jsonFilePath))
{
string json = File.ReadAllText(jsonFilePath);
LastConfigData? configData = JsonConvert.DeserializeObject<LastConfigData>(json);
// Рефлексия для получения значения свойства по имени
PropertyInfo? property = typeof(LastConfigData).GetProperty(propertyName);
if (property != null)
{
object? value = property.GetValue(configData);
return value?.ToString() ?? "";
}
}
return "";
}
#endregion
#region -- Взаимодействие с полями --
private void TextBoxButton1TextChanged(object sender, RoutedEventArgs e)
{
if (TextBoxButton1Text.Text == "")
{
TextButton1Watermark.Opacity = 1;
}
else
{
TextButton1Watermark.Opacity = 0;
}
}
private void TextBoxButton1LinkTextChanged(object sender, RoutedEventArgs e)
{
if (TextBoxButton1Link.Text == "")
{
TextButton1LinkWatermark.Opacity = 1;
}
else
{
TextButton1LinkWatermark.Opacity = 0;
}
}
private void TextBoxButton2TextChanged(object sender, RoutedEventArgs e)
{
if (TextBoxButton2Text.Text == "")
{
TextButton2Watermark.Opacity = 1;
}
else
{
TextButton2Watermark.Opacity = 0;
}
}
private void TextBoxButton2LinkTextChanged(object sender, RoutedEventArgs e)
{
if (TextBoxButton2Link.Text == "")
{
TextButton2LinkWatermark.Opacity = 1;
}
else
{
TextButton2LinkWatermark.Opacity = 0;
}
}
#endregion
#region -- Нажатие на кнопку "Save" --
private void ButtonSaveClick(object sender, RoutedEventArgs e)
{
LastConfigData configData = new()
{
ApplicationId = ReadJsonValue("ApplicationId"),
Details = ReadJsonValue("Details"),
State = ReadJsonValue("State"),
Timestamp = ReadJsonValue("Timestamp"),
LargeImageKey = ReadJsonValue("LargeImageKey"),
LargeImageText = ReadJsonValue("LargeImageText"),
SmallImageKey = ReadJsonValue("SmallImageKey"),
SmallImageText = ReadJsonValue("SmallImageText"),
ButtonFText = TextBoxButton1Text.Text,
ButtonFLink = TextBoxButton1Link.Text,
ButtonSText = TextBoxButton2Text.Text,
ButtonSLink = TextBoxButton2Link.Text,
Autostart = ReadJsonValue("Autostart"),
};
string updatedJson = JsonConvert.SerializeObject(configData, Formatting.Indented);
File.WriteAllText(jsonFilePath, updatedJson);
mainWindow?.ButtonCheckToEnabled();
Close();
}
#endregion
#region -- Закрытие окна --
// Обработчик закрытия неосновного окна
private void AddButtons_Closing(object sender, EventArgs e)
{
// Активирует кнопку на основном окне при закрытии
mainWindow?.ActivateButtonOnMainWindow();
}
private void ImageCloseClick(object? sender, EventArgs e)
{
Close();
}
// Работа с зоной крестика
private void RedSquareMouseEnter(object sender, EventArgs e)
{
RedSquare.Opacity = 1.0;
}
private void RedSquareMouseLeave(object sender, EventArgs e)
{
RedSquare.Opacity = 0;
}
#endregion
}
}