Skip to content

Commit

Permalink
Further improvements to error handling during theme load/import
Browse files Browse the repository at this point in the history
  • Loading branch information
t1m0thyj committed Apr 21, 2019
1 parent 330612e commit 29a951a
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 71 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ This is a commonly requested feature to mimic the behavior of macOS and many Lin

You are not limited to the Mojave themes that come preinstalled with the app. Custom themes created by the community can be downloaded [here](https://github.com/t1m0thyj/WinDynamicDesktop/wiki/Community-created-themes). You can also create your own theme that uses whatever wallpaper images you want, by following the instructions in [this tutorial](https://github.com/t1m0thyj/WinDynamicDesktop/wiki/Creating-custom-themes).

### Can I translate the app into my language?

Yes, translations are welcome. For instructions on how to create them and where to submit them, see [here](https://github.com/t1m0thyj/WDD-locale).

### How can I hide the tray icon?

If you want to run the app silently with no icon in the system tray, you can do this by editing the `settings.conf` file which is in the same folder as the EXE. Change the setting `"hideTrayIcon":false` to `"hideTrayIcon":true` (or add it if it doesn't exist), then restart the app.
Expand Down
1 change: 1 addition & 0 deletions src/AboutDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions src/AboutDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,35 @@ public AboutDialog()
this.Font = SystemFonts.MessageBoxFont;
this.nameLabel.Font = new Font(this.Font.Name, this.Font.Size * 1.2F, FontStyle.Bold);

int minWidth = TextRenderer.MeasureText(this.descriptionLabel.Text, this.Font).Width;
minWidth = minWidth + (this.descriptionLabel.Width - minWidth) / 2 +
this.descriptionLabel.Location.X * 2;
int minWidth = TextRenderer.MeasureText(descriptionLabel.Text, this.Font).Width;

if (this.descriptionLabel.Width < minWidth)
{
minWidth += descriptionLabel.Location.X * 2;
nameLabel.Width = minWidth;
copyrightLabel.Width = minWidth;
descriptionLabel.Width = minWidth;
websiteLabel.Width = minWidth;
}

minWidth = minWidth + (descriptionLabel.Width - minWidth) / 2 +
descriptionLabel.Location.X * 2;

if (this.Size.Width < minWidth)
{
this.Size = new Size(minWidth, this.Size.Height);
this.CenterToScreen();
}

minWidth = TextRenderer.MeasureText(creditsButton.Text, this.Font).Width + 10;

if (this.creditsButton.Width < minWidth)
{
int oldWidth = creditsButton.Width;
creditsButton.Width = minWidth;
donateButton.Location = new Point(donateButton.Location.X + minWidth - oldWidth,
donateButton.Location.Y);
}
}

private void AboutDialog_Load(object sender, EventArgs e)
Expand Down
9 changes: 3 additions & 6 deletions src/App.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
Expand All @@ -11,7 +11,4 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
<System.Windows.Forms.ApplicationConfigurationSection>
<add key="DpiAwareness" value="PerMonitorV2" />
</System.Windows.Forms.ApplicationConfigurationSection>
</configuration>
10 changes: 6 additions & 4 deletions src/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,16 @@ private void ImportNext()
string themePath = importQueue.Peek();
this.Invoke(new Action(() => UpdateImportStatus(themePath)));
ThemeConfig theme = ThemeManager.ImportTheme(themePath);
this.Invoke(new Action(() => ThemeLoader.HandleError(theme.themeId)));
this.Invoke(new Action(() => ThemeLoader.HandleError(
Path.GetFileNameWithoutExtension(themePath))));

if (theme != null)
{
if (Path.GetExtension(themePath) == ".json")
{
downloadQueue = new Queue<ThemeConfig>(
new List<ThemeConfig>() { theme });
DownloadNext(); // TODO Test if this works
DownloadNext(); // TODO Test this
}

ThemeManager.importedThemes.Add(theme);
Expand Down Expand Up @@ -172,7 +173,7 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
{
UpdateTotalPercentage(e.ProgressPercentage);

fileTransferSpeedLabel.Text = string.Format(_("{0} MB/s"),
fileTransferSpeedLabel.Text = string.Format(_("{0} MB/s"),
(e.BytesReceived / 1024f / 1024f / stopwatch.Elapsed.TotalSeconds).ToString("0.#"));

fileSizeProgressLabel.Text = string.Format(_("{0} MB of {1} MB"),
Expand Down Expand Up @@ -211,7 +212,8 @@ await Task.Run(() => ThemeLoader.ExtractTheme(theme.themeId + "_images.zip",
}
else
{
ThemeManager.DisableTheme(theme.themeId); // TODO Handle error here for failed download
ThemeLoader.HandleError(theme.themeId, string.Format(
_("Failed to download images for the '{0}' theme"), theme.themeId));
}
}

Expand Down
23 changes: 5 additions & 18 deletions src/ThemeDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 5 additions & 15 deletions src/ThemeDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ThemeDialog()

public void ImportThemes(List<string> themePaths)
{
ProgressDialog importDialog = new ProgressDialog();
ProgressDialog importDialog = new ProgressDialog() { Owner = this };
importDialog.FormClosing += OnImportDialogClosing;
importDialog.Show();
importDialog.InitImport(themePaths);
Expand Down Expand Up @@ -164,14 +164,9 @@ private string GetCreditsText()
private List<int> GetImageList(ThemeConfig theme)
{
List<int> imageList = new List<int>();

if (!darkModeCheckbox.Checked)
{
imageList.AddRange(theme.sunriseImageList);
imageList.AddRange(theme.dayImageList);
imageList.AddRange(theme.sunsetImageList);
}

imageList.AddRange(theme.sunriseImageList);
imageList.AddRange(theme.dayImageList);
imageList.AddRange(theme.sunsetImageList);
imageList.AddRange(theme.nightImageList);
return imageList;
}
Expand Down Expand Up @@ -274,8 +269,6 @@ private void ThemeDialog_Load(object sender, EventArgs e)
imageListView1.ContextMenuStrip = contextMenuStrip1;
imageListView1.SetRenderer(new ThemeListViewRenderer());

darkModeCheckbox.Checked = JsonConfig.settings.darkMode;

Size thumbnailSize = GetThumbnailSize();
imageListView1.ThumbnailSize = thumbnailSize;
imageListView1.Items.Add(_("None"), ShrinkImage(windowsWallpaper, thumbnailSize.Width,
Expand Down Expand Up @@ -337,8 +330,7 @@ private void imageListView1_SelectionChanged(object sender, EventArgs e)
SolarData solarData = SunriseSunsetService.GetSolarData(DateTime.Today);
ThemeConfig theme = ThemeManager.themeSettings[selectedIndex - 1];
imageNumber = GetImageList(theme).IndexOf(
AppContext.wpEngine.GetImageData(solarData, theme,
darkModeCheckbox.Checked).Item1) + 1;
AppContext.wpEngine.GetImageData(solarData, theme).Item1) + 1;
}

creditsLabel.Text = GetCreditsText();
Expand Down Expand Up @@ -413,8 +405,6 @@ private void applyButton_Click(object sender, EventArgs e)
}

JsonConfig.settings.themeName = ThemeManager.currentTheme?.themeId;
JsonConfig.settings.darkMode = darkModeCheckbox.Checked;
MainMenu.darkModeItem.Checked = JsonConfig.settings.darkMode;

if (selectedIndex == 0)
{
Expand Down
24 changes: 18 additions & 6 deletions src/ThemeLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,32 @@ public static void HandleError(string themeId)
TaskbarProgress.SetState(taskbarHandle, TaskbarProgress.TaskbarStates.Error);
}

MessageBox.Show(string.Format(_("Failed to load '{0}' theme:\n{1}"), themeId,
errorMsg), _("Error"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (!ThemeManager.importMode)
{
DialogResult result = MessageBox.Show(string.Format(_("Failed to load '{0}' " +
"theme:\n{1}\n\nDo you want to disable this theme to prevent the error from " +
"happening again?"), themeId, errorMsg), _("Error"), MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
ThemeManager.DisableTheme(themeId, result == DialogResult.Yes);
}
else
{
MessageBox.Show(string.Format(_("Failed to import '{0}' theme:\n{1}"), themeId,
errorMsg), _("Error"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

if (taskbarHandle != IntPtr.Zero)
{
TaskbarProgress.SetState(taskbarHandle, TaskbarProgress.TaskbarStates.Normal);
}

errorMsg = null;
}

if (!ThemeManager.importMode)
{
ThemeManager.DisableTheme(themeId);
}
public static void HandleError(string themeId, string errorText)
{
errorMsg = errorText;
HandleError(themeId);
}

public static bool PromptDialog(string dialogText)
Expand Down
29 changes: 19 additions & 10 deletions src/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,16 @@ public static string GetThemeName(ThemeConfig theme)
return theme.displayName ?? theme.themeId.Replace('_', ' ');
}

public static void DisableTheme(string themeId)
public static void DisableTheme(string themeId, bool permanent)
{
themeSettings.RemoveAll(t => t.themeId == themeId);

if (currentTheme.themeId == themeId)
if (currentTheme != null && (currentTheme.themeId == themeId))
{
currentTheme = null;
}

bool shouldDisable = ThemeLoader.PromptDialog(_("The '{0}' theme could not be " +
"loaded. Do you want to disable it to prevent this error from happening again?"));

if (shouldDisable)
if (permanent)
{
Directory.Move(Path.Combine("themes", themeId),
Path.Combine("themes", "." + themeId));
Expand All @@ -109,17 +106,22 @@ public static ThemeConfig ImportTheme(string importPath)
}

Directory.CreateDirectory(Path.Combine("themes", themeId));
bool shouldContinue = true;
ThemeConfig theme = null;

if (Path.GetExtension(importPath) != ".json")
{
ThemeLoader.ExtractTheme(importPath, themeId);
shouldContinue = ThemeLoader.ExtractTheme(importPath, themeId);
}
else
{
File.Copy(importPath, Path.Combine("themes", themeId, "theme.json"), true);
}

ThemeConfig theme = ThemeLoader.TryLoad(themeId);
if (shouldContinue)
{
theme = ThemeLoader.TryLoad(themeId);
}

if (theme == null)
{
Expand Down Expand Up @@ -198,14 +200,21 @@ private static void DownloadMissingImages()
LaunchSequence.NextStep();
return;
}

foreach (ThemeConfig theme in
missingThemes.Where(theme => string.IsNullOrEmpty(theme.imagesZipUri)))
{
missingThemes.Remove(theme);
ThemeLoader.HandleError(theme.themeId,
string.Format(_("Failed to find images for the '{0}' theme"), theme.themeId));
}

downloadDialog = new ProgressDialog();
downloadDialog.FormClosed += OnDownloadDialogClosed;
downloadDialog.Show();

MainMenu.themeItem.Enabled = false;
downloadDialog.InitDownload(missingThemes.FindAll(
theme => !string.IsNullOrEmpty(theme.imagesZipUri))); // TODO Handle error if null or empty and missing images
downloadDialog.InitDownload(missingThemes);
}

private static void OnDownloadDialogClosed(object sender, EventArgs e)
Expand Down
7 changes: 3 additions & 4 deletions src/WallpaperChangeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public void RunScheduler(bool forceImageUpdate = false)
lastImagePath = null;
}

Tuple<int, long> imageData = GetImageData(data, ThemeManager.currentTheme,
JsonConfig.settings.darkMode);
Tuple<int, long> imageData = GetImageData(data, ThemeManager.currentTheme);
SetWallpaper(imageData.Item1);
nextImageUpdateTime = new DateTime(imageData.Item2);
}
Expand Down Expand Up @@ -109,13 +108,13 @@ private DaySegment GetCurrentDaySegment(SolarData data)
}
}

public Tuple<int, long> GetImageData(SolarData data, ThemeConfig theme, bool darkMode)
public Tuple<int, long> GetImageData(SolarData data, ThemeConfig theme)
{
int[] imageList;
DateTime segmentStart;
DateTime segmentEnd;

if (!darkMode)
if (!JsonConfig.settings.darkMode)
{
switch (GetCurrentDaySegment(data))
{
Expand Down
3 changes: 1 addition & 2 deletions src/WinDynamicDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
<OutputType>WinExe</OutputType>
<RootNamespace>WinDynamicDesktop</RootNamespace>
<AssemblyName>WinDynamicDesktop</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
Loading

0 comments on commit 29a951a

Please sign in to comment.