-
Notifications
You must be signed in to change notification settings - Fork 48
Fall back to the solid color when the background image can't load #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using System.Windows.Data; | ||
| using System.Windows.Markup; | ||
| using System.Windows.Media; | ||
| using System.Windows.Media.Imaging; | ||
|
|
||
| namespace DesktopClock; | ||
|
|
||
| /// <summary> | ||
| /// Builds the clock's background brush from the current settings, falling back to the solid outer color when a chosen background image is missing or can't be read. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Bound values, in order: BackgroundEnabled, BackgroundImagePath, OuterColor, BackgroundOpacity, BackgroundImageStretch. | ||
| /// </remarks> | ||
| public class BackgroundBrushConverter : MarkupExtension, IMultiValueConverter | ||
| { | ||
| public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) | ||
| { | ||
| var enabled = values.Length > 0 && values[0] is bool b && b; | ||
|
|
||
| // Background off means outlined text on a transparent window. | ||
| if (!enabled) | ||
| return Brushes.Transparent; | ||
|
|
||
| var path = values.Length > 1 ? values[1] as string : null; | ||
| var outerColor = values.Length > 2 && values[2] is Color color ? color : Colors.Transparent; | ||
| var opacity = values.Length > 3 && values[3] is double o ? o : 1d; | ||
| var stretch = values.Length > 4 && values[4] is Stretch s ? s : Stretch.Fill; | ||
|
|
||
| // Use the image when it loads; otherwise fall back to the solid color so the clock never ends up with an invisible background. | ||
| var image = TryLoadImage(path); | ||
| if (image != null) | ||
| return new ImageBrush(image) { Opacity = opacity, Stretch = stretch }; | ||
|
|
||
| return new SolidColorBrush(outerColor) { Opacity = opacity }; | ||
| } | ||
|
|
||
| public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => | ||
| throw new NotSupportedException(); | ||
|
|
||
| public override object ProvideValue(IServiceProvider serviceProvider) => this; | ||
|
|
||
| private static ImageSource TryLoadImage(string path) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(path)) | ||
| return null; | ||
|
|
||
| try | ||
| { | ||
| var bitmap = new BitmapImage(); | ||
| bitmap.BeginInit(); | ||
| bitmap.CacheOption = BitmapCacheOption.OnLoad; // Decode now so a missing or invalid file fails here, and don't hold the file locked. | ||
| bitmap.UriSource = new Uri(path, UriKind.Absolute); | ||
| bitmap.EndInit(); | ||
| bitmap.Freeze(); | ||
| return bitmap; | ||
| } | ||
| catch | ||
| { | ||
| // Missing file, non-image content, unavailable share, or a malformed path all fall back to the solid color. | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping the bare
catchhere, deliberately:UriFormatException,FileNotFoundException,NotSupportedException,IOException,UnauthorizedAccessException,ArgumentExceptionare all reachable from this block) and turning a bad path into a crash on someone's machine.Tokenizer.FormatWithTokenizerOrFallBack,Settings.Save/LoadFromFile/FileChanged,MainWindow.CopyToClipboard/TryPlaySound,ThemeManager.ApplyTitleBarThemeall use barecatchfor graceful fallback. A critical-exception filter here would be the only one of its kind.ThreadAbortExceptionautomatically re-throws at the end of a catch block, and a process-fatal OOM won't be meaningfully rescued by one converter propagating it.If the project ever adopts a repo-wide exception-filter convention this spot should follow it, but I don't think this PR is the place to introduce a one-off.