Skip to content

Commit 0d28e19

Browse files
committed
Fix edit dialog not showing up
1 parent 5cb4fa0 commit 0d28e19

7 files changed

Lines changed: 411 additions & 335 deletions

File tree

AutoMidiPlayer.WPF/AutoMidiPlayer.WPF.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<UseWPF>true</UseWPF>
77
<StartupObject>AutoMidiPlayer.WPF.App</StartupObject>
88
<ApplicationManifest>app.manifest</ApplicationManifest>
9-
<Version>6.6.2</Version>
9+
<Version>6.6.3</Version>
1010
<ApplicationIcon>logo.ico</ApplicationIcon>
1111
<Nullable>enable</Nullable>
1212
<RepositoryUrl>https://github.com/Jed556/AutoMidiPlayer</RepositoryUrl>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Linq;
2+
using System.Windows;
3+
using Wpf.Ui.Controls;
4+
5+
namespace AutoMidiPlayer.WPF.ModernWPF;
6+
7+
/// <summary>
8+
/// Helper class for creating ContentDialogs with proper DialogHost setup.
9+
/// </summary>
10+
public static class DialogHelper
11+
{
12+
/// <summary>
13+
/// Creates a new ContentDialog with the DialogHostEx property already set.
14+
/// </summary>
15+
public static ContentDialog CreateDialog()
16+
{
17+
var dialog = new ContentDialog();
18+
SetupDialogHost(dialog);
19+
return dialog;
20+
}
21+
22+
/// <summary>
23+
/// Sets up the DialogHostEx property for an existing ContentDialog.
24+
/// </summary>
25+
public static void SetupDialogHost(ContentDialog dialog)
26+
{
27+
var activeWindow = Application.Current.Windows.OfType<Window>().FirstOrDefault(w => w.IsActive)
28+
?? Application.Current.MainWindow;
29+
if (activeWindow != null)
30+
{
31+
var dialogHost = ContentDialogHost.GetForWindow(activeWindow);
32+
if (dialogHost != null)
33+
dialog.DialogHostEx = dialogHost;
34+
}
35+
}
36+
}

AutoMidiPlayer.WPF/ModernWPF/ImportDialog.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ namespace AutoMidiPlayer.WPF.ModernWPF;
88

99
public class ImportDialog : ContentDialog
1010
{
11+
static ImportDialog()
12+
{
13+
// Ensure the base ContentDialog style is applied to this derived dialog.
14+
DefaultStyleKeyProperty.OverrideMetadata(
15+
typeof(ImportDialog),
16+
new FrameworkPropertyMetadata(typeof(ContentDialog))
17+
);
18+
}
19+
1120
private readonly Wpf.Ui.Controls.TextBox _titleBox;
1221
private readonly Wpf.Ui.Controls.TextBox _authorBox;
1322
private readonly Wpf.Ui.Controls.TextBox _albumBox;
@@ -69,6 +78,40 @@ public uint SongMergeMilliseconds
6978

7079
public ImportDialog(string defaultTitle, int defaultKey = 0, Transpose defaultTranspose = Transpose.Ignore, string? defaultAuthor = null, string? defaultAlbum = null, DateTime? defaultDateAdded = null, double nativeBpm = 120, double? customBpm = null, bool? mergeNotes = null, uint? mergeMilliseconds = null, bool? holdNotes = null)
7180
{
81+
// Set up the DialogHost for this ContentDialog
82+
DialogHelper.SetupDialogHost(this);
83+
84+
if (Application.Current.TryFindResource(typeof(ContentDialog)) is Style dialogStyle)
85+
{
86+
Style = dialogStyle;
87+
}
88+
89+
// Keep the dialog within the active window bounds to avoid clipping on fullscreen toggle.
90+
var activeWindow = Application.Current.Windows.OfType<Window>().FirstOrDefault(w => w.IsActive)
91+
?? Application.Current.MainWindow;
92+
if (activeWindow != null)
93+
{
94+
void UpdateDialogBounds()
95+
{
96+
var maxHeight = Math.Max(0, activeWindow.ActualHeight - 120);
97+
var maxWidth = Math.Max(0, activeWindow.ActualWidth - 120);
98+
DialogMaxHeight = maxHeight;
99+
DialogMaxWidth = maxWidth;
100+
DialogMargin = new Thickness(24);
101+
}
102+
103+
UpdateDialogBounds();
104+
SizeChangedEventHandler? sizeChangedHandler = (_, _) => UpdateDialogBounds();
105+
activeWindow.SizeChanged += sizeChangedHandler;
106+
EventHandler? stateChangedHandler = (_, _) => UpdateDialogBounds();
107+
activeWindow.StateChanged += stateChangedHandler;
108+
Closed += (_, _) =>
109+
{
110+
activeWindow.SizeChanged -= sizeChangedHandler;
111+
activeWindow.StateChanged -= stateChangedHandler;
112+
};
113+
}
114+
72115
Title = "Edit Song";
73116
PrimaryButtonText = "Save";
74117
CloseButtonText = "Cancel";

AutoMidiPlayer.WPF/ViewModels/SettingsPageViewModel.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,12 @@ public async Task CheckForUpdate()
489489

490490
public async Task LocationMissing()
491491
{
492-
var dialog = new ContentDialog
493-
{
494-
Title = "Error",
495-
Content = "Could not find Game's Location, please find GenshinImpact.exe, YuanShen.exe, or xdt.exe (Heartopia)",
496-
497-
PrimaryButtonText = "Find Manually...",
498-
SecondaryButtonText = "Ignore (Notes might not play)",
499-
CloseButtonText = "Exit"
500-
};
492+
var dialog = DialogHelper.CreateDialog();
493+
dialog.Title = "Error";
494+
dialog.Content = "Could not find Game's Location, please find GenshinImpact.exe, YuanShen.exe, or xdt.exe (Heartopia)";
495+
dialog.PrimaryButtonText = "Find Manually...";
496+
dialog.SecondaryButtonText = "Ignore (Notes might not play)";
497+
dialog.CloseButtonText = "Exit";
501498

502499
var result = await dialog.ShowAsync();
503500

@@ -633,13 +630,10 @@ private async Task<bool> TrySetLocationAsync(string? location)
633630
if (!File.Exists(location)) return false;
634631
if (Path.GetFileName(location).Equals("launcher.exe", StringComparison.OrdinalIgnoreCase))
635632
{
636-
var dialog = new ContentDialog
637-
{
638-
Title = "Incorrect Location",
639-
Content = "launcher.exe is not the game, please find GenshinImpact.exe, YuanShen.exe, or xdt.exe (Heartopia)",
640-
641-
CloseButtonText = "Ok"
642-
};
633+
var dialog = DialogHelper.CreateDialog();
634+
dialog.Title = "Incorrect Location";
635+
dialog.Content = "launcher.exe is not the game, please find GenshinImpact.exe, YuanShen.exe, or xdt.exe (Heartopia)";
636+
dialog.CloseButtonText = "Ok";
643637

644638
await dialog.ShowAsync();
645639
return false;

AutoMidiPlayer.WPF/ViewModels/SongsViewModel.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,11 @@ public async Task ShowMissingFilesDialog()
298298

299299
content.Children.Add(listView);
300300

301-
var dialog = new ContentDialog
302-
{
303-
Title = "Missing Files",
304-
Content = content,
305-
PrimaryButtonText = "Remove All",
306-
CloseButtonText = "Close"
307-
};
301+
var dialog = DialogHelper.CreateDialog();
302+
dialog.Title = "Missing Files";
303+
dialog.Content = content;
304+
dialog.PrimaryButtonText = "Remove All";
305+
dialog.CloseButtonText = "Close";
308306

309307
var result = await dialog.ShowAsync();
310308

@@ -405,14 +403,12 @@ private async Task AddFile(string fileName)
405403
if (existingByHash != null)
406404
{
407405
// Show warning dialog about duplicate
408-
var dialog = new ContentDialog
409-
{
410-
Title = "Duplicate File Detected",
411-
Content = $"This MIDI file appears to be a duplicate of:\n\n" +
406+
var dialog = DialogHelper.CreateDialog();
407+
dialog.Title = "Duplicate File Detected";
408+
dialog.Content = $"This MIDI file appears to be a duplicate of:\n\n" +
412409
$"'{existingByHash.Song.Title ?? existingByHash.Song.Path}'\n\n" +
413-
$"The existing file will be used and this duplicate will be ignored.",
414-
CloseButtonText = "OK"
415-
};
410+
$"The existing file will be used and this duplicate will be ignored.";
411+
dialog.CloseButtonText = "OK";
416412
await dialog.ShowAsync();
417413
return;
418414
}

0 commit comments

Comments
 (0)