From 5300e78e2a72578fed911884a0beda922f749d55 Mon Sep 17 00:00:00 2001 From: jonteohr Date: Mon, 22 Apr 2024 14:51:31 +0200 Subject: [PATCH] Added the TyperLabel control --- ExampleApp/MainWindow.xaml | 42 +++++++------ ExampleApp/MainWindow.xaml.cs | 67 +------------------- GradientTheme/Controls/TyperLabel.cs | 94 ++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 86 deletions(-) create mode 100644 GradientTheme/Controls/TyperLabel.cs diff --git a/ExampleApp/MainWindow.xaml b/ExampleApp/MainWindow.xaml index 5f86be5..59ed0ab 100644 --- a/ExampleApp/MainWindow.xaml +++ b/ExampleApp/MainWindow.xaml @@ -3,6 +3,8 @@ 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" + xmlns:controls="clr-namespace:GradientTheme.Controls;assembly=GradientTheme" + xmlns:sys="clr-namespace:System;assembly=mscorlib" mc:Ignorable="d" Title="MyApp" Height="450" Width="800" MouseDown="MainWindow_OnMouseDown" @@ -47,31 +49,31 @@ - + + + + + + - - - + + - - - - - - - - - - - - - + + + + + One Title + Another Title + Great Title + + + + + - diff --git a/ExampleApp/MainWindow.xaml.cs b/ExampleApp/MainWindow.xaml.cs index 4cc0ace..c62083f 100644 --- a/ExampleApp/MainWindow.xaml.cs +++ b/ExampleApp/MainWindow.xaml.cs @@ -21,65 +21,11 @@ public partial class MainWindow : Window [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); - private int m_typeSpeed = 150; - private int m_timerspeed = 3; - private bool m_typing; - - public string WindowTitle { get; private set; } - private string[] Titles = - [ - "A nice app", - "App title", - "Cool title", - "Useful stuff!", - "This title changes", - "Captivating title", - "Lorem ipsum" - ]; - public MainWindow() { - WindowTitle = GetRandomTitle(); - InitializeComponent(); - DataContext = this; - var timer = new Timer(); - timer.Interval = m_timerspeed * 1000; - timer.AutoReset = true; - timer.Elapsed += TimerOnElapsed; - timer.Enabled = true; - timer.Start(); - } - - private void TimerOnElapsed(object? sender, ElapsedEventArgs e) - { - while (!m_typing) - { - m_typing = true; - for (var i = WindowTitle.Length - 1; i < WindowTitle.Length; i--) - { - if (i < 0) - break; - - WindowTitle = WindowTitle.Remove(i); - SafeThreadInvoker(() => TitleLabel.Content = WindowTitle); - Thread.Sleep(m_typeSpeed); - } - - WindowTitle = GetRandomTitle(); - - for (var i = 0; i <= WindowTitle.Length; i++) - { - var tmp = WindowTitle.Substring(0, i); - SafeThreadInvoker(() => TitleLabel.Content = tmp); - Thread.Sleep(m_typeSpeed); - } - - break; - } - - m_typing = false; + DataContext = this; } private void CloseBtn_OnClick(object sender, RoutedEventArgs e) @@ -108,15 +54,4 @@ private void SafeThreadInvoker(Action a) else a(); } - - private string GetRandomTitle() - { - var rand = new Random(); - - var generated = Titles[rand.Next(0, Titles.Length)]; - while (generated == WindowTitle) - generated = Titles[rand.Next(0, Titles.Length)]; - - return generated; - } } \ No newline at end of file diff --git a/GradientTheme/Controls/TyperLabel.cs b/GradientTheme/Controls/TyperLabel.cs new file mode 100644 index 0000000..602ce25 --- /dev/null +++ b/GradientTheme/Controls/TyperLabel.cs @@ -0,0 +1,94 @@ +using System.Timers; +using Timer = System.Timers.Timer; +using System.Windows.Controls; +using System.Windows.Data; + +namespace GradientTheme.Controls; + +public class TyperLabel : Label +{ + private const int TypeSpeed = 150; + private const int TimerSpeed = 3; + private bool m_typing; + private string WindowTitle { get; set; } + private string m_lastTitle; + + private string[] m_titles; + public string[] Titles + { + get => m_titles; + set + { + m_titles = value; + Content = WindowTitle = GetRandomTitle(); + } + } + + public TyperLabel() + { + var binding = new Binding("WindowTitle"); + BindingOperations.SetBinding(this, TyperLabel.ContentProperty, binding); + + var timer = new Timer(); + timer.Interval = TimerSpeed * 1000; + timer.AutoReset = true; + timer.Elapsed += TimerOnElapsed; + timer.Enabled = true; + timer.Start(); + } + + private void TimerOnElapsed(object? sender, ElapsedEventArgs e) + { + while (!m_typing) + { + m_typing = true; + + m_lastTitle = WindowTitle; + + for (var i = WindowTitle.Length - 1; i < WindowTitle.Length; i--) + { + if (i < 0) + break; + + WindowTitle = WindowTitle.Remove(i); + SafeThreadInvoker(() => Content = WindowTitle); + Thread.Sleep(TypeSpeed); + } + + WindowTitle = GetRandomTitle(); + + for (var i = 0; i <= WindowTitle.Length; i++) + { + var tmp = WindowTitle.Substring(0, i); + SafeThreadInvoker(() => Content = tmp); + Thread.Sleep(TypeSpeed); + } + + break; + } + + m_typing = false; + } + + private void SafeThreadInvoker(Action a) + { + if (!CheckAccess()) + Dispatcher.Invoke(a); + else + a(); + } + + private string GetRandomTitle() + { + var rand = new Random(); + + string generated; + do + { + generated = Titles[rand.Next(0, Titles.Length)]; + } + while (generated == m_lastTitle); + + return generated; + } +} \ No newline at end of file