Add SelectableLabel Custom Renderer to allow copy of note text (#1564)

* Add SelectableLabel Custom Renderer to allow copy of note text
- Remove SelectableLabelEffect

* Remove editor changes from text custom field
This commit is contained in:
Jake Fink 2021-10-08 08:49:15 -04:00 committed by GitHub
parent 4aad34cd75
commit d3734c63fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 106 additions and 42 deletions

View file

@ -111,7 +111,6 @@
<Compile Include="Autofill\SavedItem.cs" />
<Compile Include="Effects\FabShadowEffect.cs" />
<Compile Include="Effects\FixedSizeEffect.cs" />
<Compile Include="Effects\SelectableLabelEffect.cs" />
<Compile Include="Effects\TabBarEffect.cs" />
<Compile Include="Push\FirebaseMessagingService.cs" />
<Compile Include="Receivers\ClearClipboardAlarmReceiver.cs" />
@ -148,6 +147,7 @@
<Compile Include="Utilities\AppCenterHelper.cs" />
<Compile Include="Utilities\ThemeHelpers.cs" />
<Compile Include="WebAuthCallbackActivity.cs" />
<Compile Include="Renderers\SelectableLabelRenderer.cs" />
</ItemGroup>
<ItemGroup>
<AndroidAsset Include="Assets\FontAwesome.ttf" />

View file

@ -1,23 +0,0 @@
using Android.Widget;
using Bit.Droid.Effects;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportEffect(typeof(SelectableLabelEffect), "SelectableLabelEffect")]
namespace Bit.Droid.Effects
{
public class SelectableLabelEffect : PlatformEffect
{
protected override void OnAttached()
{
if (Control is TextView textView)
{
textView.SetTextIsSelectable(true);
}
}
protected override void OnDetached()
{
}
}
}

View file

@ -0,0 +1,25 @@
using System;
using Android.Content;
using Bit.App.Controls;
using Bit.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(SelectableLabel), typeof(SelectableLabelRenderer))]
namespace Bit.Droid.Renderers
{
public class SelectableLabelRenderer : LabelRenderer
{
public SelectableLabelRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.SetTextIsSelectable(true);
}
}
}
}

View file

@ -0,0 +1,10 @@
using System;
using Xamarin.Forms;
namespace Bit.App.Controls
{
public class SelectableLabel : Label
{
}
}

View file

@ -1,11 +0,0 @@
using Xamarin.Forms;
namespace Bit.App.Effects
{
public class SelectableLabelEffect : RoutingEffect
{
public SelectableLabelEffect()
: base("Bitwarden.SelectableLabelEffect")
{ }
}
}

View file

@ -526,14 +526,9 @@
StyleClass="box-header, box-header-platform" />
</StackLayout>
<StackLayout StyleClass="box-row">
<Label
<controls:SelectableLabel
Text="{Binding Cipher.Notes, Mode=OneWay}"
StyleClass="box-value"
LineBreakMode="WordWrap">
<Label.Effects>
<effects:SelectableLabelEffect />
</Label.Effects>
</Label>
StyleClass="box-value"/>
</StackLayout>
<BoxView StyleClass="box-row-separator" />
</StackLayout>

View file

@ -0,0 +1,67 @@
using System;
using System.ComponentModel;
using Bit.App.Controls;
using Bit.iOS.Core.Renderers;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(SelectableLabel), typeof(SelectableLabelRenderer))]
namespace Bit.iOS.Core.Renderers
{
public class SelectableLabelRenderer : ViewRenderer<Label, UITextView>
{
UITextView uiTextView;
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
if (Control == null)
{
uiTextView = new UITextView();
}
uiTextView.Selectable = true;
uiTextView.Editable = false;
uiTextView.ScrollEnabled = false;
uiTextView.TextContainerInset = UIEdgeInsets.Zero;
uiTextView.TextContainer.LineFragmentPadding = 0;
uiTextView.BackgroundColor = UIColor.Clear;
uiTextView.Text = Element.Text;
uiTextView.TextColor = Element.TextColor.ToUIColor();
switch (Element.FontAttributes)
{
case FontAttributes.None:
uiTextView.Font = UIFont.SystemFontOfSize(new nfloat(Element.FontSize));
break;
case FontAttributes.Bold:
uiTextView.Font = UIFont.BoldSystemFontOfSize(new nfloat(Element.FontSize));
break;
case FontAttributes.Italic:
uiTextView.Font = UIFont.ItalicSystemFontOfSize(new nfloat(Element.FontSize));
break;
default:
uiTextView.Font = UIFont.BoldSystemFontOfSize(new nfloat(Element.FontSize));
break;
}
SetNativeControl(uiTextView);
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Label.TextProperty.PropertyName)
{
if (Control != null && Element != null && !string.IsNullOrWhiteSpace(Element.Text))
{
uiTextView.Text = Element.Text;
}
}
}
}
}

View file

@ -188,6 +188,7 @@
<Compile Include="Views\StepperTableViewCell.cs" />
<Compile Include="Views\SwitchTableViewCell.cs" />
<Compile Include="Views\Toast.cs" />
<Compile Include="Renderers\SelectableLabelRenderer.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\App\App.csproj">