using System.Text.Json;
using Windows.Graphics;
using Microsoft.UI.Windowing;
using XFEEDToolBox.Views;
namespace XFEEDToolBox;
public partial class App
{
private static async Task RunLayoutProbeAsync(string reportPath)
{
var samples=new List<object>();
foreach(var scenario in new[]{("windowed",1510,980),("wide",2560,1440),("4K",3840,2088),("maximized",0,0),("restored",1510,980)})
{
var presenter=(OverlappedPresenter)MainWindow.AppWindow.Presenter;
if(scenario.Item2==0)presenter.Maximize();
else{presenter.Restore(false);MainWindow.AppWindow.Resize(new SizeInt32(scenario.Item2,scenario.Item3));}
MainWindow.AppWindow.Hide();await Task.Delay(200);
var size=MainWindow.AppWindow.Size;
foreach(var pageType in Pages)
{
AppShellPage.Current!.Navigate(pageType);await Task.Delay(100);
var page=AppShellPage.Current.CurrentPage!;page.UpdateLayout();
var root=page.Content as FrameworkElement;
var scroll=root as ScrollViewer??page.FindName("PageScroll") as ScrollViewer;
var content=scroll?.Content as FrameworkElement??root;
var offset=content?.TransformToVisual(page).TransformPoint(new(0,0));
var frame=Microsoft.UI.Xaml.Media.VisualTreeHelper.GetParent(page) as FrameworkElement;
samples.Add(new{scenario=scenario.Item1,windowWidth=size.Width,windowHeight=size.Height,page=pageType.Name,
pageWidth=page.ActualWidth,pageHeight=page.ActualHeight,rootWidth=root?.ActualWidth,
contentWidth=content?.ActualWidth,contentX=offset?.X,contentRight=offset?.X+content?.ActualWidth,
viewportWidth=scroll?.ViewportWidth,scrollableWidth=scroll?.ScrollableWidth,
horizontalOffset=scroll?.HorizontalOffset,zoomFactor=scroll?.ZoomFactor,
rootAlignment=root?.HorizontalAlignment.ToString(),contentAlignment=content?.HorizontalAlignment.ToString(),
parent=frame?.GetType().Name,parentWidth=frame?.ActualWidth,xamlWidth=page.XamlRoot?.Size.Width});
if(scenario.Item1 is "windowed" or "wide" or "4K"&&pageType==typeof(MainPage)||scenario.Item1=="windowed"&&pageType==typeof(RefitPage)||scenario.Item1=="wide"&&pageType is not null&&(pageType==typeof(EngineeringPage)||pageType==typeof(RefitPage)||pageType==typeof(MaterialsPage)||pageType==typeof(GalaxyMapPage)||pageType==typeof(UpgradeListPage)))
await CapturePageAsync(page,Path.Combine(Path.GetDirectoryName(Path.GetFullPath(reportPath))!,"screenshots",scenario.Item1+"-"+pageType.Name+".png"));
}
}
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(reportPath))!);
await File.WriteAllTextAsync(reportPath,JsonSerializer.Serialize(new{samples,error=ViewModel.ErrorMessage,gameActionsInvoked=false},new JsonSerializerOptions{WriteIndented=true}));
MainWindow.Close();
}
private static async Task CapturePageAsync(Page page,string path)
{
var position=MainWindow.AppWindow.Position;
MainWindow.AppWindow.Move(new(-30000,-30000));MainWindow.AppWindow.Show(false);
await Task.Delay(200);((FrameworkElement)MainWindow.Content).UpdateLayout();
var bitmap=new Microsoft.UI.Xaml.Media.Imaging.RenderTargetBitmap();
await bitmap.RenderAsync((UIElement)MainWindow.Content);var pixels=await bitmap.GetPixelsAsync();
MainWindow.AppWindow.Hide();MainWindow.AppWindow.Move(position);
using var reader=Windows.Storage.Streams.DataReader.FromBuffer(pixels);var bytes=new byte[pixels.Length];reader.ReadBytes(bytes);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);using var output=File.Open(path,FileMode.Create);
var encoder=await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId,output.AsRandomAccessStream());
encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8,Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied,(uint)bitmap.PixelWidth,(uint)bitmap.PixelHeight,96,96,bytes);await encoder.FlushAsync();
}
}
using System.Text.Json;
using Windows.Graphics;
using Microsoft.UI.Windowing;
using XFEEDToolBox.Views;
namespace XFEEDToolBox;
public partial class App
{
private static async Task RunLayoutProbeAsync(string reportPath)
{
var samples=new List<object>();
foreach(var scenario in new[]{("windowed",1510,980),("wide",2560,1440),("4K",3840,2088),("maximized",0,0),("restored",1510,980)})
{
var presenter=(OverlappedPresenter)MainWindow.AppWindow.Presenter;
if(scenario.Item2==0)presenter.Maximize();
else{presenter.Restore(false);MainWindow.AppWindow.Resize(new SizeInt32(scenario.Item2,scenario.Item3));}
MainWindow.AppWindow.Hide();await Task.Delay(200);
var size=MainWindow.AppWindow.Size;
foreach(var pageType in Pages)
{
AppShellPage.Current!.Navigate(pageType);await Task.Delay(100);
var page=AppShellPage.Current.CurrentPage!;page.UpdateLayout();
var root=page.Content as FrameworkElement;
var scroll=root as ScrollViewer??page.FindName("PageScroll") as ScrollViewer;
var content=scroll?.Content as FrameworkElement??root;
var offset=content?.TransformToVisual(page).TransformPoint(new(0,0));
var frame=Microsoft.UI.Xaml.Media.VisualTreeHelper.GetParent(page) as FrameworkElement;
samples.Add(new{scenario=scenario.Item1,windowWidth=size.Width,windowHeight=size.Height,page=pageType.Name,
pageWidth=page.ActualWidth,pageHeight=page.ActualHeight,rootWidth=root?.ActualWidth,
contentWidth=content?.ActualWidth,contentX=offset?.X,contentRight=offset?.X+content?.ActualWidth,
viewportWidth=scroll?.ViewportWidth,scrollableWidth=scroll?.ScrollableWidth,
horizontalOffset=scroll?.HorizontalOffset,zoomFactor=scroll?.ZoomFactor,
rootAlignment=root?.HorizontalAlignment.ToString(),contentAlignment=content?.HorizontalAlignment.ToString(),
parent=frame?.GetType().Name,parentWidth=frame?.ActualWidth,xamlWidth=page.XamlRoot?.Size.Width});
if(scenario.Item1 is "windowed" or "wide" or "4K"&&pageType==typeof(MainPage)||scenario.Item1=="windowed"&&pageType==typeof(RefitPage)||scenario.Item1=="wide"&&pageType is not null&&(pageType==typeof(EngineeringPage)||pageType==typeof(RefitPage)||pageType==typeof(MaterialsPage)||pageType==typeof(GalaxyMapPage)||pageType==typeof(UpgradeListPage)))
await CapturePageAsync(page,Path.Combine(Path.GetDirectoryName(Path.GetFullPath(reportPath))!,"screenshots",scenario.Item1+"-"+pageType.Name+".png"));
}
}
Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(reportPath))!);
await File.WriteAllTextAsync(reportPath,JsonSerializer.Serialize(new{samples,error=ViewModel.ErrorMessage,gameActionsInvoked=false},new JsonSerializerOptions{WriteIndented=true}));
MainWindow.Close();
}
private static async Task CapturePageAsync(Page page,string path)
{
var position=MainWindow.AppWindow.Position;
MainWindow.AppWindow.Move(new(-30000,-30000));MainWindow.AppWindow.Show(false);
await Task.Delay(200);((FrameworkElement)MainWindow.Content).UpdateLayout();
var bitmap=new Microsoft.UI.Xaml.Media.Imaging.RenderTargetBitmap();
await bitmap.RenderAsync((UIElement)MainWindow.Content);var pixels=await bitmap.GetPixelsAsync();
MainWindow.AppWindow.Hide();MainWindow.AppWindow.Move(position);
using var reader=Windows.Storage.Streams.DataReader.FromBuffer(pixels);var bytes=new byte[pixels.Length];reader.ReadBytes(bytes);
Directory.CreateDirectory(Path.GetDirectoryName(path)!);using var output=File.Open(path,FileMode.Create);
var encoder=await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId,output.AsRandomAccessStream());
encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8,Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied,(uint)bitmap.PixelWidth,(uint)bitmap.PixelHeight,96,96,bytes);await encoder.FlushAsync();
}
}