2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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 #include "runtime/browser/native_window.h"
19 #include <Ecore_Wayland.h>
20 #include <ewk_chromium.h>
23 #include "common/logger.h"
28 const char* kEdjePath = "/usr/share/edje/xwalk/xwalk_tizen.edj";
29 const char* kWinowRotationEventKey = "wm,rotation,changed";
30 const char* kWinowFocusedEventKey = "focused";
31 const char* kWinowUnfocusedEventKey = "unfocused";
35 NativeWindow::NativeWindow()
37 window_type_(Type::NORMAL),
45 NativeWindow::~NativeWindow() {
47 evas_object_del(window_);
50 void NativeWindow::Initialize() {
52 LOGGER(DEBUG) << "already initialized";
56 window_ = CreateWindowInternal();
57 elm_win_conformant_set(window_, EINA_TRUE);
59 ecore_wl_screen_size_get(&w, &h);
60 evas_object_resize(window_, w, h);
61 elm_win_autodel_set(window_, EINA_TRUE);
62 evas_object_smart_callback_add(window_, "delete,request",
63 DidDeleteRequested, this);
64 evas_object_smart_callback_add(window_, "profile,changed",
65 DidProfileChanged, this);
67 #define EVAS_SIZE_EXPAND_FILL(obj) \
68 evas_object_size_hint_weight_set(obj, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); \
69 evas_object_size_hint_align_set(obj, EVAS_HINT_FILL, EVAS_HINT_FILL);
72 Evas_Object* bg = evas_object_rectangle_add(evas_object_evas_get(window_));
73 evas_object_color_set(bg, 0, 0, 0, 255);
74 EVAS_SIZE_EXPAND_FILL(bg);
75 elm_win_resize_object_add(window_, bg);
76 evas_object_render_op_set(bg, EVAS_RENDER_BLEND);
80 Evas_Object* conformant = elm_conformant_add(window_);
81 EVAS_SIZE_EXPAND_FILL(conformant);
82 elm_win_resize_object_add(window_, conformant);
83 evas_object_show(conformant);
86 Evas_Object* top_layout = elm_layout_add(conformant);
87 elm_layout_file_set(top_layout, kEdjePath, "web-application");
88 EVAS_SIZE_EXPAND_FILL(top_layout);
89 elm_object_content_set(conformant, top_layout);
90 evas_object_show(top_layout);
93 Evas_Object* focus = elm_button_add(top_layout);
94 elm_theme_extension_add(NULL, kEdjePath);
95 elm_object_style_set(focus, "wrt");
96 elm_object_part_content_set(top_layout, "elm.swallow.content", focus);
97 EVAS_SIZE_EXPAND_FILL(focus);
98 elm_access_object_unregister(focus);
99 evas_object_show(focus);
103 auto focus_callback = [](void* user_data,
106 NativeWindow* window = static_cast<NativeWindow*>(user_data);
107 window->DidFocusChanged(true);
109 auto unfocus_callback = [](void* user_data,
112 NativeWindow* window = static_cast<NativeWindow*>(user_data);
113 window->DidFocusChanged(false);
116 evas_object_smart_callback_add(focus,
117 kWinowFocusedEventKey,
120 evas_object_smart_callback_add(focus,
121 kWinowUnfocusedEventKey,
126 auto rotation_callback = [](void* user_data,
129 NativeWindow* window = static_cast<NativeWindow*>(user_data);
130 int degree = elm_win_rotation_get(obj);
131 window->DidRotation(degree);
133 evas_object_smart_callback_add(window_,
134 kWinowRotationEventKey,
139 natural_orientation_ = ScreenOrientation::LANDSCAPE_PRIMARY;
141 natural_orientation_ = ScreenOrientation::PORTRAIT_PRIMARY;
147 void NativeWindow::DidDeleteRequested(void* /*data*/,
148 Evas_Object* /*obj*/, void* /*event_info*/) {
149 LOGGER(DEBUG) << "didDeleteRequested";
153 void NativeWindow::DidProfileChanged(void* /*data*/,
154 Evas_Object* /*obj*/, void* /*event_info*/) {
155 LOGGER(DEBUG) << "didProfileChanged";
158 Evas_Object* NativeWindow::evas_object() const {
162 void NativeWindow::SetContent(Evas_Object* content) {
164 // If any object was already set as a content object in the same part,
165 // the previous object will be deleted automatically with this call.
166 // If the content is NULL, this call will just delete the previous object.
167 // If the If you wish to preserve it,
168 // issue elm_object_part_content_unset() on it first.
170 evas_object_show(content);
172 elm_object_part_content_unset(focus_, "elm.swallow.content"));
173 elm_object_part_content_set(focus_, "elm.swallow.content", content);
174 ewk_view_focus_set(focus_, EINA_TRUE);
177 // attached webview was resized by evas_norender API
178 evas_norender(evas_object_evas_get(window_));
181 void NativeWindow::DidRotation(int degree) {
183 auto it = handler_table_.begin();
184 for ( ; it != handler_table_.end(); ++it) {
189 void NativeWindow::DidFocusChanged(bool got) {
190 if (content_ != NULL) {
191 ewk_view_focus_set(content_, got ? EINA_TRUE : EINA_FALSE);
195 int NativeWindow::AddRotationHandler(RotationHandler handler) {
196 int id = handler_id_++;
197 handler_table_[id] = handler;
201 void NativeWindow::RemoveRotationHandler(int id) {
202 handler_table_.erase(id);
205 void NativeWindow::SetRotationLock(int degree) {
207 rotation_ = degree % 360;
208 elm_win_wm_rotation_preferred_rotation_set(window_, rotation_);
211 void NativeWindow::SetRotationLock(ScreenOrientation orientation) {
212 int portrait_natural_angle[] = {
213 0, // PORTRAIT_PRIMARY
214 180, // PORTRAIT_SECONDARY
215 270, // LANDSCAPE_PRIMARY
216 90, // LANDSCAPE_SECONDARY
220 int landscape_natural_angle[] = {
221 270, // PORTRAIT_PRIMARY
222 90, // PORTRAIT_SECONDARY
223 0, // LANDSCAPE_PRIMARY
224 180, // LANDSCAPE_SECONDARY
228 auto& convert_table =
229 natural_orientation_ == ScreenOrientation::PORTRAIT_PRIMARY ?
230 portrait_natural_angle :
231 landscape_natural_angle;
232 SetRotationLock(convert_table[static_cast<int>(orientation)]);
236 void NativeWindow::SetAutoRotation() {
237 elm_win_wm_rotation_preferred_rotation_set(window_, -1);
238 if (elm_win_wm_rotation_supported_get(window_)) {
239 const int rotation[4] = {0, 90, 180, 270};
240 elm_win_wm_rotation_available_rotations_set(window_, rotation, 4);
242 rotation_ = elm_win_rotation_get(window_);
245 void NativeWindow::Show() {
246 evas_object_show(window_);
249 void NativeWindow::Active() {
250 elm_win_activate(window_);
253 void NativeWindow::InActive() {
254 elm_win_lower(window_);
257 void NativeWindow::FullScreen(bool enable) {
258 elm_win_indicator_opacity_set(window_,
259 enable ? ELM_WIN_INDICATOR_TRANSPARENT : ELM_WIN_INDICATOR_OPAQUE);
262 } // namespace runtime