Close confirmation is now working.
This commit is contained in:
@@ -5,8 +5,12 @@
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Progrart.Dialogs.CloseConfirmationDialog">
|
||||
<StackPanel>
|
||||
<TextBlock FontSize="18" Name="TitleBlock" TextWrapping="Wrap"/>
|
||||
<TextBlock Name="MessageBlock" TextWrapping="Wrap"/>
|
||||
<TextBlock FontSize="18" Name="TitleBlock" TextWrapping="Wrap">
|
||||
Unsaved Changes
|
||||
</TextBlock>
|
||||
<TextBlock Name="MssageBlock" TextWrapping="Wrap">
|
||||
Save all files or discard changes?
|
||||
</TextBlock>
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition/>
|
||||
@@ -14,7 +18,7 @@
|
||||
<ColumnDefinition/>
|
||||
</Grid.ColumnDefinitions>
|
||||
<Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Classes="accent" Name="OKBtn" Content="Save All"/>
|
||||
<Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Classes="accent" Grid.Column="1" Name="DiscardBtn" Content="Discard And Close"/>
|
||||
<Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Grid.Column="1" Name="DiscardBtn" Content="Discard And Close"/>
|
||||
<Button HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Name="CancelBtn" Grid.Column="2" Content="Cancel"/>
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
|
||||
@@ -1,13 +1,64 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using DialogHostAvalonia;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Progrart.Dialogs;
|
||||
|
||||
public partial class CloseConfirmationDialog : UserControl
|
||||
{
|
||||
public Func<Task<bool>>? onOK = null;
|
||||
public Func<Task<bool>>? onDiscard = null;
|
||||
public Func<bool>? onCancel = null;
|
||||
public CloseConfirmationDialog()
|
||||
{
|
||||
InitializeComponent();
|
||||
OKBtn.Click += OKBtn_Click;
|
||||
CancelBtn.Click += CancelBtn_Click;
|
||||
DiscardBtn.Click += async (sender, e) => await Discard();
|
||||
}
|
||||
private void CancelBtn_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
{
|
||||
Cancel();
|
||||
}
|
||||
|
||||
private void Cancel()
|
||||
{
|
||||
if (!(onCancel?.Invoke()) ?? true)
|
||||
{
|
||||
DialogHost.Close(null);
|
||||
}
|
||||
}
|
||||
|
||||
private async void OKBtn_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
{
|
||||
await Confirmed();
|
||||
}
|
||||
|
||||
private async Task Confirmed()
|
||||
{
|
||||
if (onOK is not null)
|
||||
{
|
||||
if (!await onOK())
|
||||
{
|
||||
DialogHost.Close(null);
|
||||
}
|
||||
}
|
||||
else
|
||||
DialogHost.Close(null);
|
||||
}
|
||||
private async Task Discard()
|
||||
{
|
||||
if (onDiscard is not null)
|
||||
{
|
||||
if (!await onDiscard())
|
||||
{
|
||||
DialogHost.Close(null);
|
||||
}
|
||||
}
|
||||
else
|
||||
DialogHost.Close(null);
|
||||
}
|
||||
}
|
||||
@@ -222,12 +222,21 @@ public partial class MainView : UserControl
|
||||
|
||||
private void MenuItem_SaveAll_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
{
|
||||
Task.Run(async () => await MainTabHost.Foreach(async (page) =>
|
||||
SaveAll();
|
||||
}
|
||||
|
||||
private void SaveAll(Action? OnDone = null)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await MainTabHost.Foreach(async (page) =>
|
||||
{
|
||||
if (page is IEditorPage editor)
|
||||
await editor.Save();
|
||||
return false;
|
||||
}));
|
||||
});
|
||||
OnDone?.Invoke();
|
||||
});
|
||||
}
|
||||
|
||||
private async void CreateProjectMenuItem_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
@@ -310,17 +319,36 @@ public partial class MainView : UserControl
|
||||
});
|
||||
return v;
|
||||
}
|
||||
private void ExitItem_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
public bool TryExit()
|
||||
{
|
||||
|
||||
if (CloseCheck())
|
||||
{
|
||||
CloseConfirmationDialog dialog = new CloseConfirmationDialog();
|
||||
|
||||
dialog.onOK = async () =>
|
||||
{
|
||||
SaveAll(() =>
|
||||
{
|
||||
Environment.Exit(0);
|
||||
});
|
||||
return false;
|
||||
};
|
||||
dialog.onDiscard = async () =>
|
||||
{
|
||||
Environment.Exit(0);
|
||||
return false;
|
||||
};
|
||||
DialogHost.Show(dialog);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Environment.Exit(0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
private void ExitItem_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
|
||||
{
|
||||
TryExit();
|
||||
}
|
||||
}
|
||||
@@ -8,5 +8,5 @@
|
||||
x:Class="Progrart.Views.MainWindow"
|
||||
Icon="/Assets/Background.png"
|
||||
Title="Progrart">
|
||||
<views:MainView />
|
||||
<views:MainView Name="MainAppView"/>
|
||||
</Window>
|
||||
|
||||
@@ -9,5 +9,12 @@ public partial class MainWindow : Window
|
||||
InitializeComponent();
|
||||
this.ExtendClientAreaToDecorationsHint = true;
|
||||
this.ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.PreferSystemChrome;
|
||||
this.Closing += MainWindow_Closing;
|
||||
}
|
||||
|
||||
private void MainWindow_Closing(object? sender, WindowClosingEventArgs e)
|
||||
{
|
||||
e.Cancel=!MainAppView.TryExit();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user