Skip to content

Commit

Permalink
Use TrySetWallpaperImageAsync function in UWP app
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Jul 29, 2018
1 parent 3b1e8e6 commit 6221a33
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/AboutDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AboutDialog()
private void AboutDialog_Load(object sender, EventArgs e)
{
iconBox.Image = (new Icon(Properties.Resources.AppIcon, 64, 64)).ToBitmap();

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
nameLabel.Text += " " + version;

Expand Down
10 changes: 5 additions & 5 deletions src/AppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private void EnforceSingleInstance()

if (!isNewInstance)
{
MessageBox.Show("Another instance of WinDynamicDesktop is already running.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show("Another instance of WinDynamicDesktop is already running.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

Environment.Exit(0);
}
Expand Down Expand Up @@ -109,7 +109,7 @@ private void OnDarkModeClick(object sender, EventArgs e)
{
ToggleDarkMode();
}

private void OnStartOnBootClick(object sender, EventArgs e)
{
_startupManager.ToggleStartOnBoot();
Expand Down Expand Up @@ -161,8 +161,8 @@ private void OnDownloadDialogClosed(object sender, EventArgs e)
if (!Directory.Exists("images"))
{
DialogResult result = MessageBox.Show("Failed to download images. Click Retry to " +
"try again or Cancel to exit the program.", "Error", MessageBoxButtons.RetryCancel,
MessageBoxIcon.Error);
"try again or Cancel to exit the program.", "Error",
MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);

if (result == DialogResult.Retry)
{
Expand Down
12 changes: 10 additions & 2 deletions src/DesktopHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ public static string GetCurrentDirectory()
{
return Path.GetDirectoryName(Application.ExecutablePath);
}

public static void SetWallpaper(string imageFilename)
{
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images",
imageFilename);

WallpaperChanger.EnableTransitions();
WallpaperChanger.SetWallpaper(imagePath);
}
}

class DesktopStartupManager : StartupManager
{
private bool startOnBoot;
private string registryStartupLocation = @"Software\Microsoft\Windows\CurrentVersion\Run";

public DesktopStartupManager(MenuItem startupMenuItem) : base(startupMenuItem)
Expand Down
17 changes: 9 additions & 8 deletions src/InputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ private void okButton_Click(object sender, EventArgs e)
}

MessageBox.Show("Location set successfully to: " + data.display_name +
Environment.NewLine + "(Latitude = " + data.lat + ", Longitude = " + data.lon + ")",
"Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
Environment.NewLine + "(Latitude = " + data.lat + ", Longitude = " + data.lon +
")", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

this.Close();
}
else
{
MessageBox.Show("The location you entered was invalid, or you are not connected to " +
"the Internet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show("The location you entered was invalid, or you are not connected " +
"to the Internet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

okButton.Enabled = true;
Expand All @@ -78,10 +78,11 @@ private void OnFormClosing(object sender, FormClosingEventArgs e)
{
if (JsonConfig.settings.location == null)
{
DialogResult result = MessageBox.Show("This app cannot display wallpapers until you " +
"have entered a valid location, so that it can calculate sunrise and sunset" +
"times for your location. Are you sure you want to cancel and quit the program?",
"Question", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
DialogResult result = MessageBox.Show("WinDynamicDesktop cannot display" +
"wallpapers until you have entered a valid location, so that it can " +
"calculate sunrise and sunset times for your location. Are you sure you want " +
"to cancel and quit the program?", "Question", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);

if (result == DialogResult.Yes)
{
Expand Down
2 changes: 1 addition & 1 deletion src/LocationIQ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LocationIQService
{
private static string apiKey = Encoding.UTF8.GetString(Convert.FromBase64String(
"cGsuYmRhNTk1NDRhN2VjZWMxYjAxMDZkNzg5MzdlMDQzOTk ="));

public static LocationIQData GetLocationData(string locationStr)
{
var client = new RestClient("https://us1.locationiq.org");
Expand Down
8 changes: 4 additions & 4 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
File.AppendAllText(logFilename, errorMessage);

MessageBox.Show("See the logfile '" + logFilename + "' for details", "Errors occurred",
MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show("See the logfile '" + logFilename + "' for details",
"Errors occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch
{
MessageBox.Show("The logfile '" + logFilename + "' could not be opened:" +
Environment.NewLine + " " + errorMessage, "Errors occurred", MessageBoxButtons.OK,
MessageBoxIcon.Error);
Environment.NewLine + " " + errorMessage, "Errors occurred",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.4.0")]
[assembly: AssemblyVersion("1.4.1")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
26 changes: 13 additions & 13 deletions src/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public static void Initialize(NotifyIcon notifyIcon)
_notifyIcon = notifyIcon;
_notifyIcon.ContextMenu.MenuItems.Add(8, new MenuItem("&Check for Updates Now",
OnUpdateItemClick));
_notifyIcon.ContextMenu.MenuItems.Add(9, new MenuItem("C&heck Automatically Once a Week",
OnAutoUpdateItemClick));
_notifyIcon.ContextMenu.MenuItems.Add(9, new MenuItem(
"C&heck Automatically Once a Week", OnAutoUpdateItemClick));
_notifyIcon.ContextMenu.MenuItems[9].Checked = !JsonConfig.settings.disableAutoUpdate;
_notifyIcon.ContextMenu.MenuItems.Add(10, new MenuItem("-"));

Expand Down Expand Up @@ -84,16 +84,16 @@ public static void CheckManual()

if (latestVersion == null)
{
MessageBox.Show("WinDynamicDesktop could not connect to the Internet to check for updates.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
MessageBox.Show("WinDynamicDesktop could not connect to the Internet to check " +
"for updates.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else if (IsUpdateAvailable(currentVersion, latestVersion))
{
DialogResult result = MessageBox.Show("There is a newer version of WinDynamicDesktop " +
"available. Would you like to visit the download page?" + Environment.NewLine +
Environment.NewLine + "Current Version: " + currentVersion + Environment.NewLine +
"Latest Version: " + latestVersion, "Update Available", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
DialogResult result = MessageBox.Show("There is a newer version of " +
"WinDynamicDesktop available. Would you like to visit the download page?" +
Environment.NewLine + Environment.NewLine + "Current Version: " +
currentVersion + Environment.NewLine + "Latest Version: " + latestVersion,
"Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.Yes)
{
Expand All @@ -102,8 +102,8 @@ public static void CheckManual()
}
else
{
MessageBox.Show("You already have the latest version of WinDynamicDesktop installed.",
"Up To Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show("You already have the latest version of WinDynamicDesktop " +
"installed.", "Up To Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

Expand All @@ -119,8 +119,8 @@ private static void CheckAuto()
else if (IsUpdateAvailable(currentVersion, latestVersion))
{
_notifyIcon.BalloonTipTitle = "Update Available";
_notifyIcon.BalloonTipText = "WinDynamicDesktop " + latestVersion + " is available. " +
"Click here to download it.";
_notifyIcon.BalloonTipText = "WinDynamicDesktop " + latestVersion +
" is available. Click here to download it.";
_notifyIcon.ShowBalloonTip(10000);
}

Expand Down
13 changes: 13 additions & 0 deletions src/UwpDesktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace WinDynamicDesktop
{
abstract class StartupManager
{
internal bool startOnBoot;
internal MenuItem _menuItem;

public StartupManager(MenuItem startupMenuItem)
Expand Down Expand Up @@ -58,5 +59,17 @@ public static StartupManager GetStartupManager(MenuItem startupMenuItem)
return new UwpStartupManager(startupMenuItem);
}
}

public static void SetWallpaper(string imageFilename)
{
if (!IsRunningAsUwp())
{
DesktopHelper.SetWallpaper(imageFilename);
}
else
{
UwpHelper.SetWallpaper(imageFilename);
}
}
}
}
17 changes: 9 additions & 8 deletions src/UwpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ public static string GetCurrentDirectory()

public static async void SetWallpaper(string imageFilename)
{
//var uri = new Uri("ms-appx://Local/images/" + imageFilename);
//var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "images", imageFilename));
var profileSettings = Windows.System.UserProfile.UserProfilePersonalizationSettings.Current;
var uri = new Uri("ms-appdata:///local/images/" + imageFilename);
var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);

var profileSettings =
Windows.System.UserProfile.UserProfilePersonalizationSettings.Current;
await profileSettings.TrySetWallpaperImageAsync(file);
}
}

class UwpStartupManager : StartupManager
{
private bool startOnBoot;

public UwpStartupManager(MenuItem startupMenuItem) : base(startupMenuItem)
{
UpdateStatus();
}

private async void UpdateStatus()
{
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync("WinDynamicDesktopUwp");
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync(
"WinDynamicDesktopUwp");

switch (startupTask.State)
{
Expand All @@ -56,7 +56,8 @@ private async void UpdateStatus()

public override async void ToggleStartOnBoot()
{
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync("WinDynamicDesktopUwp");
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync(
"WinDynamicDesktopUwp");

if (!startOnBoot)
{
Expand Down
18 changes: 11 additions & 7 deletions src/WallpaperChangeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ public WallpaperChangeScheduler()
public void LoadImageLists()
{
nightImages = JsonConfig.imageSettings.nightImageList;
dayImages = JsonConfig.settings.darkMode ? nightImages : JsonConfig.imageSettings.dayImageList;

if (!JsonConfig.settings.darkMode)
{
dayImages = JsonConfig.imageSettings.dayImageList;
}
else
{
dayImages = nightImages;
}
}

private string GetDateString(int todayDelta = 0)
Expand Down Expand Up @@ -81,19 +89,15 @@ private void StartTimer(long intervalTicks, TimeSpan maxInterval)
private void SetWallpaper(int imageId)
{
string imageFilename = String.Format(JsonConfig.imageSettings.imageFilename, imageId);
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images", imageFilename);

WallpaperChanger.EnableTransitions();
WallpaperChanger.SetWallpaper(imagePath);
//UwpHelper.SetWallpaper(imageFilename);
UwpDesktop.SetWallpaper(imageFilename);

lastImageId = imageId;
}

public void RunScheduler(bool forceRefresh = false)
{
wallpaperTimer.Stop();

string currentDate = GetDateString();
bool shouldRefresh = currentDate != lastDate || forceRefresh;

Expand Down
4 changes: 2 additions & 2 deletions src/WallpaperChanger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ out IntPtr result
public static void EnableTransitions()
{
IntPtr result = IntPtr.Zero;
SendMessageTimeout(FindWindow("Progman", IntPtr.Zero), 0x52c, IntPtr.Zero, IntPtr.Zero, 0, 500,
out result);
SendMessageTimeout(FindWindow("Progman", IntPtr.Zero), 0x52c, IntPtr.Zero, IntPtr.Zero,
0, 500, out result);
}

public static void SetWallpaper(string imagePath)
Expand Down
2 changes: 1 addition & 1 deletion uwp/Package.appxmanifest
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10" IgnorableNamespaces="uap mp rescap desktop">
<Identity Name="38719TimothyJohnson.WinDynamicDesktop" Publisher="CN=3C822DA5-D64C-40A9-A84A-5502C3EDD8CD" Version="1.4.0.0" />
<Identity Name="38719TimothyJohnson.WinDynamicDesktop" Publisher="CN=3C822DA5-D64C-40A9-A84A-5502C3EDD8CD" Version="1.4.1.0" />
<Properties>
<DisplayName>WinDynamicDesktop</DisplayName>
<PublisherDisplayName>Timothy Johnson</PublisherDisplayName>
Expand Down

0 comments on commit 6221a33

Please sign in to comment.