Skip to content

Commit

Permalink
add Pick-again button
Browse files Browse the repository at this point in the history
  • Loading branch information
jjling2011 committed Aug 14, 2024
1 parent 61345e4 commit 54a883a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 52 deletions.
83 changes: 49 additions & 34 deletions MusicManager/Views/FormTagsEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 43 additions & 18 deletions MusicManager/Views/FormTagsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ private void FormTagsEditor_Load(object sender, EventArgs e)
}

#region UI handler
private void btnPickRandomMusic_Click(object sender, EventArgs e)
{
var idx = cboxSource.SelectedIndex;
UpdateMatchingText(idx);
UpdatePreviews();
}

private void btnClose_Click(object sender, EventArgs e)
{
Expand Down Expand Up @@ -62,17 +68,7 @@ private void cboxSource_SelectedIndexChanged(object sender, EventArgs e)
Properties.Settings.Default.tagsEditorIndex = idx;
Properties.Settings.Default.Save();

var tag = "";
try
{
var file = GetFirstMusic();
tag = GetTagFromFile(idx, file);
}
catch { }

lbMatching.Text = tag;
toolTip1.SetToolTip(lbMatching, tag);

UpdateMatchingText(idx);
UpdatePreviews();
}

Expand Down Expand Up @@ -175,7 +171,8 @@ void TryUpdatePreview(Label label, TextBox tbox)
text = Regex.Replace(tag, matchRegex, repalceRegex);
}
catch { }
label.Text = text;
label.Text = $"\"{text}\"";
toolTip1.SetToolTip(label, label.Text);
}

void UpdatePreviews()
Expand All @@ -185,17 +182,31 @@ void UpdatePreviews()
TryUpdatePreview(lbTitle, tboxTitle);
}

string GetFirstMusic()
string PickOneRandomMusic()
{
var src = tboxFolder.Text;
foreach (
string file in Directory.EnumerateFiles(src, "*.*", SearchOption.AllDirectories)
)

var r = new Random(Guid.NewGuid().GetHashCode());
var c = 1;
var pick = "";

foreach (var file in Directory.EnumerateFiles(src, "*.*", SearchOption.AllDirectories))
{
if (Utils.Tools.IsMusicFile(file))
if (!Utils.Tools.IsMusicFile(file))
{
continue;
}

// https://stackoverflow.com/questions/32328620/read-random-line-from-a-large-text-file
if (r.Next(c) == 0)
{
return file;
pick = file;
}
c++;
}
if (!string.IsNullOrEmpty(pick))
{
return pick;
}
throw new Exception("find no music file");
}
Expand Down Expand Up @@ -281,6 +292,20 @@ string album
}
}

void UpdateMatchingText(int idx)
{
var tag = "";
try
{
var file = PickOneRandomMusic();
tag = GetTagFromFile(idx, file);
}
catch { }

lbMatching.Text = tag;
toolTip1.SetToolTip(lbMatching, tag);
}

private string GetTagFromFile(int index, string file)
{
var tags = TagLib.File.Create(file).Tag;
Expand Down

0 comments on commit 54a883a

Please sign in to comment.