summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/Naviframe.cs
blob: 8d31756582f69eebb1de7d42a9f7faaa25afde9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * 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
{
    /// <summary>
    /// The NaviframeEventArgs is a event args class for navi frame.
    /// Inherits EventArgs
    /// </summary>
    public class NaviframeEventArgs : EventArgs
    {
        /// <summary>
        /// Sets or gets the content object. The name of content part is "elm.swallow.content".
        /// </summary>
        public EvasObject Content { get; set; }
    }
    /// <summary>
    /// Naviframe is a widget to stands for navigation frame. It's a views manager for applications.
    /// Inherits Widget
    /// </summary>
    public class Naviframe : Widget
    {
        SmartEvent _transitionFinished;
        readonly List<NaviItem> _itemStack = new List<NaviItem>();

        /// <summary>
        /// Creates and initializes a new instance of Naviframe class.
        /// </summary>
        /// <param name="parent">The parent is a given container which will be attached by Naviframe as a child. It's <see cref="EvasObject"/> type.</param>
        public Naviframe(EvasObject parent) : base(parent)
        {
            _transitionFinished = new SmartEvent(this, this.RealHandle, "transition,finished");
            _transitionFinished.On += (s, e) => AnimationFinished?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        /// Popped will be triggered when NaviItem is removed.
        /// </summary>
        /// <remarks>
        /// 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
        /// </remarks>
        public event EventHandler<NaviframeEventArgs> Popped;

        /// <summary>
        /// AnimationFinished will be triggered when animation is finished.
        /// </summary>
        public event EventHandler AnimationFinished;

        /// <summary>
        /// Gets the list of navi item
        /// </summary>
        public IReadOnlyList<NaviItem> NavigationStack
        {
            get { return _itemStack; }
        }

        /// <summary>
        /// Sets or gets the the preserve content objects when items are popped.
        /// </summary>
        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);
            }
        }

        /// <summary>
        /// Sets or gets whether the default back button is enabled
        /// </summary>
        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);
            }
        }

        /// <summary>
        /// Push a new item to the top of the naviframe stack and show it.
        /// The title and style are null.
        /// </summary>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <returns>The created item or null upon failure.</returns>
        public NaviItem Push(EvasObject content)
        {
            return Push(content, null);
        }

        /// <summary>
        /// Push a new item to the top of the naviframe stack and show it.
        /// The style are null.
        /// </summary>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <param name="title">The current item title. null would be default.</param>
        /// <returns></returns>
        public NaviItem Push(EvasObject content, string title)
        {
            return Push(content, title, null);
        }

        /// <summary>
        /// Push a new item to the top of the naviframe stack and show it.
        /// </summary>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <param name="title">The current item title. null would be default.</param>
        /// <param name="style">The current item style name. null would be default.</param>
        /// <returns>The created item or null upon failure.</returns>
        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;
        }

        /// <summary>
        /// Insert a new item into the naviframe before item.
        /// The title is "" and the style is null.
        /// </summary>
        /// <param name="before">The item which the new item is inserted before.</param>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <returns>The created item or null upon failure.</returns>
        public NaviItem InsertBefore(NaviItem before, EvasObject content)
        {
            return InsertBefore(before, content, "");
        }

        /// <summary>
        /// Insert a new item into the naviframe before item.
        /// The style is null.
        /// </summary>
        /// <param name="before">The item which the new item is inserted before.</param>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <param name="title">The current item title. null would be default.</param>
        /// <returns>The created item or null upon failure.</returns>
        public NaviItem InsertBefore(NaviItem before, EvasObject content, string title)
        {
            return InsertBefore(before, content, title, null);
        }

        /// <summary>
        /// Insert a new item into the naviframe before item.
        /// </summary>
        /// <param name="before">The item which the new item is inserted before.</param>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <param name="title">The current item title. null would be default.</param>
        /// <param name="style">The current item style name. null would be default.</param>
        /// <returns>The created item or null upon failure.</returns>
        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;
        }

        /// <summary>
        /// Insert a new item into the naviframe after item.
        /// The title is "" and the style is null.
        /// </summary>
        /// <param name="after">The item which the new item is inserted after.</param>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <returns>The created item or null upon failure.</returns>
        public NaviItem InsertAfter(NaviItem after, EvasObject content)
        {
            return InsertAfter(after, content, "");
        }

        /// <summary>
        /// Insert a new item into the naviframe after item.
        /// The style is null.
        /// </summary>
        /// <param name="after">The item which the new item is inserted after.</param>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <param name="title">The current item title. null would be default.</param>
        /// <returns>The created item or null upon failure.</returns>
        public NaviItem InsertAfter(NaviItem after, EvasObject content, string title)
        {
            return InsertAfter(after, content, title, null);
        }

        /// <summary>
        /// Insert a new item into the naviframe after item.
        /// </summary>
        /// <param name="after">The item which the new item is inserted after.</param>
        /// <param name="content">The main content object. The name of content part is "elm.swallow.content".</param>
        /// <param name="title">The current item title. null would be default.</param>
        /// <param name="style">The current item style name. null would be default.</param>
        /// <returns>The created item or null upon failure.</returns>
        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;
        }

        /// <summary>
        /// Pop an item that is on top of the stack.
        /// </summary>
        public void Pop()
        {
            Interop.Elementary.elm_naviframe_item_pop(RealHandle);
        }

        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 });
        }
    }
}