175 lines
4.6 KiB
C#
175 lines
4.6 KiB
C#
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Effects;
|
||
using System.Windows.Media.Imaging;
|
||
|
||
using CamBooth.App.Core.AppSettings;
|
||
using CamBooth.App.Core.Logging;
|
||
|
||
using Wpf.Ui.Controls;
|
||
|
||
using Image = Wpf.Ui.Controls.Image;
|
||
using MessageBox = System.Windows.MessageBox;
|
||
using TextBlock = Wpf.Ui.Controls.TextBlock;
|
||
|
||
namespace CamBooth.App.Features.PictureGallery;
|
||
|
||
public partial class PictureGalleryPage : Page
|
||
{
|
||
private readonly AppSettingsService _appSettingsService;
|
||
|
||
private readonly Logger _logger;
|
||
|
||
private readonly PictureGalleryService _pictureGalleryService;
|
||
|
||
private ContentDialog? _openContentDialog;
|
||
|
||
|
||
public PictureGalleryPage(AppSettingsService appSettingsService, Logger logger, PictureGalleryService pictureGalleryService)
|
||
{
|
||
this._appSettingsService = appSettingsService;
|
||
this._logger = logger;
|
||
this._pictureGalleryService = pictureGalleryService;
|
||
this.InitializeComponent();
|
||
this.Initialize();
|
||
}
|
||
|
||
|
||
private async void Initialize()
|
||
{
|
||
try
|
||
{
|
||
this.LoadPictures(12);
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
this._logger.Error(e.Message);
|
||
}
|
||
}
|
||
|
||
|
||
private void ContentDialog_OnButtonClicked(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||
{
|
||
if (args.Button == ContentDialogButton.Primary)
|
||
{
|
||
MessageBox.Show($"Print the shit baby {sender.Tag}");
|
||
}
|
||
}
|
||
|
||
|
||
private void LoadPictures(int howManyPictures = 0)
|
||
{
|
||
int loop = 0;
|
||
|
||
this.Dispatcher.Invoke(
|
||
() =>
|
||
{
|
||
do
|
||
{
|
||
BitmapImage thumbnail = this._pictureGalleryService.ThumbnailsOrderedByNewestDescending[loop];
|
||
|
||
TextBlock? textBlock = new();
|
||
Hyperlink? hyperlink = new();
|
||
hyperlink.Click += this.Hyperlink_OnClick;
|
||
hyperlink.Tag = thumbnail.UriSource;
|
||
hyperlink.TextDecorations = null;
|
||
Image? imageControl = new()
|
||
{
|
||
Source = thumbnail,
|
||
Width = 392,
|
||
Margin = new Thickness(4)
|
||
};
|
||
hyperlink.Inlines.Add(new InlineUIContainer(imageControl));
|
||
|
||
textBlock.Inlines.Add(hyperlink);
|
||
this.PicturesPanel.Items.Add(textBlock);
|
||
loop++;
|
||
}
|
||
while ((loop < howManyPictures || howManyPictures == 0) && loop < this._pictureGalleryService.ThumbnailsOrderedByNewestDescending.Count);
|
||
});
|
||
}
|
||
|
||
|
||
public void CloseOpenDialog()
|
||
{
|
||
void CloseDialog()
|
||
{
|
||
if (this._openContentDialog is null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
this._openContentDialog.ButtonClicked -= this.ContentDialog_OnButtonClicked;
|
||
this._openContentDialog.GetType().GetMethod("Hide", Type.EmptyTypes)?.Invoke(this._openContentDialog, null);
|
||
this.RootContentDialogPresenter.Content = null;
|
||
this._openContentDialog = null;
|
||
}
|
||
|
||
if (this.Dispatcher.CheckAccess())
|
||
{
|
||
CloseDialog();
|
||
return;
|
||
}
|
||
|
||
this.Dispatcher.Invoke(CloseDialog);
|
||
}
|
||
|
||
|
||
private void Hyperlink_OnClick(object sender, RoutedEventArgs e)
|
||
{
|
||
Uri? picturePathUri = ((Hyperlink)sender).Tag as Uri;
|
||
if (picturePathUri is null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
string picturePath = Uri.UnescapeDataString(picturePathUri.AbsolutePath);
|
||
Application.Current.Dispatcher.BeginInvoke(
|
||
async () =>
|
||
{
|
||
this.CloseOpenDialog();
|
||
ContentDialog contentDialog = new(this.RootContentDialogPresenter);
|
||
this._openContentDialog = contentDialog;
|
||
Image imageToShow = new()
|
||
{
|
||
MaxHeight = 570,
|
||
Background = new SolidColorBrush(Colors.White),
|
||
VerticalAlignment = VerticalAlignment.Center,
|
||
Source = PictureGalleryService.CreateThumbnail(picturePath, 450, 300)
|
||
};
|
||
|
||
contentDialog.VerticalAlignment = VerticalAlignment.Top;
|
||
contentDialog.PrimaryButtonAppearance = ControlAppearance.Primary;
|
||
contentDialog.CloseButtonAppearance = ControlAppearance.Light;
|
||
contentDialog.Background = new SolidColorBrush(Colors.White);
|
||
contentDialog.Foreground = new SolidColorBrush(Colors.White);
|
||
// contentDialog.SetCurrentValue(ContentDialog.TitleProperty, "Hello World");
|
||
contentDialog.SetCurrentValue(ContentControl.ContentProperty, imageToShow);
|
||
contentDialog.SetCurrentValue(ContentDialog.CloseButtonTextProperty, "Schließen");
|
||
contentDialog.SetCurrentValue(ContentDialog.PrimaryButtonTextProperty, "Drucken");
|
||
|
||
// contentDialog.SetCurrentValue(ContentDialog.PrimaryButtonIconProperty, PictureGalleryService.CreateRegularSymbolIcon(SymbolRegular.Print48, Colors.Tomato));
|
||
|
||
contentDialog.Tag = picturePath;
|
||
contentDialog.ButtonClicked += this.ContentDialog_OnButtonClicked;
|
||
|
||
try
|
||
{
|
||
await contentDialog.ShowAsync();
|
||
}
|
||
finally
|
||
{
|
||
contentDialog.ButtonClicked -= this.ContentDialog_OnButtonClicked;
|
||
if (ReferenceEquals(this._openContentDialog, contentDialog))
|
||
{
|
||
this._openContentDialog = null;
|
||
}
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
|