Maui icon indicating copy to clipboard operation
Maui copied to clipboard

[Proposal] RemoveBorderEffect

Open TheCodeTraveler opened this issue 4 years ago • 1 comments

RemoveBorderEffect

  • [x] Proposed
  • [ ] Prototype: Not Started
  • [ ] Implementation: Not Started
    • [ ] iOS Support
    • [ ] Android Support
    • [ ] macOS Support
    • [ ] Windows Support
  • [ ] Unit Tests: Not Started
  • [ ] Sample: Not Started
  • [ ] Documentation: Not Started

Summary

Removes a border from a VisualElement

Detailed Design

RemoveBorderEffect.shared.cs

public class RemoveBorderEffect : RoutingEffect
{
}

RemoveBorderEffect.Android.cs

public class RemoveBorderEffect : PlatformEffect
{
  Drawable? originalBackground;

  protected override void OnAttached()
  {
	originalBackground = Control.Background;

	var shape = new ShapeDrawable(new RectShape());
	if (shape.Paint != null)
	{
		shape.Paint.Color = global::Android.Graphics.Color.Transparent;
		shape.Paint.StrokeWidth = 0;
		shape.Paint.SetStyle(Paint.Style.Stroke);
	}

	Control.Background = shape;
  }

  protected override void OnDetached() => Control.Background = originalBackground;
}

RemoveBorderEffect.iOS.cs

public class RemoveBorderEffect : PlatformEffect
{
  UITextBorderStyle? oldBorderStyle;
  
  UITextField? TextField => Control as UITextField;
  
  protected override void OnAttached()
  {
	oldBorderStyle = TextField?.BorderStyle;
	SetBorderStyle(UITextBorderStyle.None);
  }
  
  protected override void OnDetached() => SetBorderStyle(oldBorderStyle);

  void SetBorderStyle(UITextBorderStyle? borderStyle)
  {
	if (TextField != null && borderStyle.HasValue)
		TextField.BorderStyle = borderStyle.Value;
  }
}

RemoveBorderEffect.uwp.cs

public class RemoveBorderEffect : PlatformEffect
{
  Thickness oldBorderThickness;
  
  protected override void OnAttached()
  {
	if (Control is Control uwpControl)
	{
		oldBorderThickness = uwpControl.BorderThickness;
		uwpControl.BorderThickness = new Thickness(0.0);
	}
  }
  
  protected override void OnDetached()
  {
	if (Control is Control uwpControl)
	{
		uwpControl.BorderThickness = oldBorderThickness;
	}
  }
}

Usage Syntax

XAML Usage

<Label
    <Label.Effects>
        <local: RemoveBorderEffect/>
    </Label.Effects>
</Label>

C# Usage

var label = new Label();
label.Effects.Add(new RemoveBorderEffect());

TheCodeTraveler avatar Sep 26 '21 17:09 TheCodeTraveler

Blocked. Pending the result of https://github.com/CommunityToolkit/Maui/issues/124

TheCodeTraveler avatar Oct 07 '21 20:10 TheCodeTraveler

any updates here? seems issue 124 is closed

KSemenenko avatar Nov 07 '22 13:11 KSemenenko

With the new handlers approach, I ask if this is still valuable. I mean on Forms side this change requires a lot of files and work, with MAUI looks very straight forward to be implemented by devs and (from a maintainer point of view) is one less API to maintain

pictos avatar Nov 07 '22 15:11 pictos

I am facing this problem again, it was in Xamarin.Froms 2.0 and it is still there. Maybe we need to solve this problem somehow =)

KSemenenko avatar Nov 07 '22 16:11 KSemenenko

@KSemenenko, which problem?

pictos avatar Nov 07 '22 16:11 pictos

Borderless Entry

KSemenenko avatar Nov 07 '22 16:11 KSemenenko

@KSemenenko you can open an issue on Maui repo itself, since it's related to a control... Maybe it's something that you can solve already using an style.

pictos avatar Nov 07 '22 17:11 pictos

In order to remove the Entry border you can do the following for android and ios:

EntryHandler.Mapper.AppendToMapping("Background", (handler, view) =>
{
#if ANDROID
	var shape = new Android.Graphics.Drawables.ShapeDrawable(new Android.Graphics.Drawables.Shapes.RectShape());

	if (shape.Paint is not null)
	{
		shape.Paint.Color = Android.Graphics.Color.Transparent;
		shape.Paint.StrokeWidth = 0;
		shape.Paint.SetStyle(Android.Graphics.Paint.Style.Stroke);
	}
	handler.PlatformView.Background = shape;
#elif IOS || MACCATALYST
	handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#endif
});

For windows just add these resources on your Platform/Windows/App.xaml

<maui:MauiWinUIApplication.Resources>
	<Thickness x:Key="TextControlBorderThemeThickness">0</Thickness>
	<Thickness x:Key="TextControlBorderThemeThicknessFocused">0</Thickness>
</maui:MauiWinUIApplication.Resources>

With that I still believe it's a very simple code to keep here.

pictos avatar Nov 11 '22 00:11 pictos

Reopening Proposal.

Only Proposals moved to the Closed Project Column and Completed Project Column can be closed.

ghost avatar Nov 11 '22 00:11 ghost

@pictos thanks a lot for sample of code! 👍

KSemenenko avatar Nov 11 '22 07:11 KSemenenko