tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Enum.Material;
using DPumpHydr.WinFrmUI.RLT.Forms;
using DPumpHydr.WinFrmUI.RLT.Manager;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Util.MaterialAnimations;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region MaterialSnackBar
 
    public class MaterialSnackBar : MaterialForm
    {
        private const int TOP_PADDING_SINGLE_LINE = 6;
        private const int LEFT_RIGHT_PADDING = 16;
        private const int BUTTON_PADDING = 8;
        private const int BUTTON_HEIGHT = 36;
 
        private MaterialButton _actionButton = new();
        private Timer _duration = new(); // Timer that checks when the drop down is fully visible
 
        private AnimationManager _AnimationManager;
        private bool _closingAnimationDone = false;
        private bool _useAccentColor;
        private bool CloseAnimation = false;
 
        #region "Events"
 
        [Category("Action")]
        [Description("Fires when Action button is clicked")]
        public event EventHandler ActionButtonClick;
 
        #endregion
 
        [Category("Material"), DefaultValue(false), DisplayName("Use Accent Color")]
        public bool UseAccentColor
        {
            get => _useAccentColor;
            set { _useAccentColor = value; Invalidate(); }
        }
 
        [Category("Material"), DefaultValue(2000)]
        public int Duration
        {
            get => _duration.Interval;
            set => _duration.Interval = value;
        }
 
        private string _text;
        [Category("Material"), DefaultValue("SnackBar text")]
        public new string Text
        {
            get => _text;
            set
            {
                _text = value;
                UpdateRects();
                Invalidate();
            }
        }
 
        private bool _showActionButton;
        [Category("Material"), DefaultValue(false), DisplayName("Show Action Button")]
        public bool ShowActionButton
        {
            get => _showActionButton;
            set { _showActionButton = value; UpdateRects(); Invalidate(); }
        }
 
        private string _actionButtonText;
        [Category("Material"), DefaultValue("OK")]
        public string ActionButtonText
        {
            get => _actionButtonText;
            set
            {
                _actionButtonText = value;
                Invalidate();
            }
        }
 
        //public ObservableCollection<MaterialButton> Buttons { get; set; }
 
        [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
            int nLeftRect,     // x-coordinate of upper-left corner
            int nTopRect,      // y-coordinate of upper-left corner
            int nRightRect,    // x-coordinate of lower-right corner
            int nBottomRect,   // y-coordinate of lower-right corner
            int nWidthEllipse, // width of ellipse
            int nHeightEllipse // height of ellipse
        );
 
        public MaterialSnackBar(string Text, int Duration, bool ShowActionButton, string ActionButtonText, bool UseAccentColor)
        {
            this.Text = Text;
            this.Duration = Duration;
            TopMost = true;
            ShowInTaskbar = false;
            Sizable = false;
 
            BackColor = SkinManager.SnackBarBackgroundColor;
            FormStyle = FormStyles.StatusAndActionBar_None;
 
            this.ActionButtonText = ActionButtonText;
            this.UseAccentColor = UseAccentColor;
            Height = 48;
            MinimumSize = new Size(344, 48);
            MaximumSize = new Size(568, 48);
 
            this.ShowActionButton = ShowActionButton;
 
            Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 6, 6));
 
            _AnimationManager = new AnimationManager();
            _AnimationManager.AnimationType = AnimationType.EaseOut;
            _AnimationManager.Increment = 0.03;
            _AnimationManager.OnAnimationProgress += _AnimationManager_OnAnimationProgress;
 
            _duration.Tick += new EventHandler(duration_Tick);
 
            _actionButton = new MaterialButton
            {
                AutoSize = false,
                NoAccentTextColor = SkinManager.SnackBarTextButtonNoAccentTextColor,
                DrawShadows = false,
                Type = MaterialButton.MaterialButtonType.Text,
                UseAccentColor = _useAccentColor,
                Visible = _showActionButton,
                Text = _actionButtonText
            };
            _actionButton.Click += (sender, e) =>
            {
                ActionButtonClick?.Invoke(this, new EventArgs());
                _closingAnimationDone = false;
                Close();
            };
 
            if (!Controls.Contains(_actionButton))
            {
                Controls.Add(_actionButton);
            }
 
            UpdateRects();
 
        }
 
        public MaterialSnackBar() : this("SnackBar Text", 3000, false, "OK", false)
        {
        }
 
        public MaterialSnackBar(string Text) : this(Text, 3000, false, "OK", false)
        {
        }
 
        public MaterialSnackBar(string Text, int Duration) : this(Text, Duration, false, "OK", false)
        {
        }
 
        public MaterialSnackBar(string Text, string ActionButtonText) : this(Text, 3000, true, ActionButtonText, false)
        {
        }
 
        public MaterialSnackBar(string Text, string ActionButtonText, bool UseAccentColor) : this(Text, 3000, true, ActionButtonText, UseAccentColor)
        {
        }
 
        public MaterialSnackBar(string Text, int Duration, string ActionButtonText) : this(Text, Duration, true, ActionButtonText, false)
        {
        }
 
        public MaterialSnackBar(string Text, int Duration, string ActionButtonText, bool UseAccentColor) : this(Text, Duration, true, ActionButtonText, UseAccentColor)
        {
        }
 
        private void UpdateRects()
        {
            if (_showActionButton == true)
            {
                int _buttonWidth = TextRenderer.MeasureText(ActionButtonText, SkinManager.GetFontByType(MaterialSkinManager.FontType.Button)).Width + 32;
                Rectangle _actionbuttonBounds = new(Width - BUTTON_PADDING - _buttonWidth, TOP_PADDING_SINGLE_LINE, _buttonWidth, BUTTON_HEIGHT);
                _actionButton.Width = _actionbuttonBounds.Width;
                _actionButton.Height = _actionbuttonBounds.Height;
                _actionButton.Text = _actionButtonText;
                _actionButton.Top = _actionbuttonBounds.Top;
                _actionButton.UseAccentColor = _useAccentColor;
            }
            else
            {
                _actionButton.Width = 0;
            }
            _actionButton.Left = Width - BUTTON_PADDING - _actionButton.Width;  //Button minimum width management
            _actionButton.Visible = _showActionButton;
 
            Width = TextRenderer.MeasureText(_text, SkinManager.GetFontByType(MaterialSkinManager.FontType.Body2)).Width + (2 * LEFT_RIGHT_PADDING) + _actionButton.Width + 48;
            Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 6, 6));
 
        }
        private void duration_Tick(object sender, EventArgs e)
        {
            _duration.Stop();
            _closingAnimationDone = false;
            Close();
        }
 
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            UpdateRects();
        }
 
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Location = new Point(Convert.ToInt32(Owner.Location.X + (Owner.Width / 2) - (Width / 2)), Convert.ToInt32(Owner.Location.Y + Owner.Height - 60));
            _AnimationManager.StartNewAnimation(AnimationDirection.In);
            _duration.Start();
        }
 
        void _AnimationManager_OnAnimationProgress(object sender)
        {
            if (CloseAnimation)
            {
                Opacity = _AnimationManager.GetProgress();
            }
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
 
            e.Graphics.Clear(BackColor);
 
            // Calc text Rect
            Rectangle textRect = new(
                LEFT_RIGHT_PADDING,
                0,
                Width - (2 * LEFT_RIGHT_PADDING) - _actionButton.Width,
                Height);
 
            //Draw  Text
            using MaterialNativeTextRenderer NativeText = new(g);
            // Draw header text
            NativeText.DrawTransparentText(
                _text,
                SkinManager.GetLogFontByType(MaterialSkinManager.FontType.Body2),
                SkinManager.SnackBarTextHighEmphasisColor,
                textRect.Location,
                textRect.Size,
                MaterialNativeTextRenderer.TextAlignFlags.Left | MaterialNativeTextRenderer.TextAlignFlags.Middle);
        }
 
        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = !_closingAnimationDone;
            if (!_closingAnimationDone)
            {
                CloseAnimation = true;
                _AnimationManager.Increment = 0.06;
                _AnimationManager.OnAnimationFinished += _AnimationManager_OnAnimationFinished;
                _AnimationManager.StartNewAnimation(AnimationDirection.Out);
            }
            base.OnClosing(e);
        }
 
        void _AnimationManager_OnAnimationFinished(object sender)
        {
            _closingAnimationDone = true;
            Close();
        }
 
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            _closingAnimationDone = false;
            Close();
        }
 
        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ClientSize = new Size(344, 48);
            this.Name = "SnackBar";
            this.ResumeLayout(false);
        }
 
        protected override void WndProc(ref Message message)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_MOVE = 0xF010;
 
            switch (message.Msg)
            {
                case WM_SYSCOMMAND:
                    int command = message.WParam.ToInt32() & 0xfff0;
                    if (command == SC_MOVE)
                    {
                        return;
                    }
 
                    break;
            }
 
            base.WndProc(ref message);
        }
 
        public new void Show()
        {
            if (Owner == null)
            {
                throw new Exception("Owner is null. Set Owner first.");
            }
        }
    }
 
    #endregion
}