OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/stack_view/stack_card.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/browser/ui/rtl_geometry.h" |
| 10 #import "ios/chrome/browser/ui/stack_view/card_view.h" |
| 11 #import "ios/chrome/browser/ui/ui_util.h" |
| 12 |
| 13 @interface StackCard () { |
| 14 id<StackCardViewProvider> _viewProvider; |
| 15 base::scoped_nsobject<CardView> _view; |
| 16 } |
| 17 |
| 18 // The pixel-aligned frame generated by applying |self.layout| under the current |
| 19 // language direction. |
| 20 @property(nonatomic, readonly) CGRect frame; |
| 21 |
| 22 // Applies |self.layout| to the underlying CardView if it exists. |
| 23 - (void)applyLayout; |
| 24 |
| 25 @end |
| 26 |
| 27 @implementation StackCard |
| 28 |
| 29 @synthesize layout = _layout; |
| 30 @synthesize synchronizeView = _synchronizeView; |
| 31 @synthesize isActiveTab = _isActiveTab; |
| 32 @synthesize tabID = _tabID; |
| 33 |
| 34 - (instancetype)initWithViewProvider:(id<StackCardViewProvider>)viewProvider { |
| 35 if ((self = [super init])) { |
| 36 DCHECK(viewProvider); |
| 37 _viewProvider = viewProvider; |
| 38 _synchronizeView = YES; |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 - (instancetype)init { |
| 44 NOTREACHED(); |
| 45 return nil; |
| 46 } |
| 47 |
| 48 - (void)releaseView { |
| 49 if (self.viewIsLive) |
| 50 _view.reset(); |
| 51 } |
| 52 |
| 53 #pragma mark - Properties |
| 54 |
| 55 - (CardView*)view { |
| 56 if (!_view) { |
| 57 _view.reset([[_viewProvider cardViewWithFrame:self.frame forStackCard:self] |
| 58 retain]); |
| 59 _view.get().isActiveTab = _isActiveTab; |
| 60 } |
| 61 return _view.get(); |
| 62 } |
| 63 |
| 64 - (void)setLayout:(LayoutRect)layout { |
| 65 if (!LayoutRectEqualToRect(_layout, layout)) { |
| 66 _layout = layout; |
| 67 [self applyLayout]; |
| 68 } |
| 69 } |
| 70 |
| 71 - (CGSize)size { |
| 72 return self.layout.size; |
| 73 } |
| 74 |
| 75 - (void)setSize:(CGSize)size { |
| 76 CGSize oldSize = self.size; |
| 77 if (!CGSizeEqualToSize(oldSize, size)) { |
| 78 _layout.size = size; |
| 79 _layout.position.leading += (oldSize.width - size.width) / 2.0; |
| 80 _layout.position.originY += (oldSize.height - size.height) / 2.0; |
| 81 [self applyLayout]; |
| 82 } |
| 83 } |
| 84 |
| 85 - (void)setSynchronizeView:(BOOL)synchronizeView { |
| 86 if (_synchronizeView != synchronizeView) { |
| 87 _synchronizeView = synchronizeView; |
| 88 [self applyLayout]; |
| 89 } |
| 90 } |
| 91 |
| 92 - (void)setIsActiveTab:(BOOL)isActiveTab { |
| 93 _isActiveTab = isActiveTab; |
| 94 _view.get().isActiveTab = _isActiveTab; |
| 95 } |
| 96 |
| 97 - (BOOL)viewIsLive { |
| 98 return _view != nil; |
| 99 } |
| 100 |
| 101 - (CGRect)frame { |
| 102 return AlignRectOriginAndSizeToPixels(LayoutRectGetRect(self.layout)); |
| 103 } |
| 104 |
| 105 #pragma mark - |
| 106 |
| 107 - (void)applyLayout { |
| 108 if (!self.viewIsLive || !self.synchronizeView) |
| 109 return; |
| 110 self.view.frame = self.frame; |
| 111 } |
| 112 |
| 113 @end |
OLD | NEW |