Skip to content

Commit

Permalink
[Slider] Add appearance properties and enable UIAppearance. (#2796)
Browse files Browse the repository at this point in the history
* Add Slider appearance properties and enable UIAppearance.

Adds `thumbRadius` and `thumbElevation` as UIAppearance properties.

Makes `color`, `disabledColor`, and `trackBackgroundColor` UIAppearance-compatible.

* Added unit tests for slider.

* Remove infeasible coding unit test for now.

* Line-length formatting.

* Rename and condense color tests, sort by order that they appear in the header.

* Rename tests and mark the thumb section.

* Replace `_thumbTrack.thumbRadius` usage with `self.thumbRadius`.

* Remove NSCoding-related helper, moved to another branch.

* Search-and-replace mixup.

* Line-length fixups.
  • Loading branch information
ajsecord authored Jan 7, 2018
1 parent a63e861 commit 294643f
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 209 deletions.
1 change: 1 addition & 0 deletions MaterialComponents.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ Pod::Spec.new do |mdc|
component.source_files = "components/#{component.base_name}/src/*.{h,m}", "components/#{component.base_name}/src/private/*.{h,m}"

component.dependency "MaterialComponents/Palettes"
component.dependency "MaterialComponents/ShadowElevations"
component.dependency "MaterialComponents/private/ThumbTrack"
end

Expand Down
25 changes: 20 additions & 5 deletions components/Slider/src/MDCSlider.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>

#import "MaterialShadowElevations.h"

@protocol MDCSliderDelegate;

/**
Expand All @@ -31,7 +33,7 @@
set the right and left track images (if you wanted a custom track)
set the right (background track) color
Same features
set color for thumb via @c thumbColor
set color for thumb via @c color
set color of track via @c trackColor
New features
making the slider a snap to discrete values via @c numberOfDiscreteValues.
Expand All @@ -47,22 +49,35 @@ IB_DESIGNABLE
Default color is blue.
*/
@property(nonatomic, strong, null_resettable) UIColor *color;
@property(nonatomic, strong, null_resettable) UIColor *color UI_APPEARANCE_SELECTOR;

/**
The color of the cursor (thumb) and track while the slider is disabled.
Default color is gray.
*/
@property(nonatomic, strong, null_resettable) UIColor *disabledColor;
@property(nonatomic, strong, null_resettable) UIColor *disabledColor UI_APPEARANCE_SELECTOR;

/*
/**
The color of the unfilled track that the cursor moves along (right side).
Default color is gray.
*/
@property(nonatomic, strong, null_resettable) UIColor *trackBackgroundColor;
@property(nonatomic, strong, null_resettable) UIColor *trackBackgroundColor UI_APPEARANCE_SELECTOR;

/**
The radius of the cursor (thumb).
Default value is 6 points.
*/
@property(nonatomic, assign) CGFloat thumbRadius UI_APPEARANCE_SELECTOR;

/**
The elevation of the cursor (thumb).
Default value is MDCElevationNone.
*/
@property(nonatomic, assign) MDCShadowElevation thumbElevation UI_APPEARANCE_SELECTOR;
/**
The number of discrete values that the slider can take.
Expand Down
22 changes: 19 additions & 3 deletions components/Slider/src/MDCSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
static const CGFloat kSliderDefaultWidth = 100.0f;
static const CGFloat kSliderFrameHeight = 27.0f;
static const CGFloat kSliderMinTouchSize = 48.0f;
static const CGFloat kSliderThumbRadius = 6.0f;
static const CGFloat kSliderDefaultThumbRadius = 6.0f;
static const CGFloat kSliderAccessibilityIncrement = 0.1f; // Matches UISlider's percent increment.
static const CGFloat kSliderLightThemeTrackAlpha = 0.26f;

Expand Down Expand Up @@ -59,7 +59,7 @@ - (void)commonMDCSliderInit {
_thumbTrack.delegate = self;
_thumbTrack.disabledTrackHasThumbGaps = YES;
_thumbTrack.trackEndsAreInset = YES;
_thumbTrack.thumbRadius = kSliderThumbRadius;
_thumbTrack.thumbRadius = kSliderDefaultThumbRadius;
_thumbTrack.thumbIsSmallerWhenDisabled = YES;
_thumbTrack.thumbIsHollowAtStart = YES;
_thumbTrack.thumbGrowsWhenDragging = YES;
Expand Down Expand Up @@ -117,6 +117,22 @@ - (UIColor *)color {
return _thumbTrack.primaryColor;
}

- (void)setThumbRadius:(CGFloat)thumbRadius {
_thumbTrack.thumbRadius = thumbRadius;
}

- (CGFloat)thumbRadius {
return _thumbTrack.thumbRadius;
}

- (void)setThumbElevation:(MDCShadowElevation)thumbElevation {
_thumbTrack.thumbElevation = thumbElevation;
}

- (MDCShadowElevation)thumbElevation {
return _thumbTrack.thumbElevation;
}

- (NSUInteger)numberOfDiscreteValues {
return _thumbTrack.numDiscreteValues;
}
Expand Down Expand Up @@ -227,7 +243,7 @@ - (CGSize)intrinsicContentSize {
}

- (BOOL)pointInside:(CGPoint)point withEvent:(__unused UIEvent *)event {
CGFloat dx = MIN(0, kSliderThumbRadius - kSliderMinTouchSize / 2);
CGFloat dx = MIN(0, self.thumbRadius - kSliderMinTouchSize / 2);
CGFloat dy = MIN(0, (self.bounds.size.height - kSliderMinTouchSize) / 2);
CGRect rect = CGRectInset(self.bounds, dx, dy);
return CGRectContainsPoint(rect, point);
Expand Down
Loading

0 comments on commit 294643f

Please sign in to comment.