Skip to content

Commit 0834e7b

Browse files
authored
Merge pull request #107 from redth-org/bugfix/gh106-customization
Allow customization of ProgressHUD through ProgressHUDAppearance
2 parents c200ea3 + f35db92 commit 0834e7b

File tree

8 files changed

+214
-123
lines changed

8 files changed

+214
-123
lines changed

BTProgressHUD/BTProgressHUD.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ public static void Dismiss()
6969
public static bool IsVisible
7070
=> ProgressHUD.ForDefaultWindow()?.IsVisible ?? false;
7171

72-
73-
74-
7572
public static void Show(UIWindow forWindow, string? status = null, float progress = -1, MaskType maskType = MaskType.None)
7673
{
7774
ProgressHUD.For(forWindow)?.Show(status, progress, maskType);

BTProgressHUD/ProgressHUD.cs

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ public sealed class ProgressHUD : UIView
3434

3535
private static readonly NSObject obj = new();
3636

37-
private UIImage? _errorImage;
38-
private UIImage? _successImage;
39-
private UIImage? _infoImage;
40-
private UIImage? _errorOutlineImage;
41-
private UIImage? _successOutlineImage;
42-
private UIImage? _infoOutlineImage;
43-
private UIImage? _errorOutlineFullImage;
44-
private UIImage? _successOutlineFullImage;
45-
private UIImage? _infoOutlineFullImage;
37+
private UIImage? _errorImage = ProgressHUDAppearance.ErrorImage;
38+
private UIImage? _successImage = ProgressHUDAppearance.SuccessImage;
39+
private UIImage? _infoImage = ProgressHUDAppearance.InfoImage;
40+
private UIImage? _errorOutlineImage = ProgressHUDAppearance.ErrorOutlineImage;
41+
private UIImage? _successOutlineImage = ProgressHUDAppearance.SuccessOutlineImage;
42+
private UIImage? _infoOutlineImage = ProgressHUDAppearance.InfoOutlineImage;
43+
private UIImage? _errorOutlineFullImage = ProgressHUDAppearance.ErrorOutlineFullImage;
44+
private UIImage? _successOutlineFullImage = ProgressHUDAppearance.SuccessOutlineFullImage;
45+
private UIImage? _infoOutlineFullImage = ProgressHUDAppearance.InfoOutlineFullImage;
4646

4747
private MaskType _maskType;
4848
private NSTimer? _fadeoutTimer;
@@ -100,22 +100,12 @@ public ProgressHUD(UIWindow window) : base(window.Bounds)
100100
BackgroundColor = UIColor.Clear;
101101
Alpha = 0;
102102
AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
103-
104-
SetOSSpecificLookAndFeel();
105103
}
106104

107105
public UIWindow HudWindow { get; private set; }
108106

109107
public static CGRect KeyboardSize { get; private set; } = CGRect.Empty;
110108

111-
public UIColor HudBackgroundColour { get; set; } = UIColor.FromWhiteAlpha(0.0f, 0.8f);
112-
public UIColor HudForegroundColor { get; set; } = UIColor.White;
113-
public UIColor HudStatusShadowColor { get; set; } = UIColor.Black;
114-
public UIColor HudToastBackgroundColor { get; set; } = UIColor.Clear;
115-
public UIFont HudFont { get; set; } = UIFont.BoldSystemFontOfSize(16f);
116-
public UITextAlignment HudTextAlignment { get; set; } = UITextAlignment.Center;
117-
public Ring Ring { get; } = new();
118-
119109
public UIImage ErrorImage
120110
{
121111
get => _errorImage ?? ImageHelper.ErrorImage.Value!;
@@ -215,9 +205,6 @@ public UIImage InfoOutlineFullImage
215205

216206
return For(window);
217207
}
218-
219-
public float RingRadius { get; set; } = 14f;
220-
public float RingThickness { get; set; } = 6f;
221208

222209
CAShapeLayer RingLayer
223210
{
@@ -226,7 +213,8 @@ CAShapeLayer RingLayer
226213
if (_ringLayer == null)
227214
{
228215
var center = new CGPoint(HudView.Frame.Width / 2, HudView.Frame.Height / 2);
229-
_ringLayer = ShapeHelper.CreateRingLayer(center, RingRadius, RingThickness, Ring.Color);
216+
_ringLayer = ShapeHelper.CreateRingLayer(center, ProgressHUDAppearance.RingRadius,
217+
ProgressHUDAppearance.RingThickness, ProgressHUDAppearance.RingColor);
230218
HudView.Layer.AddSublayer(_ringLayer);
231219
}
232220
return _ringLayer;
@@ -240,7 +228,8 @@ CAShapeLayer RingLayer
240228
if (_backgroundRingLayer == null)
241229
{
242230
var center = new CGPoint(HudView.Frame.Width / 2, HudView.Frame.Height / 2);
243-
_backgroundRingLayer = ShapeHelper.CreateRingLayer(center, RingRadius, RingThickness, Ring.BackgroundColor);
231+
_backgroundRingLayer = ShapeHelper.CreateRingLayer(center, ProgressHUDAppearance.RingRadius,
232+
ProgressHUDAppearance.RingThickness, ProgressHUDAppearance.RingBackgroundColor);
244233
_backgroundRingLayer.StrokeEnd = 1;
245234
HudView.Layer.AddSublayer(_backgroundRingLayer);
246235
}
@@ -269,13 +258,13 @@ UIView HudView
269258
var hudView = new UIToolbar
270259
{
271260
Translucent = true,
272-
BarTintColor = HudBackgroundColour,
273-
BackgroundColor = HudBackgroundColour,
261+
BarTintColor = ProgressHUDAppearance.HudBackgroundColor,
262+
BackgroundColor = ProgressHUDAppearance.HudBackgroundColor,
274263
AutoresizingMask =
275264
UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleTopMargin |
276265
UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin
277266
};
278-
hudView.Layer.CornerRadius = 10;
267+
hudView.Layer.CornerRadius = ProgressHUDAppearance.HudCornerRadius;
279268
hudView.Layer.MasksToBounds = true;
280269

281270
AddSubview(hudView);
@@ -292,12 +281,12 @@ UILabel StringLabel
292281
{
293282
_stringLabel ??= new UILabel
294283
{
295-
BackgroundColor = HudToastBackgroundColor,
284+
BackgroundColor = ProgressHUDAppearance.HudToastBackgroundColor,
296285
AdjustsFontSizeToFitWidth = true,
297-
TextAlignment = HudTextAlignment,
286+
TextAlignment = ProgressHUDAppearance.HudTextAlignment,
298287
BaselineAdjustment = UIBaselineAdjustment.AlignCenters,
299-
TextColor = HudForegroundColor,
300-
Font = HudFont,
288+
TextColor = ProgressHUDAppearance.HudTextColor,
289+
Font = ProgressHUDAppearance.HudFont,
301290
Lines = 0
302291
};
303292

@@ -321,9 +310,8 @@ UIButton CancelHudButton
321310
BackgroundColor = UIColor.Clear,
322311
UserInteractionEnabled = true
323312
};
324-
_cancelHud.TitleLabel.Font = HudFont;
325-
326-
_cancelHud.SetTitleColor(HudForegroundColor, UIControlState.Normal);
313+
_cancelHud.TitleLabel.Font = ProgressHUDAppearance.HudButtonFont;
314+
_cancelHud.SetTitleColor(ProgressHUDAppearance.HudButtonTextColor, UIControlState.Normal);
327315
UserInteractionEnabled = true;
328316
}
329317

@@ -364,11 +352,14 @@ UIActivityIndicatorView SpinnerView
364352
{
365353
get
366354
{
367-
_spinnerView ??= new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge)
355+
_spinnerView ??= new UIActivityIndicatorView(
356+
OperatingSystem.IsMacCatalystVersionAtLeast(13, 1) || OperatingSystem.IsIOSVersionAtLeast(13) ?
357+
UIActivityIndicatorViewStyle.Large :
358+
UIActivityIndicatorViewStyle.WhiteLarge)
368359
{
369360
HidesWhenStopped = true,
370361
Bounds = new CGRect(0, 0, 37, 37),
371-
Color = HudForegroundColor
362+
Color = ProgressHUDAppearance.RingColor
372363
};
373364

374365
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
@@ -379,20 +370,6 @@ UIActivityIndicatorView SpinnerView
379370
}
380371
}
381372

382-
private void SetOSSpecificLookAndFeel()
383-
{
384-
HudBackgroundColour =
385-
(System.OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
386-
? UIColor.SystemBackground.ColorWithAlpha(0.8f) : UIColor.White.ColorWithAlpha(0.8f);
387-
HudForegroundColor =
388-
(System.OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
389-
? UIColor.Label.ColorWithAlpha(0.8f) : UIColor.FromWhiteAlpha(0.0f, 0.8f);
390-
HudStatusShadowColor =
391-
(System.OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
392-
? UIColor.Label.ColorWithAlpha(0.8f) : UIColor.FromWhiteAlpha(200f / 255f, 0.8f);
393-
RingThickness = 1f;
394-
}
395-
396373
public void Show(string? status = null, float progress = -1, MaskType maskType = MaskType.None, double timeoutMs = 1000)
397374
{
398375
obj.InvokeOnMainThread(() => ShowProgressWorker(progress, status, maskType, timeoutMs: timeoutMs));
@@ -510,9 +487,6 @@ private void ShowProgressWorker(
510487
ToastPosition toastPosition = ToastPosition.Center, string? cancelCaption = null, Action? cancelCallback = null,
511488
double timeoutMs = 1000, bool showContinuousProgress = false, UIImage? displayContinuousImage = null)
512489
{
513-
if (TintColor != null)
514-
Ring.ResetStyle(TintColor);
515-
516490
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
517491
if (OverlayView.Superview == null)
518492
{
@@ -557,7 +531,7 @@ private void ShowProgressWorker(
557531
}
558532

559533
RingLayer.StrokeEnd = 0.0f;
560-
StartProgressTimer(TimeSpan.FromMilliseconds(Ring.ProgressUpdateInterval));
534+
StartProgressTimer(TimeSpan.FromMilliseconds(ProgressHUDAppearance.RingProgressUpdateInterval));
561535
}
562536
else
563537
{
@@ -651,7 +625,7 @@ private void ShowImageWorker(UIImage image, string? status, MaskType maskType, T
651625
Show(null, -1F, maskType);
652626
}
653627

654-
ImageView.TintColor = HudForegroundColor;
628+
ImageView.TintColor = ProgressHUDAppearance.HudImageTintColor;
655629
ImageView.Image = image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
656630
ImageView.Hidden = false;
657631
StringLabel.Text = status;
@@ -762,9 +736,6 @@ private void RemoveHud()
762736
UnRegisterNotifications();
763737
NSNotificationCenter.DefaultCenter.RemoveObserver(this);
764738

765-
if (TintColor != null)
766-
Ring.ResetStyle(TintColor);
767-
768739
CancelRingLayerAnimation();
769740
StringLabel.RemoveFromSuperview();
770741
SpinnerView.RemoveFromSuperview();
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using UIKit;
3+
4+
namespace BigTed;
5+
6+
public static class ProgressHUDAppearance
7+
{
8+
public static UIColor DefaultHudBackgroundColor { get; } =
9+
OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13) ?
10+
UIColor.SystemBackground.ColorWithAlpha(0.8f) :
11+
UIColor.White.ColorWithAlpha(0.8f);
12+
13+
public static UIColor DefaultForegroundColor { get; } =
14+
OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13) ?
15+
UIColor.Label.ColorWithAlpha(0.8f) :
16+
UIColor.FromWhiteAlpha(0.0f, 0.8f);
17+
18+
public static UIColor DefaultHudToastBackgroundColor { get; } = UIColor.Clear;
19+
public static UIFont DefaultHudFont { get; } = UIFont.BoldSystemFontOfSize(16f);
20+
public static UITextAlignment DefaultHudTextAlignment { get; } = UITextAlignment.Center;
21+
22+
public static UIColor DefaultRingColor = UIColor.SystemBlue;
23+
24+
public static UIColor DefaultRingBackgroundColor =
25+
OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13)
26+
? UIColor.SystemBackground
27+
: DefaultHudBackgroundColor;
28+
29+
public const float DefaultRingRadius = 14f;
30+
public const float DefaultRingThickness = 1f;
31+
public const float DefaultHudCornerRadius = 10f;
32+
public const double DefaultProgressUpdateInterval = 300;
33+
34+
/// <summary>
35+
/// Get or set definite progress indicator ring radius to control its size
36+
/// </summary>
37+
public static float RingRadius { get; set; } = DefaultRingRadius;
38+
39+
/// <summary>
40+
/// Get or set definite progress indicator ring stroke thickness
41+
/// </summary>
42+
public static float RingThickness { get; set; } = DefaultRingThickness;
43+
44+
/// <summary>
45+
/// Get or set update interval for definite progress indicator ring animation
46+
/// </summary>
47+
public static double RingProgressUpdateInterval { get; set; } = DefaultProgressUpdateInterval;
48+
49+
/// <summary>
50+
/// Get or set definite progress indicator ring foreground color
51+
/// </summary>
52+
public static UIColor RingColor { get; set; } = DefaultRingColor;
53+
54+
/// <summary>
55+
/// Get or set definite progress indicator ring background color
56+
/// </summary>
57+
public static UIColor RingBackgroundColor { get; set; } = DefaultRingBackgroundColor;
58+
59+
/// <summary>
60+
/// Get or set hud corner radius
61+
/// </summary>
62+
public static float HudCornerRadius { get; set; } = DefaultHudCornerRadius;
63+
64+
/// <summary>
65+
/// Get or set hud background color
66+
/// </summary>
67+
public static UIColor HudBackgroundColor { get; set; } = DefaultHudBackgroundColor;
68+
69+
/// <summary>
70+
/// Get or set image tint color
71+
/// </summary>
72+
public static UIColor HudImageTintColor { get; set; } = DefaultForegroundColor;
73+
74+
/// <summary>
75+
/// Get or set background color of toast
76+
/// </summary>
77+
public static UIColor HudToastBackgroundColor { get; set; } = DefaultHudToastBackgroundColor;
78+
79+
/// <summary>
80+
/// Get or set font used for texts shown in hud
81+
/// </summary>
82+
public static UIFont HudFont { get; set; } = DefaultHudFont;
83+
84+
/// <summary>
85+
/// Get or set color of texts shown in hud
86+
/// </summary>
87+
public static UIColor HudTextColor { get; set; } = DefaultForegroundColor;
88+
89+
/// <summary>
90+
/// Get or set font to use for cancel button
91+
/// </summary>
92+
public static UIFont HudButtonFont { get; set; } = DefaultHudFont;
93+
94+
/// <summary>
95+
/// Get or set text color on cancel button
96+
/// </summary>
97+
public static UIColor HudButtonTextColor { get; set; } = DefaultForegroundColor;
98+
99+
/// <summary>
100+
/// Get or set alignment on texts shown in hud
101+
/// </summary>
102+
public static UITextAlignment HudTextAlignment { get; set; } = DefaultHudTextAlignment;
103+
104+
public static UIImage? ErrorImage { get; set; }
105+
public static UIImage? SuccessImage { get; set; }
106+
public static UIImage? InfoImage { get; set; }
107+
public static UIImage? ErrorOutlineImage { get; set; }
108+
public static UIImage? SuccessOutlineImage { get; set; }
109+
public static UIImage? InfoOutlineImage { get; set; }
110+
public static UIImage? ErrorOutlineFullImage { get; set; }
111+
public static UIImage? SuccessOutlineFullImage { get; set; }
112+
public static UIImage? InfoOutlineFullImage { get; set; }
113+
114+
public static void ResetToDefaults()
115+
{
116+
RingRadius = DefaultRingRadius;
117+
RingThickness = DefaultRingThickness;
118+
RingProgressUpdateInterval = DefaultProgressUpdateInterval;
119+
RingColor = DefaultRingColor;
120+
RingBackgroundColor = DefaultRingBackgroundColor;
121+
122+
HudCornerRadius = DefaultHudCornerRadius;
123+
HudBackgroundColor = DefaultHudBackgroundColor;
124+
HudImageTintColor = DefaultForegroundColor;
125+
HudToastBackgroundColor = DefaultHudToastBackgroundColor;
126+
HudFont = DefaultHudFont;
127+
HudButtonFont = DefaultHudFont;
128+
HudTextColor = DefaultForegroundColor;
129+
HudButtonTextColor = DefaultForegroundColor;
130+
HudTextAlignment = DefaultHudTextAlignment;
131+
}
132+
}

BTProgressHUD/Ring.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)