-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
384 lines (340 loc) · 18 KB
/
Copy pathProgram.cs
File metadata and controls
384 lines (340 loc) · 18 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using TagLib;
namespace TrueKit
{
public class LogForm : Form
{
public RichTextBox LogBox;
public LogForm()
{
this.Text = "TrueKit Debug Console";
this.Size = new Size(550, 450);
this.BackColor = Color.FromArgb(24, 24, 26);
this.StartPosition = FormStartPosition.CenterScreen;
LogBox = new RichTextBox
{
Dock = DockStyle.Fill,
BackColor = Color.FromArgb(30, 30, 32),
ForeColor = Color.FromArgb(76, 175, 80),
ReadOnly = true,
BorderStyle = BorderStyle.None,
Font = new Font("Consolas", 10),
Padding = new Padding(10)
};
this.Controls.Add(LogBox);
LoadIconFromResource(this);
this.FormClosing += (s, e) => { e.Cancel = true; this.Hide(); };
}
private void LoadIconFromResource(Form form)
{
try
{
var assembly = Assembly.GetExecutingAssembly();
string? resourceName = Array.Find(assembly.GetManifestResourceNames(), name => name.EndsWith("icon.ico"));
if (!string.IsNullOrEmpty(resourceName))
{
using (Stream? stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream != null) form.Icon = new Icon(stream);
}
}
}
catch { }
}
public void AddLog(string msg)
{
if (LogBox.InvokeRequired) LogBox.Invoke(new Action(() => AddLog(msg)));
else LogBox.AppendText($"[{DateTime.Now:HH:mm:ss}] {msg}\n");
}
}
public class MainForm : Form
{
private readonly Color ClrBack = Color.FromArgb(24, 24, 26);
private readonly Color ClrPanel = Color.FromArgb(32, 32, 35);
private readonly Color ClrInput = Color.FromArgb(45, 45, 50);
private readonly Color ClrText = Color.FromArgb(240, 240, 240);
private readonly Color ClrAccent = Color.FromArgb(94, 136, 252);
private readonly Color ClrAccentHover = Color.FromArgb(114, 156, 255);
private readonly Color ClrDim = Color.FromArgb(140, 140, 150);
private Panel dropZone = null!;
private PictureBox picCover = null!;
private Button btnSave = null!, btnClear = null!, btnExtractCover = null!, btnDebug = null!;
private Label lblStatus = null!;
private LogForm debugWindow = new LogForm();
private System.Windows.Forms.TextBox txtTitle = null!, txtArtist = null!, txtAlbum = null!, txtYear = null!,
txtGenre = null!, txtTrack = null!, txtDisc = null!, txtBpm = null!, txtComposer = null!, txtComment = null!;
private string? currentSourcePath;
private byte[]? pendingCoverData;
public MainForm()
{
SetupWindow();
InitializeUI();
debugWindow.AddLog("TrueKit Audio Tag Editor Initialized.");
}
private void SetupWindow()
{
this.Text = " TrueKit — Audio Tag Editor PRO";
this.Size = new Size(1200, 680);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.BackColor = ClrBack;
this.ForeColor = ClrText;
this.Font = new Font("Segoe UI", 10F, FontStyle.Regular);
this.StartPosition = FormStartPosition.CenterScreen;
try
{
var assembly = Assembly.GetExecutingAssembly();
string? resourceName = Array.Find(assembly.GetManifestResourceNames(), name => name.EndsWith("icon.ico"));
if (!string.IsNullOrEmpty(resourceName))
{
using (Stream? stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream != null) this.Icon = new Icon(stream);
}
}
}
catch (Exception ex)
{
debugWindow.AddLog("Resource Icon error: " + ex.Message);
}
}
private void InitializeUI()
{
var mainLayout = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 1, RowCount = 3, Padding = new Padding(25) };
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 90F)); // Зона дропа стала чуть больше
mainLayout.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
mainLayout.RowStyles.Add(new RowStyle(SizeType.Absolute, 70F));
dropZone = new Panel { Dock = DockStyle.Fill, BackColor = ClrPanel, AllowDrop = true, Cursor = Cursors.Hand };
var lblDrop = new Label
{
Text = "DRAG & DROP AUDIO FILE HERE",
TextAlign = ContentAlignment.MiddleCenter,
Dock = DockStyle.Fill,
Font = new Font("Segoe UI", 13, FontStyle.Bold),
ForeColor = ClrDim,
Enabled = true
};
lblDrop.Click += (s, e) => SelectAudioFile();
dropZone.Controls.Add(lblDrop);
dropZone.Click += (s, e) => SelectAudioFile();
dropZone.DragEnter += (s, e) => { if (e.Data!.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Copy; dropZone.BackColor = ClrInput; lblDrop.ForeColor = ClrAccent; } };
dropZone.DragLeave += (s, e) => { dropZone.BackColor = ClrPanel; lblDrop.ForeColor = ClrDim; };
dropZone.DragDrop += (s, e) =>
{
dropZone.BackColor = ClrPanel;
lblDrop.ForeColor = ClrDim;
var files = (string[]?)e.Data?.GetData(DataFormats.FileDrop);
if (files != null && files.Length > 0 && System.IO.File.Exists(files[0]))
LoadAudioFile(files[0]);
};
var contentLayout = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 2, RowCount = 1 };
contentLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 320F));
contentLayout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
var coverLayout = new FlowLayoutPanel { Dock = DockStyle.Fill, FlowDirection = FlowDirection.TopDown };
picCover = new PictureBox { Size = new Size(300, 300), SizeMode = PictureBoxSizeMode.Zoom, BackColor = ClrPanel, Margin = new Padding(0, 20, 0, 15), Cursor = Cursors.Hand };
picCover.Click += (s, e) => SelectCoverImage();
btnExtractCover = CreateButton("EXTRACT COVER", ClrInput, false);
btnExtractCover.Width = 300;
btnExtractCover.Height = 40;
btnExtractCover.Enabled = false;
btnExtractCover.Click += (s, e) => ExtractCover();
coverLayout.Controls.AddRange(new Control[] { picCover, btnExtractCover });
var fieldsGrid = new TableLayoutPanel { Dock = DockStyle.Fill, ColumnCount = 4, RowCount = 10, Padding = new Padding(20, 10, 0, 0) };
fieldsGrid.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 18));
fieldsGrid.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 32));
fieldsGrid.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 18));
fieldsGrid.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 32));
txtTitle = AddField(fieldsGrid, "Title", 0, 0, 3);
txtArtist = AddField(fieldsGrid, "Artist", 1, 0, 3);
txtAlbum = AddField(fieldsGrid, "Album", 2, 0, 3);
txtComposer = AddField(fieldsGrid, "Composer", 3, 0, 3);
txtGenre = AddField(fieldsGrid, "Genre", 4, 0, 3);
txtYear = AddField(fieldsGrid, "Year", 5, 0);
txtBpm = AddField(fieldsGrid, "BPM", 5, 2);
txtTrack = AddField(fieldsGrid, "Track #", 6, 0);
txtDisc = AddField(fieldsGrid, "Disc #", 6, 2);
txtComment = AddField(fieldsGrid, "Note", 7, 0, 3);
contentLayout.Controls.Add(coverLayout, 0, 0);
contentLayout.Controls.Add(fieldsGrid, 1, 0);
var bottomLayout = new Panel { Dock = DockStyle.Fill };
btnSave = CreateButton("SAVE TAGS", ClrAccent, true);
btnSave.Location = new Point(950, 10);
btnSave.Size = new Size(180, 45);
btnSave.ForeColor = Color.White;
btnSave.Enabled = false;
btnClear = CreateButton("CLEAR", ClrInput, false);
btnClear.Location = new Point(810, 10);
btnClear.Size = new Size(130, 45);
btnDebug = CreateButton("DEBUG", ClrBack, false);
btnDebug.Location = new Point(710, 10);
btnDebug.Size = new Size(90, 45);
btnDebug.Click += (s, e) => debugWindow.Show();
lblStatus = new Label { Text = "Status: Waiting for file...", AutoSize = false, Width = 600, Height = 45, Location = new Point(0, 10), TextAlign = ContentAlignment.MiddleLeft, ForeColor = ClrDim, Font = new Font("Segoe UI", 10, FontStyle.Italic) };
btnSave.Click += OnSaveClick;
btnClear.Click += (s, e) => ClearAll();
bottomLayout.Controls.AddRange(new Control[] { btnSave, btnClear, btnDebug, lblStatus });
mainLayout.Controls.Add(dropZone, 0, 0);
mainLayout.Controls.Add(contentLayout, 0, 1);
mainLayout.Controls.Add(bottomLayout, 0, 2);
this.Controls.Add(mainLayout);
}
private void SetCoverImage(byte[] data)
{
try
{
using var ms = new MemoryStream(data);
using var tempImg = Image.FromStream(ms);
var oldImage = picCover.Image;
picCover.Image = new Bitmap(tempImg);
oldImage?.Dispose();
}
catch (Exception ex)
{
debugWindow.AddLog("Cover loading error: " + ex.Message);
}
}
private void SelectAudioFile()
{
using var ofd = new OpenFileDialog { Filter = "Audio Files|*.mp3;*.flac;*.wav;*.m4a;*.ogg" };
if (ofd.ShowDialog() == DialogResult.OK) LoadAudioFile(ofd.FileName);
}
private void SelectCoverImage()
{
using var ofd = new OpenFileDialog { Filter = "Images|*.jpg;*.png;*.jpeg" };
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
pendingCoverData = System.IO.File.ReadAllBytes(ofd.FileName);
SetCoverImage(pendingCoverData);
btnExtractCover.Enabled = true;
debugWindow.AddLog("New cover image selected.");
}
catch { debugWindow.AddLog("Failed to load image."); }
}
}
private void ClearAll()
{
debugWindow.AddLog("Workspace cleared.");
txtTitle.Clear(); txtArtist.Clear(); txtAlbum.Clear(); txtYear.Clear();
txtGenre.Clear(); txtTrack.Clear(); txtDisc.Clear(); txtBpm.Clear();
txtComposer.Clear(); txtComment.Clear();
var oldImage = picCover.Image;
picCover.Image = null;
oldImage?.Dispose();
pendingCoverData = null;
btnExtractCover.Enabled = false;
btnSave.Enabled = false;
currentSourcePath = null;
lblStatus.Text = "Status: Waiting for file...";
lblStatus.ForeColor = ClrDim;
}
private System.Windows.Forms.TextBox AddField(TableLayoutPanel panel, string labelText, int row, int col, int colSpan = 1)
{
panel.Controls.Add(new Label { Text = labelText, Dock = DockStyle.Fill, TextAlign = ContentAlignment.MiddleRight, ForeColor = ClrDim, Font = new Font("Segoe UI Semibold", 9.5F) }, col, row);
var tb = new System.Windows.Forms.TextBox { Dock = DockStyle.Fill, BackColor = ClrInput, ForeColor = ClrText, BorderStyle = BorderStyle.FixedSingle, Margin = new Padding(5, 5, 5, 5), Font = new Font("Segoe UI", 11) };
panel.Controls.Add(tb, col + 1, row);
if (colSpan > 1) panel.SetColumnSpan(tb, colSpan);
return tb;
}
private Button CreateButton(string text, Color baseColor, bool isPrimary)
{
var btn = new Button { Text = text, FlatStyle = FlatStyle.Flat, BackColor = baseColor, ForeColor = ClrText, Font = new Font("Segoe UI", 10, isPrimary ? FontStyle.Bold : FontStyle.Regular), Cursor = Cursors.Hand };
btn.FlatAppearance.BorderSize = 0;
btn.MouseEnter += (s, e) => btn.BackColor = isPrimary ? ClrAccentHover : ControlPaint.Light(baseColor, 0.1f);
btn.MouseLeave += (s, e) => btn.BackColor = baseColor;
return btn;
}
private void LoadAudioFile(string path)
{
try
{
debugWindow.AddLog($"Loading: {Path.GetFileName(path)}");
currentSourcePath = path;
using var tfile = TagLib.File.Create(path);
txtTitle.Text = tfile.Tag.Title ?? "";
txtArtist.Text = tfile.Tag.Performers != null ? string.Join("; ", tfile.Tag.Performers) : "";
txtAlbum.Text = tfile.Tag.Album ?? "";
txtYear.Text = tfile.Tag.Year != 0 ? tfile.Tag.Year.ToString() : "";
txtGenre.Text = tfile.Tag.Genres != null ? string.Join("; ", tfile.Tag.Genres) : "";
txtTrack.Text = tfile.Tag.Track != 0 ? tfile.Tag.Track.ToString() : "";
txtDisc.Text = tfile.Tag.Disc != 0 ? tfile.Tag.Disc.ToString() : "";
txtBpm.Text = tfile.Tag.BeatsPerMinute != 0 ? tfile.Tag.BeatsPerMinute.ToString() : "";
txtComposer.Text = tfile.Tag.Composers != null ? string.Join("; ", tfile.Tag.Composers) : "";
txtComment.Text = tfile.Tag.Comment ?? "";
if (tfile.Tag.Pictures.Length > 0)
{
pendingCoverData = tfile.Tag.Pictures[0].Data.Data;
SetCoverImage(pendingCoverData);
btnExtractCover.Enabled = true;
}
else
{
var oldImage = picCover.Image;
picCover.Image = null;
oldImage?.Dispose();
pendingCoverData = null;
btnExtractCover.Enabled = false;
}
btnSave.Enabled = true;
lblStatus.Text = "Loaded: " + Path.GetFileName(path);
lblStatus.ForeColor = ClrAccent;
}
catch (Exception ex) { debugWindow.AddLog("ERROR: " + ex.Message); }
}
private void ExtractCover()
{
if (pendingCoverData == null) return;
using var sfd = new SaveFileDialog { Filter = "JPEG Image|*.jpg|PNG Image|*.png", FileName = "TrueKit_Cover" };
if (sfd.ShowDialog() == DialogResult.OK)
{
System.IO.File.WriteAllBytes(sfd.FileName, pendingCoverData);
debugWindow.AddLog("Cover extracted successfully.");
}
}
private void OnSaveClick(object? sender, EventArgs e)
{
if (currentSourcePath == null) return;
using var sfd = new SaveFileDialog { Filter = "Audio File|*" + Path.GetExtension(currentSourcePath), FileName = Path.GetFileName(currentSourcePath) };
if (sfd.ShowDialog() == DialogResult.OK)
{
try
{
System.IO.File.Copy(currentSourcePath, sfd.FileName, true);
using var tfile = TagLib.File.Create(sfd.FileName);
tfile.Tag.Title = txtTitle.Text;
tfile.Tag.Performers = txtArtist.Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
tfile.Tag.Album = txtAlbum.Text;
tfile.Tag.Year = uint.TryParse(txtYear.Text, out var y) ? y : 0;
tfile.Tag.Genres = txtGenre.Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
tfile.Tag.Track = uint.TryParse(txtTrack.Text, out var t) ? t : 0;
tfile.Tag.Disc = uint.TryParse(txtDisc.Text, out var d) ? d : 0;
tfile.Tag.BeatsPerMinute = uint.TryParse(txtBpm.Text, out var b) ? b : 0;
tfile.Tag.Composers = txtComposer.Text.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
tfile.Tag.Comment = txtComment.Text;
if (pendingCoverData != null)
tfile.Tag.Pictures = new TagLib.IPicture[] { new TagLib.Picture(new TagLib.ByteVector(pendingCoverData)) };
else
tfile.Tag.Pictures = Array.Empty<TagLib.IPicture>();
tfile.Save();
debugWindow.AddLog("File saved successfully.");
lblStatus.Text = "Saved Successfully: " + Path.GetFileName(sfd.FileName);
lblStatus.ForeColor = Color.FromArgb(76, 175, 80); // Успешный зеленый
}
catch (Exception ex) { debugWindow.AddLog("SAVE ERROR: " + ex.Message); }
}
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}