Skip to content

Commit

Permalink
Minor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Jan 4, 2025
1 parent 97f9a60 commit f72eab7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 27 deletions.
2 changes: 1 addition & 1 deletion JL.Windows/GUI/AddDictionaryWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<TextBlock Text="Path" TextWrapping="Wrap" Margin="0,10,0,10" HorizontalAlignment="Left" Style="{StaticResource TextBlockDefault}" />
<WrapPanel>
<TextBox Name="TextBlockPath" Width="400"
<TextBox Name="PathTextBlock" Width="400"
Text=""
Margin="10"
IsReadOnly="True" />
Expand Down
51 changes: 40 additions & 11 deletions JL.Windows/GUI/AddDictionaryWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using JL.Core.Dicts;
using JL.Core.Dicts.Options;
Expand Down Expand Up @@ -32,8 +33,11 @@ private void CancelButton_Click(object sender, RoutedEventArgs e)
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
ComboBoxDictType.ClearValue(BorderBrushProperty);
TextBlockPath.ClearValue(BorderBrushProperty);
PathTextBlock.ClearValue(BorderBrushProperty);
PathTextBlock.ClearValue(CursorProperty);
PathTextBlock.ClearValue(ToolTipProperty);
NameTextBox.ClearValue(BorderBrushProperty);
NameTextBox.ToolTip = "Dictionary name must be unique";

string? typeString = ComboBoxDictType.SelectionBoxItem.ToString();
if (string.IsNullOrEmpty(typeString))
Expand All @@ -42,34 +46,59 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
return;
}

string path = TextBlockPath.Text;
string path = PathTextBlock.Text;
string fullPath = Path.GetFullPath(path, Utils.ApplicationPath);

if (string.IsNullOrWhiteSpace(path)
|| !Path.Exists(fullPath)
|| DictUtils.Dicts.Values.Any(dict => dict.Path == path))
|| !Path.Exists(fullPath))
{
TextBlockPath.BorderBrush = Brushes.Red;
PathTextBlock.BorderBrush = Brushes.Red;
PathTextBlock.Cursor = Cursors.Help;
PathTextBlock.ToolTip = "Invalid path!";
return;
}

if (DictUtils.Dicts.Values.Any(dict => dict.Path == path))
{
PathTextBlock.BorderBrush = Brushes.Red;
PathTextBlock.Cursor = Cursors.Help;
PathTextBlock.ToolTip = "Dictionary path must be unique!";
return;
}

string name = NameTextBox.Text;
if (string.IsNullOrWhiteSpace(name)
|| name.Length > 128
|| name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0
|| DictUtils.Dicts.ContainsKey(name))
|| name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
{
NameTextBox.BorderBrush = Brushes.Red;
NameTextBox.ToolTip = "Invalid dictionary name!";
return;
}

if (DictUtils.Dicts.ContainsKey(name))
{
NameTextBox.BorderBrush = Brushes.Red;
NameTextBox.ToolTip = "Dictionary name must be unique!";
return;
}

DictType type = typeString.GetEnum<DictType>();
if (DictUtils.YomichanDictTypes.Contains(type))
{
bool validPath = Directory.EnumerateFiles(fullPath, type is DictType.NonspecificKanjiYomichan ? "kanji_bank_*.json" : "term_bank_*.json", SearchOption.TopDirectoryOnly).Any();
bool validPath = Directory.EnumerateFiles(fullPath,
type is DictType.NonspecificKanjiYomichan
? "kanji_bank_*.json"
: type is DictType.PitchAccentYomichan
? "term*bank_*.json"
: "term_bank_*.json",
SearchOption.TopDirectoryOnly).Any();

if (!validPath)
{
TextBlockPath.BorderBrush = Brushes.Red;
PathTextBlock.BorderBrush = Brushes.Red;
PathTextBlock.Cursor = Cursors.Help;
PathTextBlock.ToolTip = "No valid file was found at the specified path!";
return;
}
}
Expand All @@ -96,7 +125,7 @@ private void BrowseForDictionaryFile(string filter)

if (openFileDialog.ShowDialog() is true)
{
TextBlockPath.Text = Utils.GetPath(openFileDialog.FileName);
PathTextBlock.Text = Utils.GetPath(openFileDialog.FileName);
}
}

Expand All @@ -109,7 +138,7 @@ private void BrowseForDictionaryFolder()

if (openFolderDialog.ShowDialog() is true)
{
TextBlockPath.Text = Utils.GetPath(openFolderDialog.FolderName);
PathTextBlock.Text = Utils.GetPath(openFolderDialog.FolderName);
}
}

Expand Down
2 changes: 1 addition & 1 deletion JL.Windows/GUI/EditDictionaryWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<TextBlock Text="Path" TextWrapping="Wrap" Margin="0,10,0,10" HorizontalAlignment="Left" Style="{StaticResource TextBlockDefault}" />
<WrapPanel>
<TextBox Name="TextBlockPath" Width="400"
<TextBox Name="PathTextBlock" Width="400"
Text=""
Margin="10"
IsReadOnly="True" />
Expand Down
57 changes: 43 additions & 14 deletions JL.Windows/GUI/EditDictionaryWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Frozen;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using JL.Core.Dicts;
using JL.Core.Dicts.Options;
Expand Down Expand Up @@ -36,27 +37,46 @@ private void CancelButton_Click(object sender, RoutedEventArgs e)

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
TextBlockPath.ClearValue(BorderBrushProperty);
ComboBoxDictType.ClearValue(BorderBrushProperty);
PathTextBlock.ClearValue(BorderBrushProperty);
PathTextBlock.ClearValue(CursorProperty);
PathTextBlock.ClearValue(ToolTipProperty);
NameTextBox.ClearValue(BorderBrushProperty);
NameTextBox.ToolTip = "Dictionary name must be unique";

string path = TextBlockPath.Text;
string path = PathTextBlock.Text;
string fullPath = Path.GetFullPath(path, Utils.ApplicationPath);

if (string.IsNullOrWhiteSpace(path)
|| (_dict.Path != path
&& (!Path.Exists(fullPath) || DictUtils.Dicts.Values.Any(dict => dict.Path == path))))
if (string.IsNullOrWhiteSpace(path))
{
TextBlockPath.BorderBrush = Brushes.Red;
PathTextBlock.BorderBrush = Brushes.Red;
PathTextBlock.Cursor = Cursors.Help;
PathTextBlock.ToolTip = "Invalid path!";
return;
}

if (_dict.Path != path && (!Path.Exists(fullPath) || DictUtils.Dicts.Values.Any(dict => dict.Path == path)))
{
PathTextBlock.BorderBrush = Brushes.Red;
PathTextBlock.Cursor = Cursors.Help;
PathTextBlock.ToolTip = "Dictionary path must be unique!";
return;
}

string name = NameTextBox.Text;
if (string.IsNullOrWhiteSpace(name)
|| name.Length > 128
|| name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0
|| (!_dict.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && DictUtils.Dicts.ContainsKey(name)))
|| name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
{
NameTextBox.BorderBrush = Brushes.Red;
NameTextBox.ToolTip = "Invalid dictionary name!";
return;
}

if (!_dict.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && DictUtils.Dicts.ContainsKey(name))
{
NameTextBox.BorderBrush = Brushes.Red;
NameTextBox.ToolTip = "Dictionary name must be unique!";
return;
}

Expand All @@ -67,10 +87,19 @@ private void SaveButton_Click(object sender, RoutedEventArgs e)
{
if (DictUtils.YomichanDictTypes.Contains(_dict.Type))
{
bool validPath = Directory.EnumerateFiles(fullPath, _dict.Type is DictType.NonspecificKanjiYomichan ? "kanji_bank_*.json" : "term_bank_*.json", SearchOption.TopDirectoryOnly).Any();
bool validPath = Directory.EnumerateFiles(fullPath,
_dict.Type is DictType.NonspecificKanjiYomichan
? "kanji_bank_*.json"
: _dict.Type is DictType.PitchAccentYomichan
? "term*bank_*.json"
: "term_bank_*.json",
SearchOption.TopDirectoryOnly).Any();

if (!validPath)
{
TextBlockPath.BorderBrush = Brushes.Red;
PathTextBlock.BorderBrush = Brushes.Red;
PathTextBlock.Cursor = Cursors.Help;
PathTextBlock.ToolTip = "No valid file was found at the specified path!";
return;
}
}
Expand Down Expand Up @@ -143,7 +172,7 @@ private void BrowseForDictionaryFile(string filter)

if (openFileDialog.ShowDialog() is true)
{
TextBlockPath.Text = Utils.GetPath(openFileDialog.FileName);
PathTextBlock.Text = Utils.GetPath(openFileDialog.FileName);
}
}

Expand All @@ -162,7 +191,7 @@ private void BrowseForDictionaryFolder()

if (openFolderDialog.ShowDialog() is true)
{
TextBlockPath.Text = Utils.GetPath(openFolderDialog.FolderName);
PathTextBlock.Text = Utils.GetPath(openFolderDialog.FolderName);
}
}

Expand All @@ -171,14 +200,14 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
string type = _dict.Type.GetDescription() ?? _dict.Type.ToString();
_ = ComboBoxDictType.Items.Add(type);
ComboBoxDictType.SelectedValue = type;
TextBlockPath.Text = _dict.Path;
PathTextBlock.Text = _dict.Path;

bool isNotCustomDict = _dict.Type is not DictType.ProfileCustomNameDictionary
and not DictType.ProfileCustomWordDictionary
and not DictType.CustomNameDictionary
and not DictType.CustomWordDictionary;

TextBlockPath.IsEnabled = isNotCustomDict;
PathTextBlock.IsEnabled = isNotCustomDict;
FolderBrowseButton.IsEnabled = isNotCustomDict;

NameTextBox.Text = _dict.Name;
Expand Down

0 comments on commit f72eab7

Please sign in to comment.