Skip to content

Commit

Permalink
popup: Implement TextPopupElement view for MAUI (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstefarov authored Sep 15, 2023
1 parent 1e8ad8a commit 3c74bba
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Border Background="#99ffffff" x:Name="popupPanel" IsVisible="False">
<Border HorizontalOptions="Center" VerticalOptions="Center" Background="{AppThemeBinding Dark=Black, Light=White}" Margin="0,50" Padding="20">
<Grid>
<esriTK:PopupViewer x:Name="popupViewer" Margin="20" MaximumWidthRequest="400" HeightRequest="400" PopupAttachmentClicked="popupViewer_PopupAttachmentClicked" />
<esriTK:PopupViewer x:Name="popupViewer" Padding="20" MaximumWidthRequest="400" HeightRequest="400" PopupAttachmentClicked="popupViewer_PopupAttachmentClicked" />
<Button BorderWidth="0" Text="X" HorizontalOptions="End" VerticalOptions="Start" Clicked="CloseButton_Click" BackgroundColor="Transparent" TextColor="{AppThemeBinding Dark=White, Light=Black}" Margin="5" />
</Grid>
</Border>
Expand Down
14 changes: 13 additions & 1 deletion src/Toolkit/Toolkit/Internal/HtmlUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if WPF
#if WPF || MAUI
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -72,6 +72,18 @@ public override string ToString()
sb.Append('}');
return sb.ToString();
}

public void InheritAttributes(MarkupNode parent)
{
// Copy style attributes from the parent node, unless overridden on this node
IsBold ??= parent.IsBold;
IsItalic ??= parent.IsItalic;
IsUnderline ??= parent.IsUnderline;
FontSize ??= parent.FontSize;
FontColor ??= parent.FontColor;
BackColor ??= parent.BackColor;
Alignment ??= parent.Alignment;
}
}

/// <summary>
Expand Down
71 changes: 42 additions & 29 deletions src/Toolkit/Toolkit/UI/Controls/PopupViewer/PopupMediaView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,9 @@ private void UpdateImage()
if (img.Source is not BitmapImage bmi || bmi.UriSource?.OriginalString != sourceUrl)
#endif
{
if (sourceUrl.StartsWith("data:image/"))
if (TryCreateImageSource(sourceUrl, out var source))
{
// might be base64
var idx = sourceUrl.IndexOf(";base64,");
if (idx > 11 && sourceUrl.Length > idx + 8)
{
try
{
var base64data = sourceUrl.Substring(idx + 8);
var data = Convert.FromBase64String(base64data);
#if MAUI
var source = new StreamImageSource() { Stream = (token) => Task.FromResult<Stream>(new MemoryStream(data)) };
#else
var source = new BitmapImage();
source.BeginInit();
source.StreamSource = new MemoryStream(data);
source.EndInit();
#endif
img.Source = source;
}
catch { }
}
}
else if (sourceUrl != null && Uri.TryCreate(sourceUrl, UriKind.Absolute, out Uri? result))
{
#if MAUI
img.Source = new RuntimeStreamImageSource(result);
#else
img.Source = new BitmapImage(result);
#endif
img.Source = source;
}
}
}
Expand Down Expand Up @@ -234,6 +207,46 @@ private void OnPopupMediaPropertyChanged()
InvalidateMeasure(); // Forces recalculation of available space for generating a new chart
}
}

// Also used for embedded images in TextPopupElement views
internal static bool TryCreateImageSource(string? sourceUri, out ImageSource? source)
{
if (sourceUri != null && sourceUri.StartsWith("data:image/"))
{
// might be base64
var idx = sourceUri.IndexOf(";base64,");
if (idx > 11 && sourceUri.Length > idx + 8)
{
try
{
var base64data = sourceUri.Substring(idx + 8);
var data = Convert.FromBase64String(base64data);
#if MAUI
var newSource = new StreamImageSource { Stream = (token) => Task.FromResult<Stream>(new MemoryStream(data)) };
#else
var newSource = new BitmapImage();
newSource.BeginInit();
newSource.StreamSource = new MemoryStream(data);
newSource.EndInit();
#endif
source = newSource;
return true;
}
catch { }
}
}
else if (Uri.TryCreate(sourceUri, UriKind.Absolute, out Uri? result))
{
#if MAUI
source = new RuntimeStreamImageSource(result);
#else
source = new BitmapImage(result);
#endif
return true;
}
source = null;
return false;
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static object BuildDefaultTemplate()
root.Add(roottitle);
ScrollView scrollView = new ScrollView() { HorizontalScrollBarVisibility = ScrollBarVisibility.Never };
#if WINDOWS
scrollView.Margin = new Thickness(0, 0, -10, 0);
scrollView.Padding = new Thickness(0, 0, 10, 0);
#endif
scrollView.SetBinding(ScrollView.VerticalScrollBarVisibilityProperty, new Binding(nameof(VerticalScrollBarVisibility), source: RelativeBindingSource.TemplatedParent));
Grid.SetRow(scrollView, 1);
Expand Down
Loading

0 comments on commit 3c74bba

Please sign in to comment.