/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.Collections.Generic;
namespace ElmSharp
{
///
/// The NaviframeEventArgs is a event args class for navi frame.
/// Inherits EventArgs
///
public class NaviframeEventArgs : EventArgs
{
///
/// Sets or gets the content object. The name of content part is "elm.swallow.content".
///
public EvasObject Content { get; set; }
}
///
/// Naviframe is a widget to stands for navigation frame. It's a views manager for applications.
/// Inherits Widget
///
public class Naviframe : Widget
{
SmartEvent _transitionFinished;
readonly List _itemStack = new List();
///
/// Creates and initializes a new instance of Naviframe class.
///
/// The parent is a given container which will be attached by Naviframe as a child. It's type.
public Naviframe(EvasObject parent) : base(parent)
{
_transitionFinished = new SmartEvent(this, this.RealHandle, "transition,finished");
_transitionFinished.On += (s, e) => AnimationFinished?.Invoke(this, EventArgs.Empty);
}
///
/// Popped will be triggered when NaviItem is removed.
///
///
/// It is always called when NaviItem was removed.
/// (even if removed by NaviItem.Delete())
/// This event will be invoked in progress of Pop/Delete operation.
/// After called Popped event, Pop/Delete method will be returned
///
public event EventHandler Popped;
///
/// AnimationFinished will be triggered when animation is finished.
///
public event EventHandler AnimationFinished;
///
/// Gets the list of navi item
///
public IReadOnlyList NavigationStack
{
get { return _itemStack; }
}
///
/// Sets or gets the the preserve content objects when items are popped.
///
public bool PreserveContentOnPop
{
get
{
return Interop.Elementary.elm_naviframe_content_preserve_on_pop_get(RealHandle);
}
set
{
Interop.Elementary.elm_naviframe_content_preserve_on_pop_set(RealHandle, value);
}
}
///
/// Sets or gets whether the default back button is enabled
///
public bool DefaultBackButtonEnabled
{
get
{
return Interop.Elementary.elm_naviframe_prev_btn_auto_pushed_get(RealHandle);
}
set
{
Interop.Elementary.elm_naviframe_prev_btn_auto_pushed_set(RealHandle, value);
}
}
///
/// Push a new item to the top of the naviframe stack and show it.
/// The title and style are null.
///
/// The main content object. The name of content part is "elm.swallow.content".
/// The created item or null upon failure.
public NaviItem Push(EvasObject content)
{
return Push(content, null);
}
///
/// Push a new item to the top of the naviframe stack and show it.
/// The style are null.
///
/// The main content object. The name of content part is "elm.swallow.content".
/// The current item title. null would be default.
///
public NaviItem Push(EvasObject content, string title)
{
return Push(content, title, null);
}
///
/// Push a new item to the top of the naviframe stack and show it.
///
/// The main content object. The name of content part is "elm.swallow.content".
/// The current item title. null would be default.
/// The current item style name. null would be default.
/// The created item or null upon failure.
public NaviItem Push(EvasObject content, string title, string style)
{
IntPtr item = Interop.Elementary.elm_naviframe_item_push(RealHandle, title, IntPtr.Zero, IntPtr.Zero, content.Handle, style);
NaviItem naviItem = NaviItem.FromNativeHandle(item, content);
_itemStack.Add(naviItem);
naviItem.Popped += ItemPoppedHandler;
return naviItem;
}
///
/// Insert a new item into the naviframe before item.
/// The title is "" and the style is null.
///
/// The item which the new item is inserted before.
/// The main content object. The name of content part is "elm.swallow.content".
/// The created item or null upon failure.
public NaviItem InsertBefore(NaviItem before, EvasObject content)
{
return InsertBefore(before, content, "");
}
///
/// Insert a new item into the naviframe before item.
/// The style is null.
///
/// The item which the new item is inserted before.
/// The main content object. The name of content part is "elm.swallow.content".
/// The current item title. null would be default.
/// The created item or null upon failure.
public NaviItem InsertBefore(NaviItem before, EvasObject content, string title)
{
return InsertBefore(before, content, title, null);
}
///
/// Insert a new item into the naviframe before item.
///
/// The item which the new item is inserted before.
/// The main content object. The name of content part is "elm.swallow.content".
/// The current item title. null would be default.
/// The current item style name. null would be default.
/// The created item or null upon failure.
public NaviItem InsertBefore(NaviItem before, EvasObject content, string title, string style)
{
IntPtr item = Interop.Elementary.elm_naviframe_item_insert_before(RealHandle, before, title, IntPtr.Zero, IntPtr.Zero, content, null);
NaviItem naviItem = NaviItem.FromNativeHandle(item, content);
int idx = _itemStack.IndexOf(before);
_itemStack.Insert(idx, naviItem);
naviItem.Popped += ItemPoppedHandler;
return naviItem;
}
///
/// Insert a new item into the naviframe after item.
/// The title is "" and the style is null.
///
/// The item which the new item is inserted after.
/// The main content object. The name of content part is "elm.swallow.content".
/// The created item or null upon failure.
public NaviItem InsertAfter(NaviItem after, EvasObject content)
{
return InsertAfter(after, content, "");
}
///
/// Insert a new item into the naviframe after item.
/// The style is null.
///
/// The item which the new item is inserted after.
/// The main content object. The name of content part is "elm.swallow.content".
/// The current item title. null would be default.
/// The created item or null upon failure.
public NaviItem InsertAfter(NaviItem after, EvasObject content, string title)
{
return InsertAfter(after, content, title, null);
}
///
/// Insert a new item into the naviframe after item.
///
/// The item which the new item is inserted after.
/// The main content object. The name of content part is "elm.swallow.content".
/// The current item title. null would be default.
/// The current item style name. null would be default.
/// The created item or null upon failure.
public NaviItem InsertAfter(NaviItem after, EvasObject content, string title, string style)
{
IntPtr item = Interop.Elementary.elm_naviframe_item_insert_after(RealHandle, after, title, IntPtr.Zero, IntPtr.Zero, content, null);
NaviItem naviItem = NaviItem.FromNativeHandle(item, content);
int idx = _itemStack.IndexOf(after);
_itemStack.Insert(idx + 1, naviItem);
naviItem.Popped += ItemPoppedHandler;
return naviItem;
}
///
/// Pop an item that is on top of the stack.
///
public void Pop()
{
Interop.Elementary.elm_naviframe_item_pop(RealHandle);
}
///
/// Creates a widget handle.
///
/// Parent EvasObject
/// Handle IntPtr
protected override IntPtr CreateHandle(EvasObject parent)
{
IntPtr handle = Interop.Elementary.elm_layout_add(parent);
Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
RealHandle = Interop.Elementary.elm_naviframe_add(handle);
Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
return handle;
}
void ItemPoppedHandler(object sender, EventArgs e)
{
NaviItem item = sender as NaviItem;
if (item == null)
return;
_itemStack.Remove(item);
Popped?.Invoke(this, new NaviframeEventArgs() { Content = item.Content });
}
}
}