react-native-macos/React/Views/RCTSlider.m

331 строка
9.7 KiB
Mathematica
Исходник Обычный вид История

/**
Squashed commit of the following: commit 4c43e301d97042e9eec4401dd81b8ebce053f691 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:49:02 2019 -0700 Squashed commit of the following: commit 862f3fb5d9e3161feab6e5e0e61c7b28e8ac1ef9 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:37 2019 -0700 Revert "Merge 1b12e2caaadf094ce9327deb7e5fe49297e8a115 into 9f1e90947421ad0ea2de1b534117c500dc8c9744" This reverts commit d036bc4c5768d6a3327c5766e1791719cb11693f, reversing changes made to 9f1e90947421ad0ea2de1b534117c500dc8c9744. commit 41477c33c1bcc78f40da00ac7e86ab4dd3f7d062 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:23 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.1" This reverts commit 852df1af0e489063db15d74f0d241ea930778b9c. commit 4993963205e737849e4880309ef86aeac9f2a765 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:16 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.2" This reverts commit 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 41f8beadb2030593208ffc6ff29339991dde3798 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:46:07 2019 -0700 Revert "Merge 4f3c37886c3f8d0af9571dbed0ec1361d51219a6 into 774513fec948ee5595543e23ab4e7b9cccbdcbf1" This reverts commit a84049078437ce6e0852a30367b4c2d56be7b884, reversing changes made to 774513fec948ee5595543e23ab4e7b9cccbdcbf1. commit 143e70ec6228e110cd94a5ae9c56309040c1f1eb Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:57 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.3" This reverts commit 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 138191b0d6bf9e4370fb5126dc2095a3fb122a57 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:45:16 2019 -0700 Revert "Merge d8be239facee73f3ba7b664154766dbfb8049050 into 54dfee820d107cb897c5aca14131cf7fbde616d7" This reverts commit faae1e56f805e2fd953218be92b2c0d1753a7c3c, reversing changes made to 54dfee820d107cb897c5aca14131cf7fbde616d7. commit 40a77c8365c5273df0bba4fc2b0439db17d247d0 Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:44 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.4" This reverts commit 043f6330e7a7fe7a73ac4e406c157f61cf1e7f1c. commit adf5c26c8f7132a6643ecfea116226632173447b Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:42 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.5" This reverts commit 4004f889c28f42535b9cc08d439311b2f5802359. commit e68d497a58e32d26f0d04130895954d7b654c2bf Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:39 2019 -0700 Revert "Fix the builds... (#20)" This reverts commit 745eeda26fafa79fd9110e20647ace54c80d3dab. commit 4cdef1eef22edbcdc9ca86e31846a175b944999e Author: REDMOND\acoates <acoates@microsoft.com> Date: Tue Mar 26 10:34:31 2019 -0700 Revert "Applying package update to 0.58.6-microsoft.6" This reverts commit 34a307738806da39704d7f45428920144e0bed53.
2019-03-26 20:54:53 +03:00
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTSlider.h"
2019-03-01 21:09:07 +03:00
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
#import <Quartz/Quartz.h>
@protocol RCTSliderCellDelegate <NSObject>
@optional
- (void)sliderCell:(NSSliderCell *)sliderCell didPress:(BOOL)press;
@end
@interface RCTSliderCell : NSSliderCell
@property (nonatomic, assign) BOOL pressed;
@property (nonatomic, weak) id<RCTSliderCellDelegate> delegate;
@property (nonatomic, strong) NSImage *knobImage;
@property (nonatomic, strong) NSImage *minimumValueImage;
@property (nonatomic, strong) NSImage *maximumValueImage;
@property (nonatomic, strong) NSColor *minimumTrackTintColor;
@property (nonatomic, strong) NSColor *maximumTrackTintColor;
@property (nonatomic, assign) NSRect currentKnobRect;
@end
@implementation RCTSliderCell
- (void)setPressed:(BOOL)pressed
{
if (pressed == _pressed) {
return;
}
_pressed = pressed;
id<RCTSliderCellDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(sliderCell:didPress:)]) {
[delegate sliderCell:self didPress:_pressed];
}
}
- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
{
self.pressed = YES;
BOOL val = [super startTrackingAt:startPoint inView:controlView];
[self drawInteriorWithFrame:controlView.bounds inView:controlView];
return val;
}
- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
{
self.pressed = NO;
[super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
}
- (void)drawKnob:(NSRect)knobRect
{
if (!_knobImage) {
[super drawKnob:knobRect];
return;
}
CGFloat dx = (knobRect.size.width - _knobImage.size.width) / 2.0;
CGFloat dy = (knobRect.size.height - _knobImage.size.height) / 2.0;
_currentKnobRect = CGRectInset(knobRect, dx, dy);
[_knobImage drawInRect:NSIntegralRect(_currentKnobRect)];
}
- (void)drawBarInside:(NSRect)cellFrame flipped:(BOOL)flipped
{
if (![self usesCustomTrackImage]) {
[super drawBarInside:cellFrame flipped:flipped];
return;
}
NSRect trackRect = cellFrame;
trackRect.size.height = CGRectGetHeight(cellFrame);
trackRect = NSOffsetRect(trackRect, trunc(NSMidX(cellFrame) - NSMidX(trackRect)), trunc(NSMidY(cellFrame) - NSMidY(trackRect)));
CGFloat value = self.doubleValue / (self.maxValue - self.minValue);
NSRect beforeKnobRect, afterKnobRect;
NSDivideRect(trackRect, &beforeKnobRect, &afterKnobRect, value * NSWidth(cellFrame), NSMinXEdge);
[_minimumValueImage drawInRect:NSIntegralRect(beforeKnobRect)];
[_maximumValueImage drawInRect:NSIntegralRect(afterKnobRect)];
if (_minimumTrackTintColor) {
[_minimumTrackTintColor set];
NSRectFill(beforeKnobRect);
}
if (_maximumTrackTintColor) {
[_maximumTrackTintColor set];
NSRectFill(afterKnobRect);
}
}
- (BOOL)usesCustomTrackImage
{
return _minimumValueImage || _maximumValueImage || _minimumTrackTintColor || _maximumTrackTintColor;
}
@end
#endif
#if TARGET_OS_OSX
@interface RCTSlider () <RCTSliderCellDelegate>
@end
#endif // ]TODO(macOS ISS#2323203)
@implementation RCTSlider
{
float _unclippedValue;
}
2019-03-01 21:09:07 +03:00
#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
- (instancetype)initWithFrame:(NSRect)frameRect
{
if (self = [super initWithFrame:frameRect]) {
self.cell.controlSize = NSRegularControlSize;
((RCTSliderCell*)self.cell).delegate = self;
}
return self;
}
+ (Class)cellClass
{
return RCTSliderCell.class;
}
- (float)value
{
return self.floatValue;
}
- (void)setValue:(float)value animated:(__unused BOOL)animated
{
self.animator.floatValue = value;
}
- (void)setMinimumTrackTintColor:(NSColor *)minimumTrackTintColor
{
((RCTSliderCell*)self.cell).minimumTrackTintColor = minimumTrackTintColor;
}
- (NSColor*)minimumTrackTintColor
{
return ((RCTSliderCell*)self.cell).minimumTrackTintColor;
}
- (void)setMaximumTrackTintColor:(NSColor *)maximumTrackTintColor
{
((RCTSliderCell*)self.cell).maximumTrackTintColor = maximumTrackTintColor;
}
- (NSColor*)maximumTrackTintColor
{
return ((RCTSliderCell*)self.cell).maximumTrackTintColor;
}
- (void)setNeedsDisplayInRect:(__unused NSRect)invalidRect
{
[super setNeedsDisplayInRect:self.bounds];
}
- (void)sliderCell:(__unused NSSliderCell *)sliderCell didPress:(BOOL)press
{
id<RCTSliderDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(slider:didPress:)]) {
[delegate slider:self didPress:press];
}
}
/* This method is part of the NSAnimatablePropertyContainer which is adopted by NSView (and hence all its
* subclasses. It is used to retrieve the default animation that should be performed to animate a given
* property. If no default animation is provided, the property is not considered implicitly animatable.
*
* By default NSSlider does not provide an implicit animation for its "floatValue" property. So, we will
* provide one here thus making the "floatValue" animatable.
*/
+ (id)defaultAnimationForKey:(NSString *)key
{
if ([key isEqualToString:@"floatValue"]) {
// By default, use simple linear interpolation.
return [CABasicAnimation animation];
}
/* You may wish to add handlers here for the other many properties that can affect a slider's value
* such as intValue, doubleValue, ... */
else {
// Defer to super's implementation for any keys we don't specifically handle.
return [super defaultAnimationForKey:key];
}
}
#endif // ]TODO(macOS ISS#2323203)
- (void)setValue:(float)value
{
_unclippedValue = value;
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
super.value = value;
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
self.floatValue = value;
#endif // ]TODO(macOS ISS#2323203)
}
- (void)setMinimumValue:(float)minimumValue
{
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
super.minimumValue = minimumValue;
super.value = _unclippedValue;
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
_minimumValue = minimumValue;
self.minValue = minimumValue;
self.floatValue = _unclippedValue;
#endif // ]TODO(macOS ISS#2323203)
}
- (void)setMaximumValue:(float)maximumValue
{
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
super.maximumValue = maximumValue;
super.value = _unclippedValue;
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
_maximumValue = maximumValue;
self.maxValue = maximumValue;
self.floatValue = _unclippedValue;
#endif // ]TODO(macOS ISS#2323203)
}
- (void)setTrackImage:(UIImage *)trackImage
{
if (trackImage != _trackImage) {
_trackImage = trackImage;
CGFloat width = trackImage.size.width / 2;
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
UIImage *minimumTrackImage = [trackImage resizableImageWithCapInsets:(UIEdgeInsets){
0, width, 0, width
} resizingMode:UIImageResizingModeStretch];
UIImage *maximumTrackImage = [trackImage resizableImageWithCapInsets:(UIEdgeInsets){
0, width, 0, width
} resizingMode:UIImageResizingModeStretch];
[self setMinimumTrackImage:minimumTrackImage forState:UIControlStateNormal];
[self setMaximumTrackImage:maximumTrackImage forState:UIControlStateNormal];
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
trackImage.capInsets = NSEdgeInsetsMake(0, width-0.5, 0, width-0.5);
trackImage.resizingMode = NSImageResizingModeStretch;
((RCTSliderCell*)self.cell).minimumValueImage = trackImage;
((RCTSliderCell*)self.cell).maximumValueImage = trackImage;
#endif // ]TODO(macOS ISS#2323203)
}
}
- (void)setMinimumTrackImage:(UIImage *)minimumTrackImage
{
_trackImage = nil;
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
minimumTrackImage = [minimumTrackImage resizableImageWithCapInsets:(UIEdgeInsets){
0, minimumTrackImage.size.width, 0, 0
} resizingMode:UIImageResizingModeStretch];
[self setMinimumTrackImage:minimumTrackImage forState:UIControlStateNormal];
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
minimumTrackImage.capInsets = NSEdgeInsetsMake(0, minimumTrackImage.size.width-0.5, 0, 0);
minimumTrackImage.resizingMode = NSImageResizingModeStretch;
((RCTSliderCell*)self.cell).minimumValueImage = minimumTrackImage;
#endif // ]TODO(macOS ISS#2323203)
}
- (UIImage *)minimumTrackImage
{
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
return [self thumbImageForState:UIControlStateNormal];
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
return ((RCTSliderCell*)self.cell).minimumValueImage;
#endif // ]TODO(macOS ISS#2323203)
}
- (void)setMaximumTrackImage:(UIImage *)maximumTrackImage
{
_trackImage = nil;
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
maximumTrackImage = [maximumTrackImage resizableImageWithCapInsets:(UIEdgeInsets){
0, 0, 0, maximumTrackImage.size.width
} resizingMode:UIImageResizingModeStretch];
[self setMaximumTrackImage:maximumTrackImage forState:UIControlStateNormal];
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
maximumTrackImage.capInsets = NSEdgeInsetsMake(0, 0, 0, maximumTrackImage.size.width-0.5);
maximumTrackImage.resizingMode = NSImageResizingModeStretch;
((RCTSliderCell*)self.cell).maximumValueImage = maximumTrackImage;
#endif // ]TODO(macOS ISS#2323203)
}
- (UIImage *)maximumTrackImage
{
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
return [self thumbImageForState:UIControlStateNormal];
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
return ((RCTSliderCell*)self.cell).maximumValueImage;
#endif // ]TODO(macOS ISS#2323203)
}
- (void)setThumbImage:(UIImage *)thumbImage
{
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
[self setThumbImage:thumbImage forState:UIControlStateNormal];
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
((RCTSliderCell*)self.cell).knobImage = thumbImage;
#endif // ]TODO(macOS ISS#2323203)
}
- (UIImage *)thumbImage
{
2019-03-01 21:09:07 +03:00
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
return [self thumbImageForState:UIControlStateNormal];
2019-03-01 21:09:07 +03:00
#else // [TODO(macOS ISS#2323203)
return ((RCTSliderCell*)self.cell).knobImage;
#endif // ]TODO(macOS ISS#2323203)
}
@end