/// <summary> /// Create a new notification, to be displayed with Show() later /// </summary> /// <param name="body">content of the notification</param> /// <param name="duration">life time in seconds, if 0 then it's a sticky notif</param> /// <param name="defaultWidth"></param> /// <param name="screenToUse">Specify the screen on which you want to dispaly the notif</param> public YamuiNotifications(string body, int duration, int defaultWidth = 300, Screen screenToUse = null) { InitializeComponent(); _screen = screenToUse ?? Screen.PrimaryScreen; Load += YamuiNotificationsLoad; Activated += YamuiNotificationsActivated; Shown += YamuiNotificationsShown; FormClosed += YamuiNotificationsFormClosed; // register to the panel onclicked event and propagate it as a public field of this class contentLabel.LinkClicked += OnLinkClicked; // close all notif button ShowCloseAllVisibleButton = true; OnCloseAllVisible = OnCloseAllVisibleNotif; // find max height taken by the html Width = _screen.WorkingArea.Width/2; contentLabel.Text = body; var prefHeight = Math.Min(contentLabel.Height + 18 + ((duration > 0) ? 10 : 0), _screen.WorkingArea.Height - 50); // now we got the final height, resize width until height changes int j = 0; int detla = 100; int curWidth = Width; do { curWidth -= detla; Width = Math.Min(_screen.WorkingArea.Width/2, curWidth); contentLabel.Text = body; if (contentLabel.Height > prefHeight) { curWidth += detla; detla /= 2; } j++; } while (j < 10); Width = Math.Max(curWidth, defaultWidth); Height = Math.Min(contentLabel.Height + 18 + ((duration > 0) ? 10 : 0), _screen.WorkingArea.Height - 50); // do we need to animate a panel on the bottom to visualise time left if (duration > 0) { _progressSimplePanel = new YamuiSimplePanel { BackColor = YamuiThemeManager.Current.AccentColor, AutoScroll = false, Location = new Point(1, Height - 11), Name = "progressPanel", Size = new Size(Width - 2, 10), TabStop = false, UseCustomBackColor = true }; Controls.Add(_progressSimplePanel); _duration = duration*1000; } else _duration = 0; // fade out animation Opacity = 0d; Tag = false; Closing += YamuiNotificationsOnClosing; // fade in animation Transition.run(this, "Opacity", 1d, new TransitionType_Acceleration(200)); }
/// <summary> /// Create a new notification, to be displayed with Show() later /// </summary> public YamuiNotification(string htmlTitle, string htmlMessage, int duration, Screen screenToUse = null, int formMinWidth = 0, int formMaxWidth = 0, int formMaxHeight = 0, EventHandler<HtmlLinkClickedEventArgs> onLinkClicked = null) { // close all notif button ShowCloseAllVisibleButton = true; OnCloseAllVisible = OnCloseAllVisibleNotif; InitializeComponent(); // correct input if needed _screen = screenToUse ?? Screen.PrimaryScreen; if (formMaxWidth == 0) formMaxWidth = _screen.WorkingArea.Width - 20; if (formMaxHeight == 0) formMaxHeight = _screen.WorkingArea.Height - 20; if (formMinWidth == 0) formMinWidth = 300; contentPanel.NoBackgroundImage = true; // Set title, it will define a new minimum width for the message box var space = FormButtonWidth + BorderWidth*2 + titleLabel.Location.X + 5; titleLabel.SetNeededSize(htmlTitle, formMinWidth - space, formMaxWidth - space, true); formMinWidth = formMinWidth.ClampMin(titleLabel.Width + space); var newPadding = Padding; newPadding.Bottom = newPadding.Bottom + (duration > 0 ? 8 : 0); newPadding.Top = titleLabel.Height + 10; Padding = newPadding; titleLabel.Location = new Point(5,5); // set content label space = Padding.Left + Padding.Right; contentLabel.SetNeededSize(htmlMessage, formMinWidth - space, formMaxWidth - space, true); contentPanel.ContentPanel.Size = contentLabel.Size; if (onLinkClicked != null) contentLabel.LinkClicked += onLinkClicked; // set form size Size = new Size(contentPanel.ContentPanel.Width + space, (Padding.Top + Padding.Bottom + contentLabel.Height).ClampMax(formMaxHeight)); if (contentPanel.HasScrolls) Width += 10; MinimumSize = Size; // do we need to animate a panel on the bottom to visualise time left? if (duration > 0) { _progressSimplePanel = new YamuiSimplePanel { BackColor = YamuiThemeManager.Current.AccentColor, AutoScroll = false, Location = new Point(1, Height - 9), Name = "progressPanel", Size = new Size(Width - 2, 8), TabStop = false, UseCustomBackColor = true }; Controls.Add(_progressSimplePanel); _duration = duration * 1000; } else _duration = 0; // for the outro animation Tag = false; // fade in animation Transition.run(this, "AnimationOpacity", 0d, 1d, new TransitionType_Acceleration(200)); }