-
Notifications
You must be signed in to change notification settings - Fork 2k
[iOS] Fix Switch custom colors on iOS 26 #35385
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
base: inflight/current
Are you sure you want to change the base?
Changes from all commits
9de9c5d
5e26284
77d186c
ede9f54
f9b4662
97b0216
6eb2184
2519380
d274312
fa5652e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| using System; | ||
| using CoreFoundation; | ||
| using CoreGraphics; | ||
| using UIKit; | ||
|
|
||
| namespace Microsoft.Maui.Platform | ||
| { | ||
| internal class MauiSwitch : UISwitch | ||
| { | ||
| WeakReference<ISwitch>? _virtualView; | ||
| bool _colorReapplyQueued; | ||
| bool _isReapplyingColors; | ||
| bool _needsColorReapply; | ||
| bool _hasMauiTrackColorOverride; | ||
|
|
||
| public MauiSwitch(CGRect frame) : base(frame) | ||
| { | ||
| } | ||
|
|
||
| public void Connect(ISwitch virtualView) | ||
| { | ||
| _virtualView = new(virtualView); | ||
| SetNeedsColorReapply(); | ||
| } | ||
|
|
||
| public void Disconnect() | ||
| { | ||
| _virtualView = null; | ||
| _needsColorReapply = false; | ||
| } | ||
|
|
||
| public void SetNeedsColorReapply() | ||
| { | ||
| var virtualView = VirtualView; | ||
|
|
||
| if (virtualView is null || virtualView.ShouldPreserveNativeDefaults()) | ||
| { | ||
| _needsColorReapply = false; | ||
| return; | ||
| } | ||
|
|
||
| _needsColorReapply = true; | ||
| SetNeedsLayout(); | ||
| QueueColorReapply(); | ||
| } | ||
|
|
||
| public override void MovedToWindow() | ||
| { | ||
| base.MovedToWindow(); | ||
| if (Window is not null) | ||
| { | ||
| SetNeedsColorReapply(); | ||
| } | ||
| } | ||
|
|
||
| public override void LayoutSubviews() | ||
| { | ||
| base.LayoutSubviews(); | ||
| QueueColorReapply(); | ||
| } | ||
|
|
||
| public override void TraitCollectionDidChange(UITraitCollection? previousTraitCollection) | ||
| { | ||
| base.TraitCollectionDidChange(previousTraitCollection); | ||
| SetNeedsColorReapply(); | ||
| } | ||
|
|
||
| void QueueColorReapply() | ||
| { | ||
| if (_colorReapplyQueued || !_needsColorReapply) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _colorReapplyQueued = true; | ||
|
|
||
| DispatchQueue.MainQueue.DispatchAsync(() => | ||
| { | ||
| _colorReapplyQueued = false; | ||
| TryReapplyColors(); | ||
| }); | ||
| } | ||
|
|
||
| void TryReapplyColors() | ||
| { | ||
| if (_isReapplyingColors || !_needsColorReapply) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var virtualView = VirtualView; | ||
|
|
||
| if (virtualView is null || virtualView.ShouldPreserveNativeDefaults()) | ||
| { | ||
| _needsColorReapply = false; | ||
| return; | ||
| } | ||
|
|
||
| if (!this.IsReadyForColorReapply()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _isReapplyingColors = true; | ||
|
|
||
| try | ||
| { | ||
| this.ApplyTrackColor(virtualView); | ||
|
AdamEssenmacher marked this conversation as resolved.
AdamEssenmacher marked this conversation as resolved.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [major] Native defaults preservation —
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in fa5652e |
||
| this.ApplyThumbColor(virtualView); | ||
| _needsColorReapply = false; | ||
| } | ||
| finally | ||
| { | ||
| _isReapplyingColors = false; | ||
| } | ||
| } | ||
|
|
||
| ISwitch? VirtualView => | ||
| _virtualView is not null && _virtualView.TryGetTarget(out var virtualView) ? virtualView : null; | ||
|
|
||
| internal bool HasMauiTrackColorOverride => _hasMauiTrackColorOverride; | ||
|
|
||
| internal void MarkMauiTrackColorOverride() | ||
| { | ||
| _hasMauiTrackColorOverride = true; | ||
| } | ||
|
|
||
| internal void ClearMauiTrackColorOverride() | ||
| { | ||
| _hasMauiTrackColorOverride = false; | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.