using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Titano.Core.Diagnostics;
using Titano.Ui.Theming;
using Titano.Uploader.DesignTime;
namespace Titano.Uploader.Shell;
///
/// Titano.Uploader.exe --capture <dir>: renders the window at its default 760×700 — first run (connect),
/// queue running, settings panel — from in the dark and light themes, then once more in
/// Arabic (right to left, dark), and exits.
///
public static class ScreenCapture
{
private const double Width = 760;
private const double Height = 700;
/// Where the PNG files go.
/// Switches the UI language (a culture name; null = the OS language).
public static void Run(string directory, Action applyLanguage)
{
Directory.CreateDirectory(directory);
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, () =>
{
try
{
applyLanguage(null);
CaptureScreens(directory, string.Empty, [AppTheme.Dark, AppTheme.Light]);
// Mirrored layout with paths, addresses and file names kept left to right (BRIEF §16).
applyLanguage("ar");
CaptureScreens(directory, "-ar", [AppTheme.Dark]);
Log.Info("capture", $"Screenshots written to {Path.GetFullPath(directory)}");
}
catch (Exception ex)
{
Log.Error("capture", "Capture failed", ex);
Environment.ExitCode = 1;
}
finally
{
Application.Current.Shutdown();
}
});
}
/// A fresh window on fresh sample data, so every text is built in the current language.
private static void CaptureScreens(string directory, string language, IReadOnlyList themes)
{
var sample = new SampleData();
var vm = sample.CreateViewModel();
var window = new MainWindow(vm, sample.Settings)
{
WindowStartupLocation = WindowStartupLocation.Manual,
Left = -20000,
Top = -20000,
Width = Width,
Height = Height,
ShowActivated = false,
ShowInTaskbar = false,
};
window.Show();
try
{
foreach (var theme in themes)
{
var suffix = language + "-" + theme.ToString().ToLowerInvariant();
ThemeManager.Apply(theme);
sample.ShowConnect();
Save(window, Path.Combine(directory, $"uploader-connect{suffix}.png"));
sample.ShowQueue();
Save(window, Path.Combine(directory, $"uploader-queue{suffix}.png"));
vm.IsSettingsOpen = true;
Save(window, Path.Combine(directory, $"uploader-settings{suffix}.png"));
vm.IsSettingsOpen = false;
}
}
finally
{
window.Close();
vm.Dispose();
}
}
private static void Save(Window window, string path)
{
// Let bindings, templates and deferred layout complete.
window.UpdateLayout();
window.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, () => { });
window.UpdateLayout();
if (VisualTreeHelper.GetChildrenCount(window) == 0) return;
var root = (FrameworkElement)VisualTreeHelper.GetChild(window, 0);
var dpi = VisualTreeHelper.GetDpi(window);
var width = (int)Math.Ceiling(root.ActualWidth * dpi.DpiScaleX);
var height = (int)Math.Ceiling(root.ActualHeight * dpi.DpiScaleY);
if (width <= 0 || height <= 0) return;
var bitmap = new RenderTargetBitmap(width, height, dpi.PixelsPerInchX, dpi.PixelsPerInchY, PixelFormats.Pbgra32);
bitmap.Render(window.FlowDirection == FlowDirection.RightToLeft ? Mirrored(root) : root);
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
using var stream = File.Create(path);
encoder.Save(stream);
}
///
/// A right-to-left window is mirrored by a transform on the window itself, which rendering its content alone
/// would leave out (mirrored glyphs, left-to-right layout): draw the content through that mirror.
///
private static DrawingVisual Mirrored(FrameworkElement root)
{
var size = new Rect(0, 0, root.ActualWidth, root.ActualHeight);
var visual = new DrawingVisual();
using var dc = visual.RenderOpen();
dc.PushTransform(new MatrixTransform(-1, 0, 0, 1, size.Width, 0));
dc.DrawRectangle(new VisualBrush(root) { Stretch = Stretch.Fill, ViewboxUnits = BrushMappingMode.Absolute, Viewbox = size }, null, size);
return visual;
}
}