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_CLOCK_RENDER_CLOCK_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CLOCK_RENDER_CLOCK_H 18 19 #include "base/utils/time_util.h" 20 #include "core/components/declaration/clock/clock_declaration.h" 21 #include "core/components/image/render_image.h" 22 #include "core/components/text/render_text.h" 23 #include "core/components/text/text_component.h" 24 #include "core/pipeline/base/render_node.h" 25 26 namespace OHOS::Ace { 27 28 using DayToNightCallback = std::function<void()>; 29 using NightToDayCallback = std::function<void()>; 30 using OnHourCallback = std::function<void(const std::string& hour)>; 31 using AccessibilityTimeCallback = std::function<void(double hour, double minute)>; 32 33 class RenderClockHand : public RenderNode { 34 DECLARE_ACE_TYPE(RenderClockHand, RenderNode); 35 36 public: 37 static RefPtr<RenderNode> Create(); Update(const RefPtr<Component> & component)38 void Update(const RefPtr<Component>& component) override {} 39 void PerformLayout() override; SetHourHand(const RefPtr<RenderImage> & renderHourHand)40 void SetHourHand(const RefPtr<RenderImage>& renderHourHand) 41 { 42 renderHourHand_ = renderHourHand; 43 AddChild(renderHourHand_); 44 } SetMinuteHand(const RefPtr<RenderImage> & renderMinuteHand)45 void SetMinuteHand(const RefPtr<RenderImage>& renderMinuteHand) 46 { 47 renderMinuteHand_ = renderMinuteHand; 48 AddChild(renderMinuteHand_); 49 } SetSecondHand(const RefPtr<RenderImage> & renderSecondHand)50 void SetSecondHand(const RefPtr<RenderImage>& renderSecondHand) 51 { 52 renderSecondHand_ = renderSecondHand; 53 AddChild(renderSecondHand_); 54 } SetHoursWest(double hoursWest)55 void SetHoursWest(double hoursWest) 56 { 57 hoursWest_ = hoursWest; 58 } SetDayToNightCallback(const DayToNightCallback & dayToNightCallback)59 void SetDayToNightCallback(const DayToNightCallback& dayToNightCallback) 60 { 61 dayToNightCallback_ = dayToNightCallback; 62 } SetNightToDayCallback(const NightToDayCallback & nightToDayCallback)63 void SetNightToDayCallback(const NightToDayCallback& nightToDayCallback) 64 { 65 nightToDayCallback_ = nightToDayCallback; 66 } SetOnHourCallback(const OnHourCallback & hourCallback)67 void SetOnHourCallback(const OnHourCallback& hourCallback) 68 { 69 onHourCallback_ = hourCallback; 70 } SetAccessibilityTimeCallback(const AccessibilityTimeCallback & accessibilityTimeCallback)71 void SetAccessibilityTimeCallback(const AccessibilityTimeCallback& accessibilityTimeCallback) 72 { 73 accessibilityTimeCallback_ = accessibilityTimeCallback; 74 } SetIsDay(bool isLight)75 void SetIsDay(bool isLight) 76 { 77 isDay_ = isLight; 78 } GetIsDay()79 bool GetIsDay() const 80 { 81 return isDay_; 82 } SetNeedStop(bool needStop)83 void SetNeedStop(bool needStop) 84 { 85 needStop_ = needStop; 86 } 87 RequestRenderForNextSecond()88 virtual void RequestRenderForNextSecond() {} 89 90 protected: 91 RefPtr<RenderImage> renderHourHand_; 92 RefPtr<RenderImage> renderMinuteHand_; 93 RefPtr<RenderImage> renderSecondHand_; 94 // hours west of Greenwich, for e.g., [hoursWest] is [-8] in UTC+8. 95 // Valid range of [hoursWest] is [-14, 12]. Set default value to DBL_MAX to use current time zone by default. 96 double hoursWest_ = DBL_MAX; 97 DayToNightCallback dayToNightCallback_; 98 NightToDayCallback nightToDayCallback_; 99 OnHourCallback onHourCallback_; 100 AccessibilityTimeCallback accessibilityTimeCallback_; 101 bool isDay_ = true; 102 bool needStop_ = false; 103 }; 104 105 class RenderClock : public RenderNode { 106 DECLARE_ACE_TYPE(RenderClock, RenderNode); 107 108 public: 109 ~RenderClock() override = default; 110 111 static RefPtr<RenderNode> Create(); 112 void Update(const RefPtr<Component>& component) override; 113 void PerformLayout() override; 114 void CalculateLayoutSize(); 115 void UpdateAccessibilityInfo(double hour, double minute); 116 void UpdateRenderText(double digitSize, const Color& digitColor); 117 void UpdateRenderImage(RefPtr<RenderImage>& renderImage, const std::string& imageSrc); 118 static void LayoutClockImage(const RefPtr<RenderImage>& renderImage, const Size& imageComponentSize); 119 void UseNightConfig(); 120 void UseDayConfig(); 121 void CheckNightConfig(); 122 123 protected: 124 RenderClock(); 125 126 bool setScreenCallback_ = false; 127 std::string clockFaceNightSrc_; 128 std::string hourHandNightSrc_; 129 std::string minuteHandNightSrc_; 130 std::string secondHandNightSrc_; 131 Color digitColorNight_ = Color::TRANSPARENT; 132 133 RefPtr<RenderClockHand> renderClockHand_; 134 RefPtr<RenderImage> renderClockFace_; 135 RefPtr<RenderImage> renderHourHand_; 136 RefPtr<RenderImage> renderMinuteHand_; 137 RefPtr<RenderImage> renderSecondHand_; 138 std::vector<RefPtr<RenderText>> digitRenderNodes_; 139 std::vector<RefPtr<TextComponent>> digitComponentNodes_; 140 std::vector<double> radians_; 141 Offset paintOffset_; 142 Dimension defaultSize_; 143 // ratio of digit-radius and half of side length of clock-face-image. 144 // digit-radius is used to calculate digit offset whose range is (0, 1]. 145 // e.g., when size of clock-face-image is 200 x 200, digit "3" is [200 / 2 x 0.7 = 70] right of the center. 146 double digitRadiusRatio_ = 0.7; 147 // ratio of digit-size and side length of clock-face-image, which is used to decide font-size of digit. 148 // e.g., when size of clock-face-image is 200 x 200, font-size of digit is 200 x 0.08 = 16 149 // its range is (0, 1.0 / 7.0]. 150 double digitSizeRatio_ = 0.08; 151 Size drawSize_; 152 RefPtr<ClockDeclaration> declaration_; 153 }; 154 155 } // namespace OHOS::Ace 156 157 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_CLOCK_RENDER_CLOCK_H 158