blob: 7249695ffeb0f4968d0a9b8a0183288081a9836e [file] [log] [blame]
Alexandre Lision0f66bd32016-01-18 11:30:45 -05001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2016-2019 Savoir-faire Linux Inc.
Alexandre Lision0f66bd32016-01-18 11:30:45 -05003 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Anthony Léonarde7d62ed2018-01-25 10:51:47 -05004 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Alexandre Lision0f66bd32016-01-18 11:30:45 -05005 *
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 Lision0f66bd32016-01-18 11:30:45 -050022#import "NSColor+RingTheme.h"
23
Kateryna Kostiukefc665d2018-09-17 15:42:43 -040024
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050025@implementation IMTableCellView {
26 uint64_t interaction;
27}
28
Kateryna Kostiukae660fd2018-04-24 14:10:41 -040029NSString* const MESSAGE_MARGIN = @"10";
30NSString* const TIME_BOX_HEIGHT = @"34";
31
32@synthesize msgView, msgBackground, timeBox, transferedImage;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050033@synthesize photoView;
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050034@synthesize acceptButton;
35@synthesize declineButton;
36@synthesize progressIndicator;
37@synthesize statusLabel;
Kateryna Kostiukefc665d2018-09-17 15:42:43 -040038@synthesize openImagebutton;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050039
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050040- (void) setupDirection
Alexandre Lision0f66bd32016-01-18 11:30:45 -050041{
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050042 if ([self.identifier containsString:@"Right"]) {
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040043 self.msgBackground.pointerDirection = RIGHT;
Kateryna Kostiuk33089872017-07-14 16:43:59 -040044 self.msgBackground.bgColor = [NSColor ringLightBlue];
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040045 self.messageFailed.image = [NSColor image: [NSImage imageNamed:@"ic_action_cancel.png"] tintedWithColor:[[NSColor errorColor] lightenColorByValue:0.05]];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050046 }
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040047 else {
48 self.msgBackground.pointerDirection = LEFT;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040049 self.msgBackground.bgColor = @available(macOS 10.14, *) ? [NSColor controlColor] : [NSColor whiteColor];
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040050 }
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050051}
52
53- (void) setupForInteraction:(uint64_t)inter
54{
55 interaction = inter;
56 [self setupDirection];
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040057 [self.msgView setBackgroundColor:[NSColor clearColor]];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050058 [self.msgView setString:@""];
Kateryna Kostiukae660fd2018-04-24 14:10:41 -040059 [self.msgView setTranslatesAutoresizingMaskIntoConstraints:NO];
60 [self.msgBackground setTranslatesAutoresizingMaskIntoConstraints:NO];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050061 [self.msgView setEditable:NO];
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040062 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 Kostiuk4f37d952018-12-04 13:19:17 -050064 msgView.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay;
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050065}
Alexandre Lision0f66bd32016-01-18 11:30:45 -050066
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040067- (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 Kostiuk9d8b7922018-05-02 12:52:53 -040075- (void) updateMessageConstraint:(CGFloat) width andHeight: (CGFloat) height timeIsVisible: (bool) visible isTopPadding: (bool) padding
Alexandre Lision0f66bd32016-01-18 11:30:45 -050076{
Kateryna Kostiukae660fd2018-04-24 14:10:41 -040077 [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 Lision0f66bd32016-01-18 11:30:45 -050084
Kateryna Kostiukae660fd2018-04-24 14:10:41 -040085 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 Kostiuk9d8b7922018-05-02 12:52:53 -0400104 formatTime = padding ? [NSString stringWithFormat:@"V:[timeBox(15)]"] : [NSString stringWithFormat:@"V:[timeBox(1)]"];
Kateryna Kostiukae660fd2018-04-24 14:10:41 -0400105 [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 Kostiuk4f37d952018-12-04 13:19:17 -0500114 //update message frame immediatly
115 [self.msgView setNeedsDisplay:YES];
Kateryna Kostiukae660fd2018-04-24 14:10:41 -0400116}
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 Kostiukefc665d2018-09-17 15:42:43 -0400136- (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 Kostiukae660fd2018-04-24 14:10:41 -0400152- (void) invalidateImageConstraints {
153[NSLayoutConstraint deactivateConstraints:[self.transferedImage constraints]];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500154}
155
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500156- (uint64_t)interaction
157{
158 return interaction;
159}
160
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500161@end