-
Notifications
You must be signed in to change notification settings - Fork 6
feat(zoom):digital zoom #74
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,155 @@ | ||
| using System; | ||
| using Avalonia; | ||
| using Avalonia.Controls; | ||
| using Avalonia.Input; | ||
| using Avalonia.Media; | ||
| using Avalonia.Media.Imaging; | ||
|
|
||
| namespace OpenIPC.Viewer.App.Controls; | ||
|
|
||
| // Thumbnail of the whole frame with the zoomed-in part outlined, so the user | ||
| // knows where they are at 6×. Press or drag on it to move the view there. | ||
| // Width follows the host (a fifth of it, within limits) so it stays small on a | ||
| // phone preview and readable on a desktop window. | ||
| public sealed class ZoomMinimap : Control | ||
| { | ||
| public static readonly StyledProperty<ZoomPanHost?> HostProperty = | ||
| AvaloniaProperty.Register<ZoomMinimap, ZoomPanHost?>(nameof(Host)); | ||
|
|
||
| public static readonly StyledProperty<RtspVideoView?> VideoProperty = | ||
| AvaloniaProperty.Register<ZoomMinimap, RtspVideoView?>(nameof(Video)); | ||
|
|
||
| private const double MinThumbWidth = 96; | ||
| private const double MaxThumbWidth = 200; | ||
| private const double HostFraction = 0.2; | ||
|
|
||
| // The thumbnail repaints a few times a second, not per frame: downscaling | ||
| // a 4K frame at full frame rate for a 160px preview is wasted work. | ||
| private static readonly TimeSpan FrameInterval = TimeSpan.FromMilliseconds(250); | ||
|
|
||
| private static readonly IBrush BackgroundBrush = new SolidColorBrush(Color.FromArgb(0xB0, 0, 0, 0)); | ||
| private static readonly IBrush ShadeBrush = new SolidColorBrush(Color.FromArgb(0x70, 0, 0, 0)); | ||
| private static readonly IPen FramePen = new Pen(new SolidColorBrush(Color.FromArgb(0x80, 0xFF, 0xFF, 0xFF)), 1); | ||
| private static readonly IPen ViewportPen = new Pen(Brushes.White, 1.5); | ||
|
|
||
| private DateTime _lastFrameRepaint; | ||
|
|
||
| static ZoomMinimap() | ||
| { | ||
| HostProperty.Changed.AddClassHandler<ZoomMinimap>((m, e) => m.OnHostChanged( | ||
| e.OldValue as ZoomPanHost, e.NewValue as ZoomPanHost)); | ||
| VideoProperty.Changed.AddClassHandler<ZoomMinimap>((m, e) => m.OnVideoChanged( | ||
| e.OldValue as RtspVideoView, e.NewValue as RtspVideoView)); | ||
| } | ||
|
|
||
| public ZoomPanHost? Host { get => GetValue(HostProperty); set => SetValue(HostProperty, value); } | ||
| public RtspVideoView? Video { get => GetValue(VideoProperty); set => SetValue(VideoProperty, value); } | ||
|
|
||
| private void OnHostChanged(ZoomPanHost? oldHost, ZoomPanHost? newHost) | ||
| { | ||
| if (oldHost is not null) | ||
| { | ||
| oldHost.ViewportChanged -= OnViewportChanged; | ||
| oldHost.PropertyChanged -= OnHostPropertyChanged; | ||
| } | ||
| if (newHost is not null) | ||
| { | ||
| newHost.ViewportChanged += OnViewportChanged; | ||
| newHost.PropertyChanged += OnHostPropertyChanged; | ||
| } | ||
| InvalidateMeasure(); | ||
| InvalidateVisual(); | ||
| } | ||
|
|
||
| private void OnVideoChanged(RtspVideoView? oldVideo, RtspVideoView? newVideo) | ||
| { | ||
| if (oldVideo is not null) oldVideo.FrameRendered -= OnFrameRendered; | ||
| if (newVideo is not null) newVideo.FrameRendered += OnFrameRendered; | ||
| InvalidateVisual(); | ||
| } | ||
|
|
||
| private void OnViewportChanged(object? sender, EventArgs e) => InvalidateVisual(); | ||
|
|
||
| private void OnHostPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e) | ||
| { | ||
| if (e.Property == BoundsProperty || e.Property == ZoomPanHost.ContentAspectProperty) | ||
| InvalidateMeasure(); | ||
| } | ||
|
|
||
| private void OnFrameRendered(object? sender, EventArgs e) | ||
| { | ||
| if (!IsEffectivelyVisible) return; | ||
| var now = DateTime.UtcNow; | ||
| if (now - _lastFrameRepaint < FrameInterval) return; | ||
| _lastFrameRepaint = now; | ||
| InvalidateVisual(); | ||
| } | ||
|
|
||
| private double Aspect => Host is { ContentAspect: > 0 } h ? h.ContentAspect : 16.0 / 9.0; | ||
|
|
||
| protected override Size MeasureOverride(Size availableSize) | ||
| { | ||
| var hostWidth = Host?.Bounds.Width ?? 0; | ||
| var w = Math.Clamp(hostWidth * HostFraction, MinThumbWidth, MaxThumbWidth); | ||
| if (!double.IsInfinity(availableSize.Width)) w = Math.Min(w, availableSize.Width); | ||
| return new Size(w, Math.Round(w / Aspect)); | ||
| } | ||
|
|
||
| public override void Render(DrawingContext context) | ||
| { | ||
| var bounds = new Rect(Bounds.Size); | ||
| if (bounds.Width <= 0 || bounds.Height <= 0) return; | ||
|
|
||
| context.DrawRectangle(BackgroundBrush, null, bounds, 3, 3); | ||
|
|
||
| if (Video?.CurrentFrame is { } frame) | ||
| { | ||
| using (context.PushRenderOptions(new RenderOptions { BitmapInterpolationMode = BitmapInterpolationMode.MediumQuality })) | ||
| context.DrawImage(frame, new Rect(frame.Size), bounds); | ||
| } | ||
|
|
||
| if (Host is { } host) | ||
| { | ||
| var (x, y, w, h) = host.VisibleContent; | ||
| var view = new Rect(x * bounds.Width, y * bounds.Height, w * bounds.Width, h * bounds.Height); | ||
|
|
||
| // Dim what's off screen so the outlined window reads at a glance. | ||
| context.DrawRectangle(ShadeBrush, null, new Rect(0, 0, bounds.Width, view.Top)); | ||
| context.DrawRectangle(ShadeBrush, null, new Rect(0, view.Bottom, bounds.Width, bounds.Height - view.Bottom)); | ||
| context.DrawRectangle(ShadeBrush, null, new Rect(0, view.Top, view.Left, view.Height)); | ||
| context.DrawRectangle(ShadeBrush, null, new Rect(view.Right, view.Top, bounds.Width - view.Right, view.Height)); | ||
| context.DrawRectangle(null, ViewportPen, view); | ||
| } | ||
|
|
||
| context.DrawRectangle(null, FramePen, bounds, 3, 3); | ||
| } | ||
|
|
||
| protected override void OnPointerPressed(PointerPressedEventArgs e) | ||
| { | ||
| base.OnPointerPressed(e); | ||
| e.Pointer.Capture(this); | ||
| MoveViewTo(e.GetPosition(this)); | ||
| e.Handled = true; | ||
|
Comment on lines
+127
to
+132
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. 4. Secondary clicks move the zoomed view ZoomMinimap.OnPointerPressed captures and recenters the viewport for every pointer press without checking whether the primary button is pressed. A right- or middle-click over the minimap consequently changes the user's zoom location, while the main zoom host correctly gates drag behavior on IsLeftButtonPressed. Agent Prompt
|
||
| } | ||
|
|
||
| protected override void OnPointerMoved(PointerEventArgs e) | ||
| { | ||
| base.OnPointerMoved(e); | ||
| if (!ReferenceEquals(e.Pointer.Captured, this)) return; | ||
| MoveViewTo(e.GetPosition(this)); | ||
| e.Handled = true; | ||
| } | ||
|
|
||
| protected override void OnPointerReleased(PointerReleasedEventArgs e) | ||
| { | ||
| base.OnPointerReleased(e); | ||
| e.Pointer.Capture(null); | ||
| e.Handled = true; | ||
| } | ||
|
|
||
| private void MoveViewTo(Point p) | ||
| { | ||
| if (Host is null || Bounds.Width <= 0 || Bounds.Height <= 0) return; | ||
| Host.CenterOn(p.X / Bounds.Width, p.Y / Bounds.Height); | ||
| } | ||
| } | ||
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,47 @@ | ||
| <UserControl xmlns="https://github.com/avaloniaui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:controls="using:OpenIPC.Viewer.App.Controls" | ||
| xmlns:svc="using:OpenIPC.Viewer.App.Services" | ||
| x:Class="OpenIPC.Viewer.App.Controls.ZoomOverlay" | ||
| IsVisible="False"> | ||
|
|
||
| <UserControl.Styles> | ||
| <!-- Compact chip buttons, quiet until hovered. --> | ||
| <Style Selector="Button.zoom-chip"> | ||
| <Setter Property="Width" Value="28" /> | ||
| <Setter Property="Height" Value="24" /> | ||
| <Setter Property="Padding" Value="0" /> | ||
| <Setter Property="CornerRadius" Value="3" /> | ||
| <Setter Property="FontSize" Value="14" /> | ||
| <Setter Property="Foreground" Value="White" /> | ||
| <Setter Property="Background" Value="Transparent" /> | ||
| <Setter Property="HorizontalContentAlignment" Value="Center" /> | ||
| <Setter Property="VerticalContentAlignment" Value="Center" /> | ||
| </Style> | ||
| <Style Selector="Button.zoom-chip:pointerover /template/ ContentPresenter"> | ||
| <Setter Property="Background" Value="#33FFFFFF" /> | ||
| <Setter Property="TextElement.Foreground" Value="White" /> | ||
| </Style> | ||
| </UserControl.Styles> | ||
|
|
||
| <!-- Minimap of the whole frame (press/drag to move there) and a | ||
| −/level/+/reset chip. Shown only while the host is zoomed in. --> | ||
| <StackPanel Spacing="6"> | ||
| <controls:ZoomMinimap Name="Minimap" HorizontalAlignment="Left" /> | ||
| <Border HorizontalAlignment="Left" Background="#B0000000" CornerRadius="3" Padding="2"> | ||
| <StackPanel Orientation="Horizontal" Spacing="2"> | ||
| <Button Classes="zoom-chip" Content="−" Click="OnZoomOutClick" | ||
| ToolTip.Tip="{Binding [Viewer.ZoomOut], Source={x:Static svc:Localizer.Instance}}" /> | ||
| <TextBlock Name="ZoomLabel" | ||
| MinWidth="36" TextAlignment="Center" VerticalAlignment="Center" | ||
| FontFamily="{StaticResource FontMono}" FontSize="11" Foreground="White" /> | ||
| <Button Classes="zoom-chip" Content="+" Click="OnZoomInClick" | ||
| ToolTip.Tip="{Binding [Viewer.ZoomIn], Source={x:Static svc:Localizer.Instance}}" /> | ||
| <Button Classes="zoom-chip" Click="OnZoomResetClick" | ||
| ToolTip.Tip="{Binding [Zoom.Reset], Source={x:Static svc:Localizer.Instance}}"> | ||
| <Path Classes="lucide" Data="{StaticResource IconClose}" Stroke="White" Width="12" Height="12" /> | ||
| </Button> | ||
| </StackPanel> | ||
| </Border> | ||
| </StackPanel> | ||
| </UserControl> |
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,55 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using Avalonia; | ||
| using Avalonia.Controls; | ||
| using Avalonia.Interactivity; | ||
|
|
||
| namespace OpenIPC.Viewer.App.Controls; | ||
|
|
||
| // Digital-zoom chrome shared by the live and recording pages: minimap plus a | ||
| // −/level/+/reset chip for a ZoomPanHost. Hides itself while not zoomed. | ||
| public sealed partial class ZoomOverlay : UserControl | ||
| { | ||
| public static readonly StyledProperty<ZoomPanHost?> HostProperty = | ||
| AvaloniaProperty.Register<ZoomOverlay, ZoomPanHost?>(nameof(Host)); | ||
|
|
||
| public static readonly StyledProperty<RtspVideoView?> VideoProperty = | ||
| AvaloniaProperty.Register<ZoomOverlay, RtspVideoView?>(nameof(Video)); | ||
|
|
||
| public ZoomOverlay() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
|
|
||
| static ZoomOverlay() | ||
| { | ||
| HostProperty.Changed.AddClassHandler<ZoomOverlay>((o, e) => | ||
| o.OnHostChanged(e.OldValue as ZoomPanHost, e.NewValue as ZoomPanHost)); | ||
| VideoProperty.Changed.AddClassHandler<ZoomOverlay>((o, _) => o.Minimap.Video = o.Video); | ||
| } | ||
|
|
||
| public ZoomPanHost? Host { get => GetValue(HostProperty); set => SetValue(HostProperty, value); } | ||
| public RtspVideoView? Video { get => GetValue(VideoProperty); set => SetValue(VideoProperty, value); } | ||
|
|
||
| private void OnHostChanged(ZoomPanHost? oldHost, ZoomPanHost? newHost) | ||
| { | ||
| if (oldHost is not null) oldHost.ViewportChanged -= OnViewportChanged; | ||
| if (newHost is not null) newHost.ViewportChanged += OnViewportChanged; | ||
| Minimap.Host = newHost; | ||
| Refresh(); | ||
| } | ||
|
|
||
| private void OnViewportChanged(object? sender, EventArgs e) => Refresh(); | ||
|
|
||
| private void Refresh() | ||
| { | ||
| IsVisible = Host?.IsZoomed ?? false; | ||
| ZoomLabel.Text = string.Format(CultureInfo.InvariantCulture, "{0:0.0}×", Host?.Zoom ?? 1.0); | ||
| } | ||
|
|
||
| private void OnZoomInClick(object? sender, RoutedEventArgs e) => Host?.ZoomIn(); | ||
|
|
||
| private void OnZoomOutClick(object? sender, RoutedEventArgs e) => Host?.ZoomOut(); | ||
|
|
||
| private void OnZoomResetClick(object? sender, RoutedEventArgs e) => Host?.Reset(); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
3. The map distorts non-widescreen video
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools