1 /* 2 * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_BUTTON_BUTTON_DECLARATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_BUTTON_BUTTON_DECLARATION_H 18 19 #include "frameworks/core/components/declaration/button/button_progress_controller.h" 20 #include "frameworks/core/components/declaration/common/declaration.h" 21 #include "frameworks/bridge/common/dom/dom_type.h" 22 23 namespace OHOS::Ace { 24 25 struct ButtonAttribute : Attribute { 26 std::string buttonType; 27 std::string iconSrc; 28 std::string placement; 29 std::string textData; 30 bool isAutoFocus = false; 31 bool isDisabled = false; 32 bool isWaiting = false; 33 }; 34 35 struct ButtonStyle : Style { 36 TextStyle textStyle; 37 BorderEdge borderEdge; 38 Edge padding; 39 Dimension diameter; 40 Dimension minWidth; 41 Dimension width = -1.0_vp; 42 Dimension height = -1.0_vp; 43 Dimension radius; 44 Dimension iconWidth; 45 Dimension iconHeight; 46 Dimension innerLeftPadding; 47 Color focusColor; 48 Color backgroundColor; 49 Color clickedColor; 50 Color disabledColor; 51 Color disabledTextColor; 52 Color focusAnimationColor; 53 Color hoverColor; 54 Color progressColor; 55 Color progressFocusColor; 56 Color textFocusColor; 57 bool bgColorDefined = false; 58 bool matchTextDirection = false; 59 bool heightDefined = false; 60 bool fontSizeDefined = false; 61 bool textColorDefined = false; 62 bool radiusDefined = false; 63 double blendOpacity = 0.0; 64 }; 65 66 struct ButtonEvent : Event { 67 EventMarker clickEventId; 68 }; 69 70 struct ButtonMethod : Method { SetProgressButtonMethod71 void SetProgress(const RefPtr<ButtonProgressController>& controller, uint32_t progress) const 72 { 73 controller->SetProgress(progress); 74 } 75 }; 76 77 class ButtonDeclaration : public Declaration { 78 DECLARE_ACE_TYPE(ButtonDeclaration, Declaration); 79 80 public: 81 ButtonDeclaration() = default; 82 ~ButtonDeclaration() override = default; 83 84 void InitializeStyle() override; 85 GetType()86 const std::string& GetType() const 87 { 88 auto& attr = static_cast<ButtonAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 89 return attr.buttonType; 90 } 91 GetIconSrc()92 const std::string& GetIconSrc() const 93 { 94 auto& attr = static_cast<ButtonAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 95 return attr.iconSrc; 96 } 97 GetPlacement()98 const std::string& GetPlacement() const 99 { 100 auto& attr = static_cast<ButtonAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 101 return attr.placement; 102 } 103 GetTextData()104 const std::string& GetTextData() const 105 { 106 auto& attr = static_cast<ButtonAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 107 return attr.textData; 108 } 109 GetAutoFocusState()110 bool GetAutoFocusState() const 111 { 112 auto& attr = static_cast<ButtonAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 113 return attr.isAutoFocus; 114 } 115 GetDisabledState()116 bool GetDisabledState() const 117 { 118 auto& attr = static_cast<ButtonAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 119 return attr.isDisabled; 120 } 121 GetWaitingState()122 bool GetWaitingState() const 123 { 124 auto& attr = static_cast<ButtonAttribute&>(GetAttribute(AttributeTag::SPECIALIZED_ATTR)); 125 return attr.isWaiting; 126 } 127 GetPadding()128 const Edge& GetPadding() const 129 { 130 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 131 return style.padding; 132 } 133 GetTextStyle()134 const TextStyle& GetTextStyle() const 135 { 136 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 137 return style.textStyle; 138 } 139 GetWidth()140 const Dimension& GetWidth() const 141 { 142 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 143 return style.width; 144 } 145 GetHeight()146 const Dimension& GetHeight() const 147 { 148 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 149 return style.height; 150 } 151 GetMinWidth()152 const Dimension& GetMinWidth() const 153 { 154 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 155 return style.minWidth; 156 } 157 GetRectRadius()158 const Dimension& GetRectRadius() const 159 { 160 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 161 return style.radius; 162 } 163 GetProgressDiameter()164 const Dimension& GetProgressDiameter() const 165 { 166 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 167 return style.diameter; 168 } 169 GetInnerPadding()170 const Dimension& GetInnerPadding() const 171 { 172 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 173 return style.innerLeftPadding; 174 } 175 GetIconHeight()176 const Dimension& GetIconHeight() const 177 { 178 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 179 return style.iconHeight; 180 } 181 GetIconWidth()182 const Dimension& GetIconWidth() const 183 { 184 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 185 return style.iconWidth; 186 } 187 GetBackgroundColor()188 const Color& GetBackgroundColor() const 189 { 190 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 191 return style.backgroundColor; 192 } 193 GetClickedColor()194 const Color& GetClickedColor() const 195 { 196 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 197 return style.clickedColor; 198 } 199 GetDisabledColor()200 const Color& GetDisabledColor() const 201 { 202 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 203 return style.disabledColor; 204 } 205 GetDisabledTextColor()206 const Color& GetDisabledTextColor() const 207 { 208 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 209 return style.disabledTextColor; 210 } 211 GetFocusColor()212 const Color& GetFocusColor() const 213 { 214 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 215 return style.focusColor; 216 } 217 GetHoverColor()218 const Color& GetHoverColor() const 219 { 220 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 221 return style.hoverColor; 222 } 223 GetProgressColor()224 const Color& GetProgressColor() const 225 { 226 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 227 return style.progressColor; 228 } 229 GetProgressFocusColor()230 const Color& GetProgressFocusColor() const 231 { 232 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 233 return style.progressFocusColor; 234 } 235 GetFocusAnimationColor()236 const Color& GetFocusAnimationColor() const 237 { 238 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 239 return style.focusAnimationColor; 240 } 241 GetFocusTextColor()242 const Color& GetFocusTextColor() const 243 { 244 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 245 return style.textFocusColor; 246 } 247 GetBorderEdge()248 const BorderEdge& GetBorderEdge() const 249 { 250 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 251 return style.borderEdge; 252 } 253 GetMatchTextDirection()254 bool GetMatchTextDirection() const 255 { 256 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 257 return style.matchTextDirection; 258 } 259 GetBgColorState()260 bool GetBgColorState() const 261 { 262 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 263 return style.bgColorDefined; 264 } 265 GetTextColorState()266 bool GetTextColorState() const 267 { 268 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 269 return style.textColorDefined; 270 } 271 GetFontSizeState()272 bool GetFontSizeState() const 273 { 274 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 275 return style.fontSizeDefined; 276 } 277 GetHeightState()278 bool GetHeightState() const 279 { 280 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 281 return style.heightDefined; 282 } 283 GetRadiusState()284 bool GetRadiusState() const 285 { 286 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 287 return style.radiusDefined; 288 } 289 GetBlendOpacity()290 double GetBlendOpacity() const 291 { 292 auto& style = static_cast<ButtonStyle&>(GetStyle(StyleTag::SPECIALIZED_STYLE)); 293 return style.blendOpacity; 294 } 295 GetClickedEventId()296 const EventMarker& GetClickedEventId() const 297 { 298 auto& event = static_cast<ButtonEvent&>(GetEvent(EventTag::SPECIALIZED_EVENT)); 299 return event.clickEventId; 300 } 301 GetRemoteMessageEventId()302 const EventMarker& GetRemoteMessageEventId() const 303 { 304 auto& event = static_cast<ButtonEvent&>(GetEvent(EventTag::SPECIALIZED_REMOTE_MESSAGE_EVENT)); 305 return event.clickEventId; 306 } 307 GetButtonController()308 const RefPtr<ButtonProgressController> GetButtonController() const 309 { 310 return buttonController_; 311 } 312 SetType(const std::string & type)313 void SetType(const std::string& type) 314 { 315 auto& attribute = MaybeResetAttribute<ButtonAttribute>(AttributeTag::SPECIALIZED_ATTR); 316 attribute.buttonType = type; 317 } 318 SetIconSrc(const std::string & src)319 void SetIconSrc(const std::string& src) 320 { 321 auto& attribute = MaybeResetAttribute<ButtonAttribute>(AttributeTag::SPECIALIZED_ATTR); 322 attribute.iconSrc = src; 323 } 324 SetPlacement(const std::string & placement)325 void SetPlacement(const std::string& placement) 326 { 327 auto& attribute = MaybeResetAttribute<ButtonAttribute>(AttributeTag::SPECIALIZED_ATTR); 328 attribute.placement = placement; 329 } 330 SetTextData(const std::string & data)331 void SetTextData(const std::string& data) 332 { 333 auto& attribute = MaybeResetAttribute<ButtonAttribute>(AttributeTag::SPECIALIZED_ATTR); 334 attribute.textData = data; 335 } 336 SetAutoFocusState(bool state)337 void SetAutoFocusState(bool state) 338 { 339 auto& attribute = MaybeResetAttribute<ButtonAttribute>(AttributeTag::SPECIALIZED_ATTR); 340 attribute.isAutoFocus = state; 341 } 342 SetDisabledState(bool state)343 void SetDisabledState(bool state) 344 { 345 auto& attribute = MaybeResetAttribute<ButtonAttribute>(AttributeTag::SPECIALIZED_ATTR); 346 attribute.isDisabled = state; 347 } 348 SetWaitingState(bool state)349 void SetWaitingState(bool state) 350 { 351 auto& attribute = MaybeResetAttribute<ButtonAttribute>(AttributeTag::SPECIALIZED_ATTR); 352 attribute.isWaiting = state; 353 } 354 SetPadding(const Edge & edge)355 void SetPadding(const Edge& edge) 356 { 357 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 358 style.padding = edge; 359 } 360 SetTextStyle(const TextStyle & textStyle)361 void SetTextStyle(const TextStyle& textStyle) 362 { 363 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 364 style.textStyle = textStyle; 365 } 366 SetMinWidth(const Dimension & width)367 void SetMinWidth(const Dimension& width) 368 { 369 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 370 style.minWidth = width; 371 } 372 SetWidth(const Dimension & width)373 void SetWidth(const Dimension& width) 374 { 375 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 376 style.width = width; 377 } 378 SetHeight(const Dimension & height)379 void SetHeight(const Dimension& height) 380 { 381 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 382 style.height = height; 383 } 384 SetRectRadius(const Dimension & radius)385 void SetRectRadius(const Dimension& radius) 386 { 387 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 388 style.radius = radius; 389 } 390 SetProgressDiameter(const Dimension & diameter)391 void SetProgressDiameter(const Dimension& diameter) 392 { 393 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 394 style.diameter = diameter; 395 } 396 SetInnerPadding(const Dimension & padding)397 void SetInnerPadding(const Dimension& padding) 398 { 399 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 400 style.innerLeftPadding = padding; 401 } 402 SetIconHeight(const Dimension & height)403 void SetIconHeight(const Dimension& height) 404 { 405 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 406 style.iconHeight = height; 407 } 408 SetIconWidth(const Dimension & width)409 void SetIconWidth(const Dimension& width) 410 { 411 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 412 style.iconWidth = width; 413 } 414 SetBackgroundColor(const Color & color)415 void SetBackgroundColor(const Color& color) 416 { 417 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 418 style.backgroundColor = color; 419 } 420 SetClickedColor(const Color & color)421 void SetClickedColor(const Color& color) 422 { 423 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 424 style.clickedColor = color; 425 } 426 SetDisabledColor(const Color & color)427 void SetDisabledColor(const Color& color) 428 { 429 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 430 style.disabledColor = color; 431 } 432 SetDisabledTextColor(const Color & color)433 void SetDisabledTextColor(const Color& color) 434 { 435 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 436 style.disabledTextColor = color; 437 } 438 SetFocusColor(const Color & color)439 void SetFocusColor(const Color& color) 440 { 441 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 442 style.focusColor = color; 443 } 444 SetHoverColor(const Color & color)445 void SetHoverColor(const Color& color) 446 { 447 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 448 style.hoverColor = color; 449 } 450 SetProgressColor(const Color & color)451 void SetProgressColor(const Color& color) 452 { 453 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 454 style.progressColor = color; 455 } 456 SetProgressFocusColor(const Color & color)457 void SetProgressFocusColor(const Color& color) 458 { 459 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 460 style.progressFocusColor = color; 461 } 462 SetFocusAnimationColor(const Color & color)463 void SetFocusAnimationColor(const Color& color) 464 { 465 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 466 style.focusAnimationColor = color; 467 } 468 SetFocusTextColor(const Color & color)469 void SetFocusTextColor(const Color& color) 470 { 471 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 472 style.textFocusColor = color; 473 } 474 SetBorderEdge(const BorderEdge & borderEdge)475 void SetBorderEdge(const BorderEdge& borderEdge) 476 { 477 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 478 style.borderEdge = borderEdge; 479 } 480 SetMatchTextDirection(bool match)481 void SetMatchTextDirection(bool match) 482 { 483 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 484 style.matchTextDirection = match; 485 } 486 SetBgColorState(bool state)487 void SetBgColorState(bool state) 488 { 489 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 490 style.bgColorDefined = state; 491 } 492 SetTextColorState(bool state)493 void SetTextColorState(bool state) 494 { 495 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 496 style.textColorDefined = state; 497 } 498 SetFontSizeState(bool state)499 void SetFontSizeState(bool state) 500 { 501 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 502 style.fontSizeDefined = state; 503 } 504 SetHeightState(bool state)505 void SetHeightState(bool state) 506 { 507 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 508 style.heightDefined = state; 509 } 510 SetRadiusState(bool state)511 void SetRadiusState(bool state) 512 { 513 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 514 style.radiusDefined = state; 515 } 516 SetBlendOpacity(double opacity)517 void SetBlendOpacity(double opacity) 518 { 519 auto& style = MaybeResetStyle<ButtonStyle>(StyleTag::SPECIALIZED_STYLE); 520 style.blendOpacity = opacity; 521 } 522 SetClickedEventId(const EventMarker & eventId)523 void SetClickedEventId(const EventMarker& eventId) 524 { 525 auto& event = MaybeResetEvent<ButtonEvent>(EventTag::SPECIALIZED_EVENT); 526 event.clickEventId = eventId; 527 } 528 SetRemoteMessageEventId(const EventMarker & eventId)529 void SetRemoteMessageEventId(const EventMarker& eventId) 530 { 531 auto& event = MaybeResetEvent<ButtonEvent>(EventTag::SPECIALIZED_REMOTE_MESSAGE_EVENT); 532 event.clickEventId = eventId; 533 } 534 SetClickedFunction(std::function<void ()> && callback)535 void SetClickedFunction(std::function<void()>&& callback) 536 { 537 auto& event = MaybeResetEvent<ButtonEvent>(EventTag::SPECIALIZED_EVENT); 538 event.clickEventId.SetPreFunction(std::move(callback)); 539 } 540 SetButtonController(const RefPtr<ButtonProgressController> & controller)541 void SetButtonController(const RefPtr<ButtonProgressController>& controller) 542 { 543 buttonController_ = controller; 544 } 545 546 protected: 547 void InitSpecialized() override; 548 bool SetSpecializedAttr(const std::pair<std::string, std::string>& attr) override; 549 bool SetSpecializedStyle(const std::pair<std::string, std::string>& style) override; 550 bool SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event) override; 551 void CallSpecializedMethod(const std::string& method, const std::string& args) override; 552 553 private: 554 RefPtr<ButtonProgressController> buttonController_; 555 }; 556 557 } // namespace OHOS::Ace 558 559 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_BUTTON_BUTTON_DECLARATION_H 560