!1100 增加自定义动画transitionController及transitionContext模块

Merge pull request !1100 from chenhaiying/m5
This commit is contained in:
openharmony_ci
2022-07-11 03:32:18 +00:00
committed by Gitee
17 changed files with 619 additions and 84 deletions
+2 -2
View File
@@ -422,11 +422,11 @@ HWTEST_F(DisplayPowerTest, window_life_cycle_001, Function | MediumTest | Level2
DisplayManager::GetInstance().SuspendBegin(PowerStateChangeReason::POWER_BUTTON);
usleep(SLEEP_TIME_IN_US);
ASSERT_EQ(false, window->GetShowState());
ASSERT_EQ(false, window->GetWindowState() == WindowState::STATE_SHOWN);
DisplayManager::GetInstance().NotifyDisplayEvent(DisplayEvent::UNLOCK);
usleep(SLEEP_TIME_IN_US);
ASSERT_EQ(true, window->GetShowState());
ASSERT_EQ(true, window->GetWindowState() == WindowState::STATE_SHOWN);
window->Destroy();
}
+1 -1
View File
@@ -173,7 +173,7 @@ public:
virtual const std::string& GetWindowName() const = 0;
virtual uint32_t GetWindowId() const = 0;
virtual uint32_t GetWindowFlags() const = 0;
virtual bool GetShowState() const = 0;
virtual WindowState GetWindowState() const = 0;
virtual WMError SetFocusable(bool isFocusable) = 0;
virtual bool GetFocusable() const = 0;
virtual WMError SetTouchable(bool isTouchable) = 0;
+11
View File
@@ -96,6 +96,17 @@ enum class WindowBlurLevel : uint32_t {
WINDOW_BLUR_HIGH
};
enum class WindowState : uint32_t {
STATE_INITIAL,
STATE_CREATED,
STATE_SHOWN,
STATE_HIDDEN,
STATE_FROZEN,
STATE_DESTROYED,
STATE_BOTTOM = STATE_DESTROYED,
STATE_UNFROZEN,
};
enum class WMError : int32_t {
WM_OK = 0,
WM_DO_NOTHING,
@@ -42,6 +42,7 @@ config("window_native_kit_config") {
ohos_shared_library("window_native_kit") {
sources = [
"window_napi/js_transition_controller.cpp",
"window_napi/js_window.cpp",
"window_napi/js_window_listener.cpp",
"window_napi/js_window_register_manager.cpp",
+91 -21
View File
@@ -617,6 +617,20 @@ declare namespace window {
*/
hide(): Promise<void>;
/**
* hide window with animation.
* @since 9
* @systemapi
*/
hideWithAnimation(callback: AsyncCallback<void>): void;
/**
* hide window with animation.
* @since 9
* @systemapi
*/
hideWithAnimation(): Promise<void>;
/**
* show sub window.
* @since 7
@@ -624,11 +638,25 @@ declare namespace window {
show(callback: AsyncCallback<void>): void;
/**
* show sub window.
* show window.
* @since 7
*/
show(): Promise<void>;
/**
* show window with animation.
* @since 9
* @systemapi
*/
showWithAnimation(callback: AsyncCallback<void>): void;
/**
* show window with animation.
* @since 9
* @systemapi
*/
showWithAnimation(): Promise<void>;
/**
* Destroy the sub window.
* @since 7
@@ -1072,33 +1100,75 @@ declare namespace window {
*/
setForbidSplitMove(isForbidSplitMove: boolean): Promise<void>;
/**
* Sets opacity of window
* @param opacity Interval is 0.f-1.f.
* @systemapi
* @since 9
*/
* Sets opacity of window
* @param opacity Interval is 0.f-1.f.
* @systemapi
* @since 9
*/
setOpacitySync(opacity: number): void;
/**
* Sets scale options of window.
* @param scaleOptions scale param of window.
* @systemapi
* @since 9
*/
* Sets scale options of window.
* @param scaleOptions scale param of window.
* @systemapi
* @since 9
*/
setScaleSync(scaleOptions: ScaleOptions): void;
/**
* Sets rotate options of window.
* @param rotateOptions rotate param of window.
* @systemapi
* @since 9
*/
* Sets rotate options of window.
* @param rotateOptions rotate param of window.
* @systemapi
* @since 9
*/
setRotateSync(rotateOptions: RotateOptions): void;
/**
* Sets whether is transparent or not.
* @param translateOptions translate param of window.
* @systemapi
* @since 9
*/
* Sets whether is transparent or not.
* @param translateOptions translate param of window.
* @systemapi
* @since 9
*/
setTranslateSync(translateOptions: TranslateOptions): void;
/**
* Get Transition Controller.
* @systemapi
* @since 9
*/
getTransitionControllerSync(): TransitionController;
}
/**
* Transition Context
* @syscap SystemCapability.WindowManager.WindowManager.Core
* @systempi
* @since 9
*/
interface TransitionContext {
/**
* The target window with animation
*/
toWindow: Window
/**
* Set complete state of animation transition
* @param isCompleted is Completed if true, or not if false.
*/
completeTransition(isCompleted: boolean): void;
}
/**
* Transition Controller
* @syscap SystemCapability.WindowManager.WindowManager.Core
* @systempi
* @since 9
*/
interface TransitionController {
/**
* Animation configuration when showing window
* @param context transition Context.
*/
animationForShown(context: TransitionContext): void;
/**
* Animation configuration when hiding window
* @param context transition context.
*/
animationForHidden(context: TransitionContext): void;
}
enum WindowStageEventType {
@@ -0,0 +1,250 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "js_transition_controller.h"
#include "js_runtime_utils.h"
#include "window.h"
#include "window_helper.h"
#include "window_manager_hilog.h"
#include "window_option.h"
namespace OHOS {
namespace Rosen {
using namespace AbilityRuntime;
namespace {
constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "JsTransitionController"};
}
static int jsTransCxtCtorCnt = 0;
static int jsTransCxtDtorCnt = 0;
static int jsTransCtrlCtorCnt = 0;
static int jsTransCtrlDtorCnt = 0;
JsTransitionContext::JsTransitionContext(sptr<Window> window, bool isShownTransContext)
: windowToken_(window), isShownTransContext_(isShownTransContext)
{
WLOGFI("[NAPI] JsTransitionContext constructorCnt: %{public}d", ++jsTransCxtCtorCnt);
}
JsTransitionContext::~JsTransitionContext()
{
WLOGFI("[NAPI] ~JsTransitionContext deConstructorCnt: %{public}d", ++jsTransCxtDtorCnt);
}
void JsTransitionContext::Finalizer(NativeEngine* engine, void* data, void* hint)
{
WLOGFI("[NAPI]JsTransitionContext::Finalizer");
std::unique_ptr<JsTransitionContext>(static_cast<JsTransitionContext*>(data));
}
NativeValue* JsTransitionContext::CompleteTransition(NativeEngine* engine, NativeCallbackInfo* info)
{
WLOGFI("[NAPI]CompleteTransition");
JsTransitionContext* me = CheckParamsAndGetThis<JsTransitionContext>(engine, info);
return (me != nullptr) ? me->OnCompleteTransition(*engine, *info) : nullptr;
}
NativeValue* JsTransitionContext::OnCompleteTransition(NativeEngine& engine, NativeCallbackInfo& info)
{
WLOGFI("[NAPI]OnCompleteTransition");
WMError errCode = WMError::WM_OK;
if (info.argc < 1 || info.argc > 2) { // 2: maximum params
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
errCode = WMError::WM_ERROR_INVALID_PARAM;
}
bool transitionCompleted = false;
if (errCode == WMError::WM_OK && !ConvertFromJsValue(engine, info.argv[0], transitionCompleted)) {
WLOGFE("[NAPI]Failed to convert parameter to transitionCompleted");
errCode = WMError::WM_ERROR_INVALID_PARAM;
}
AsyncTask::CompleteCallback complete =
[weakWindow = windowToken_, errCode, transitionCompleted, isShownTransContext = isShownTransContext_](
NativeEngine& engine, AsyncTask& task, int32_t status) {
HandleScope handleScope(engine);
auto window = weakWindow.promote();
if (window == nullptr || errCode != WMError::WM_OK) {
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
WLOGFE("[NAPI]window is nullptr or get invalid param");
return;
}
WMError ret = WMError::WM_OK;
if (!isShownTransContext) {
window->UpdateSurfaceNodeAfterCustomAnimation(false); // remove from rs tree after animation
if (!transitionCompleted) {
ret = window->Show(); // hide success
}
} else {
if (!transitionCompleted) {
ret = window->Hide(); // hide success
}
}
if (ret != WMError::WM_OK) {
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window destroy failed"));
return;
}
WLOGFI("[NAPI]Window [%{public}u, %{public}s] CompleteTransition %{public}d end",
window->GetWindowId(), window->GetWindowName().c_str(), transitionCompleted);
task.Resolve(engine, engine.CreateUndefined());
};
NativeValue* lastParam = (info.argc <= 1) ? nullptr :
(info.argv[1]->TypeOf() == NATIVE_FUNCTION ? info.argv[1] : nullptr);
NativeValue* result = nullptr;
AsyncTask::Schedule("JsTransitionContext::OnCompleteTransition",
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
return result;
}
static NativeValue* CreateJsTransitionContextObject(NativeEngine& engine, std::shared_ptr<NativeReference> jsWin,
sptr<Window> window, bool isShownTransContext)
{
WLOGFI("[NAPI]CreateJsTransitionContextObject");
NativeValue *objValue = engine.CreateObject();
NativeObject *object = ConvertNativeValueTo<NativeObject>(objValue);
if (object == nullptr) {
WLOGFE("[NAPI]Failed to get object");
return nullptr;
}
std::unique_ptr<JsTransitionContext> jsTransContext = std::make_unique<JsTransitionContext>(window,
isShownTransContext);
object->SetNativePointer(jsTransContext.release(), JsTransitionContext::Finalizer, nullptr);
if (jsWin != nullptr && jsWin->Get() != nullptr) {
object->SetProperty("toWindow", jsWin->Get());
} else {
WLOGFE("[NAPI]jsWin is nullptr");
return nullptr;
}
BindNativeFunction(engine, *object, "completeTransition", JsTransitionContext::CompleteTransition);
return objValue;
}
JsTransitionController::JsTransitionController(NativeEngine& engine, std::shared_ptr<NativeReference> jsWin,
sptr<Window> window)
: engine_(engine), jsWin_(jsWin), windowToken_(window), weakRef_(wptr<JsTransitionController> (this))
{
WLOGFI("[NAPI] JsTransitionController constructorCnt: %{public}d", ++jsTransCtrlCtorCnt);
}
JsTransitionController::~JsTransitionController()
{
WLOGFI("[NAPI] ~JsTransitionController deConstructorCnt: %{public}d", ++jsTransCtrlDtorCnt);
}
void JsTransitionController::AnimationForShown()
{
WLOGFI("[NAPI]AnimationForShown");
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[self = weakRef_](NativeEngine&, AsyncTask&, int32_t) {
auto thisController = self.promote();
if (thisController == nullptr) {
WLOGFE("this transition Controller is null!");
return;
}
HandleScope handleScope(thisController->engine_);
auto jsWin = thisController->jsWin_.lock();
auto window = thisController->windowToken_.promote();
if (jsWin == nullptr || window == nullptr) {
WLOGFE("native window or jsWindow is null!");
return;
}
auto state = window->GetWindowState();
if (state != WindowState::STATE_SHOWN) {
WLOGFE("animation shown configuration for state %{public}u not support!", static_cast<uint32_t>(state));
return;
}
NativeValue* jsTransContextObj = CreateJsTransitionContextObject(
thisController->engine_, jsWin, window, true);
if (jsTransContextObj == nullptr) {
return;
}
NativeValue *argv[] = { jsTransContextObj };
thisController->CallJsMethod("animationForShown", argv, ArraySize(argv));
// add to rs tree before animation
window->UpdateSurfaceNodeAfterCustomAnimation(true);
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule("JsTransitionController::AnimationForShown", engine_,
std::make_unique<AsyncTask>(callback, std::move(execute), std::move(complete)));
}
void JsTransitionController::AnimationForHidden()
{
WLOGFI("[NAPI]AnimationForHidden");
std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback> (
[self = weakRef_](NativeEngine&, AsyncTask&, int32_t) {
auto thisController = self.promote();
if (thisController == nullptr) {
WLOGFE("this transition Controller is null!");
return;
}
HandleScope handleScope(thisController->engine_);
auto jsWin = thisController->jsWin_.lock();
auto window = thisController->windowToken_.promote();
if (jsWin == nullptr || window == nullptr) {
WLOGFE("native window or jsWindow is null!");
return;
}
auto state = window->GetWindowState();
if (state != WindowState::STATE_HIDDEN) {
WLOGFE("animation hidden configuration for state %{public}u not support!",
static_cast<uint32_t>(state));
return;
}
NativeValue* jsTransContextObj = CreateJsTransitionContextObject(
thisController->engine_, jsWin, window, false);
if (jsTransContextObj == nullptr) {
return;
}
NativeValue *argv[] = { jsTransContextObj };
thisController->CallJsMethod("animationForHidden", argv, ArraySize(argv));
}
);
NativeReference* callback = nullptr;
std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
AsyncTask::Schedule("JsTransitionController::AnimationForHidden", engine_,
std::make_unique<AsyncTask>(callback, std::move(execute), std::move(complete)));
}
void JsTransitionController::CallJsMethod(const std::string& methodName, NativeValue* const* argv, size_t argc)
{
WLOGFI("Call js function:%{public}s.", methodName.c_str());
auto self = jsTransControllerObj_.lock();
if (self == nullptr) {
WLOGFE("JsController is null!");
return;
}
auto jsControllerValue = self->Get();
auto jsControllerObj = ConvertNativeValueTo<NativeObject>(jsControllerValue);
if (jsControllerObj == nullptr) {
WLOGFE("JsControllerObj is null!");
return;
}
auto method = jsControllerObj->GetProperty(methodName.c_str());
if (method == nullptr || method->TypeOf() == NATIVE_UNDEFINED) {
WLOGFE("Failed to get %{public}s from object", methodName.c_str());
return;
}
engine_.CallFunction(jsControllerValue, method, argv, argc);
}
void JsTransitionController::SetJsController(std::shared_ptr<NativeReference> jsVal)
{
jsTransControllerObj_ = jsVal;
}
}
}
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_JS_TRANSITION_CONTROLLER_H
#define OHOS_JS_TRANSITION_CONTROLLER_H
#include "native_engine/native_engine.h"
#include "native_engine/native_value.h"
#include "window.h"
namespace OHOS {
namespace Rosen {
class JsTransitionContext {
public:
JsTransitionContext(sptr<Window> window, bool shownState);
~JsTransitionContext();
static void Finalizer(NativeEngine* engine, void* data, void* hint);
static NativeValue* CompleteTransition(NativeEngine* engine, NativeCallbackInfo* info);
private:
NativeValue* OnCompleteTransition(NativeEngine& engine, NativeCallbackInfo& info);
wptr<Window> windowToken_;
bool isShownTransContext_ = false;
};
class JsTransitionController : public IAnimationTransitionController {
public:
JsTransitionController(NativeEngine& engine, std::shared_ptr<NativeReference> jsWin, sptr<Window> window);
~JsTransitionController();
void AnimationForShown() override;
void AnimationForHidden() override;
void SetJsController(std::shared_ptr<NativeReference> jsVal);
private:
void CallJsMethod(const std::string& methodName, NativeValue* const* argv, size_t argc);
NativeEngine& engine_;
std::weak_ptr<NativeReference> jsTransControllerObj_;
std::weak_ptr<NativeReference> jsWin_;
wptr<Window> windowToken_;
wptr<JsTransitionController> weakRef_ = nullptr;
};
}
}
#endif
@@ -16,6 +16,7 @@
#include "js_window.h"
#include <new>
#include <ui/rs_surface_node.h>
#include "js_transition_controller.h"
#include "js_window_utils.h"
#include "window.h"
#include "window_helper.h"
@@ -47,12 +48,12 @@ JsWindow::JsWindow(const sptr<Window>& window)
WLOGFI("[NAPI]Destroy window %{public}s in js window", windowName.c_str());
};
windowToken_->RegisterWindowDestroyedListener(func);
WLOGFI("[NAPI] JsWindow constructor Count: %{public}d", ++ctorCnt);
WLOGFI("[NAPI] JsWindow constructorCnt: %{public}d", ++ctorCnt);
}
JsWindow::~JsWindow()
{
WLOGFI("[NAPI]~JsWindow deconstructor Count:%{public}d", ++dtorCnt);
WLOGFI("[NAPI]~JsWindow deConstructorCnt:%{public}d", ++dtorCnt);
windowToken_ = nullptr;
}
@@ -66,7 +67,7 @@ std::string JsWindow::GetWindowName()
void JsWindow::Finalizer(NativeEngine* engine, void* data, void* hint)
{
WLOGFI("[NAPI]Finalizer Count:%{public}d", ++finalizerCnt);
WLOGFI("[NAPI]finalizerCnt:%{public}d", ++finalizerCnt);
auto jsWin = std::unique_ptr<JsWindow>(static_cast<JsWindow*>(data));
if (jsWin == nullptr) {
WLOGFE("[NAPI]jsWin is nullptr");
@@ -88,6 +89,13 @@ NativeValue* JsWindow::Show(NativeEngine* engine, NativeCallbackInfo* info)
return (me != nullptr) ? me->OnShow(*engine, *info) : nullptr;
}
NativeValue* JsWindow::ShowWithAnimation(NativeEngine* engine, NativeCallbackInfo* info)
{
WLOGFI("[NAPI]ShowWithAnimation");
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
return (me != nullptr) ? me->OnShowWithAnimation(*engine, *info) : nullptr;
}
NativeValue* JsWindow::Destroy(NativeEngine* engine, NativeCallbackInfo* info)
{
WLOGFI("[NAPI]Destroy");
@@ -102,6 +110,13 @@ NativeValue* JsWindow::Hide(NativeEngine* engine, NativeCallbackInfo* info)
return (me != nullptr) ? me->OnHide(*engine, *info) : nullptr;
}
NativeValue* JsWindow::HideWithAnimation(NativeEngine* engine, NativeCallbackInfo* info)
{
WLOGFI("[NAPI]HideWithAnimation");
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
return (me != nullptr) ? me->OnHideWithAnimation(*engine, *info) : nullptr;
}
NativeValue* JsWindow::MoveTo(NativeEngine* engine, NativeCallbackInfo* info)
{
WLOGFI("[NAPI]MoveTo");
@@ -347,6 +362,13 @@ NativeValue* JsWindow::SetTranslateSync(NativeEngine* engine, NativeCallbackInfo
return (me != nullptr) ? me->OnSetTranslateSync(*engine, *info) : nullptr;
}
NativeValue* JsWindow::GetTransitionControllerSync(NativeEngine* engine, NativeCallbackInfo* info)
{
WLOGFI("[NAPI]GetTransitionControllerSync");
JsWindow* me = CheckParamsAndGetThis<JsWindow>(engine, info);
return (me != nullptr) ? me->OnGetTransitionControllerSync(*engine, *info) : nullptr;
}
NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info)
{
WMError errCode = WMError::WM_OK;
@@ -363,7 +385,7 @@ NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info)
WLOGFE("[NAPI]window is nullptr or get invalid param");
return;
}
WMError ret = weakWindow->Show();
WMError ret = weakWindow->Show(0, false);
if (ret == WMError::WM_OK) {
task.Resolve(engine, engine.CreateUndefined());
} else {
@@ -372,15 +394,52 @@ NativeValue* JsWindow::OnShow(NativeEngine& engine, NativeCallbackInfo& info)
WLOGFI("[NAPI]Window [%{public}u, %{public}s] show end, ret = %{public}d",
weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
};
NativeValue* result = nullptr;
NativeValue* lastParam = (info.argc == 0) ? nullptr :
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
NativeValue* result = nullptr;
AsyncTask::Schedule("JsWindow::OnShow",
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
return result;
}
NativeValue* JsWindow::OnShowWithAnimation(NativeEngine& engine, NativeCallbackInfo& info)
{
WMError errCode = WMError::WM_OK;
if (info.argc > 1) {
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
errCode = WMError::WM_ERROR_INVALID_PARAM;
}
auto winType = windowToken_->GetType();
if (!WindowHelper::IsSystemWindow(winType)) {
WLOGFE("[NAPI]window Type %{public}u is not supported", static_cast<uint32_t>(winType));
errCode = WMError::WM_ERROR_INVALID_PARAM;
}
wptr<Window> weakToken(windowToken_);
AsyncTask::CompleteCallback complete =
[weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
auto weakWindow = weakToken.promote();
if (weakWindow == nullptr || errCode != WMError::WM_OK) {
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
WLOGFE("[NAPI]window is nullptr or get invalid param");
return;
}
WMError ret = weakWindow->Show(0, true);
if (ret == WMError::WM_OK) {
task.Resolve(engine, engine.CreateUndefined());
} else {
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window show failed"));
}
WLOGFI("[NAPI]Window [%{public}u, %{public}s] ShowWithAnimation end, ret = %{public}d",
weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
};
NativeValue* result = nullptr;
NativeValue* lastParam = (info.argc == 0) ? nullptr :
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
AsyncTask::Schedule("JsWindow::OnShowWithAnimation",
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
return result;
}
NativeValue* JsWindow::OnDestroy(NativeEngine& engine, NativeCallbackInfo& info)
{
WMError errCode = WMError::WM_OK;
@@ -432,7 +491,7 @@ NativeValue* JsWindow::OnHide(NativeEngine& engine, NativeCallbackInfo& info)
WLOGFE("[NAPI]window is nullptr or get invalid param");
return;
}
WMError ret = weakWindow->Hide();
WMError ret = weakWindow->Hide(0, false);
if (ret == WMError::WM_OK) {
task.Resolve(engine, engine.CreateUndefined());
} else {
@@ -442,14 +501,52 @@ NativeValue* JsWindow::OnHide(NativeEngine& engine, NativeCallbackInfo& info)
weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
};
NativeValue* result = nullptr;
NativeValue* lastParam = (info.argc == 0) ? nullptr :
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
NativeValue* result = nullptr;
AsyncTask::Schedule("JsWindow::OnHide",
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
return result;
}
NativeValue* JsWindow::OnHideWithAnimation(NativeEngine& engine, NativeCallbackInfo& info)
{
WMError errCode = WMError::WM_OK;
if (info.argc > 1) {
WLOGFE("[NAPI]Argc is invalid: %{public}zu", info.argc);
errCode = WMError::WM_ERROR_INVALID_PARAM;
}
auto winType = windowToken_->GetType();
if (!WindowHelper::IsSystemWindow(winType)) {
WLOGFE("[NAPI]window Type %{public}u is not supported", static_cast<uint32_t>(winType));
errCode = WMError::WM_ERROR_INVALID_PARAM;
}
wptr<Window> weakToken(windowToken_);
AsyncTask::CompleteCallback complete =
[weakToken, errCode](NativeEngine& engine, AsyncTask& task, int32_t status) {
auto weakWindow = weakToken.promote();
if (weakWindow == nullptr || errCode != WMError::WM_OK) {
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(errCode)));
WLOGFE("[NAPI]window is nullptr or get invalid param");
return;
}
WMError ret = weakWindow->Hide(0, true);
if (ret == WMError::WM_OK) {
task.Resolve(engine, engine.CreateUndefined());
} else {
task.Reject(engine, CreateJsError(engine, static_cast<int32_t>(ret), "Window show failed"));
}
WLOGFI("[NAPI]Window [%{public}u, %{public}s] HideWithAnimation end, ret = %{public}d",
weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), ret);
};
NativeValue* result = nullptr;
NativeValue* lastParam = (info.argc == 0) ? nullptr :
(info.argv[0]->TypeOf() == NATIVE_FUNCTION ? info.argv[0] : nullptr);
AsyncTask::Schedule("JsWindow::OnHideWithAnimation",
engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
return result;
}
NativeValue* JsWindow::OnMoveTo(NativeEngine& engine, NativeCallbackInfo& info)
{
WMError errCode = WMError::WM_OK;
@@ -1053,7 +1150,7 @@ NativeValue* JsWindow::OnIsShowing(NativeEngine& engine, NativeCallbackInfo& inf
WLOGFE("[NAPI]window is nullptr or get invalid param");
return;
}
bool state = weakWindow->GetShowState();
bool state = weakWindow->GetWindowState() == WindowState::STATE_SHOWN;
task.Resolve(engine, CreateJsValue(engine, state));
WLOGFI("[NAPI]Window [%{public}u, %{public}s] get show state end, state = %{public}u",
weakWindow->GetWindowId(), weakWindow->GetWindowName().c_str(), state);
@@ -1870,6 +1967,57 @@ NativeValue* JsWindow::OnSetTranslateSync(NativeEngine& engine, NativeCallbackIn
return engine.CreateUndefined();
}
void JsWindow::CreateTransitionController(NativeEngine& engine)
{
if (windowToken_ == nullptr) {
WLOGFE("[NAPI]windowToken_ is nullptr not match");
return;
}
if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
WLOGFE("[NAPI]CreateTransitionController is not allowed since window is not system window");
return;
}
NativeValue* objValue = engine.CreateObject();
auto name = GetWindowName();
std::shared_ptr<NativeReference> jsWindowObj = FindJsWindowObject(name);
if (jsWindowObj == nullptr || jsWindowObj->Get() == nullptr) {
return;
}
sptr<JsTransitionController> nativeController = new JsTransitionController(
engine, jsWindowObj, windowToken_);
NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
if (object == nullptr) {
WLOGFE("[NAPI]Failed to convert to TransitionController Object");
return;
}
object->SetNativePointer(new wptr<JsTransitionController>(nativeController),
[](NativeEngine*, void* data, void*) {
WLOGFE("Finalizer for wptr form native Transition Controller is called");
delete static_cast<wptr<JsTransitionController>*>(data);
}, nullptr);
windowToken_->RegisterAnimationTransitionController(nativeController);
jsTransControllerObj_.reset(engine.CreateReference(objValue, 1));
nativeController->SetJsController(jsTransControllerObj_);
WLOGFI("[NAPI]Window [%{public}u, %{public}s] CreateTransitionController end",
windowToken_->GetWindowId(), windowToken_->GetWindowName().c_str());
}
NativeValue* JsWindow::OnGetTransitionControllerSync(NativeEngine& engine, NativeCallbackInfo& info)
{
if (windowToken_ == nullptr || info.argc > 0) {
WLOGFE("[NAPI]windowToken_ is nullptr or params %{public}zu not match", info.argc);
return engine.CreateUndefined();
}
if (!WindowHelper::IsSystemWindow(windowToken_->GetType())) {
WLOGFE("[NAPI]OnCreateTransitionControllerSync is not allowed since window is not system window");
return engine.CreateUndefined();
}
if (jsTransControllerObj_ == nullptr || jsTransControllerObj_->Get() == nullptr) {
CreateTransitionController(engine);
}
return jsTransControllerObj_->Get();
}
std::shared_ptr<NativeReference> FindJsWindowObject(std::string windowName)
{
WLOGFI("[NAPI]Try to find window %{public}s in g_jsWindowMap", windowName.c_str());
@@ -1910,8 +2058,10 @@ NativeValue* CreateJsWindowObject(NativeEngine& engine, sptr<Window>& window)
void BindFunctions(NativeEngine& engine, NativeObject* object)
{
BindNativeFunction(engine, *object, "show", JsWindow::Show);
BindNativeFunction(engine, *object, "showWithAnimation", JsWindow::ShowWithAnimation);
BindNativeFunction(engine, *object, "destroy", JsWindow::Destroy);
BindNativeFunction(engine, *object, "hide", JsWindow::Hide);
BindNativeFunction(engine, *object, "hideWithAnimation", JsWindow::HideWithAnimation);
BindNativeFunction(engine, *object, "moveTo", JsWindow::MoveTo);
BindNativeFunction(engine, *object, "resetSize", JsWindow::Resize);
BindNativeFunction(engine, *object, "setWindowType", JsWindow::SetWindowType);
@@ -1947,6 +2097,7 @@ void BindFunctions(NativeEngine& engine, NativeObject* object)
BindNativeFunction(engine, *object, "setScaleSync", JsWindow::SetScaleSync);
BindNativeFunction(engine, *object, "setRotateSync", JsWindow::SetRotateSync);
BindNativeFunction(engine, *object, "setTranslateSync", JsWindow::SetTranslateSync);
BindNativeFunction(engine, *object, "getTransitionControllerSync", JsWindow::GetTransitionControllerSync);
}
} // namespace Rosen
} // namespace OHOS
@@ -34,8 +34,10 @@ public:
~JsWindow();
static void Finalizer(NativeEngine* engine, void* data, void* hint);
static NativeValue* Show(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* ShowWithAnimation(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* Destroy(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* Hide(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* HideWithAnimation(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* MoveTo(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* Resize(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* SetWindowType(NativeEngine* engine, NativeCallbackInfo* info);
@@ -74,14 +76,17 @@ public:
static NativeValue* SetScaleSync(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* SetRotateSync(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* SetTranslateSync(NativeEngine* engine, NativeCallbackInfo* info);
static NativeValue* GetTransitionControllerSync(NativeEngine* engine, NativeCallbackInfo* info);
private:
std::string GetWindowName();
bool ParseScaleOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans);
bool ParseRotateOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans);
bool ParseTranslateOption(NativeEngine& engine, NativeObject* jsObject, Transform& trans);
NativeValue* OnShow(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnShowWithAnimation(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnDestroy(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnHide(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnHideWithAnimation(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnMoveTo(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnResize(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnSetWindowType(NativeEngine& engine, NativeCallbackInfo& info);
@@ -121,8 +126,12 @@ private:
NativeValue* OnSetScaleSync(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnSetRotateSync(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnSetTranslateSync(NativeEngine& engine, NativeCallbackInfo& info);
NativeValue* OnGetTransitionControllerSync(NativeEngine& engine, NativeCallbackInfo& info);
void CreateTransitionController(NativeEngine& engine);
sptr<Window> windowToken_ = nullptr;
std::unique_ptr<JsWindowRegisterManager> registerManager_ = nullptr;
std::shared_ptr<NativeReference> jsTransControllerObj_ = nullptr;
};
} // namespace Rosen
} // namespace OHOS
@@ -533,7 +533,7 @@ bool ParseJsDoubleValue(NativeObject* jsObject, NativeEngine& engine, const std:
NativeValue* value = jsObject->GetProperty(name.c_str());
if (value->TypeOf() != NATIVE_UNDEFINED) {
if (!ConvertFromJsValue(engine, value, data)) {
WLOGFE("[NAPI]Failed to convert parameter to scale %{public}s", name.c_str());
WLOGFE("[NAPI]Failed to convert parameter to data: %{public}s", name.c_str());
return false;
}
} else {
@@ -37,6 +37,7 @@ JsWindowStage::JsWindowStage(const std::shared_ptr<Rosen::WindowScene>& windowSc
JsWindowStage::~JsWindowStage()
{
}
void JsWindowStage::Finalizer(NativeEngine* engine, void* data, void* hint)
{
WLOGFI("[NAPI]Finalizer");
-11
View File
@@ -29,17 +29,6 @@ enum class LifeCycleEvent : uint32_t {
DESTROY_EVENT,
};
enum class WindowState : uint32_t {
STATE_INITIAL,
STATE_CREATED,
STATE_SHOWN,
STATE_HIDDEN,
STATE_FROZEN,
STATE_DESTROYED,
STATE_BOTTOM = STATE_DESTROYED,
STATE_UNFROZEN,
};
enum class WindowStateChangeReason : uint32_t {
NORMAL,
KEYGUARD,
+1 -1
View File
@@ -108,7 +108,7 @@ public:
virtual WindowMode GetMode() const override;
virtual WindowBlurLevel GetWindowBackgroundBlur() const override;
virtual float GetAlpha() const override;
virtual bool GetShowState() const override;
virtual WindowState GetWindowState() const override;
virtual WMError SetFocusable(bool isFocusable) override;
virtual bool GetFocusable() const override;
virtual WMError SetTouchable(bool isTouchable) override;
+20 -21
View File
@@ -225,9 +225,9 @@ float WindowImpl::GetAlpha() const
return property_->GetAlpha();
}
bool WindowImpl::GetShowState() const
WindowState WindowImpl::GetWindowState() const
{
return state_ == WindowState::STATE_SHOWN;
return state_;
}
WMError WindowImpl::SetFocusable(bool isFocusable)
@@ -960,28 +960,10 @@ WMError WindowImpl::UpdateSurfaceNodeAfterCustomAnimation(bool isAdd)
WMError WindowImpl::PreProcessShow(uint32_t reason, bool withAnimation)
{
WindowStateChangeReason stateChangeReason = static_cast<WindowStateChangeReason>(reason);
if (stateChangeReason == WindowStateChangeReason::KEYGUARD ||
stateChangeReason == WindowStateChangeReason::TOGGLING) {
state_ = WindowState::STATE_SHOWN;
NotifyAfterForeground();
return WMError::WM_OK;
}
if (state_ == WindowState::STATE_FROZEN) {
WLOGFE("window is frozen, can not be shown, windowId: %{public}u", property_->GetWindowId());
return WMError::WM_ERROR_INVALID_OPERATION;
}
if (state_ == WindowState::STATE_SHOWN) {
if (property_->GetWindowType() == WindowType::WINDOW_TYPE_DESKTOP) {
WLOGFI("desktop window [id:%{public}u] is shown, minimize all app windows", property_->GetWindowId());
SingletonContainer::Get<WindowAdapter>().MinimizeAllAppWindows(property_->GetDisplayId());
} else {
WLOGFI("window is already shown id: %{public}u, raise to top", property_->GetWindowId());
SingletonContainer::Get<WindowAdapter>().ProcessPointDown(property_->GetWindowId());
}
CALL_LIFECYCLE_LISTENER(AfterForeground);
return WMError::WM_OK;
}
SetDefaultOption();
AdjustWindowAnimationFlag(withAnimation);
// show failed when current mode is not support or window only supports split mode and can show when locked
@@ -1005,7 +987,24 @@ WMError WindowImpl::Show(uint32_t reason, bool withAnimation)
if (!IsWindowValid()) {
return WMError::WM_ERROR_INVALID_WINDOW;
}
WindowStateChangeReason stateChangeReason = static_cast<WindowStateChangeReason>(reason);
if (stateChangeReason == WindowStateChangeReason::KEYGUARD ||
stateChangeReason == WindowStateChangeReason::TOGGLING) {
state_ = WindowState::STATE_SHOWN;
NotifyAfterForeground();
return WMError::WM_OK;
}
if (state_ == WindowState::STATE_SHOWN) {
if (property_->GetWindowType() == WindowType::WINDOW_TYPE_DESKTOP) {
WLOGFI("desktop window [id:%{public}u] is shown, minimize all app windows", property_->GetWindowId());
SingletonContainer::Get<WindowAdapter>().MinimizeAllAppWindows(property_->GetDisplayId());
} else {
WLOGFI("window is already shown id: %{public}u, raise to top", property_->GetWindowId());
SingletonContainer::Get<WindowAdapter>().ProcessPointDown(property_->GetWindowId());
}
CALL_LIFECYCLE_LISTENER(AfterForeground);
return WMError::WM_OK;
}
WMError ret = PreProcessShow(reason, withAnimation);
if (ret != WMError::WM_OK) {
return ret;
@@ -153,8 +153,8 @@ HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow02, Function | MediumTest
ASSERT_EQ(WMError::WM_OK, fltWin->Show());
ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
ASSERT_EQ(false, scene->GetMainWindow()->GetShowState());
ASSERT_EQ(true, fltWin->GetShowState());
ASSERT_EQ(false, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
@@ -179,8 +179,8 @@ HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow03, Function | MediumTest
ASSERT_EQ(WMError::WM_OK, fltWin->Show());
ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
ASSERT_EQ(false, fltWin->GetShowState());
ASSERT_EQ(true, scene->GetMainWindow()->GetShowState());
ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
@@ -206,7 +206,7 @@ HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow04, Function | MediumTest
ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
ASSERT_EQ(nullptr, scene->GetMainWindow());
ASSERT_EQ(false, fltWin->GetShowState());
ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
}
@@ -252,8 +252,8 @@ HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow06, Function | MediumTest
ASSERT_EQ(WMError::WM_OK, fltWin->Show());
ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
ASSERT_EQ(false, scene->GetMainWindow()->GetShowState());
ASSERT_EQ(true, fltWin->GetShowState());
ASSERT_EQ(false, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(true, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
@@ -278,8 +278,8 @@ HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow07, Function | MediumTest
ASSERT_EQ(WMError::WM_OK, fltWin->Show());
ASSERT_EQ(WMError::WM_OK, fltWin->Hide());
ASSERT_EQ(false, fltWin->GetShowState());
ASSERT_EQ(true, scene->GetMainWindow()->GetShowState());
ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(true, scene->GetMainWindow()->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(WMError::WM_OK, scene->GoBackground());
ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
@@ -305,7 +305,7 @@ HWTEST_F(WindowAppFloatingWindowTest, AppFloatingWindow08, Function | MediumTest
ASSERT_EQ(WMError::WM_OK, scene->GoDestroy());
ASSERT_EQ(nullptr, scene->GetMainWindow());
ASSERT_EQ(false, fltWin->GetShowState());
ASSERT_EQ(false, fltWin->GetWindowState() == WindowState::STATE_SHOWN);
ASSERT_EQ(WMError::WM_OK, fltWin->Destroy());
}
+6 -6
View File
@@ -755,10 +755,10 @@ HWTEST_F(WindowImplTest, Minimize01, Function | SmallTest | Level3)
window->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
EXPECT_CALL(m->Mock(), AddWindow(_)).Times(1).WillOnce(Return(WMError::WM_OK));
window->Show();
ASSERT_TRUE(window->GetShowState());
ASSERT_TRUE((window->GetWindowState() == WindowState::STATE_SHOWN));
EXPECT_CALL(m->Mock(), RemoveWindow(_)).Times(1).WillOnce(Return(WMError::WM_OK));
window->Minimize();
ASSERT_FALSE(window->GetShowState());
ASSERT_FALSE((window->GetWindowState() == WindowState::STATE_SHOWN));
EXPECT_CALL(m->Mock(), DestroyWindow(_)).Times(1).WillOnce(Return(WMError::WM_OK));
ASSERT_EQ(WMError::WM_OK, window->Destroy());
}
@@ -780,9 +780,9 @@ HWTEST_F(WindowImplTest, Minimize02, Function | SmallTest | Level3)
window->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
window->SetWindowMode(WindowMode::WINDOW_MODE_FULLSCREEN);
window->Show();
ASSERT_TRUE(window->GetShowState());
ASSERT_TRUE((window->GetWindowState() == WindowState::STATE_SHOWN));
window->Minimize();
ASSERT_TRUE(window->GetShowState());
ASSERT_TRUE((window->GetWindowState() == WindowState::STATE_SHOWN));
EXPECT_CALL(m->Mock(), DestroyWindow(_)).Times(1).WillOnce(Return(WMError::WM_OK));
ASSERT_EQ(WMError::WM_OK, window->Destroy());
}
@@ -868,7 +868,7 @@ HWTEST_F(WindowImplTest, MoveTo01, Function | SmallTest | Level3)
EXPECT_CALL(m->Mock(), CreateWindow(_, _, _, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
ASSERT_EQ(WMError::WM_OK, window->Create(""));
ASSERT_EQ(WindowMode::WINDOW_MODE_FLOATING, window->GetMode());
ASSERT_FALSE(window->GetShowState());
ASSERT_FALSE((window->GetWindowState() == WindowState::STATE_SHOWN));
const float moveRatio = 0.5;
Rect newRect = {winRect.posX_ * moveRatio, winRect.posY_ * moveRatio, winRect.width_, winRect.height_};
window->MoveTo(newRect.posX_, newRect.posY_);
@@ -895,7 +895,7 @@ HWTEST_F(WindowImplTest, Resize01, Function | SmallTest | Level3)
EXPECT_CALL(m->Mock(), CreateWindow(_, _, _, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
ASSERT_EQ(WMError::WM_OK, window->Create(""));
ASSERT_EQ(WindowMode::WINDOW_MODE_FLOATING, window->GetMode());
ASSERT_FALSE(window->GetShowState());
ASSERT_FALSE((window->GetWindowState() == WindowState::STATE_SHOWN));
const float resizeRatio = 0.5;
Rect newRect = {winRect.posX_, winRect.posY_, winRect.width_ * resizeRatio, winRect.height_ * resizeRatio};
window->Resize(newRect.width_, newRect.height_);
+1 -1
View File
@@ -654,7 +654,7 @@ WMError WindowManagerProxy::UpdateRsTree(uint32_t windowId, bool isAdd)
{
MessageParcel data;
MessageParcel reply;
MessageOption option;
MessageOption option(MessageOption::TF_ASYNC);
if (!data.WriteInterfaceToken(GetDescriptor())) {
WLOGFE("WriteInterfaceToken failed");