From 567c565bf4797153d2fb3ef31f253718b216e308 Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 30 Sep 2020 20:37:44 +0300 Subject: [PATCH] Added Title support to notification form --- ShareX/Forms/NotificationForm.cs | 49 ++++++++++++++++++++++++++------ ShareX/TaskHelpers.cs | 11 ++++--- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/ShareX/Forms/NotificationForm.cs b/ShareX/Forms/NotificationForm.cs index 6a3210e6d..d3d2cc3c3 100644 --- a/ShareX/Forms/NotificationForm.cs +++ b/ShareX/Forms/NotificationForm.cs @@ -36,14 +36,18 @@ public class NotificationFormConfig : IDisposable public int Duration { get; set; } public int FadeDuration { get; set; } public ContentAlignment Placement { get; set; } - public int Offset { get; set; } = 3; + public int Offset { get; set; } = 5; public Size Size { get; set; } public bool IsValid => (Duration > 0 || FadeDuration > 0) && Size.Width > 0 && Size.Height > 0; - public int TextPadding { get; set; } = 8; - public Font TextFont { get; set; } = new Font("Arial", 10); + public int TextPadding { get; set; } = 10; + public Font TextFont { get; set; } = new Font("Arial", 11); + public Color TextColor { get; set; } = Color.FromArgb(210, 210, 210); + public Font TitleFont { get; set; } = new Font("Arial", 11, FontStyle.Bold); + public Color TitleColor { get; set; } = Color.FromArgb(255, 255, 255); public Bitmap Image { get; set; } public string Text { get; set; } + public string Title { get; set; } public string FilePath { get; set; } public string URL { get; set; } public ToastClickAction LeftClickAction { get; set; } @@ -57,6 +61,11 @@ public void Dispose() TextFont.Dispose(); } + if (TitleFont != null) + { + TitleFont.Dispose(); + } + if (Image != null) { Image.Dispose(); @@ -73,7 +82,10 @@ public class NotificationForm : Form private int fadeInterval = 50; private float opacityDecrement; private int urlPadding = 3; + private int titleSpace = 3; + private Size titleRenderSize; private Size textRenderSize; + private Size totalRenderSize; protected override CreateParams CreateParams { @@ -103,8 +115,18 @@ private NotificationForm(NotificationFormConfig config) } else if (!string.IsNullOrEmpty(Config.Text)) { - textRenderSize = TextRenderer.MeasureText(Config.Text, Config.TextFont, Config.Size.Offset(-Config.TextPadding * 2), TextFormatFlags.Left | TextFormatFlags.EndEllipsis); - Config.Size = new Size(textRenderSize.Width + (Config.TextPadding * 2), textRenderSize.Height + (Config.TextPadding * 2) + 2); + textRenderSize = TextRenderer.MeasureText(Config.Text, Config.TextFont, Config.Size.Offset(-Config.TextPadding * 2), + TextFormatFlags.Left | TextFormatFlags.EndEllipsis); + totalRenderSize = textRenderSize; + + if (!string.IsNullOrEmpty(Config.Title)) + { + titleRenderSize = TextRenderer.MeasureText(Config.Title, Config.TitleFont, Config.Size.Offset(-Config.TextPadding * 2), + TextFormatFlags.Left | TextFormatFlags.EndEllipsis); + totalRenderSize = new Size(Math.Max(textRenderSize.Width, titleRenderSize.Width), titleRenderSize.Height + titleSpace + textRenderSize.Height); + } + + Config.Size = new Size(totalRenderSize.Width + (Config.TextPadding * 2), totalRenderSize.Height + (Config.TextPadding * 2) + 2); } Point position = Helpers.GetPosition(Config.Placement, Config.Offset, Screen.PrimaryScreen.WorkingArea.Size, Config.Size); @@ -211,9 +233,20 @@ protected override void OnPaint(PaintEventArgs e) g.FillRectangle(brush, rect); } - Rectangle textRect = new Rectangle(Config.TextPadding, Config.TextPadding, textRenderSize.Width + 2, textRenderSize.Height + 2); - TextRenderer.DrawText(g, Config.Text, Config.TextFont, textRect, Color.Black, TextFormatFlags.Left | TextFormatFlags.EndEllipsis); - TextRenderer.DrawText(g, Config.Text, Config.TextFont, textRect.LocationOffset(1), Color.White, TextFormatFlags.Left | TextFormatFlags.EndEllipsis); + Rectangle textRect; + + if (!string.IsNullOrEmpty(Config.Title)) + { + Rectangle titleRect = new Rectangle(Config.TextPadding, Config.TextPadding, titleRenderSize.Width + 2, titleRenderSize.Height + 2); + TextRenderer.DrawText(g, Config.Title, Config.TitleFont, titleRect, Color.White, TextFormatFlags.Left | TextFormatFlags.EndEllipsis); + textRect = new Rectangle(Config.TextPadding, Config.TextPadding + titleRect.Height + titleSpace, textRenderSize.Width + 2, textRenderSize.Height + 2); + } + else + { + textRect = new Rectangle(Config.TextPadding, Config.TextPadding, textRenderSize.Width + 2, textRenderSize.Height + 2); + } + + TextRenderer.DrawText(g, Config.Text, Config.TextFont, textRect, Color.FromArgb(240, 240, 240), TextFormatFlags.Left | TextFormatFlags.EndEllipsis); } Color borderColor = ShareXResources.UseCustomTheme ? ShareXResources.Theme.BorderColor : SystemColors.ControlText; diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index 545e29d47..d595e4559 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -816,15 +816,17 @@ public static void OpenScreenColorPicker(TaskSettings taskSettings = null) if (!taskSettings.AdvancedSettings.DisableNotifications) { + // TODO: Translate + string tipTitle = "ShareX - " + "Screen color picker"; string tipText = string.Format(Resources.TaskHelpers_OpenQuickScreenColorPicker_Copied_to_clipboard___0_, text); switch (taskSettings.GeneralSettings.PopUpNotification) { case PopUpNotificationType.BalloonTip: - ShowBalloonTip(tipText, ToolTipIcon.Info, 3000); + ShowBalloonTip(tipText, ToolTipIcon.Info, 3000, tipTitle); break; case PopUpNotificationType.ToastNotification: - ShowNotificationTip(tipText); + ShowNotificationTip(tipText, tipTitle); break; } } @@ -1869,7 +1871,7 @@ public static void ShowBalloonTip(string text, ToolTipIcon icon, int timeout, st } } - public static void ShowNotificationTip(string text) + public static void ShowNotificationTip(string text, string title = "ShareX") { NotificationFormConfig toastConfig = new NotificationFormConfig() { @@ -1877,7 +1879,8 @@ public static void ShowNotificationTip(string text) FadeDuration = (int)(Program.DefaultTaskSettings.AdvancedSettings.ToastWindowFadeDuration * 1000), Placement = Program.DefaultTaskSettings.AdvancedSettings.ToastWindowPlacement, Size = Program.DefaultTaskSettings.AdvancedSettings.ToastWindowSize, - Text = text + Text = text, + Title = title }; NotificationForm.Show(toastConfig);