2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef TIZEN_APPFW_WIDGET_APP_HPP_
18 #define TIZEN_APPFW_WIDGET_APP_HPP_
20 #include <widget_app.h>
21 #include <widget_app_efl.h>
27 #include <app_common.hpp>
28 #include <bundle_cpp.h>
30 namespace tizen_appfw {
32 class WidgetAppBase : public app_common::AppBase<
33 decltype(widget_app_add_event_handler)*,
34 decltype(widget_app_remove_event_handler)*> {
36 using Remover = decltype(widget_app_remove_event_handler)*;
39 : AppBase(widget_app_add_event_handler,
40 widget_app_remove_event_handler) {}
42 virtual void OnCreate() {}
43 virtual void OnTerminate() {}
50 virtual std::unique_ptr<InstanceBase> Create(widget_context_h h) = 0;
53 enum class DestroyType {
58 InstanceBase(widget_context_h h) : handle_(h) {}
59 virtual ~InstanceBase() = default;
61 virtual void OnCreate(const tizen_base::Bundle& content, int w, int h) = 0;
62 virtual void OnDestroy(DestroyType reason, tizen_base::Bundle* content) = 0;
63 virtual void OnPause() = 0;
64 virtual void OnResume() = 0;
65 virtual void OnResize(int w, int h) = 0;
66 virtual void OnUpdate(const tizen_base::Bundle& content, bool force) = 0;
67 widget_context_h GetHandle() const {
72 widget_app_terminate_context(handle_);
75 std::string GetId() const {
76 return std::string(widget_app_get_id(handle_));
79 void SetContextInfo(const tizen_base::Bundle& info) {
80 widget_app_context_set_content_info(handle_, info.GetHandle());
83 void SetTitle(const std::string& title) {
84 widget_app_context_set_title(handle_, title.c_str());
87 Evas_Object* GetElmWin() const {
88 Evas_Object* win = nullptr;
89 widget_app_get_elm_win(handle_, &win);
94 widget_context_h handle_;
97 int Run(int argc, char** argv,
98 std::unique_ptr<InstanceBase::Factory> factory) {
99 factory_ = std::move(factory);
100 widget_app_lifecycle_callback_s callback = {
101 create : [](void *user_data) -> widget_class_h {
102 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
104 widget_instance_lifecycle_callback_s callback = {
105 create : [](widget_context_h context, bundle* content, int w, int h,
106 void* user_data) -> int {
107 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
108 auto i = b->factory_->Create(context);
109 i->OnCreate(tizen_base::Bundle(content, false, false), w, h);
110 b->instances_.push_back(std::move(i));
113 destroy : [](widget_context_h context,
114 widget_app_destroy_type_e reason, bundle* content,
115 void* user_data) -> int {
116 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
118 for (auto& i : b->instances_) {
119 if (i->GetHandle() == context) {
120 tizen_base::Bundle bd(content, false, false);
122 static_cast<InstanceBase::DestroyType>(reason), &bd);
123 b->instances_.remove(i);
130 pause : [](widget_context_h context, void* user_data) -> int {
131 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
132 auto* i = b->FindInst(context);
138 resume : [](widget_context_h context, void* user_data) -> int {
139 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
140 auto* i = b->FindInst(context);
146 resize : [](widget_context_h context, int w, int h,
147 void* user_data) -> int {
148 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
149 auto* i = b->FindInst(context);
155 update : [](widget_context_h context, bundle* content, int force,
156 void* user_data) -> int {
157 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
158 auto* i = b->FindInst(context);
161 i->OnUpdate(tizen_base::Bundle(content), force);
165 auto h = widget_app_class_create(callback, user_data);
168 terminate : [](void* user_data) {
169 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
174 return widget_app_main(argc, argv, &callback, this);
177 void Exit() noexcept {
182 InstanceBase* FindInst(widget_context_h h) {
183 for (auto& i : instances_) {
184 if (i->GetHandle() == h) {
192 std::unique_ptr<InstanceBase::Factory> factory_;
193 std::list<std::unique_ptr<InstanceBase>> instances_;
196 } // namespace tizen_appfw
198 #endif // TIZEN_APPFW_WIDGET_APP_HPP_