Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Add new ContentButton (#263)
Browse files Browse the repository at this point in the history
* Add new ContentButton

* Change Renderer inheritance and fix typo

* Restructure ContentButton and ContentButtonRenderer

* Fix layout issue

* Remove OnLayoutUpdate method

* Fix typo
  • Loading branch information
shyunMin authored Apr 1, 2020
1 parent 03c4f07 commit 0369e53
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using Tizen.Wearable.CircularUI.Forms;
using Tizen.Wearable.CircularUI.Forms.Renderer;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Tizen;
using Xamarin.Forms.Platform.Tizen.Native;
using XForms = Xamarin.Forms.Forms;
using XFLayout = Xamarin.Forms.Layout;
using EButton = ElmSharp.Button;
using EColor = ElmSharp.Color;
using System.ComponentModel;

[assembly: ExportRenderer(typeof(ContentButton), typeof(ContentButtonRenderer))]

namespace Tizen.Wearable.CircularUI.Forms.Renderer
{
public class ContentButtonRenderer : LayoutRenderer
{
EButton _button;

ContentButton Button => Element as ContentButton;

protected override void OnElementChanged(ElementChangedEventArgs<XFLayout> e)
{
base.OnElementChanged(e);
Initialize();
}

void Initialize()
{
if (_button == null)
{
_button = new EButton(XForms.NativeParent);
_button.BackgroundColor = EColor.Transparent;
_button.SetPartColor("effect", EColor.Transparent);
_button.SetPartColor("effect_pressed", EColor.Transparent);
_button.Show();

_button.Pressed += OnPressed;
_button.Released += OnReleased;
_button.Clicked += OnClicked;

Control.PackEnd(_button);
}
}

protected override void UpdateLayout()
{
base.UpdateLayout();

_button.Geometry = Control.Geometry;
_button.RaiseTop();
}

void OnPressed(object sender, EventArgs args)
{
Button?.SendPressed();
}

void OnReleased(object sender, EventArgs args)
{
Button?.SendReleased();
}

void OnClicked(object sender, EventArgs args)
{
Button?.SendClicked();
}
}
}
184 changes: 184 additions & 0 deletions src/Tizen.Wearable.CircularUI.Forms/ContentButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.ComponentModel;
using System.Windows.Input;
using Xamarin.Forms;

namespace Tizen.Wearable.CircularUI.Forms
{
/// <summary>
/// The ContentButton is a Button, which allows you to customize the View to be displayed.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public class ContentButton : ContentView, IButtonController
{
/// <summary>
/// BindableProperty. Identifies the Command bindable property.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(ContentButton), null,
propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);

/// <summary>
/// BindableProperty. Identifies the CommandParameter bindable property.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(ContentButton), null,
propertyChanged: (bindable, oldvalue, newvalue) => CommandCanExcuteChanged(bindable, EventArgs.Empty));

/// <summary>
/// Gets or sets command that is executed when the button is clicked.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public ICommand Command
{
get => (ICommand)GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

/// <summary>
/// Gets or sets command paramter that is executed when the button is clicked.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public object CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}

/// <summary>
/// Occurs when the button is clicked.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public event EventHandler Clicked;

/// <summary>
/// Occurs when the button is pressed.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public event EventHandler Pressed;

/// <summary>
/// Occurs when the button is released.
/// </summary>
/// <since_tizen> 4 </since_tizen>
public event EventHandler Released;

bool IsEnabledCore
{
set => SetValueCore(IsEnabledProperty, value);
}

/// <summary>
/// For internal use.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendClicked()
{
if (IsEnabled)
{
Command?.Execute(CommandParameter);
Clicked?.Invoke(this, EventArgs.Empty);
}
}

/// <summary>
/// For internal use.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendPressed()
{
if (IsEnabled)
{
Pressed?.Invoke(this, EventArgs.Empty);
}
}

/// <summary>
/// For internal use.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public void SendReleased()
{
if (IsEnabled)
{
Released?.Invoke(this, EventArgs.Empty);
}
}

protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();

View content = Content;
if (content != null)
{
SetInheritedBindingContext(content, BindingContext);
}
}

static void OnCommandChanged(BindableObject bindable, object oldCommand, object newCommand)
{
ContentButton button = (ContentButton)bindable;
if (newCommand is ICommand command)
{
command.CanExecuteChanged += button.OnCommandCanExecuteChanged;
}
CommandChanged(button);
}

static void CommandChanged(ContentButton button)
{
if(button.Command != null)
{
CommandCanExcuteChanged(button, EventArgs.Empty);
}
else
{
button.IsEnabledCore = true;
}
}

static void OnCommandChanging(BindableObject bindable, object oldCommand, object newCommand)
{
ContentButton button = (ContentButton)bindable;
if (oldCommand != null)
{
(oldCommand as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
}
}

static void CommandCanExcuteChanged(object sender, EventArgs e)
{
var button = (ContentButton)sender;
if (button.Command != null)
{
button.IsEnabledCore = button.Command.CanExecute(button.CommandParameter);
}
}

void OnCommandCanExecuteChanged(object sender, EventArgs e)
{
ContentButton button = (ContentButton)sender;
if (button.Command != null)
{
button.IsEnabledCore = button.Command.CanExecute(button.CommandParameter);
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions test/WearableUIGallery/WearableUIGallery/TC/TCContentButton.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:w="clr-namespace:Tizen.Wearable.CircularUI.Forms;assembly=Tizen.Wearable.CircularUI.Forms"
xmlns:tizen="clr-namespace:Xamarin.Forms.PlatformConfiguration.TizenSpecific;assembly=Xamarin.Forms.Core"
x:Class="WearableUIGallery.TC.ContentButtonTestPage">
<ContentPage.Content>
<StackLayout VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand">
<Label x:Name="label"
HorizontalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"
Text="Test"/>
<w:ContentButton x:Name="button"
Clicked="OnButtonClicked"
Pressed="OnButtonPressed"
Released="OnButtonReleased"
Command="{Binding ClickCommand}">
<w:ContentButton.Content>
<AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Image x:Name="buttonBg" Source="button_bg.png" Opacity="0.25" Aspect="AspectFill" tizen:Image.BlendColor="Transparent" AbsoluteLayout.LayoutBounds=".5,.5,89,66" AbsoluteLayout.LayoutFlags="PositionProportional" />
<Image x:Name="buttonBorder" Source="button_border.png" Aspect="AspectFill" tizen:Image.BlendColor="DarkGreen" AbsoluteLayout.LayoutBounds=".5,.5,89,66" AbsoluteLayout.LayoutFlags="PositionProportional" />
<Image x:Name="buttonIcon" Source="home.png" tizen:Image.BlendColor="DarkGreen" AbsoluteLayout.LayoutBounds=".5,.5,36,36" AbsoluteLayout.LayoutFlags="PositionProportional" />
</AbsoluteLayout>
</w:ContentButton.Content>
</w:ContentButton>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace WearableUIGallery.TC
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ContentButtonTestPage : ContentPage
{
public ContentButtonTestPage()
{
InitializeComponent();

ClickCommand = new Command(execute: () =>
{
label.Text = "clicked";
});
}

public ICommand ClickCommand { get; private set; }

private void OnButtonClicked(object sender, EventArgs e)
{
Console.WriteLine($"ContentButton clicked event is invoked!!");
}

private void OnButtonPressed(object sender, EventArgs e)
{
Xamarin.Forms.PlatformConfiguration.TizenSpecific.Image.SetBlendColor(buttonBg, Color.Gray);
}

private void OnButtonReleased(object sender, EventArgs e)
{
Xamarin.Forms.PlatformConfiguration.TizenSpecific.Image.SetBlendColor(buttonBg, Color.Transparent);
}
}
}
1 change: 1 addition & 0 deletions test/WearableUIGallery/WearableUIGallery/TCData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static class TCData
static TCData()
{
TCs = new ObservableCollection<TCDescribe>();
TCs.Add(new TCDescribe { Title = "ContentButtonTest", Class = typeof(ContentButtonTestPage) });
TCs.Add(new TCDescribe
{
Title = "ShellTest",
Expand Down

0 comments on commit 0369e53

Please sign in to comment.