From cdd181a0fb044eff95ea55e9e96c1e1ad67966d0 Mon Sep 17 00:00:00 2001 From: Muhammad Wahyu Pamengas Date: Fri, 15 Jun 2018 01:40:36 +0800 Subject: [PATCH 1/4] New feature: Live Background -Updated references -Added new settings (for live background) -Small changes here and there --- MoeIDE/EditorBackground.cs | 5 + MoeIDE/ImageSwitcher.xaml | 13 +++ MoeIDE/ImageSwitcher.xaml.cs | 197 +++++++++++++++++++++++++++++++++++ MoeIDE/MoeIDE.csproj | 55 +++++----- MoeIDE/SettingsModel.cs | 10 +- MoeIDE/SettingsPage.xaml | 74 +++++++++++-- MoeIDE/VSPackage.resx | 28 ++++- MoeIDE/WindowBackground.cs | 62 ++++++++--- 8 files changed, 396 insertions(+), 48 deletions(-) create mode 100644 MoeIDE/ImageSwitcher.xaml create mode 100644 MoeIDE/ImageSwitcher.xaml.cs diff --git a/MoeIDE/EditorBackground.cs b/MoeIDE/EditorBackground.cs index 7783d80..a96801f 100644 --- a/MoeIDE/EditorBackground.cs +++ b/MoeIDE/EditorBackground.cs @@ -114,6 +114,11 @@ private void SetSolidBrush(ThemeChangedEventArgs e) var color = uiShell.GetThemedWPFColor(EnvironmentColors.SystemWindowColorKey); var brush = new SolidColorBrush(color); brush.Freeze(); + if (hostRootVisual == null) + { + var source = PresentationSource.FromVisual(control) as HwndSource; + hostRootVisual = source.RootVisual as Panel; + } hostRootVisual.Background = brush; } diff --git a/MoeIDE/ImageSwitcher.xaml b/MoeIDE/ImageSwitcher.xaml new file mode 100644 index 0000000..2968a62 --- /dev/null +++ b/MoeIDE/ImageSwitcher.xaml @@ -0,0 +1,13 @@ + + + + + + diff --git a/MoeIDE/ImageSwitcher.xaml.cs b/MoeIDE/ImageSwitcher.xaml.cs new file mode 100644 index 0000000..ac8caad --- /dev/null +++ b/MoeIDE/ImageSwitcher.xaml.cs @@ -0,0 +1,197 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Timers; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Animation; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.Windows.Threading; + +namespace Meowtrix.MoeIDE +{ + /// + /// Interaction logic for ImageSwitcher.xaml + /// + public partial class ImageSwitcher : UserControl + { + public static readonly DependencyProperty StretchProperty = + DependencyProperty.Register("Stretch", typeof(Stretch), typeof(ImageSwitcher), new PropertyMetadata(Stretch.UniformToFill)); + + public Stretch Stretch + { + get { return (Stretch)GetValue(StretchProperty); } + set { SetValue(StretchProperty, value); } + } + + public static readonly DependencyProperty IntervalProperty = + DependencyProperty.Register("Interval", typeof(double), typeof(ImageSwitcher), new PropertyMetadata(120000.0)); + + public double Interval + { + get { return (double)GetValue(IntervalProperty); } + set { SetValue(IntervalProperty, value); } + } + + public enum Transitions + { + [Description("Smooth fade between 2 images")]FADE, // Implemented :D + [Description("Fade-out old image, fade-in new image")] FADE_IN_OUT, // todo: later + [Description("Slide new image to the left")] SLIDE_TO_LEFT, // todo: later + [Description("Slide new image to the right")] SLIDE_TO_RIGHT, //todo: later + } + + private DispatcherTimer backgroundChanger; + private Queue imageFiles; + private Storyboard story = new Storyboard(); + private bool Hibernating = true; + + public double TransitionDuration; + public Transitions TransitionType; + public Image ActiveImage; + + public ImageSwitcher(string mustExistsFolderPath) + { + TransitionType = Transitions.FADE; + Interval = 30000.0; + Stretch = Stretch.UniformToFill; + TransitionDuration = 2000.0; + var files = Directory.EnumerateFiles(mustExistsFolderPath).Where(f => f.EndsWith(".jpg") || f.EndsWith(".png")); + imageFiles = new Queue(files); + backgroundChanger = new DispatcherTimer + { + Interval = TimeSpan.FromMilliseconds(Interval) // 2 minutes | todo: implement on setting Page + }; + backgroundChanger.Tick += BackgroundChanger_Tick; + + InitializeComponent(); + } + + public void Hibernate() + { + backgroundChanger.Stop(); + Hibernating = true; + } + + public void Wake() + { + if (!Hibernating) + { + return; + } + + if (imageFiles.Count > 0) + { + var t = imageFiles.Dequeue(); + SwitchTo(LoadImage(t)); + imageFiles.Enqueue(t); + } + if (imageFiles.Count > 1) + { + backgroundChanger.Start(); + } + Hibernating = false; + } + + private void BackgroundChanger_Tick(object sender, EventArgs e) + { + backgroundChanger.Stop(); + + var t = imageFiles.Dequeue(); + ImageSource newImg = LoadImage(t); + imageFiles.Enqueue(t); + SwitchTo(newImg, TransitionType); + + backgroundChanger.Start(); + } + + public void SwitchTo(ImageSource newImage) + { + SwitchTo(newImage, TransitionType); + } + + public void SwitchTo(ImageSource newImage, Transitions transition) + { + if (newImage == null) + { + return; + } + switch (transition) + { + default: + case Transitions.FADE: + if (ActiveImage == behind) + { + front.Source = newImage; + ActiveImage = front; + front.BeginAnimation(OpacityProperty, + new DoubleAnimation(1, + new Duration(TimeSpan.FromMilliseconds(TransitionDuration)))); + } + else + { + behind.Source = newImage; + ActiveImage = behind; + front.BeginAnimation(OpacityProperty, + new DoubleAnimation(0, + new Duration(TimeSpan.FromMilliseconds(TransitionDuration)))); + } + break; + case Transitions.FADE_IN_OUT: + break; + case Transitions.SLIDE_TO_LEFT: + break; + case Transitions.SLIDE_TO_RIGHT: + break; + } + } + + private BitmapFrame LoadImage(string filename) + { + var imagesource = BitmapFrame.Create(new Uri(filename), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + imagesource.Freeze(); + return imagesource; + } + + public void ChangeFolder(string folderPath) + { + backgroundChanger.Stop(); + + var files = Directory.EnumerateFiles(folderPath).Where(f => f.EndsWith(".jpg") || f.EndsWith(".png")); + imageFiles = new Queue(files); + if (!Hibernating) + { + if (imageFiles.Count > 0) + { + var t = imageFiles.Dequeue(); + SwitchTo(LoadImage(t)); + imageFiles.Enqueue(t); + } + if (imageFiles.Count > 1) + { + backgroundChanger.Start(); + } + } + } + + private void behind_SourceUpdated(object sender, DataTransferEventArgs e) + { + ActiveImage = behind; + } + + private void front_SourceUpdated(object sender, DataTransferEventArgs e) + { + ActiveImage = front; + } + } +} diff --git a/MoeIDE/MoeIDE.csproj b/MoeIDE/MoeIDE.csproj index be9c70f..d6aedcb 100644 --- a/MoeIDE/MoeIDE.csproj +++ b/MoeIDE/MoeIDE.csproj @@ -55,6 +55,9 @@ + + ImageSwitcher.xaml + @@ -113,30 +116,30 @@ False - - - + + + - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -164,13 +167,17 @@ + + Designer + MSBuild:Compile + Designer MSBuild:Compile - + {15cacac9-9e3c-4056-a26d-b4b9cd7fe596} Meowtrix.WPF.Extend BuiltProjectOutputGroup%3bBuiltProjectOutputGroupDependencies%3bGetCopyToOutputDirectoryItems%3bSatelliteDllsProjectOutputGroup diff --git a/MoeIDE/SettingsModel.cs b/MoeIDE/SettingsModel.cs index 574f77a..8af7a61 100644 --- a/MoeIDE/SettingsModel.cs +++ b/MoeIDE/SettingsModel.cs @@ -9,18 +9,22 @@ namespace Meowtrix.MoeIDE public class SettingsModel { [Serializable] - public class ImageInfo + public class SettingPack { public string Filename { get; set; } + public bool IsLive { get; set; } = false; + public string Folderpath { get; set; } + public double Interval { get; set; } = 30000.0; + public double TransitionDuration { get; set; } = 2000.0; public Stretch Stretch { get; set; } = Stretch.UniformToFill; public HorizontalAlignment HorizontalAlignment { get; set; } = HorizontalAlignment.Center; public VerticalAlignment VerticalAlignment { get; set; } = VerticalAlignment.Center; public Color BackColor { get; set; } = Colors.Transparent; public double Opacity { get; set; } = 1.0; public double Blur { get; set; } = 0.0; - public ImageInfo Clone() => (ImageInfo)MemberwiseClone(); + public SettingPack Clone() => (SettingPack)MemberwiseClone(); } - public ImageInfo MainBackground { get; set; } = new ImageInfo(); + public SettingPack MainBackground { get; set; } = new SettingPack(); public SettingsModel Clone() => new SettingsModel { MainBackground = this.MainBackground.Clone() diff --git a/MoeIDE/SettingsPage.xaml b/MoeIDE/SettingsPage.xaml index 5dc16b0..1c8788e 100644 --- a/MoeIDE/SettingsPage.xaml +++ b/MoeIDE/SettingsPage.xaml @@ -6,7 +6,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:meowtrix="urn:meowtrix" d:DataContext="{d:DesignInstance local:SettingsModel}" - d:DesignHeight="300" d:DesignWidth="300" + d:DesignWidth="300" Focusable="True" mc:Ignorable="d"> @@ -19,7 +19,73 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -36,10 +102,6 @@ - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - MoeIDE @@ -128,7 +127,7 @@ ※Please select a transparent theme in Environment->General->Color Theme to get best experience. - Main Background + Static Background Settings File name @@ -151,6 +150,31 @@ Blur radius + + Select Background Mode + + + Static + + + Live (Experimental) + + + Live Background Settings + + + Image Settings + + + Folder path + + + Interval (Milliseconds) + + + Trans. Duration + + resources\moeide.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/MoeIDE/WindowBackground.cs b/MoeIDE/WindowBackground.cs index 3b13707..18e794c 100644 --- a/MoeIDE/WindowBackground.cs +++ b/MoeIDE/WindowBackground.cs @@ -10,44 +10,80 @@ namespace Meowtrix.MoeIDE public class WindowBackground { private readonly Border parentBorder = new Border(); - private readonly Image imagecontrol = new Image(); + private readonly ImageSwitcher liveImageControl = null; + private readonly Image staticImageControl = new Image(); + private bool IsLiveMode = false; + public WindowBackground(Window window) { if (window.IsLoaded) Window_Loaded(window, null); window.Loaded += Window_Loaded; SettingsManager.SettingsUpdated += SettingsUpdated; + liveImageControl = new ImageSwitcher(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)); } private void SettingsUpdated(SettingsModel oldSettings, SettingsModel newSettings) { try { - var imagesource = BitmapFrame.Create(new Uri(newSettings.MainBackground.Filename), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); - imagesource.Freeze(); - imagecontrol.Source = imagesource; - imagecontrol.Stretch = newSettings.MainBackground.Stretch; - imagecontrol.HorizontalAlignment = newSettings.MainBackground.HorizontalAlignment; - imagecontrol.VerticalAlignment = newSettings.MainBackground.VerticalAlignment; + IsLiveMode = newSettings.MainBackground.IsLive; + if (IsLiveMode) + { + liveImageControl.ChangeFolder(newSettings.MainBackground.Folderpath); + liveImageControl.Stretch = newSettings.MainBackground.Stretch; + liveImageControl.Interval = newSettings.MainBackground.Interval; + liveImageControl.TransitionDuration = newSettings.MainBackground.TransitionDuration; + liveImageControl.HorizontalAlignment = newSettings.MainBackground.HorizontalAlignment; + liveImageControl.VerticalAlignment = newSettings.MainBackground.VerticalAlignment; + + liveImageControl.Wake(); + } + else + { + liveImageControl.Hibernate(); + + var imagesource = BitmapFrame.Create(new Uri(newSettings.MainBackground.Filename), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + imagesource.Freeze(); + staticImageControl.Source = imagesource; + staticImageControl.Stretch = newSettings.MainBackground.Stretch; + staticImageControl.HorizontalAlignment = newSettings.MainBackground.HorizontalAlignment; + staticImageControl.VerticalAlignment = newSettings.MainBackground.VerticalAlignment; + } + + var br = new SolidColorBrush(newSettings.MainBackground.BackColor); br.Freeze(); parentBorder.Background = br; - imagecontrol.Opacity = newSettings.MainBackground.Opacity; + + liveImageControl.Opacity = newSettings.MainBackground.Opacity; + staticImageControl.Opacity = newSettings.MainBackground.Opacity; + double blur = newSettings.MainBackground.Blur; if (blur == 0.0) - imagecontrol.Effect = null; - else imagecontrol.Effect = new BlurEffect { Radius = blur }; + { + liveImageControl.Effect = null; + staticImageControl.Effect = null; + } + else + { + liveImageControl.Effect = new BlurEffect { Radius = blur }; + staticImageControl.Effect = new BlurEffect { Radius = blur }; + } + + parentBorder.Child = IsLiveMode ? (UIElement)liveImageControl : staticImageControl; } catch { - imagecontrol.Source = null; + liveImageControl.SwitchTo(null); + staticImageControl.Source = null; + parentBorder.Child = staticImageControl; } } private void Window_Loaded(object sender, RoutedEventArgs e) { var mainwindow = (Window)sender; - - parentBorder.Child = imagecontrol; + var cache = new BitmapCache { SnapsToDevicePixels = true }; cache.Freeze(); parentBorder.CacheMode = cache; From afec56d0741f4125340afb3dc486df289d41c1a8 Mon Sep 17 00:00:00 2001 From: Muhammad Wahyu Pamengas Date: Fri, 15 Jun 2018 02:00:24 +0800 Subject: [PATCH 2/4] New feature: Live Background -Updated references -Added new settings (for live background) -Small changes here and there --- MoeIDE/ImageSwitcher.xaml.cs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/MoeIDE/ImageSwitcher.xaml.cs b/MoeIDE/ImageSwitcher.xaml.cs index ac8caad..c42d250 100644 --- a/MoeIDE/ImageSwitcher.xaml.cs +++ b/MoeIDE/ImageSwitcher.xaml.cs @@ -40,7 +40,19 @@ public Stretch Stretch public double Interval { get { return (double)GetValue(IntervalProperty); } - set { SetValue(IntervalProperty, value); } + set + { + if ((double)GetValue(IntervalProperty) != value) + { + backgroundChanger.Stop(); + backgroundChanger.Interval = TimeSpan.FromMilliseconds(value); + if (!Hibernating) + { + backgroundChanger.Start(); + } + SetValue(IntervalProperty, value); + } + } } public enum Transitions From 682aa81d81048d1c5b208d66f4317d2c7ad0a758 Mon Sep 17 00:00:00 2001 From: Muhammad Wahyu Pamengas Date: Fri, 15 Jun 2018 03:35:39 +0800 Subject: [PATCH 3/4] New feature: Live Background -bugfixes --- MoeIDE/ImageSwitcher.xaml.cs | 17 ++++++++++++----- MoeIDE/WindowBackground.cs | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/MoeIDE/ImageSwitcher.xaml.cs b/MoeIDE/ImageSwitcher.xaml.cs index c42d250..75b33c1 100644 --- a/MoeIDE/ImageSwitcher.xaml.cs +++ b/MoeIDE/ImageSwitcher.xaml.cs @@ -44,11 +44,14 @@ public double Interval { if ((double)GetValue(IntervalProperty) != value) { - backgroundChanger.Stop(); - backgroundChanger.Interval = TimeSpan.FromMilliseconds(value); - if (!Hibernating) + if (backgroundChanger != null) { - backgroundChanger.Start(); + backgroundChanger.Stop(); + backgroundChanger.Interval = TimeSpan.FromMilliseconds(value); + if (!Hibernating) + { + backgroundChanger.Start(); + } } SetValue(IntervalProperty, value); } @@ -177,6 +180,10 @@ private BitmapFrame LoadImage(string filename) public void ChangeFolder(string folderPath) { + if (imageFiles.Count > 0 && imageFiles.Peek().Contains(folderPath)) + { + return; + } backgroundChanger.Stop(); var files = Directory.EnumerateFiles(folderPath).Where(f => f.EndsWith(".jpg") || f.EndsWith(".png")); @@ -186,7 +193,7 @@ public void ChangeFolder(string folderPath) if (imageFiles.Count > 0) { var t = imageFiles.Dequeue(); - SwitchTo(LoadImage(t)); + SwitchTo(LoadImage(t), TransitionType); imageFiles.Enqueue(t); } if (imageFiles.Count > 1) diff --git a/MoeIDE/WindowBackground.cs b/MoeIDE/WindowBackground.cs index 18e794c..478c1d4 100644 --- a/MoeIDE/WindowBackground.cs +++ b/MoeIDE/WindowBackground.cs @@ -74,7 +74,7 @@ private void SettingsUpdated(SettingsModel oldSettings, SettingsModel newSetting } catch { - liveImageControl.SwitchTo(null); + liveImageControl.Hibernate(); staticImageControl.Source = null; parentBorder.Child = staticImageControl; } From 3fca15a721824bdbc1f22ed1bc2a4137099178e2 Mon Sep 17 00:00:00 2001 From: Muhammad Wahyu Pamengas Date: Sun, 17 Jun 2018 11:16:02 +0800 Subject: [PATCH 4/4] Minor bugfix -fixed: slideshow randomly stopped when settings applied -cleaned unused Usings --- MoeIDE/ImageSwitcher.xaml.cs | 13 +++++-------- MoeIDE/SettingsModel.cs | 4 ++-- MoeIDE/WindowBackground.cs | 31 +++++++++++++++---------------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/MoeIDE/ImageSwitcher.xaml.cs b/MoeIDE/ImageSwitcher.xaml.cs index 75b33c1..b13497a 100644 --- a/MoeIDE/ImageSwitcher.xaml.cs +++ b/MoeIDE/ImageSwitcher.xaml.cs @@ -3,19 +3,12 @@ using System.ComponentModel; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; using System.Windows.Threading; namespace Meowtrix.MoeIDE @@ -102,6 +95,10 @@ public void Wake() { if (!Hibernating) { + if (!backgroundChanger.IsEnabled) + { + backgroundChanger.Start(); + } return; } @@ -213,4 +210,4 @@ private void front_SourceUpdated(object sender, DataTransferEventArgs e) ActiveImage = front; } } -} +} \ No newline at end of file diff --git a/MoeIDE/SettingsModel.cs b/MoeIDE/SettingsModel.cs index 8af7a61..cd007ed 100644 --- a/MoeIDE/SettingsModel.cs +++ b/MoeIDE/SettingsModel.cs @@ -24,10 +24,10 @@ public class SettingPack public double Blur { get; set; } = 0.0; public SettingPack Clone() => (SettingPack)MemberwiseClone(); } - public SettingPack MainBackground { get; set; } = new SettingPack(); + public SettingPack MainSetting { get; set; } = new SettingPack(); public SettingsModel Clone() => new SettingsModel { - MainBackground = this.MainBackground.Clone() + MainSetting = this.MainSetting.Clone() }; } } diff --git a/MoeIDE/WindowBackground.cs b/MoeIDE/WindowBackground.cs index 478c1d4..4ed3802 100644 --- a/MoeIDE/WindowBackground.cs +++ b/MoeIDE/WindowBackground.cs @@ -26,15 +26,15 @@ private void SettingsUpdated(SettingsModel oldSettings, SettingsModel newSetting { try { - IsLiveMode = newSettings.MainBackground.IsLive; + IsLiveMode = newSettings.MainSetting.IsLive; if (IsLiveMode) { - liveImageControl.ChangeFolder(newSettings.MainBackground.Folderpath); - liveImageControl.Stretch = newSettings.MainBackground.Stretch; - liveImageControl.Interval = newSettings.MainBackground.Interval; - liveImageControl.TransitionDuration = newSettings.MainBackground.TransitionDuration; - liveImageControl.HorizontalAlignment = newSettings.MainBackground.HorizontalAlignment; - liveImageControl.VerticalAlignment = newSettings.MainBackground.VerticalAlignment; + liveImageControl.ChangeFolder(newSettings.MainSetting.Folderpath); + liveImageControl.Stretch = newSettings.MainSetting.Stretch; + liveImageControl.Interval = newSettings.MainSetting.Interval; + liveImageControl.TransitionDuration = newSettings.MainSetting.TransitionDuration; + liveImageControl.HorizontalAlignment = newSettings.MainSetting.HorizontalAlignment; + liveImageControl.VerticalAlignment = newSettings.MainSetting.VerticalAlignment; liveImageControl.Wake(); } @@ -42,23 +42,22 @@ private void SettingsUpdated(SettingsModel oldSettings, SettingsModel newSetting { liveImageControl.Hibernate(); - var imagesource = BitmapFrame.Create(new Uri(newSettings.MainBackground.Filename), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + var imagesource = BitmapFrame.Create(new Uri(newSettings.MainSetting.Filename), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); imagesource.Freeze(); staticImageControl.Source = imagesource; - staticImageControl.Stretch = newSettings.MainBackground.Stretch; - staticImageControl.HorizontalAlignment = newSettings.MainBackground.HorizontalAlignment; - staticImageControl.VerticalAlignment = newSettings.MainBackground.VerticalAlignment; + staticImageControl.Stretch = newSettings.MainSetting.Stretch; + staticImageControl.HorizontalAlignment = newSettings.MainSetting.HorizontalAlignment; + staticImageControl.VerticalAlignment = newSettings.MainSetting.VerticalAlignment; } - - var br = new SolidColorBrush(newSettings.MainBackground.BackColor); + var br = new SolidColorBrush(newSettings.MainSetting.BackColor); br.Freeze(); parentBorder.Background = br; - liveImageControl.Opacity = newSettings.MainBackground.Opacity; - staticImageControl.Opacity = newSettings.MainBackground.Opacity; + liveImageControl.Opacity = newSettings.MainSetting.Opacity; + staticImageControl.Opacity = newSettings.MainSetting.Opacity; - double blur = newSettings.MainBackground.Blur; + double blur = newSettings.MainSetting.Blur; if (blur == 0.0) { liveImageControl.Effect = null;