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 /* LCOV_EXCL_START */
101 widget_app_lifecycle_callback_s callback = {
102 create : [](void *user_data) -> widget_class_h {
103 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
105 widget_instance_lifecycle_callback_s callback = {
106 create : [](widget_context_h context, bundle* content, int w, int h,
107 void* user_data) -> int {
108 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
109 auto i = b->factory_->Create(context);
110 i->OnCreate(tizen_base::Bundle(content, false, false), w, h);
111 b->instances_.push_back(std::move(i));
114 destroy : [](widget_context_h context,
115 widget_app_destroy_type_e reason, bundle* content,
116 void* user_data) -> int {
117 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
119 for (auto& i : b->instances_) {
120 if (i->GetHandle() == context) {
121 tizen_base::Bundle bd(content, false, false);
123 static_cast<InstanceBase::DestroyType>(reason), &bd);
124 b->instances_.remove(i);
131 pause : [](widget_context_h context, void* user_data) -> int {
132 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
133 auto* i = b->FindInst(context);
139 resume : [](widget_context_h context, void* user_data) -> int {
140 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
141 auto* i = b->FindInst(context);
147 resize : [](widget_context_h context, int w, int h,
148 void* user_data) -> int {
149 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
150 auto* i = b->FindInst(context);
156 update : [](widget_context_h context, bundle* content, int force,
157 void* user_data) -> int {
158 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
159 auto* i = b->FindInst(context);
162 i->OnUpdate(tizen_base::Bundle(content), force);
166 auto h = widget_app_class_create(callback, user_data);
169 terminate : [](void* user_data) {
170 WidgetAppBase* b = static_cast<WidgetAppBase*>(user_data);
176 return widget_app_main(argc, argv, &callback, this);
179 void Exit() noexcept {
184 InstanceBase* FindInst(widget_context_h h) {
185 for (auto& i : instances_) {
186 if (i->GetHandle() == h) {
194 std::unique_ptr<InstanceBase::Factory> factory_;
195 std::list<std::unique_ptr<InstanceBase>> instances_;
198 } // namespace tizen_appfw
200 #endif // TIZEN_APPFW_WIDGET_APP_HPP_