Skip to content

Commit

Permalink
Don't use dp for progress ring
Browse files Browse the repository at this point in the history
  • Loading branch information
FrayxRulez committed Dec 5, 2024
1 parent 16c9ea1 commit b0ea9d3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 51 deletions.
22 changes: 12 additions & 10 deletions Telegram/Controls/DownloadsIndicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ enum State

private State _state;

private ProgressBarRing ProgressBar;

public DownloadsIndicator()
{
DefaultStyleKey = typeof(DownloadsIndicator);
Expand Down Expand Up @@ -73,6 +75,8 @@ public DownloadsIndicator()

protected override void OnApplyTemplate()
{
ProgressBar = GetTemplateChild(nameof(ProgressBar)) as ProgressBarRing;

var target = GetTemplateChild("Target") as FrameworkElement;
if (target != null)
{
Expand Down Expand Up @@ -133,22 +137,20 @@ private CompositionScopedBatch PrepareBatch()

#region Progress

private double _progress;
public double Progress
{
get { return (double)GetValue(ProgressProperty); }
set { SetValue(ProgressProperty, value); }
get => _progress;
set => OnProgressChanged(_progress, _progress = value);
}

public static readonly DependencyProperty ProgressProperty =
DependencyProperty.Register("Progress", typeof(double), typeof(DownloadsIndicator), new PropertyMetadata(0d, OnProgressChanged));

private static void OnProgressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private void OnProgressChanged(double oldValue, double newValue)
{
((DownloadsIndicator)d).OnProgressChanged((double)e.NewValue, (double)e.OldValue);
}
if (ProgressBar != null)
{
ProgressBar.Value = newValue;
}

private void OnProgressChanged(double newValue, double oldValue)
{
if (newValue == 0 && _state != State.Normal)
{
_state = State.Normal;
Expand Down
36 changes: 11 additions & 25 deletions Telegram/Controls/FileButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public partial class FileButton : GlyphHyperlinkButton
{
private Grid RootGrid;

private ProgressBarRing ProgressBar;

private TextBlock ContentPresenter1;
private TextBlock ContentPresenter2;
private TextBlock _label;
Expand Down Expand Up @@ -69,38 +71,22 @@ protected override void OnApplyTemplate()
{
RootGrid = GetTemplateChild(nameof(RootGrid)) as Grid;

ProgressBar = GetTemplateChild(nameof(ProgressBar)) as ProgressBarRing;

ContentPresenter1 = GetTemplateChild(nameof(ContentPresenter1)) as TextBlock;
ContentPresenter2 = GetTemplateChild(nameof(ContentPresenter2)) as TextBlock;

ContentPresenter1.Text = Glyph ?? string.Empty;
ContentPresenter2.Text = string.Empty;

_label = ContentPresenter1;
}

#region Progress

public double InternalProgress
{
get => (double)GetValue(InternalProgressProperty);
set
if (ProgressBar != null)
{
try
{
SetValue(InternalProgressProperty, value);
}
catch
{
// All the remote procedure calls must be wrapped in a try-catch block
}
ProgressBar.Value = _enqueuedProgress;
}
}

public static readonly DependencyProperty InternalProgressProperty =
DependencyProperty.Register("InternalProgress", typeof(double), typeof(FileButton), new PropertyMetadata(0.0));

#endregion

#region ProgressVisibility

public Visibility ProgressVisibility
Expand All @@ -118,17 +104,17 @@ public double Progress
{
set
{
if (_shouldEnqueueProgress || !IsConnected)
if (_shouldEnqueueProgress || ProgressBar == null || !IsConnected)
{
_enqueuedProgress = value;
}
else if (_state is MessageContentState.Downloading or MessageContentState.Uploading)
{
InternalProgress = Math.Max(0.05, value);
ProgressBar.Value = Math.Max(0.05, value);
}
else
{
InternalProgress = value;
ProgressBar.Value = value;
}
}
}
Expand Down Expand Up @@ -376,10 +362,10 @@ private void OnDownloadingCompleted(object sender, CompositionBatchCompletedEven
{
try
{
if (_state == MessageContentState.Downloading && IsConnected)
if (_state == MessageContentState.Downloading && ProgressBar != null && IsConnected)
{
OnGlyphChanged(Icons.Cancel, Icons.ArrowDownload, true, Strings.AccActionCancelDownload, false);
InternalProgress = _enqueuedProgress;
ProgressBar.Value = _enqueuedProgress;
}

_shouldEnqueueProgress = false;
Expand Down
11 changes: 9 additions & 2 deletions Telegram/Controls/ProgressBarRing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Telegram.Controls
{
public partial class ProgressBarRing : ProgressBar
public partial class ProgressBarRing : Control
{
private readonly FrameworkElementState _manager;

Expand Down Expand Up @@ -124,7 +124,14 @@ private void OnUnloaded(object sender, RoutedEventArgs e)
}
}

protected override void OnValueChanged(double oldValue, double newValue)
private double _value;
public double Value
{
get => _value;
set => OnValueChanged(_value, _value = value);
}

private void OnValueChanged(double oldValue, double newValue)
{
if (double.IsNaN(newValue))
{
Expand Down
16 changes: 8 additions & 8 deletions Telegram/Themes/Accent.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
AutomationProperties.AccessibilityView="Raw" />

<controls:ProgressBarRing Background="Transparent"
<controls:ProgressBarRing x:Name="ProgressBar"
Background="Transparent"
Foreground="{ThemeResource MessageMediaForegroundBrush}"
Value="{TemplateBinding InternalProgress}"
IsHitTestVisible="False" />
</Grid>
</ControlTemplate>
Expand Down Expand Up @@ -327,13 +327,13 @@
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
AutomationProperties.AccessibilityView="Raw" />

<controls:ProgressBarRing Radius="7"
<controls:ProgressBarRing x:Name="ProgressBar"
Radius="7"
Center="10"
Width="20"
Height="20"
Background="Transparent"
Foreground="{ThemeResource MessageMediaForegroundBrush}"
Value="{TemplateBinding InternalProgress}"
IsHitTestVisible="False" />
</Grid>
</ControlTemplate>
Expand Down Expand Up @@ -478,9 +478,9 @@
</Grid>
</Border>

<controls:ProgressBarRing Background="Transparent"
<controls:ProgressBarRing x:Name="ProgressBar"
Background="Transparent"
Foreground="White"
Value="{TemplateBinding InternalProgress}"
IsHitTestVisible="False" />
</Grid>
</ControlTemplate>
Expand Down Expand Up @@ -604,9 +604,9 @@
VerticalAlignment="Center"
AutomationProperties.AccessibilityView="Raw"
Text="{TemplateBinding Glyph}" />
<controls:ProgressBarRing Background="Transparent"
<controls:ProgressBarRing x:Name="ProgressBar"
Background="Transparent"
Foreground="White"
Value="{TemplateBinding InternalProgress}"
IsHitTestVisible="False"
Radius="17"
Center="20"
Expand Down
7 changes: 1 addition & 6 deletions Telegram/Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1375,8 +1375,7 @@
Width="24"
Height="24" />

<local:ProgressBarRing x:Name="Progress"
Value="{TemplateBinding Progress}"
<local:ProgressBarRing x:Name="ProgressBar"
Foreground="{ThemeResource TelegramForegroundAccentBrush}"
Background="Transparent"
ShrinkOut="False"
Expand Down Expand Up @@ -3822,10 +3821,6 @@
Value="{ThemeResource MessageOverlayBackgroundBrush}" />
<Setter Property="Foreground"
Value="#FFFFFFFF" />
<Setter Property="Minimum"
Value="0" />
<Setter Property="Maximum"
Value="1" />
<Setter Property="Width"
Value="48" />
<Setter Property="Height"
Expand Down

0 comments on commit b0ea9d3

Please sign in to comment.