Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- XAML
- <UserControl xmlns="https://github.com/avaloniaui"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
- x:Class="LosslessAdapterManager2.Views.Popup">
- <StackPanel x:Name="MainStackPanel" Orientation="Vertical" Margin="10 10 10 10">
- <SelectableTextBlock x:Name="Contents" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center"/>
- <StackPanel x:Name="Buttons" HorizontalAlignment="Right" Margin="0 10 0 0" Orientation="Horizontal"/>
- </StackPanel>
- </UserControl>
- Codebehind
- using Avalonia;
- using Avalonia.Controls;
- using Avalonia.Input;
- using Avalonia.Threading;
- using LosslessAdapterManager2.Utils;
- using Material.Icons;
- using System.Linq;
- using System.Threading.Tasks;
- namespace LosslessAdapterManager2.Views
- {
- public class PopupOptions
- {
- public bool topMost = false;
- public string? okButtonText = null;
- public MaterialIconKind? icon = null;
- public bool yesNo = false;
- public bool noButton = false;
- }
- public partial class Popup : Window
- {
- public bool resultIsYes = false;
- private Popup(string title, string contents, PopupOptions options)
- {
- InitializeComponent();
- Topmost = options.topMost;
- Title = title;
- Icon = WindowIconUtils.WindowIconFromMaterialIcon(options.icon != null ? options.icon.Value : MaterialIconKind.Warning);
- var _contents = this.FindControl<TextBlock>("Contents");
- if (_contents != null) {
- _contents.Text = contents;
- }
- var buttons = this.FindControl<StackPanel>("Buttons");
- if (buttons != null)
- {
- if (options.noButton)
- {
- KeyDown += (sender, e) => {
- if ((new[] { Key.Space, Key.Enter, Key.Escape }).Any(x => x == e.Key))
- Close();
- };
- }
- else if (options.yesNo)
- {
- var noButton = new Button() { Content = "No", Margin = new Thickness(10, 0, 0, 0), MinWidth = 60, HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Center };
- noButton.Click += (sender, e) =>
- {
- resultIsYes = false;
- Close();
- };
- buttons.Children.Add(noButton);
- var yesButton = new Button() { Content = "Yes", Margin = new Thickness(10, 0, 0, 0), MinWidth = 60, HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Center };
- yesButton.Click += (sender, e) =>
- {
- resultIsYes = true;
- Close();
- };
- buttons.Children.Add(yesButton);
- KeyDown += (sender, e) => {
- if ((new[] { Key.Space, Key.Enter }).Any(x => x == e.Key))
- {
- resultIsYes = true;
- Close();
- }
- if ((new[] { Key.Escape }).Any(x => x == e.Key))
- {
- resultIsYes = false;
- Close();
- }
- };
- }
- else
- {
- var okButton = new Button() { Content = options.okButtonText == null ? "OK" : options.okButtonText, MinWidth = 60, HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Center };
- okButton.Click += (sender, e) => Close();
- buttons.Children.Add(okButton);
- KeyDown += (sender, e) => {
- if ((new[] { Key.Space, Key.Enter, Key.Escape, Key.Right, Key.Down }).Any(x => x == e.Key))
- Close();
- };
- }
- var mainStackPanel = this.FindControl<StackPanel>("MainStackPanel");
- if (mainStackPanel != null)
- {
- //mainStackPanel.MaxWidth = 1000;
- mainStackPanel.Measure(new Size(1000, double.PositiveInfinity));
- Size desiredSize = mainStackPanel.DesiredSize;
- Width = desiredSize.Width+20;
- Height = desiredSize.Height;
- }
- }
- /*var okButton = this.FindControl<Button>("OKButton");
- if (okButton != null)
- {
- okButton.Click += (sender, e) => Close();
- if (options.okButtonText != null) okButton.Content = options.okButtonText;
- }*/
- }
- public static Task<bool> Show(string title, string contents)
- {
- return Show(title, contents, new PopupOptions());
- }
- public static Task<bool> Show(string title, string contents, PopupOptions options)
- {
- return Dispatcher.UIThread.InvokeAsync(() =>
- {
- var window = new Popup(title, contents, options);
- var tcs = new TaskCompletionSource<bool>();
- window.Closed += (sender, e) =>
- {
- tcs.TrySetResult(window.resultIsYes);
- };
- window.Show();
- return tcs.Task;
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment