Skip to content

Commit 6221a33

Browse files
committed
Use TrySetWallpaperImageAsync function in UWP app
1 parent 3b1e8e6 commit 6221a33

13 files changed

+80
-53
lines changed

src/AboutDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public AboutDialog()
2525
private void AboutDialog_Load(object sender, EventArgs e)
2626
{
2727
iconBox.Image = (new Icon(Properties.Resources.AppIcon, 64, 64)).ToBitmap();
28-
28+
2929
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
3030
nameLabel.Text += " " + version;
3131

src/AppContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ private void EnforceSingleInstance()
5353

5454
if (!isNewInstance)
5555
{
56-
MessageBox.Show("Another instance of WinDynamicDesktop is already running.", "Error",
57-
MessageBoxButtons.OK, MessageBoxIcon.Warning);
56+
MessageBox.Show("Another instance of WinDynamicDesktop is already running.",
57+
"Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
5858

5959
Environment.Exit(0);
6060
}
@@ -109,7 +109,7 @@ private void OnDarkModeClick(object sender, EventArgs e)
109109
{
110110
ToggleDarkMode();
111111
}
112-
112+
113113
private void OnStartOnBootClick(object sender, EventArgs e)
114114
{
115115
_startupManager.ToggleStartOnBoot();
@@ -161,8 +161,8 @@ private void OnDownloadDialogClosed(object sender, EventArgs e)
161161
if (!Directory.Exists("images"))
162162
{
163163
DialogResult result = MessageBox.Show("Failed to download images. Click Retry to " +
164-
"try again or Cancel to exit the program.", "Error", MessageBoxButtons.RetryCancel,
165-
MessageBoxIcon.Error);
164+
"try again or Cancel to exit the program.", "Error",
165+
MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
166166

167167
if (result == DialogResult.Retry)
168168
{

src/DesktopHelper.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@ public static string GetCurrentDirectory()
1515
{
1616
return Path.GetDirectoryName(Application.ExecutablePath);
1717
}
18+
19+
public static void SetWallpaper(string imageFilename)
20+
{
21+
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images",
22+
imageFilename);
23+
24+
WallpaperChanger.EnableTransitions();
25+
WallpaperChanger.SetWallpaper(imagePath);
26+
}
1827
}
19-
28+
2029
class DesktopStartupManager : StartupManager
2130
{
22-
private bool startOnBoot;
2331
private string registryStartupLocation = @"Software\Microsoft\Windows\CurrentVersion\Run";
2432

2533
public DesktopStartupManager(MenuItem startupMenuItem) : base(startupMenuItem)

src/InputDialog.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ private void okButton_Click(object sender, EventArgs e)
5555
}
5656

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

6161
this.Close();
6262
}
6363
else
6464
{
65-
MessageBox.Show("The location you entered was invalid, or you are not connected to " +
66-
"the Internet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
65+
MessageBox.Show("The location you entered was invalid, or you are not connected " +
66+
"to the Internet.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
6767
}
6868

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

8687
if (result == DialogResult.Yes)
8788
{

src/LocationIQ.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class LocationIQService
2727
{
2828
private static string apiKey = Encoding.UTF8.GetString(Convert.FromBase64String(
2929
"cGsuYmRhNTk1NDRhN2VjZWMxYjAxMDZkNzg5MzdlMDQzOTk ="));
30-
30+
3131
public static LocationIQData GetLocationData(string locationStr)
3232
{
3333
var client = new RestClient("https://us1.locationiq.org");

src/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
3434
{
3535
File.AppendAllText(logFilename, errorMessage);
3636

37-
MessageBox.Show("See the logfile '" + logFilename + "' for details", "Errors occurred",
38-
MessageBoxButtons.OK, MessageBoxIcon.Error);
37+
MessageBox.Show("See the logfile '" + logFilename + "' for details",
38+
"Errors occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
3939
}
4040
catch
4141
{
4242
MessageBox.Show("The logfile '" + logFilename + "' could not be opened:" +
43-
Environment.NewLine + " " + errorMessage, "Errors occurred", MessageBoxButtons.OK,
44-
MessageBoxIcon.Error);
43+
Environment.NewLine + " " + errorMessage, "Errors occurred",
44+
MessageBoxButtons.OK, MessageBoxIcon.Error);
4545
}
4646
}
4747
}

src/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.4.0")]
35+
[assembly: AssemblyVersion("1.4.1")]
3636
//[assembly: AssemblyFileVersion("1.0.0.0")]

src/UpdateChecker.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static void Initialize(NotifyIcon notifyIcon)
4040
_notifyIcon = notifyIcon;
4141
_notifyIcon.ContextMenu.MenuItems.Add(8, new MenuItem("&Check for Updates Now",
4242
OnUpdateItemClick));
43-
_notifyIcon.ContextMenu.MenuItems.Add(9, new MenuItem("C&heck Automatically Once a Week",
44-
OnAutoUpdateItemClick));
43+
_notifyIcon.ContextMenu.MenuItems.Add(9, new MenuItem(
44+
"C&heck Automatically Once a Week", OnAutoUpdateItemClick));
4545
_notifyIcon.ContextMenu.MenuItems[9].Checked = !JsonConfig.settings.disableAutoUpdate;
4646
_notifyIcon.ContextMenu.MenuItems.Add(10, new MenuItem("-"));
4747

@@ -84,16 +84,16 @@ public static void CheckManual()
8484

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

9898
if (result == DialogResult.Yes)
9999
{
@@ -102,8 +102,8 @@ public static void CheckManual()
102102
}
103103
else
104104
{
105-
MessageBox.Show("You already have the latest version of WinDynamicDesktop installed.",
106-
"Up To Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
105+
MessageBox.Show("You already have the latest version of WinDynamicDesktop " +
106+
"installed.", "Up To Date", MessageBoxButtons.OK, MessageBoxIcon.Information);
107107
}
108108
}
109109

@@ -119,8 +119,8 @@ private static void CheckAuto()
119119
else if (IsUpdateAvailable(currentVersion, latestVersion))
120120
{
121121
_notifyIcon.BalloonTipTitle = "Update Available";
122-
_notifyIcon.BalloonTipText = "WinDynamicDesktop " + latestVersion + " is available. " +
123-
"Click here to download it.";
122+
_notifyIcon.BalloonTipText = "WinDynamicDesktop " + latestVersion +
123+
" is available. Click here to download it.";
124124
_notifyIcon.ShowBalloonTip(10000);
125125
}
126126

src/UwpDesktop.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace WinDynamicDesktop
1010
{
1111
abstract class StartupManager
1212
{
13+
internal bool startOnBoot;
1314
internal MenuItem _menuItem;
1415

1516
public StartupManager(MenuItem startupMenuItem)
@@ -58,5 +59,17 @@ public static StartupManager GetStartupManager(MenuItem startupMenuItem)
5859
return new UwpStartupManager(startupMenuItem);
5960
}
6061
}
62+
63+
public static void SetWallpaper(string imageFilename)
64+
{
65+
if (!IsRunningAsUwp())
66+
{
67+
DesktopHelper.SetWallpaper(imageFilename);
68+
}
69+
else
70+
{
71+
UwpHelper.SetWallpaper(imageFilename);
72+
}
73+
}
6174
}
6275
}

src/UwpHelper.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ public static string GetCurrentDirectory()
1616

1717
public static async void SetWallpaper(string imageFilename)
1818
{
19-
//var uri = new Uri("ms-appx://Local/images/" + imageFilename);
20-
//var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
21-
var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "images", imageFilename));
22-
var profileSettings = Windows.System.UserProfile.UserProfilePersonalizationSettings.Current;
19+
var uri = new Uri("ms-appdata:///local/images/" + imageFilename);
20+
var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
21+
22+
var profileSettings =
23+
Windows.System.UserProfile.UserProfilePersonalizationSettings.Current;
2324
await profileSettings.TrySetWallpaperImageAsync(file);
2425
}
2526
}
2627

2728
class UwpStartupManager : StartupManager
2829
{
29-
private bool startOnBoot;
30-
3130
public UwpStartupManager(MenuItem startupMenuItem) : base(startupMenuItem)
3231
{
3332
UpdateStatus();
3433
}
3534

3635
private async void UpdateStatus()
3736
{
38-
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync("WinDynamicDesktopUwp");
37+
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync(
38+
"WinDynamicDesktopUwp");
3939

4040
switch (startupTask.State)
4141
{
@@ -56,7 +56,8 @@ private async void UpdateStatus()
5656

5757
public override async void ToggleStartOnBoot()
5858
{
59-
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync("WinDynamicDesktopUwp");
59+
var startupTask = await Windows.ApplicationModel.StartupTask.GetAsync(
60+
"WinDynamicDesktopUwp");
6061

6162
if (!startOnBoot)
6263
{

0 commit comments

Comments
 (0)