maui-linux/Xamarin.Forms.Platform.Tizen/Native/ToolbarItemButton.cs

178 строки
4.5 KiB
C#
Исходник Обычный вид История

2018-11-15 16:00:06 +03:00
using System;
using System.ComponentModel;
using ElmSharp.Accessible;
2018-11-15 16:00:06 +03:00
using EColor = ElmSharp.Color;
namespace Xamarin.Forms.Platform.Tizen.Native
{
public class ToolbarItemButton : Button
{
ToolbarItem _item;
string _defaultAccessibilityName;
string _defaultAccessibilityDescription;
bool? _defaultIsAccessibilityElement;
2018-11-15 16:00:06 +03:00
public ToolbarItemButton(ToolbarItem item) : base(Forms.NativeParent)
{
_item = item;
BackgroundColor = EColor.Transparent;
Clicked += OnClicked;
Deleted += OnDeleted;
_item.PropertyChanged += OnToolbarItemPropertyChanged;
UpdateText();
UpdateIsEnabled();
UpdateIcon();
SetAccessibilityName(true);
SetAccessibilityDescription(true);
SetIsAccessibilityElement(true);
SetLabeledBy(true);
2018-11-15 16:00:06 +03:00
}
void OnDeleted(object sender, EventArgs e)
{
Clicked -= OnClicked;
Deleted -= OnDeleted;
_item.PropertyChanged -= OnToolbarItemPropertyChanged;
}
void OnClicked(object sender, EventArgs e)
{
((IMenuItemController)_item).Activate();
2018-11-15 16:00:06 +03:00
}
void OnToolbarItemPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == ToolbarItem.TextProperty.PropertyName)
{
UpdateText();
}
else if (e.PropertyName == ToolbarItem.IsEnabledProperty.PropertyName)
{
UpdateIsEnabled();
}
2019-04-27 20:48:26 +03:00
else if (e.PropertyName == ToolbarItem.IconImageSourceProperty.PropertyName)
2018-11-15 16:00:06 +03:00
{
UpdateIcon();
}
else if (e.PropertyName == AutomationProperties.NameProperty.PropertyName)
{
SetAccessibilityName(false);
}
else if (e.PropertyName == AutomationProperties.HelpTextProperty.PropertyName)
{
SetAccessibilityDescription(false);
}
else if (e.PropertyName == AutomationProperties.IsInAccessibleTreeProperty.PropertyName)
{
SetIsAccessibilityElement(false);
}
else if (e.PropertyName == AutomationProperties.LabeledByProperty.PropertyName)
{
SetLabeledBy(false);
}
2018-11-15 16:00:06 +03:00
}
void UpdateText()
{
UpdateStyle();
Text = _item.Text;
}
void UpdateIsEnabled()
{
IsEnabled = _item.IsEnabled;
}
void UpdateIcon()
{
[WIP] Unify the image handling (#4915) * Initial code to get unifiied image handling - not yet tested - still using FileImageSource in some areas * Updated the button renderers and added tests for Android * Updated a few more of the Android renderers - also added an `IsEmpty` property to the ImageSource to indicate if this source actually contains data that can be used to try and load an image - added a few more "tests" - includes the changes for #4916 * A few more changes * Keep the default page for the sample app * Changing everything to ImageSource and going from there - Android, iOS, UWP and WPF are compiling - GTK, Mac and Tizen are not yet finished - Added a new interface for UWP to return an IconElement in addition to ImageSource (for app bar buttons) - not tested yet, nor are there any tests * Renamed the property to be more useful * All of Android is now async - still only minimal tests - also removed the bits that are in https://github.com/xamarin/Xamarin.Forms/pull/4948 * Update Xamarin.Forms.Platform.cs * A few fixes to whitespace and nameof() * Updated iOS and UWP wirth async image sources * A few fixes and WPF support * A few fixes for Android after the big merge * Updated a few more loaders: - ios - macos - tizen - gtk * Fix a few things after the merge * - cast type to FileImageSource * fix setting of title content if icon doesn't load * fix IButtonLayoutManager to return correct control * remove cast and add pack api * - fix timing issues with layout/invalidation * - remove aggresive element invalidations for now * first set of api changes * obsolete old apis and create new ones for ImageSource * obsolete messages and static ordering fix * add tests * switch default on windows to show images on tabs * - XStatic obsolete fix * fix NPC test and bring back alert check on uwp Fixes #3207 Fixes #4689
2019-04-26 23:46:13 +03:00
if (_item.IconImageSource.IsNullOrEmpty())
2018-11-15 16:00:06 +03:00
{
//On 5.0, the part content should be removed before the style is changed, otherwise, EFL does not remove the part content.
Image = null;
UpdateStyle();
}
else
{
// In reverse, the style should be set before setting the part content.
UpdateStyle();
Native.Image iconImage = new Native.Image(Forms.NativeParent);
[WIP] Unify the image handling (#4915) * Initial code to get unifiied image handling - not yet tested - still using FileImageSource in some areas * Updated the button renderers and added tests for Android * Updated a few more of the Android renderers - also added an `IsEmpty` property to the ImageSource to indicate if this source actually contains data that can be used to try and load an image - added a few more "tests" - includes the changes for #4916 * A few more changes * Keep the default page for the sample app * Changing everything to ImageSource and going from there - Android, iOS, UWP and WPF are compiling - GTK, Mac and Tizen are not yet finished - Added a new interface for UWP to return an IconElement in addition to ImageSource (for app bar buttons) - not tested yet, nor are there any tests * Renamed the property to be more useful * All of Android is now async - still only minimal tests - also removed the bits that are in https://github.com/xamarin/Xamarin.Forms/pull/4948 * Update Xamarin.Forms.Platform.cs * A few fixes to whitespace and nameof() * Updated iOS and UWP wirth async image sources * A few fixes and WPF support * A few fixes for Android after the big merge * Updated a few more loaders: - ios - macos - tizen - gtk * Fix a few things after the merge * - cast type to FileImageSource * fix setting of title content if icon doesn't load * fix IButtonLayoutManager to return correct control * remove cast and add pack api * - fix timing issues with layout/invalidation * - remove aggresive element invalidations for now * first set of api changes * obsolete old apis and create new ones for ImageSource * obsolete messages and static ordering fix * add tests * switch default on windows to show images on tabs * - XStatic obsolete fix * fix NPC test and bring back alert check on uwp Fixes #3207 Fixes #4689
2019-04-26 23:46:13 +03:00
_ = iconImage.LoadFromImageSourceAsync(_item.IconImageSource);
2018-11-15 16:00:06 +03:00
Image = iconImage;
}
}
void UpdateStyle()
{
[WIP] Unify the image handling (#4915) * Initial code to get unifiied image handling - not yet tested - still using FileImageSource in some areas * Updated the button renderers and added tests for Android * Updated a few more of the Android renderers - also added an `IsEmpty` property to the ImageSource to indicate if this source actually contains data that can be used to try and load an image - added a few more "tests" - includes the changes for #4916 * A few more changes * Keep the default page for the sample app * Changing everything to ImageSource and going from there - Android, iOS, UWP and WPF are compiling - GTK, Mac and Tizen are not yet finished - Added a new interface for UWP to return an IconElement in addition to ImageSource (for app bar buttons) - not tested yet, nor are there any tests * Renamed the property to be more useful * All of Android is now async - still only minimal tests - also removed the bits that are in https://github.com/xamarin/Xamarin.Forms/pull/4948 * Update Xamarin.Forms.Platform.cs * A few fixes to whitespace and nameof() * Updated iOS and UWP wirth async image sources * A few fixes and WPF support * A few fixes for Android after the big merge * Updated a few more loaders: - ios - macos - tizen - gtk * Fix a few things after the merge * - cast type to FileImageSource * fix setting of title content if icon doesn't load * fix IButtonLayoutManager to return correct control * remove cast and add pack api * - fix timing issues with layout/invalidation * - remove aggresive element invalidations for now * first set of api changes * obsolete old apis and create new ones for ImageSource * obsolete messages and static ordering fix * add tests * switch default on windows to show images on tabs * - XStatic obsolete fix * fix NPC test and bring back alert check on uwp Fixes #3207 Fixes #4689
2019-04-26 23:46:13 +03:00
if (_item.IconImageSource.IsNullOrEmpty())
2018-11-15 16:00:06 +03:00
{
if (string.IsNullOrEmpty(_item.Text))
{
// We assumed the default toolbar icon is "naviframe/drawer" if there are no icon and text.
this.SetNavigationDrawerStyle();
2018-11-15 16:00:06 +03:00
}
else
{
if (_item.Order == ToolbarItemOrder.Primary)
this.SetNavigationTitleRightStyle();
2018-11-15 16:00:06 +03:00
else
this.SetNavigationTitleLeftStyle();
2018-11-15 16:00:06 +03:00
}
}
else
{
this.SetDefaultStyle();
2018-11-15 16:00:06 +03:00
}
}
void SetAccessibilityName(bool initialize)
{
if (initialize && (string)_item.GetValue(AutomationProperties.NameProperty) == (default(string)))
return;
var accessibleObject = this as IAccessibleObject;
if (accessibleObject != null)
{
_defaultAccessibilityName = accessibleObject.SetAccessibilityName(_item, _defaultAccessibilityName);
}
}
void SetAccessibilityDescription(bool initialize)
{
if (initialize && (string)_item.GetValue(AutomationProperties.HelpTextProperty) == (default(string)))
return;
var accessibleObject = this as IAccessibleObject;
if (accessibleObject != null)
{
_defaultAccessibilityDescription = accessibleObject.SetAccessibilityDescription(_item, _defaultAccessibilityDescription);
}
}
void SetIsAccessibilityElement(bool initialize)
{
if (initialize && (bool?)_item.GetValue(AutomationProperties.IsInAccessibleTreeProperty) == default(bool?))
return;
var accessibleObject = this as IAccessibleObject;
if (accessibleObject != null)
{
_defaultIsAccessibilityElement = accessibleObject.SetIsAccessibilityElement(_item, _defaultIsAccessibilityElement);
}
}
void SetLabeledBy(bool initialize)
{
if (initialize && (VisualElement)_item.GetValue(AutomationProperties.LabeledByProperty) == default(VisualElement))
return;
var accessibleObject = this as IAccessibleObject;
if (accessibleObject != null)
{
accessibleObject.SetLabeledBy(_item);
}
}
2018-11-15 16:00:06 +03:00
}
}