[ElmSharp] Adds PeekInstance to PreloadedWindow (#1591)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / PreloadedWindow.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.ComponentModel;
19 using System.Reflection;
20
21 namespace ElmSharp
22 {
23     /// <summary>
24     /// Pre-created window which prepares features that takes time in advance.
25     /// </summary>
26     [EditorBrowsable(EditorBrowsableState.Never)]
27     public class PreloadedWindow : Window
28     {
29         static PreloadedWindow s_precreated;
30
31         internal PreloadedWindow(bool useBaseLayout=true) : base("PreloadWindow")
32         {
33             s_precreated = this;
34             if (useBaseLayout)
35                 InitializeBaseLayout();
36             WarmupWidgets();
37             BackButtonPressed += DummyHandler;
38             BackButtonPressed -= DummyHandler;
39             void DummyHandler(object sender, System.EventArgs e) { }
40
41             if (Elementary.GetProfile() == "wearable")
42             {
43                 WarmupWearableWidgets();
44             }
45         }
46
47         public Layout BaseLayout
48         {
49             get;
50             protected set;
51         }
52
53         public Conformant BaseConformant
54         {
55             get;
56             protected set;
57         }
58
59         public object BaseCircleSurface
60         {
61             get;
62             set;
63         }
64
65
66         public void InitializeBaseLayout()
67         {
68             var conformant = new Conformant(this);
69             BaseConformant = conformant;
70             conformant.Show();
71
72             var layout = new Layout(conformant);
73             layout.SetTheme("layout", "application", "default");
74             layout.Show();
75
76             BaseLayout = layout;
77             conformant.SetContent(BaseLayout);
78         }
79
80         public void WarmupWidgets()
81         {
82             new Entry(this).Unrealize();
83             new Scroller(this).Unrealize();
84             new Box(this).Unrealize();
85             new Label(this).Unrealize();
86             new GenList(this).Unrealize();
87             new Button(this).Unrealize();
88             new Check(this).Unrealize();
89             new Naviframe(this).Unrealize();
90             new Slider(this).Unrealize();
91             new Spinner(this).Unrealize();
92             new ProgressBar(this).Unrealize();
93             new GestureLayer(this).Unrealize();
94             new Polygon(this).Unrealize();
95             new Image(this).Unrealize();
96             //TODO: Consider to call Image.LoadAsync()
97         }
98
99         public void WarmupWearableWidgets()
100         {
101             try
102             {
103                 Assembly assem = Assembly.Load("ElmSharp.Wearable");
104                 var type = assem.GetType("ElmSharp.Wearable.Preload");
105                 type.GetMethod("WarmupWidgets", BindingFlags.Public | BindingFlags.Static).Invoke(null, new object[] { this });
106             }
107             catch (Exception e)
108             {
109                 Console.WriteLine(e.ToString());
110                 Console.WriteLine("Fail to preload ElmSharp.Wearable");
111             }
112         }
113
114         public static PreloadedWindow GetInstance()
115         {
116             var instance = s_precreated;
117             s_precreated = null;
118             return instance;
119         }
120
121         public static PreloadedWindow PeekInstance()
122         {
123             return s_precreated;
124         }
125     }
126 }