Julien-Bernard

Popup.axaml

Oct 15th, 2023
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.72 KB | Source Code | 0 0
  1. XAML
  2.  
  3. <UserControl xmlns="https://github.com/avaloniaui"
  4.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  7.              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  8.              x:Class="LosslessAdapterManager2.Views.Popup">
  9.   <StackPanel x:Name="MainStackPanel" Orientation="Vertical" Margin="10 10 10 10">
  10.     <SelectableTextBlock x:Name="Contents" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center"/>
  11.     <StackPanel x:Name="Buttons" HorizontalAlignment="Right" Margin="0 10 0 0" Orientation="Horizontal"/>
  12.   </StackPanel>
  13. </UserControl>
  14.  
  15. Codebehind
  16.  
  17. using Avalonia;
  18. using Avalonia.Controls;
  19. using Avalonia.Input;
  20. using Avalonia.Threading;
  21. using LosslessAdapterManager2.Utils;
  22. using Material.Icons;
  23. using System.Linq;
  24. using System.Threading.Tasks;
  25.  
  26. namespace LosslessAdapterManager2.Views
  27. {
  28.     public class PopupOptions
  29.     {
  30.         public bool topMost = false;
  31.         public string? okButtonText = null;
  32.         public MaterialIconKind? icon = null;
  33.         public bool yesNo = false;
  34.         public bool noButton = false;
  35.     }
  36.  
  37.     public partial class Popup : Window
  38.     {
  39.         public bool resultIsYes = false;
  40.         private Popup(string title, string contents, PopupOptions options)
  41.         {
  42.             InitializeComponent();
  43.             Topmost = options.topMost;
  44.             Title = title;
  45.             Icon = WindowIconUtils.WindowIconFromMaterialIcon(options.icon != null ? options.icon.Value : MaterialIconKind.Warning);
  46.  
  47.             var _contents = this.FindControl<TextBlock>("Contents");
  48.             if (_contents != null) {
  49.                 _contents.Text = contents;
  50.             }
  51.  
  52.             var buttons = this.FindControl<StackPanel>("Buttons");
  53.             if (buttons != null)
  54.             {
  55.                 if (options.noButton)
  56.                 {
  57.                     KeyDown += (sender, e) => {
  58.                         if ((new[] { Key.Space, Key.Enter, Key.Escape }).Any(x => x == e.Key))
  59.                             Close();
  60.                     };
  61.                 }
  62.                 else if (options.yesNo)
  63.                 {
  64.                     var noButton = new Button() { Content = "No", Margin = new Thickness(10, 0, 0, 0), MinWidth = 60, HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Center };
  65.                     noButton.Click += (sender, e) =>
  66.                     {
  67.                         resultIsYes = false;
  68.                         Close();
  69.                     };
  70.                     buttons.Children.Add(noButton);
  71.  
  72.                     var yesButton = new Button() { Content = "Yes", Margin = new Thickness(10, 0, 0, 0), MinWidth = 60, HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Center };
  73.                     yesButton.Click += (sender, e) =>
  74.                     {
  75.                         resultIsYes = true;
  76.                         Close();
  77.                     };
  78.                     buttons.Children.Add(yesButton);
  79.  
  80.                     KeyDown += (sender, e) => {
  81.                         if ((new[] { Key.Space, Key.Enter }).Any(x => x == e.Key))
  82.                         {
  83.                             resultIsYes = true;
  84.                             Close();
  85.                         }
  86.                         if ((new[] { Key.Escape }).Any(x => x == e.Key))
  87.                         {
  88.                             resultIsYes = false;
  89.                             Close();
  90.                         }
  91.                     };
  92.                 }
  93.                 else
  94.                 {
  95.                     var okButton = new Button() { Content = options.okButtonText == null ? "OK" : options.okButtonText, MinWidth = 60, HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Center };
  96.                     okButton.Click += (sender, e) => Close();
  97.                     buttons.Children.Add(okButton);
  98.  
  99.                     KeyDown += (sender, e) => {
  100.                         if ((new[] { Key.Space, Key.Enter, Key.Escape, Key.Right, Key.Down }).Any(x => x == e.Key))
  101.                             Close();
  102.                     };
  103.                 }
  104.  
  105.                 var mainStackPanel = this.FindControl<StackPanel>("MainStackPanel");
  106.                 if (mainStackPanel != null)
  107.                 {
  108.                     //mainStackPanel.MaxWidth = 1000;
  109.                     mainStackPanel.Measure(new Size(1000, double.PositiveInfinity));
  110.                     Size desiredSize = mainStackPanel.DesiredSize;
  111.                     Width = desiredSize.Width+20;
  112.                     Height = desiredSize.Height;
  113.                 }
  114.             }
  115.  
  116.             /*var okButton = this.FindControl<Button>("OKButton");
  117.             if (okButton != null)
  118.             {
  119.                 okButton.Click += (sender, e) => Close();
  120.                 if (options.okButtonText != null) okButton.Content = options.okButtonText;
  121.             }*/
  122.         }
  123.  
  124.         public static Task<bool> Show(string title, string contents)
  125.         {
  126.             return Show(title, contents, new PopupOptions());
  127.         }
  128.  
  129.         public static Task<bool> Show(string title, string contents, PopupOptions options)
  130.         {
  131.             return Dispatcher.UIThread.InvokeAsync(() =>
  132.             {
  133.                 var window = new Popup(title, contents, options);
  134.                 var tcs = new TaskCompletionSource<bool>();
  135.  
  136.                 window.Closed += (sender, e) =>
  137.                 {
  138.                     tcs.TrySetResult(window.resultIsYes);
  139.                 };
  140.  
  141.                 window.Show();
  142.  
  143.                 return tcs.Task;
  144.             });
  145.         }
  146.     }
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment