From c341b1c4496c4533e57dbaa9c110c2eeab7d7190 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 2 Mar 2023 16:37:56 +0400 Subject: [PATCH] Disable drawing animations when energy saving --- submodules/DrawingUI/Sources/DrawingPenTool.swift | 15 +++++++++++---- submodules/DrawingUI/Sources/DrawingScreen.swift | 1 + submodules/DrawingUI/Sources/DrawingView.swift | 14 ++++++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/submodules/DrawingUI/Sources/DrawingPenTool.swift b/submodules/DrawingUI/Sources/DrawingPenTool.swift index d31bb660d8..a132bb6b64 100644 --- a/submodules/DrawingUI/Sources/DrawingPenTool.swift +++ b/submodules/DrawingUI/Sources/DrawingPenTool.swift @@ -140,7 +140,7 @@ final class PenTool: DrawingElement { self.start = newStart } - if !element.isEraser && !element.isBlur { + if element.hasAnimations { let count = CGFloat(element.segments.count - self.segmentsCount) if count > 0 { let dryingPath = CGMutablePath() @@ -193,7 +193,7 @@ final class PenTool: DrawingElement { return } - if !element.isEraser && !element.isBlur { + if element.hasAnimations { let dryingPath = CGMutablePath() for segment in element.activeSegments { let segmentPath = element.pathForSegment(segment) @@ -216,7 +216,7 @@ final class PenTool: DrawingElement { context.scaleBy(x: 1.0 / parent.drawScale.width, y: 1.0 / parent.drawScale.height) element.drawSegments(in: context, from: parent.start, to: parent.segmentsCount) - if !element.isEraser && !element.isBlur { + if element.hasAnimations { element.drawActiveSegments(in: context, strokeWidth: !parent.isActiveDrying ? element.renderLineWidth * parent.dryingFactor : nil) } else { element.drawActiveSegments(in: context, strokeWidth: nil) @@ -266,6 +266,12 @@ final class PenTool: DrawingElement { private var segmentPaths: [Int: CGPath] = [:] private var useCubicBezier = true + + private let animationsEnabled: Bool + + var hasAnimations: Bool { + return self.animationsEnabled && !self.isEraser && !self.isBlur + } var isValid: Bool { if self.hasArrow { @@ -284,7 +290,7 @@ final class PenTool: DrawingElement { return normalizeDrawingRect(combinedBounds, drawingSize: self.drawingSize) } - required init(drawingSize: CGSize, color: DrawingColor, lineWidth: CGFloat, hasArrow: Bool, isEraser: Bool, isBlur: Bool, blurredImage: UIImage?) { + required init(drawingSize: CGSize, color: DrawingColor, lineWidth: CGFloat, hasArrow: Bool, isEraser: Bool, isBlur: Bool, blurredImage: UIImage?, animationsEnabled: Bool) { self.uuid = UUID() self.drawingSize = drawingSize self.color = isEraser || isBlur ? DrawingColor(rgb: 0x000000) : color @@ -292,6 +298,7 @@ final class PenTool: DrawingElement { self.isEraser = isEraser self.isBlur = isBlur self.blurredImage = blurredImage + self.animationsEnabled = animationsEnabled let minLineWidth = max(1.0, max(drawingSize.width, drawingSize.height) * 0.002) let maxLineWidth = max(10.0, max(drawingSize.width, drawingSize.height) * 0.07) diff --git a/submodules/DrawingUI/Sources/DrawingScreen.swift b/submodules/DrawingUI/Sources/DrawingScreen.swift index 087f0c124a..995cdc3864 100644 --- a/submodules/DrawingUI/Sources/DrawingScreen.swift +++ b/submodules/DrawingUI/Sources/DrawingScreen.swift @@ -1994,6 +1994,7 @@ public class DrawingScreen: ViewController, TGPhotoDrawingInterfaceController, U var drawingView: DrawingView { if self._drawingView == nil, let controller = self.controller { self._drawingView = DrawingView(size: controller.size) + self._drawingView?.animationsEnabled = self.context.sharedContext.energyUsageSettings.fullTranslucency self._drawingView?.shouldBegin = { [weak self] _ in if let strongSelf = self { if strongSelf._entitiesView?.hasSelection == true { diff --git a/submodules/DrawingUI/Sources/DrawingView.swift b/submodules/DrawingUI/Sources/DrawingView.swift index 142032d07c..58a57dd239 100644 --- a/submodules/DrawingUI/Sources/DrawingView.swift +++ b/submodules/DrawingUI/Sources/DrawingView.swift @@ -103,6 +103,8 @@ public final class DrawingView: UIView, UIGestureRecognizerDelegate, UIPencilInt private var isDrawing = false private var drawingGestureStartTimestamp: Double? + var animationsEnabled = true + private func loadTemplates() { func load(_ name: String) { if let url = getAppBundle().url(forResource: name, withExtension: "json"), @@ -920,7 +922,8 @@ public final class DrawingView: UIView, UIGestureRecognizerDelegate, UIPencilInt hasArrow: false, isEraser: false, isBlur: false, - blurredImage: nil + blurredImage: nil, + animationsEnabled: self.animationsEnabled ) element = penTool case .arrow: @@ -931,7 +934,8 @@ public final class DrawingView: UIView, UIGestureRecognizerDelegate, UIPencilInt hasArrow: true, isEraser: false, isBlur: false, - blurredImage: nil + blurredImage: nil, + animationsEnabled: self.animationsEnabled ) element = penTool case .marker: @@ -956,7 +960,8 @@ public final class DrawingView: UIView, UIGestureRecognizerDelegate, UIPencilInt hasArrow: false, isEraser: false, isBlur: true, - blurredImage: self.preparedBlurredImage + blurredImage: self.preparedBlurredImage, + animationsEnabled: self.animationsEnabled ) element = penTool case .eraser: @@ -967,7 +972,8 @@ public final class DrawingView: UIView, UIGestureRecognizerDelegate, UIPencilInt hasArrow: false, isEraser: true, isBlur: false, - blurredImage: nil + blurredImage: nil, + animationsEnabled: self.animationsEnabled ) element = penTool }