141 lines
4.0 KiB
C++
141 lines
4.0 KiB
C++
/*
|
||
* Copyright (c) CompanyNameMagicTag.
|
||
* 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 APPFWK_VIEW_H
|
||
#define APPFWK_VIEW_H
|
||
|
||
#include "components/ui_view_group.h"
|
||
#include "components/root_view.h"
|
||
#include "common/screen.h"
|
||
|
||
namespace OHOS {
|
||
template<class P>
|
||
class View {
|
||
public:
|
||
View() : presenter_(nullptr), rootContainer_(nullptr) {}
|
||
virtual ~View() {}
|
||
|
||
/**
|
||
* @brief get container view
|
||
*
|
||
* @returns container
|
||
*/
|
||
UIViewGroup* GetRootContainer() const
|
||
{
|
||
return rootContainer_;
|
||
}
|
||
|
||
/**
|
||
* @brief Scheduled by slice after funtion SetupView and before transition animator starts if exits.
|
||
*
|
||
* You should override it to prepare your own views and add them into root container
|
||
* A simple examle is as follow:
|
||
* void OnStart() override
|
||
* {
|
||
* label_ = new UILabel();
|
||
* ....
|
||
* AddViewToRootContainer(label_); // add label_ into container
|
||
* }
|
||
*/
|
||
virtual void OnStart() {}
|
||
|
||
/**
|
||
* @brief Scheduled by slice after function TearDownView.
|
||
*
|
||
* You can override it or override function of Destructor function to destroy ui views
|
||
* A simple examle is as follow:
|
||
* void OnStop() override
|
||
* {
|
||
* DELETE_IF_NOT_NULL(label_);
|
||
* ....
|
||
* }
|
||
*/
|
||
virtual void OnStop() {}
|
||
|
||
/**
|
||
* @brief Scheduled by slice to create root container add it to root view
|
||
*
|
||
* This method is called by system class AbilitySlice, you should not override it.
|
||
*/
|
||
void SetupView()
|
||
{
|
||
if (rootContainer_ != nullptr) {
|
||
printf("OnStart Error: rootContainer_ already exist");
|
||
} else {
|
||
rootContainer_ = new UIViewGroup();
|
||
}
|
||
rootContainer_->SetPosition(0, 0, RootView::GetInstance()->GetWidth(), RootView::GetInstance()->GetHeight());
|
||
RootView::GetInstance()->Add(rootContainer_);
|
||
}
|
||
|
||
/**
|
||
* @brief Scheduled by slice to remove and delete root container from root view
|
||
*
|
||
* This method is called by by system class AbilitySlice, you should not override it.
|
||
*/
|
||
void TearDownView()
|
||
{
|
||
// Todo: Only remove root container, and application should remove other children in its own implemention
|
||
RootView::GetInstance()->Remove(rootContainer_);
|
||
if (rootContainer_ != nullptr) {
|
||
rootContainer_->RemoveAll();
|
||
delete rootContainer_;
|
||
rootContainer_ = nullptr;
|
||
} else {
|
||
printf("TearDownView Error: rootContainer_ was not initialized !!!");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief draw container
|
||
*
|
||
*/
|
||
void Draw()
|
||
{
|
||
RootView::GetInstance()->Invalidate();
|
||
}
|
||
|
||
/**
|
||
* @brief This function will attach presenter to this view
|
||
*
|
||
* @param presenter presenter pointer defined by developer
|
||
*/
|
||
void Attach(P* presenter)
|
||
{
|
||
presenter_ = presenter;
|
||
}
|
||
|
||
void AddViewToRootContainer(UIView* view)
|
||
{
|
||
if (rootContainer_ == nullptr) {
|
||
printf("AddViewToContainer Error: rootContainer_ is nullptr");
|
||
return;
|
||
}
|
||
rootContainer_->Add(view);
|
||
}
|
||
|
||
protected:
|
||
P* presenter_;
|
||
|
||
private:
|
||
UIViewGroup* rootContainer_ = nullptr;
|
||
View(const View&) = delete;
|
||
View& operator=(const View&) = delete;
|
||
View(View&&) = delete;
|
||
View& operator=(View&&) = delete;
|
||
};
|
||
}
|
||
#endif
|