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/splash_screen.h"
23 #include <Ecore_Wayland.h>
24 #include <Evas_Legacy.h>
26 #include "common/logger.h"
27 #include "runtime/browser/native_window.h"
28 #include "wgt_manifest_handlers/splash_screen_handler.h"
30 using ScreenOrientation = runtime::NativeWindow::ScreenOrientation;
34 enum class BorderOption { REPEAT = 1, STRETCH, ROUND };
36 wgt::parse::ScreenOrientation ChooseOrientation(
37 const std::map<wgt::parse::ScreenOrientation,
38 wgt::parse::SplashScreenData>& splash_map,
39 ScreenOrientation screen_orientation) {
40 auto orientation_pair = splash_map.end();
42 if (screen_orientation ==
43 runtime::NativeWindow::ScreenOrientation::PORTRAIT_PRIMARY) {
45 splash_map.find(wgt::parse::ScreenOrientation::PORTRAIT);
48 splash_map.find(wgt::parse::ScreenOrientation::LANDSCAPE);
50 if (orientation_pair == splash_map.end())
51 orientation_pair = splash_map.find(wgt::parse::ScreenOrientation::AUTO);
53 if (orientation_pair != splash_map.end()) return orientation_pair->first;
54 return wgt::parse::ScreenOrientation::NONE;
57 bool ParseImageBorder(const std::vector<std::string>& borders,
58 std::vector<int>* border_values,
59 std::vector<BorderOption>* border_options) {
60 std::map<std::string, BorderOption> scaling_string;
61 scaling_string["repeat"] = BorderOption::REPEAT;
62 scaling_string["stretch"] = BorderOption::STRETCH;
63 scaling_string["round"] = BorderOption::ROUND;
65 for (const auto& border : borders) {
66 std::string::size_type px_index = border.find("px");
67 if (px_index != std::string::npos) {
68 std::string border_value(border.begin(), border.begin() + px_index);
69 border_values->push_back(std::atoi(border_value.c_str()));
71 for (const auto& border_string_val : scaling_string) {
72 std::string::size_type index = border.find(border_string_val.first);
73 if (index != std::string::npos) {
74 border_options->push_back(border_string_val.second);
79 LOGGER(DEBUG) << "Image border values:";
80 for (const auto& border_value : *border_values) {
81 LOGGER(DEBUG) << border_value;
83 LOGGER(DEBUG) << "Image border scaling values:";
84 for (const auto& border_option : *border_options) {
85 LOGGER(DEBUG) << static_cast<int>(border_option);
88 return !border_values->empty() && !border_options->empty();
95 SplashScreen::SplashScreen(
96 runtime::NativeWindow* window,
97 std::shared_ptr<const wgt::parse::SplashScreenInfo> ss_info,
98 const std::string& app_path)
102 background_(nullptr),
103 background_image_(nullptr),
105 LOGGER(DEBUG) << "start of create splash screen";
106 if (ss_info == nullptr) return;
107 auto splash_map = ss_info->splash_screen_data();
108 auto used_orientation =
109 ChooseOrientation(splash_map, window->natural_orientation());
110 if (used_orientation == wgt::parse::ScreenOrientation::NONE) return;
112 auto dimensions = GetDimensions();
114 SetBackground(splash_map[used_orientation], window->evas_object(),
115 dimensions, app_path);
116 SetImage(splash_map[used_orientation], window->evas_object(),
117 dimensions, app_path);
122 void SplashScreen::HideSplashScreen(HideReason reason) {
123 if (!is_active_) return;
124 if (reason == HideReason::RENDERED &&
125 ss_info_->ready_when() != wgt::parse::ReadyWhen::FIRSTPAINT) {
128 if (reason == HideReason::LOADFINISHED &&
129 ss_info_->ready_when() != wgt::parse::ReadyWhen::COMPLETE) {
132 if (reason == HideReason::CUSTOM &&
133 ss_info_->ready_when() != wgt::parse::ReadyWhen::CUSTOM) {
137 evas_object_hide(background_);
138 evas_object_hide(image_);
139 evas_object_del(background_);
140 evas_object_del(image_);
141 background_ = nullptr;
146 std::pair<int, int> SplashScreen::GetDimensions() {
148 ecore_wl_screen_size_get(&w, &h);
149 evas_object_resize(background_, w, h);
150 return std::make_pair(w, h);
153 void SplashScreen::SetBackground(
154 const wgt::parse::SplashScreenData& splash_data, Evas_Object* parent,
155 const SplashScreenBound& bound, const std::string& app_path) {
156 background_ = elm_bg_add(parent);
157 if (!background_) return;
158 evas_object_resize(background_, bound.first, bound.second);
160 if (splash_data.background_color != nullptr) {
161 elm_bg_color_set(background_,
162 splash_data.background_color->red,
163 splash_data.background_color->green,
164 splash_data.background_color->blue);
167 std::vector<int> border_values;
168 std::vector<BorderOption> border_options;
170 if (!splash_data.background_image.empty() &&
172 splash_data.image_border, &border_values, &border_options)) {
173 const std::string& background_image_path =
174 splash_data.background_image.front();
176 background_image_ = elm_image_add(background_);
177 evas_object_image_file_set(background_image_,
178 (app_path + background_image_path).c_str(),
180 elm_image_aspect_fixed_set(background_image_, 0);
181 evas_object_image_border_center_fill_set(background_image_,
182 EVAS_BORDER_FILL_DEFAULT);
184 evas_object_resize(background_image_, bound.first, bound.second);
186 int border_l, border_r, border_t, border_b;
187 switch (border_values.size()) {
189 border_l = border_r = border_t = border_b = -border_values[0];
192 border_t = border_b = border_values[0];
193 border_l = border_r = border_values[1];
196 border_t = border_values[0];
197 border_r = border_values[1];
198 border_b = border_values[2];
199 border_l = border_values[3];
202 border_l = border_r = border_t = border_b = 0;
204 evas_object_image_border_set(background_image_,
209 // TODO(a.szulakiewi): add scaling of horizontal and vertical borders
212 evas_object_show(background_);
213 evas_object_show(background_image_);
216 void SplashScreen::SetImage(
217 const wgt::parse::SplashScreenData& splash_data, Evas_Object* parent,
218 const SplashScreenBound& bound, const std::string& app_path) {
219 if (!background_) return;
220 image_ = elm_image_add(background_);
223 const std::string& image_path = splash_data.image.front();
224 elm_image_file_set(image_, (app_path + image_path).c_str(), NULL);
225 evas_object_resize(image_, bound.first, bound.second);
226 evas_object_show(image_);
228 } // namespace runtime