Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
added ok and cancel in modal
Browse files Browse the repository at this point in the history
  • Loading branch information
byCrookie committed Sep 16, 2022
1 parent b2ec22e commit c661507
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface IModalNavigationService
{
Task OpenModalAsync(ModalParameter parameter);
Task CloseModalAsync();
Task OkAsync();
Task CancelAsync();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TypeCode.Wpf.Helper.Navigation.Modal.Service;

public enum ModalButtons
{
Ok,
OkAndCancel
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,23 @@ public async Task OpenModalAsync(ModalParameter modalParameter)

_lastModalParameter = modalParameter;
}

public Task CancelAsync()
{
_mainViewProvider.MainWindow().Main.Opacity = 1;
_mainViewProvider.MainWindow().Main.IsEnabled = true;
_mainViewProvider.MainWindow().ModalOverlay.Visibility = Visibility.Collapsed;

return _lastModalParameter?.OnCancelAsync.Invoke() ?? Task.CompletedTask;
}

public Task CloseModalAsync()
public Task OkAsync()
{
_mainViewProvider.MainWindow().Main.Opacity = 1;
_mainViewProvider.MainWindow().Main.IsEnabled = true;
_mainViewProvider.MainWindow().ModalOverlay.Visibility = Visibility.Collapsed;

return _lastModalParameter?.OnCloseAsync.Invoke() ?? Task.CompletedTask;
return _lastModalParameter?.OnOkAsync.Invoke() ?? Task.CompletedTask;
}

private static Task CallOnNavigatedToOnCurrentViewModelAsync<T>(NavigationContext context, T viewModelInstance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ public sealed class ModalParameter
{
public ModalParameter()
{
OnCloseAsync = () => Task.CompletedTask;
Buttons = ModalButtons.Ok;
OnOkAsync = () => Task.CompletedTask;
OnCancelAsync = () => Task.CompletedTask;
}

public string? Title { get; set; }
public string? Text { get; set; }
public bool ScrollViewerDisabled { get; set; }
public Func<Task> OnCloseAsync { get; set; }
public ModalButtons Buttons { get; set; }
public Func<Task> OnOkAsync { get; set; }
public Func<Task> OnCancelAsync { get; set; }
}
14 changes: 11 additions & 3 deletions src/TypeCode.Wpf/Helper/Navigation/Modal/View/ModalView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
Expand All @@ -30,16 +31,23 @@
Grid.ColumnSpan="2">
</TextBlock>
<ScrollViewer Margin="0,5,0,0" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"
Visibility="{Binding ScrollViewerEnabled, Converter={StaticResource InverseBooleanToVisibilityConverter}}">
HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<TextBlock Padding="2" Background="Transparent"
Text="{Binding Text, Mode=OneWay}"
TextWrapping="Wrap"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</ScrollViewer>

<Button Margin="0,5,0,0" Grid.Row="2" Grid.Column="1"
Command="{Binding CancelCommand}"
Visibility="{Binding OkVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
Background="{StaticResource Primary}"
BorderBrush="{StaticResource Secondary}"
Content="Cancel" />

<Button Margin="5,5,0,0" Grid.Row="2" Grid.Column="2"
Command="{Binding OkCommand}"
Visibility="{Binding OkVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
BorderBrush="{StaticResource Secondary}"
Content="Ok" />
</Grid>
Expand Down
18 changes: 14 additions & 4 deletions src/TypeCode.Wpf/Helper/Navigation/Modal/View/ModalViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,32 @@ public Task OnNavigatedToAsync(NavigationContext context)
var parameter = context.GetParameter<ModalParameter>();
Title = parameter.Title;
Text = parameter.Text;
ScrollViewerEnabled = parameter.ScrollViewerDisabled;
OkVisible = parameter.Buttons is ModalButtons.Ok or ModalButtons.OkAndCancel;
CancelVisible = parameter.Buttons is ModalButtons.OkAndCancel;
return Task.CompletedTask;
}

[RelayCommand]
private Task OkAsync()
{
return _modalNavigationService.CloseModalAsync();
return _modalNavigationService.OkAsync();
}

[RelayCommand]
private Task CancelAsync()
{
return _modalNavigationService.CancelAsync();
}

[ObservableProperty]
private string? _title;

[ObservableProperty]
private string? _text;


[ObservableProperty]
private bool _okVisible;

[ObservableProperty]
private bool _scrollViewerEnabled;
private bool _cancelVisible;
}
6 changes: 3 additions & 3 deletions src/TypeCode.Wpf/Main/Content/MainContentViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ private Task UpdateAsync()
throw new Exception("There is no new version available.");
}

IsBannerVisible = false;

return _modalNavigationService.OpenModalAsync(new ModalParameter
{
Title = "WARNING - Update (Installer)",
Text = "The installer uses the location of the current version of TypeCode to install the update." +
" You can change the installation path (destination folder) in the advanced menu of the install wizard.",
OnCloseAsync = async () =>
OnOkAsync = async () =>
{
IsBannerVisible = false;

var name = $"TypeCode.Wpf.Setup_{_version?.NewVersion}";

var url = $"https://github.com/byCrookie/TypeCode/releases/download/{_version?.NewVersion}/{name}.msi";
Expand Down

0 comments on commit c661507

Please sign in to comment.