• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "long_press_view.h"
17 #include "ui_config.h"
18 
19 namespace OHOS {
LongPressView(UninstallApp uninstall)20 LongPressView::LongPressView(UninstallApp uninstall)
21 {
22     bStatus_ = false;
23     uninstall_ = uninstall;
24     viewGroup_ = new UIViewGroup();
25     viewGroup_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Black()));
26     viewGroup_->SetStyle(STYLE_BORDER_RADIUS, GROUP_VIEW_RADIUS);
27     viewGroup_->SetStyle(STYLE_BACKGROUND_OPA, UN_OPACITY);
28 
29     buttUninstall_ = new UILabelButton();
30     buttUninstall_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray()));
31     buttUninstall_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS);
32     buttUninstall_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
33     buttUninstall_->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED);
34     buttUninstall_->SetStyleForState(STYLE_BACKGROUND_OPA, HALF_OPACITY, UIButton::PRESSED);
35     buttUninstall_->SetText("卸载");
36     buttUninstall_->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
37     buttUninstall_->SetOnClickListener(this);
38 
39     buttCancle_ = new UILabelButton();
40     buttCancle_->SetStyle(STYLE_BACKGROUND_COLOR, Color::ColorTo32(Color::Gray()));
41     buttCancle_->SetStyle(STYLE_BORDER_RADIUS, BUTTON_RADIUS);
42     buttCancle_->SetStyle(STYLE_TEXT_COLOR, Color::ColorTo32(Color::White()));
43     buttCancle_->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_RADIUS, UIButton::PRESSED);
44     buttCancle_->SetStyleForState(STYLE_BACKGROUND_OPA, HALF_OPACITY, UIButton::PRESSED);
45     buttCancle_->SetText("取消");
46     buttCancle_->SetFont(FOND_PATH, LAUNCHER_FOND_ID);
47     buttCancle_->SetOnClickListener(this);
48 
49     viewGroup_->Add(buttUninstall_);
50     viewGroup_->Add(buttCancle_);
51     viewGroup_->SetVisible(false);
52 }
53 
~LongPressView()54 LongPressView::~LongPressView()
55 {
56     DeleteChildren(viewGroup_);
57 }
58 
RemoveLview()59 void LongPressView::RemoveLview()
60 {
61     if (bStatus_ == false) {
62         return;
63     }
64     viewParent_->Remove(viewGroup_);
65     viewGroup_->SetVisible(false);
66     viewParent_->Invalidate();
67     bStatus_ = false;
68 }
69 
Show(UIViewGroup * viewParent,AppInfo * pApp)70 void LongPressView::Show(UIViewGroup* viewParent, AppInfo* pApp)
71 {
72     const int16_t HEIGHT_DISCOUNT = 3;
73     const int16_t WIDTH_DISCOUNT = 2;
74     bStatus_ = true;
75     viewParent_ = viewParent;
76     app_ = pApp;
77     viewGroup_->SetPosition(pApp->buttonXY_.x / WIDTH_DISCOUNT + pApp->button_->GetWidth(),
78         pApp->buttonXY_.y / WIDTH_DISCOUNT + pApp->button_->GetHeight(), pApp->button_->GetWidth(),
79         (pApp->button_->GetHeight() * WIDTH_DISCOUNT) / HEIGHT_DISCOUNT + pApp->button_->GetHeight() / WIDTH_DISCOUNT);
80     buttUninstall_->SetPosition(0, 0,
81         pApp->button_->GetWidth(), pApp->button_->GetHeight() / WIDTH_DISCOUNT);
82     buttCancle_->SetPosition(0, (pApp->button_->GetHeight() * WIDTH_DISCOUNT) / HEIGHT_DISCOUNT,
83         pApp->button_->GetWidth(), pApp->button_->GetHeight() / WIDTH_DISCOUNT);
84     viewGroup_->SetVisible(true);
85     viewParent_->Add(viewGroup_);
86     viewParent_->Invalidate();
87 }
88 
OnClick(UIView & view,const ClickEvent & event)89 bool LongPressView::OnClick(UIView& view, const ClickEvent& event)
90 {
91     UIView *currentView = &view;
92     if (currentView == nullptr) {
93         return false;
94     }
95     RemoveLview();
96     if (currentView == buttUninstall_) {
97         uninstall_(app_);
98     }
99     return true;
100 }
101 } // namespace OHOS
102