|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Windows; |
| 4 | +using System.Windows.Forms; |
| 5 | +using System.Windows.Input; |
| 6 | +using System.Windows.Media.Animation; |
| 7 | +using Flow.Launcher.Infrastructure; |
| 8 | + |
| 9 | +namespace Flow.Launcher |
| 10 | +{ |
| 11 | + public partial class MsgWithButton : Window |
| 12 | + { |
| 13 | + private readonly Storyboard fadeOutStoryboard = new(); |
| 14 | + private bool closing; |
| 15 | + |
| 16 | + public MsgWithButton() |
| 17 | + { |
| 18 | + InitializeComponent(); |
| 19 | + var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); |
| 20 | + var dipWorkingArea = Win32Helper.TransformPixelsToDIP(this, |
| 21 | + screen.WorkingArea.Width, |
| 22 | + screen.WorkingArea.Height); |
| 23 | + Left = dipWorkingArea.X - Width; |
| 24 | + Top = dipWorkingArea.Y; |
| 25 | + showAnimation.From = dipWorkingArea.Y; |
| 26 | + showAnimation.To = dipWorkingArea.Y - Height; |
| 27 | + |
| 28 | + // Create the fade out storyboard |
| 29 | + fadeOutStoryboard.Completed += fadeOutStoryboard_Completed; |
| 30 | + DoubleAnimation fadeOutAnimation = new DoubleAnimation(dipWorkingArea.Y - Height, dipWorkingArea.Y, new Duration(TimeSpan.FromSeconds(5))) |
| 31 | + { |
| 32 | + AccelerationRatio = 0.2 |
| 33 | + }; |
| 34 | + Storyboard.SetTarget(fadeOutAnimation, this); |
| 35 | + Storyboard.SetTargetProperty(fadeOutAnimation, new PropertyPath(TopProperty)); |
| 36 | + fadeOutStoryboard.Children.Add(fadeOutAnimation); |
| 37 | + |
| 38 | + _ = LoadImageAsync(); |
| 39 | + |
| 40 | + imgClose.MouseUp += imgClose_MouseUp; |
| 41 | + } |
| 42 | + |
| 43 | + private async System.Threading.Tasks.Task LoadImageAsync() |
| 44 | + { |
| 45 | + imgClose.Source = await App.API.LoadImageAsync(Path.Combine(Constant.ProgramDirectory, "Images\\close.png")); |
| 46 | + } |
| 47 | + |
| 48 | + void imgClose_MouseUp(object sender, MouseButtonEventArgs e) |
| 49 | + { |
| 50 | + if (!closing) |
| 51 | + { |
| 52 | + closing = true; |
| 53 | + fadeOutStoryboard.Begin(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private void fadeOutStoryboard_Completed(object sender, EventArgs e) |
| 58 | + { |
| 59 | + Close(); |
| 60 | + } |
| 61 | + |
| 62 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")] |
| 63 | + public async void Show(string title, string buttonText, Action buttonAction, string subTitle, string iconPath) |
| 64 | + { |
| 65 | + tbTitle.Text = title; |
| 66 | + tbSubTitle.Text = subTitle; |
| 67 | + btn.Content = buttonText; |
| 68 | + btn.Click += (s, e) => buttonAction(); |
| 69 | + if (string.IsNullOrEmpty(subTitle)) |
| 70 | + { |
| 71 | + tbSubTitle.Visibility = Visibility.Collapsed; |
| 72 | + } |
| 73 | + |
| 74 | + if (!File.Exists(iconPath)) |
| 75 | + { |
| 76 | + imgIco.Source = await App.API.LoadImageAsync(Path.Combine(Constant.ProgramDirectory, "Images\\app.png")); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + imgIco.Source = await App.API.LoadImageAsync(iconPath); |
| 81 | + } |
| 82 | + |
| 83 | + Show(); |
| 84 | + |
| 85 | + await Dispatcher.InvokeAsync(async () => |
| 86 | + { |
| 87 | + if (!closing) |
| 88 | + { |
| 89 | + closing = true; |
| 90 | + await Dispatcher.InvokeAsync(fadeOutStoryboard.Begin); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments