Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rampaa committed Feb 21, 2024
1 parent 4d162d5 commit 17c5a23
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 366 deletions.
183 changes: 105 additions & 78 deletions JL.Windows/GUI/ManageAudioSourcesWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand Down Expand Up @@ -79,47 +78,46 @@ private void UpdateAudioSourcesDisplay()
IsChecked = audioSource.Active,
Margin = new Thickness(10),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center
VerticalAlignment = VerticalAlignment.Center,
Tag = audioSource
};
checkBox.Checked += CheckBox_Checked;
checkBox.Unchecked += CheckBox_Unchecked;

Button buttonIncreasePriority = new()
Button increasePriorityButton = new()
{
Width = 25,
Content = '↑',
Margin = new Thickness(1),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center
VerticalAlignment = VerticalAlignment.Center,
Tag = audioSource
};
increasePriorityButton.Click += IncreasePriorityButton_Click;

Button buttonDecreasePriority = new()
Button decreasePriorityButton = new()
{
Width = 25,
Content = '↓',
Margin = new Thickness(1),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center
};

TextBlock priority = new()
{
Name = "priority",
// Width = 20,
Width = 0,
Text = audioSource.Priority.ToString(CultureInfo.InvariantCulture),
Visibility = Visibility.Collapsed
// Margin = new Thickness(10),
VerticalAlignment = VerticalAlignment.Center,
Tag = audioSource
};
decreasePriorityButton.Click += DecreasePriorityButton_Click;

TextBlock audioSourceTypeDisplay = new()
TextBlock audioSourceTypeTextBlock = new()
{
Width = 80,
Text = audioSource.Type.GetDescription(),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(10)
};
TextBlock audioSourceUriDisplay = new()

TextBlock audioSourceUriTextBlock = new()
{
Name = nameof(audioSourceUriTextBlock),
Width = 470,
Text = uri,
TextWrapping = TextWrapping.Wrap,
Expand All @@ -128,92 +126,121 @@ private void UpdateAudioSourcesDisplay()
Margin = new Thickness(10),
Cursor = Cursors.Hand
};
audioSourceUriTextBlock.MouseEnter += AudioSourceUriTextBlock_MouseEnter;
audioSourceUriTextBlock.MouseLeave += AudioSourceUriTextBlock_MouseLeave;

audioSourceUriDisplay.MouseEnter += (_, _) => audioSourceUriDisplay.TextDecorations = TextDecorations.Underline;
audioSourceUriDisplay.MouseLeave += (_, _) => audioSourceUriDisplay.TextDecorations = null;

Button buttonRemove = new()
Button editButton = new()
{
Width = 75,
Width = 45,
Height = 30,
Content = "Remove",
Content = "Edit",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Foreground = Brushes.White,
Background = Brushes.Red,
BorderThickness = new Thickness(1)
Background = Brushes.DodgerBlue,
BorderThickness = new Thickness(1),
Margin = new Thickness(0, 0, 5, 0),
Tag = audioSource
};
editButton.Click += EditButton_Click;

Button buttonEdit = new()
Button removeButton = new()
{
Width = 45,
Width = 75,
Height = 30,
Content = "Edit",
Content = "Remove",
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Center,
Foreground = Brushes.White,
Background = Brushes.DodgerBlue,
Background = Brushes.Red,
BorderThickness = new Thickness(1),
Margin = new Thickness(0, 0, 5, 0)
Tag = audioSource
};
removeButton.Click += RemoveButton_Click;

checkBox.Unchecked += (_, _) => audioSource.Active = false;
checkBox.Checked += (_, _) => audioSource.Active = true;
_ = dockPanel.Children.Add(checkBox);
_ = dockPanel.Children.Add(increasePriorityButton);
_ = dockPanel.Children.Add(decreasePriorityButton);
_ = dockPanel.Children.Add(audioSourceTypeTextBlock);
_ = dockPanel.Children.Add(audioSourceUriTextBlock);
_ = dockPanel.Children.Add(editButton);
_ = dockPanel.Children.Add(removeButton);

buttonIncreasePriority.Click += (_, _) =>
{
PrioritizeAudioSource(audioSource);
UpdateAudioSourcesDisplay();
};
resultDockPanels.Add(dockPanel);
}

buttonDecreasePriority.Click += (_, _) =>
{
DeprioritizeAudioSource(audioSource);
UpdateAudioSourcesDisplay();
};
AudioSourceListBox.ItemsSource = resultDockPanels
.OrderBy(static dockPanel => ((AudioSource)((CheckBox)dockPanel.Children[0]).Tag).Priority);
}

buttonRemove.Click += (_, _) =>
{
if (Utils.Frontend.ShowYesNoDialog("Do you really want to remove this audio source?", "Confirmation"))
{
_ = AudioUtils.AudioSources.Remove(uri);
private void AudioSourceUriTextBlock_MouseEnter(object sender, MouseEventArgs e)
{
((TextBlock)sender).TextDecorations = TextDecorations.Underline;
}

int priorityOfDeletedAudioSource = audioSource.Priority;
private void AudioSourceUriTextBlock_MouseLeave(object sender, MouseEventArgs e)
{
((TextBlock)sender).TextDecorations = null;
}

foreach (AudioSource a in AudioUtils.AudioSources.Values)
{
if (a.Priority > priorityOfDeletedAudioSource)
{
a.Priority -= 1;
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
AudioSource audioSource = (AudioSource)((CheckBox)sender).Tag;
audioSource.Active = true;
}

UpdateAudioSourcesDisplay();
}
};
buttonEdit.Click += (_, _) =>
{
_ = new EditAudioSourceWindow(uri, audioSource) { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }.ShowDialog();
UpdateAudioSourcesDisplay();
};
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
AudioSource audioSource = (AudioSource)((CheckBox)sender).Tag;
audioSource.Active = false;
}

resultDockPanels.Add(dockPanel);
private void IncreasePriorityButton_Click(object sender, RoutedEventArgs e)
{
AudioSource audioSource = (AudioSource)((Button)sender).Tag;
PrioritizeAudioSource(audioSource);
UpdateAudioSourcesDisplay();
}

_ = dockPanel.Children.Add(checkBox);
_ = dockPanel.Children.Add(buttonIncreasePriority);
_ = dockPanel.Children.Add(buttonDecreasePriority);
_ = dockPanel.Children.Add(priority);
_ = dockPanel.Children.Add(audioSourceTypeDisplay);
_ = dockPanel.Children.Add(audioSourceUriDisplay);
_ = dockPanel.Children.Add(buttonEdit);
_ = dockPanel.Children.Add(buttonRemove);
private void DecreasePriorityButton_Click(object sender, RoutedEventArgs e)
{
AudioSource audioSource = (AudioSource)((Button)sender).Tag;
DeprioritizeAudioSource(audioSource);
UpdateAudioSourcesDisplay();
}

private void RemoveButton_Click(object sender, RoutedEventArgs e)
{
if (Utils.Frontend.ShowYesNoDialog("Do you really want to remove this audio source?", "Confirmation"))
{
Button removeButton = (Button)sender;

string uri = removeButton.Parent.GetChildByName<TextBlock>("audioSourceUriTextBlock")!.Text;
_ = AudioUtils.AudioSources.Remove(uri);

AudioSource audioSource = (AudioSource)removeButton.Tag;
int priorityOfDeletedAudioSource = audioSource.Priority;

foreach (AudioSource a in AudioUtils.AudioSources.Values)
{
if (a.Priority > priorityOfDeletedAudioSource)
{
a.Priority -= 1;
}
}

UpdateAudioSourcesDisplay();
}
}

private void EditButton_Click(object sender, RoutedEventArgs e)
{
Button editButton = (Button)sender;
AudioSource audioSource = (AudioSource)editButton.Tag;
string uri = editButton.Parent.GetChildByName<TextBlock>("audioSourceUriTextBlock")!.Text;

AudioSourceListBox.ItemsSource = resultDockPanels.OrderBy(static dockPanel =>
dockPanel.Children
.OfType<TextBlock>()
.Where(static textBlock => textBlock.Name is "priority")
.Select(static textBlockPriority => Convert.ToInt32(textBlockPriority.Text, CultureInfo.InvariantCulture)).First());
_ = new EditAudioSourceWindow(uri, audioSource) { Owner = this, WindowStartupLocation = WindowStartupLocation.CenterOwner }.ShowDialog();
UpdateAudioSourcesDisplay();
}

private static void PrioritizeAudioSource(AudioSource audioSource)
Expand Down
2 changes: 1 addition & 1 deletion JL.Windows/GUI/ManageDictionariesWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Manage Dictionaries" Height="700" Width="900" Closed="Window_Closed" ResizeMode="NoResize" IsVisibleChanged="Window_IsVisibleChanged"
Title="Manage Dictionaries" Height="700" Width="900" Closed="Window_Closed" ResizeMode="NoResize"
SizeToContent="Manual" ScrollViewer.CanContentScroll="True" Background="{DynamicResource RegionBrush}" Topmost="True" ShowInTaskbar="False"
x:ClassModifier="internal">
<Grid>
Expand Down
Loading

0 comments on commit 17c5a23

Please sign in to comment.