using System;
using ElmSharp;
using EButton = ElmSharp.Button;
namespace Xamarin.Forms.Platform.Tizen.Native
{
///
/// Base class for Dialogs.
/// A dialog is a small window that prompts the user to make a decision or enter additional information.
///
public class Dialog : Popup
{
EButton _positiveButton;
EButton _neutralButton;
EButton _negativeButton;
EvasObject _content;
string _title;
///
/// Creates a dialog window that uses the default dialog theme.
///
public Dialog(EvasObject parent) : base(parent)
{
Initialize();
}
///
/// Occurs whenever the dialog is first displayed.
///
public event EventHandler Shown;
///
/// Enumerates the three valid positions of a dialog button.
///
enum ButtonPosition
{
Positive,
Neutral,
Negative
}
///
/// Gets or sets the title of the dialog
///
public string Title
{
get
{
return _title;
}
set
{
if (_title != value)
{
ApplyTitle(value);
}
}
}
///
/// Gets or sets the content to display in that dialog.
///
public EvasObject Content
{
get
{
return _content;
}
set
{
if (_content != value)
{
ApplyContent(value);
}
}
}
///
/// Gets or sets the positive button used in the dialog
///
public EButton PositiveButton
{
get
{
return _positiveButton;
}
set
{
if (_positiveButton != value)
{
ApplyButton(ButtonPosition.Positive, value);
}
}
}
///
/// Gets or sets the neutral button used in the dialog
///
public EButton NeutralButton
{
get
{
return _neutralButton;
}
set
{
if (_neutralButton != value)
{
ApplyButton(ButtonPosition.Neutral, value);
}
}
}
///
/// Gets or sets the negative button used in the dialog
///
public EButton NegativeButton
{
get
{
return _negativeButton;
}
set
{
if (_negativeButton != value)
{
ApplyButton(ButtonPosition.Negative, value);
}
}
}
///
/// Starts the dialog and displays it on screen.
///
public new void Show()
{
base.Show();
Shown?.Invoke(this, EventArgs.Empty);
}
///
/// Handles the disposing of a dialog widget.
///
protected override void OnUnrealize()
{
_content?.Unrealize();
_positiveButton?.Unrealize();
_neutralButton?.Unrealize();
_negativeButton?.Unrealize();
ApplyButton(ButtonPosition.Positive, null);
ApplyButton(ButtonPosition.Neutral, null);
ApplyButton(ButtonPosition.Negative, null);
ApplyContent(null);
base.OnUnrealize();
}
///
/// Called when the dialog is shown.
///
/// When shown, the dialog will register itself for the back key press event handling.
protected virtual void OnShown()
{
}
///
/// Called when the dialog is dismissed.
///
/// When dismissed, the dialog will unregister itself from the back key press event handling.
protected virtual void OnDismissed()
{
}
///
/// Handles the initialization process.
///
/// Creates handlers for vital events
void Initialize()
{
// Adds a handler for the Dismissed event.
// In effect, unregisters this instance from being affected by the hardware back key presses.
Dismissed += (s, e) =>
{
OnDismissed();
//Native control should be freed after dismissed event occurred. (this is EFL's law.)
Unrealize();
};
// Adds a handler for the Shown event.
// In effect, registers this instance to be affected by the hardware back key presses.
Shown += (s, e) =>
{
OnShown();
};
}
///
/// Changes the dialog title.
///
/// New dialog title.
void ApplyTitle(string title)
{
_title = title;
SetPartText("title,text", _title);
}
///
/// Puts the button in one of the three available slots.
///
/// The slot to be occupied by the button expressed as a
/// The new button.
void ApplyButton(ButtonPosition position, EButton button)
{
if (button != null)
{
button.Style = "popup";
}
switch (position)
{
case ButtonPosition.Positive:
_positiveButton = button;
SetPartContent("button3", _positiveButton, true);
break;
case ButtonPosition.Neutral:
_neutralButton = button;
SetPartContent("button2", _neutralButton, true);
break;
case ButtonPosition.Negative:
_negativeButton = button;
SetPartContent("button1", _negativeButton, true);
break;
}
}
///
/// Updates the content of the dialog.
///
/// New dialog content.
void ApplyContent(EvasObject content)
{
_content = content;
SetPartContent("default", _content, true);
}
}
}