-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a speaker button to play audio for the selected primary spelling …
…and reading pair when in mining mode
- Loading branch information
Showing
5 changed files
with
217 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Window x:Class="JL.Windows.GUI.ReadingsAudioWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d" | ||
Title="ReadingsAudioWindow" SizeToContent="WidthAndHeight" WindowStyle="None" Topmost="True" | ||
ShowInTaskbar="False" AllowsTransparency="True" Focusable="False" x:ClassModifier="internal"> | ||
<Grid> | ||
<ListView x:Name="ReadingsListView" VirtualizingStackPanel.VirtualizationMode="Recycling" Focusable="False" | ||
ScrollViewer.HorizontalScrollBarVisibility="Disabled" | ||
VirtualizingPanel.IsVirtualizingWhenGrouping="True" VirtualizingPanel.ScrollUnit="Pixel" | ||
BorderThickness="0" ScrollViewer.CanContentScroll="True" VirtualizingPanel.IsVirtualizing="True" | ||
ScrollViewer.VerticalScrollBarVisibility="Auto" VirtualizingPanel.IsContainerVirtualizable="True" | ||
HorizontalContentAlignment="Stretch" | ||
Background="{Binding Path=Background, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" | ||
Opacity="70"> | ||
<ListView.ItemContainerStyle> | ||
<Style TargetType="{x:Type ListViewItem}"> | ||
<Setter Property="Focusable" Value="False" /> | ||
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="ReadingsListView_PreviewMouseLeftButtonDown" /> | ||
</Style> | ||
</ListView.ItemContainerStyle> | ||
</ListView> | ||
</Grid> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Input; | ||
using System.Windows.Interop; | ||
using JL.Windows.Utilities; | ||
|
||
namespace JL.Windows.GUI; | ||
/// <summary> | ||
/// Interaction logic for ReadingsAudioWindow.xaml | ||
/// </summary> | ||
internal sealed partial class ReadingsAudioWindow : Window | ||
{ | ||
private static string? s_primarySpelling; | ||
private nint _windowHandle; | ||
|
||
private static ReadingsAudioWindow? s_instance; | ||
|
||
private ReadingsAudioWindow() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
public static bool IsItVisible() | ||
{ | ||
return s_instance?.IsVisible ?? false; | ||
} | ||
|
||
public static void Show(string primarySpelling, string[] readings, Window window) | ||
{ | ||
s_primarySpelling = primarySpelling; | ||
ReadingsAudioWindow currentInstance = s_instance ??= new ReadingsAudioWindow(); | ||
currentInstance.ReadingsListView.ItemsSource = readings; | ||
currentInstance.Background = ConfigManager.PopupBackgroundColor; | ||
currentInstance.Foreground = ConfigManager.DefinitionsColor; | ||
currentInstance.FontFamily = ConfigManager.PopupFont; | ||
currentInstance.Show(); | ||
currentInstance.UpdatePosition(window.PointToScreen(Mouse.GetPosition(window))); | ||
WinApi.BringToFront(currentInstance._windowHandle); | ||
_ = currentInstance.Focus(); | ||
} | ||
|
||
protected override void OnSourceInitialized(EventArgs e) | ||
{ | ||
base.OnSourceInitialized(e); | ||
_windowHandle = new WindowInteropHelper(this).Handle; | ||
} | ||
|
||
protected override void OnActivated(EventArgs e) | ||
{ | ||
base.OnActivated(e); | ||
|
||
if (ConfigManager.Focusable) | ||
{ | ||
WinApi.AllowActivation(_windowHandle); | ||
} | ||
else | ||
{ | ||
WinApi.PreventActivation(_windowHandle); | ||
} | ||
} | ||
|
||
private async void ReadingsListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) | ||
{ | ||
string selectedReading = (string)((ListViewItem)sender).Content; | ||
Hide(); | ||
await PopupWindowUtils.PlayAudio(s_primarySpelling!, selectedReading).ConfigureAwait(false); | ||
} | ||
|
||
private void UpdatePosition(Point cursorPosition) | ||
{ | ||
double mouseX = cursorPosition.X / WindowsUtils.Dpi.DpiScaleX; | ||
double mouseY = cursorPosition.Y / WindowsUtils.Dpi.DpiScaleY; | ||
|
||
bool needsFlipX = (mouseX + ActualWidth) > (WindowsUtils.ActiveScreen.Bounds.X + WindowsUtils.DpiAwareWorkAreaWidth); | ||
bool needsFlipY = (mouseY + ActualHeight) > (WindowsUtils.ActiveScreen.Bounds.Y + WindowsUtils.DpiAwareWorkAreaHeight); | ||
|
||
double newLeft; | ||
double newTop; | ||
|
||
if (needsFlipX) | ||
{ | ||
// flip Leftwards while preventing -OOB | ||
newLeft = mouseX - ActualWidth - 5; | ||
if (newLeft < WindowsUtils.ActiveScreen.Bounds.X) | ||
{ | ||
newLeft = WindowsUtils.ActiveScreen.Bounds.X; | ||
} | ||
} | ||
else | ||
{ | ||
// no flip | ||
newLeft = mouseX - 5; | ||
} | ||
|
||
if (needsFlipY) | ||
{ | ||
// flip Upwards while preventing -OOB | ||
newTop = mouseY - (ActualHeight + 15); | ||
if (newTop < WindowsUtils.ActiveScreen.Bounds.Y) | ||
{ | ||
newTop = WindowsUtils.ActiveScreen.Bounds.Y; | ||
} | ||
} | ||
else | ||
{ | ||
// no flip | ||
newTop = mouseY + 15; | ||
} | ||
|
||
// stick to edges if +OOB | ||
if ((newLeft + ActualWidth) > (WindowsUtils.ActiveScreen.Bounds.X + WindowsUtils.DpiAwareWorkAreaWidth)) | ||
{ | ||
newLeft = WindowsUtils.ActiveScreen.Bounds.X + WindowsUtils.DpiAwareWorkAreaWidth - ActualWidth; | ||
} | ||
|
||
if ((newTop + ActualHeight) > (WindowsUtils.ActiveScreen.Bounds.Y + WindowsUtils.DpiAwareWorkAreaHeight)) | ||
{ | ||
newTop = WindowsUtils.ActiveScreen.Bounds.Y + WindowsUtils.DpiAwareWorkAreaHeight - ActualHeight; | ||
} | ||
|
||
Left = newLeft; | ||
Top = newTop; | ||
} | ||
|
||
public static void HideWindow() | ||
{ | ||
s_instance?.Hide(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters