Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame^] | 2 | * Copyright (C) 2016-2019 Savoir-faire Linux Inc. |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 4 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #import "IMTableCellView.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 22 | #import "NSColor+RingTheme.h" |
| 23 | |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 24 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 25 | @implementation IMTableCellView { |
| 26 | uint64_t interaction; |
| 27 | } |
| 28 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 29 | NSString* const MESSAGE_MARGIN = @"10"; |
| 30 | NSString* const TIME_BOX_HEIGHT = @"34"; |
| 31 | |
| 32 | @synthesize msgView, msgBackground, timeBox, transferedImage; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 33 | @synthesize photoView; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 34 | @synthesize acceptButton; |
| 35 | @synthesize declineButton; |
| 36 | @synthesize progressIndicator; |
| 37 | @synthesize statusLabel; |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 38 | @synthesize openImagebutton; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 39 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 40 | - (void) setupDirection |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 41 | { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 42 | if ([self.identifier containsString:@"Right"]) { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 43 | self.msgBackground.pointerDirection = RIGHT; |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 44 | self.msgBackground.bgColor = [NSColor ringLightBlue]; |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 45 | self.messageFailed.image = [NSColor image: [NSImage imageNamed:@"ic_action_cancel.png"] tintedWithColor:[[NSColor errorColor] lightenColorByValue:0.05]]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 46 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 47 | else { |
| 48 | self.msgBackground.pointerDirection = LEFT; |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 49 | self.msgBackground.bgColor = @available(macOS 10.14, *) ? [NSColor controlColor] : [NSColor whiteColor]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 50 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | - (void) setupForInteraction:(uint64_t)inter |
| 54 | { |
| 55 | interaction = inter; |
| 56 | [self setupDirection]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 57 | [self.msgView setBackgroundColor:[NSColor clearColor]]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 58 | [self.msgView setString:@""]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 59 | [self.msgView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 60 | [self.msgBackground setTranslatesAutoresizingMaskIntoConstraints:NO]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 61 | [self.msgView setEditable:NO]; |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 62 | acceptButton.image = [NSColor image: [NSImage imageNamed:@"ic_file_upload.png"] tintedWithColor:[NSColor greenSuccessColor]]; |
| 63 | declineButton.image = [NSColor image: [NSImage imageNamed:@"ic_action_cancel.png"] tintedWithColor:[NSColor redColor]]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 64 | msgView.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 65 | } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 66 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 67 | - (void) setupForInteraction:(uint64_t)inter isFailed:(bool) failed { |
| 68 | [self setupForInteraction:inter]; |
| 69 | if (failed) { |
| 70 | self.msgBackground.bgColor = [[NSColor errorColor] lightenColorByValue:0.2]; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 75 | - (void) updateMessageConstraint:(CGFloat) width andHeight: (CGFloat) height timeIsVisible: (bool) visible isTopPadding: (bool) padding |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 76 | { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 77 | [NSLayoutConstraint deactivateConstraints:[self.msgView constraints]]; |
| 78 | [NSLayoutConstraint deactivateConstraints:[self.timeBox constraints]]; |
| 79 | NSString* formatWidth = [NSString stringWithFormat:@"H:|-%@-[msgView(==%@)]-%@-|", |
| 80 | MESSAGE_MARGIN,[NSString stringWithFormat:@"%f", width], |
| 81 | MESSAGE_MARGIN]; |
| 82 | NSString* formatHeight = [NSString stringWithFormat:@"V:[msgView(==%@)]", |
| 83 | [NSString stringWithFormat:@"%f", height]]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 84 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 85 | NSArray* constraintsMessageHorizontal = [NSLayoutConstraint |
| 86 | constraintsWithVisualFormat:formatWidth |
| 87 | options:NSLayoutFormatAlignAllCenterY |
| 88 | metrics:nil views:NSDictionaryOfVariableBindings(msgView)]; |
| 89 | NSArray* constraintsMessageVertical = [NSLayoutConstraint |
| 90 | constraintsWithVisualFormat:formatHeight |
| 91 | options:0 |
| 92 | metrics:nil views:NSDictionaryOfVariableBindings(msgView)]; |
| 93 | |
| 94 | NSLayoutConstraint* centerMessageConstraint =[NSLayoutConstraint constraintWithItem:msgView |
| 95 | attribute:NSLayoutAttributeCenterY |
| 96 | relatedBy:NSLayoutRelationEqual |
| 97 | toItem:msgView.superview |
| 98 | attribute:NSLayoutAttributeCenterY |
| 99 | multiplier:1.f constant:0.f]; |
| 100 | |
| 101 | NSString* formatTime = [NSString stringWithFormat:@"V:[timeBox(%@)]", TIME_BOX_HEIGHT]; |
| 102 | [self.timeBox setHidden:NO]; |
| 103 | if (!visible) { |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 104 | formatTime = padding ? [NSString stringWithFormat:@"V:[timeBox(15)]"] : [NSString stringWithFormat:@"V:[timeBox(1)]"]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 105 | [self.timeBox setHidden:YES]; |
| 106 | } |
| 107 | NSArray* constraintsVerticalTimeBox = [NSLayoutConstraint |
| 108 | constraintsWithVisualFormat:formatTime |
| 109 | options:0 |
| 110 | metrics:nil views:NSDictionaryOfVariableBindings(timeBox)]; |
| 111 | NSArray* constraints = [[[constraintsMessageHorizontal arrayByAddingObjectsFromArray:constraintsMessageVertical] |
| 112 | arrayByAddingObject:centerMessageConstraint] arrayByAddingObjectsFromArray:constraintsVerticalTimeBox]; |
| 113 | [NSLayoutConstraint activateConstraints:constraints]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 114 | //update message frame immediatly |
| 115 | [self.msgView setNeedsDisplay:YES]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | - (void) updateImageConstraint: (CGFloat) width andHeight: (CGFloat) height { |
| 119 | [NSLayoutConstraint deactivateConstraints:[self.transferedImage constraints]]; |
| 120 | [self.msgBackground setHidden:YES]; |
| 121 | NSString* formatHeight = [NSString stringWithFormat:@"V:[transferedImage(==%@)]",[NSString stringWithFormat:@"%f", height]]; |
| 122 | NSString* formatWidth = [NSString stringWithFormat: |
| 123 | @"H:[transferedImage(==%@)]",[NSString stringWithFormat:@"%f", width]]; |
| 124 | NSArray* constraintsHorizontal = [NSLayoutConstraint |
| 125 | constraintsWithVisualFormat:formatWidth |
| 126 | options:0 |
| 127 | metrics:nil views:NSDictionaryOfVariableBindings(transferedImage)]; |
| 128 | NSArray* constraintsVertical = [NSLayoutConstraint |
| 129 | constraintsWithVisualFormat:formatHeight |
| 130 | options:0 |
| 131 | metrics:nil views:NSDictionaryOfVariableBindings(transferedImage)]; |
| 132 | NSArray* constraints =[constraintsHorizontal arrayByAddingObjectsFromArray:constraintsVertical] ; |
| 133 | [NSLayoutConstraint activateConstraints:constraintsHorizontal]; |
| 134 | } |
| 135 | |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 136 | - (void) updateImageConstraintWithMax: (CGFloat) maxDimension { |
| 137 | if(!self.transferedImage) {return;} |
| 138 | CGFloat widthScaleFactor = maxDimension / transferedImage.image.size.width; |
| 139 | CGFloat heightScaleFactor = maxDimension / transferedImage.image.size.height; |
| 140 | NSSize size = NSZeroSize; |
| 141 | if((widthScaleFactor >= 1) && (heightScaleFactor >= 1)) { |
| 142 | size.width = transferedImage.image.size.width; |
| 143 | size.height = transferedImage.image.size.height; |
| 144 | } else { |
| 145 | CGFloat scale = MIN(widthScaleFactor, heightScaleFactor); |
| 146 | size.width = transferedImage.image.size.width * scale; |
| 147 | size.height = transferedImage.image.size.height * scale; |
| 148 | } |
| 149 | [self updateImageConstraint:size.width andHeight: size.height]; |
| 150 | } |
| 151 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 152 | - (void) invalidateImageConstraints { |
| 153 | [NSLayoutConstraint deactivateConstraints:[self.transferedImage constraints]]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 154 | } |
| 155 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 156 | - (uint64_t)interaction |
| 157 | { |
| 158 | return interaction; |
| 159 | } |
| 160 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 161 | @end |