Added ability to change order of pinned dialogs from touchbar.

This commit is contained in:
23rd 2019-06-29 18:34:45 +03:00 committed by John Preston
parent bb9e6e7b5f
commit 9c9ea8c2c0
1 changed files with 80 additions and 17 deletions

View File

@ -43,7 +43,8 @@ namespace {
//https://developer.apple.com/design/human-interface-guidelines/macos/touch-bar/touch-bar-icons-and-images/ //https://developer.apple.com/design/human-interface-guidelines/macos/touch-bar/touch-bar-icons-and-images/
constexpr auto kIdealIconSize = 36; constexpr auto kIdealIconSize = 36;
constexpr auto kMaximumIconSize = 44; constexpr auto kMaximumIconSize = 44;
constexpr auto kScrubberHeight = 30; constexpr auto kCircleDiameter = 30;
constexpr auto kPinnedButtonsSpace = 30;
constexpr auto kCommandPlayPause = 0x002; constexpr auto kCommandPlayPause = 0x002;
constexpr auto kCommandPlaylistPrevious = 0x003; constexpr auto kCommandPlaylistPrevious = 0x003;
@ -386,6 +387,68 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
} // namespace } // namespace
@interface PinButton : NSButton
@end // @interface PinButton
@implementation PinButton {
int _startPosition;
int _tempIndex;
bool _orderChanged;
}
- (void)touchesBeganWithEvent:(NSEvent *)event {
if ([event.allTouches allObjects].count > 1) {
return;
}
_orderChanged = false;
_tempIndex = self.tag - 1;
_startPosition = [self getTouchX:event];
[super touchesBeganWithEvent:event];
}
- (void)touchesMovedWithEvent:(NSEvent *)event {
if (self.tag <= kSavedMessagesId) {
return;
}
if ([event.allTouches allObjects].count > 1) {
return;
}
const auto currentPosition = [self getTouchX:event];
const auto step = kPinnedButtonsSpace + kCircleDiameter;
if (std::abs(_startPosition - currentPosition) > step) {
const auto delta = (currentPosition > _startPosition) ? 1 : -1;
const auto newIndex = _tempIndex + delta;
const auto &order = Auth().data().pinnedChatsOrder(nullptr);
// In case the order has been changed from another device
// while the user is dragging the dialog.
if (_tempIndex >= order.size()) {
return;
}
if (newIndex >= 0 && newIndex < order.size()) {
Auth().data().reorderTwoPinnedChats(
order.at(_tempIndex).history(),
order.at(newIndex).history());
_tempIndex = newIndex;
_startPosition = currentPosition;
_orderChanged = true;
}
}
}
- (void)touchesEndedWithEvent:(NSEvent *)event {
if (_orderChanged) {
Auth().api().savePinnedOrder(nullptr);
}
[super touchesEndedWithEvent:event];
}
- (int)getTouchX:(NSEvent *)e {
return [[[e.allTouches allObjects] objectAtIndex:0] locationInView:self].x;
}
@end // @implementation PinButton
@interface PinnedDialogButton : NSCustomTouchBarItem @interface PinnedDialogButton : NSCustomTouchBarItem
@property(nonatomic, assign) int number; @property(nonatomic, assign) int number;
@ -425,10 +488,11 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
} }
self.number = num; self.number = num;
NSButton *button = [[NSButton alloc] initWithFrame:NSZeroRect]; PinButton *button = [[PinButton alloc] initWithFrame:NSZeroRect];
NSButtonCell *cell = [[NSButtonCell alloc] init]; NSButtonCell *cell = [[NSButtonCell alloc] init];
[cell setBezelStyle:NSBezelStyleCircular]; [cell setBezelStyle:NSBezelStyleCircular];
button.cell = cell; button.cell = cell;
button.tag = num;
button.target = self; button.target = self;
button.action = @selector(buttonActionPin:); button.action = @selector(buttonActionPin:);
self.view = button; self.view = button;
@ -570,17 +634,16 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
- (void) updateImage:(QPixmap)pixmap { - (void) updateImage:(QPixmap)pixmap {
NSButton *button = self.view; NSButton *button = self.view;
NSImage *image = [qt_mac_create_nsimage(pixmap) autorelease]; NSImage *image = [qt_mac_create_nsimage(pixmap) autorelease];
[image setSize:NSMakeSize(kScrubberHeight, kScrubberHeight)]; [image setSize:NSMakeSize(kCircleDiameter, kCircleDiameter)];
[button.cell setImage:image]; [button.cell setImage:image];
} }
@end @end // @implementation PinnedDialogButton
@interface PickerScrubberItemView : NSScrubberItemView @interface PickerScrubberItemView : NSScrubberItemView
@property (strong) NSImageView *imageView; @property (strong) NSImageView *imageView;
@end @end // @interface PickerScrubberItemView
@implementation PickerScrubberItemView { @implementation PickerScrubberItemView {
rpl::lifetime _lifetime; rpl::lifetime _lifetime;
Data::FileOrigin _origin; Data::FileOrigin _origin;
@ -628,25 +691,25 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
} }
- (void)updateImage { - (void)updateImage {
const auto size = _dimensions const auto size = _dimensions
.scaled(kScrubberHeight, kScrubberHeight, Qt::KeepAspectRatio); .scaled(kCircleDiameter, kCircleDiameter, Qt::KeepAspectRatio);
_imageView.image = [qt_mac_create_nsimage( _imageView.image = [qt_mac_create_nsimage(
_image->pixSingle( _image->pixSingle(
_origin, _origin,
size.width(), size.width(),
size.height(), size.height(),
kScrubberHeight, kCircleDiameter,
kScrubberHeight, kCircleDiameter,
ImageRoundRadius::None)) ImageRoundRadius::None))
autorelease]; autorelease];
} }
@end @end // @implementation PickerScrubberItemView
@interface PickerCustomTouchBarItem: NSCustomTouchBarItem @interface PickerCustomTouchBarItem: NSCustomTouchBarItem
<NSScrubberDelegate, <NSScrubberDelegate,
NSScrubberDataSource, NSScrubberDataSource,
NSScrubberFlowLayoutDelegate> NSScrubberFlowLayoutDelegate>
@end @end // @interface PickerCustomTouchBarItem
#pragma mark - #pragma mark -
@ -723,9 +786,9 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
- (NSSize)scrubber:(NSScrubber *)scrubber layout:(NSScrubberFlowLayout *)layout sizeForItemAtIndex:(NSInteger)index { - (NSSize)scrubber:(NSScrubber *)scrubber layout:(NSScrubberFlowLayout *)layout sizeForItemAtIndex:(NSInteger)index {
if (const auto t = _stickers[index].title; !t.isEmpty()) { if (const auto t = _stickers[index].title; !t.isEmpty()) {
return NSMakeSize( return NSMakeSize(
WidthFromString(Q2NSString(t)) + 30, kScrubberHeight); WidthFromString(Q2NSString(t)) + kCircleDiameter, kCircleDiameter);
} }
return NSMakeSize(kScrubberHeight, kScrubberHeight); return NSMakeSize(kCircleDiameter, kCircleDiameter);
} }
- (void)scrubber:(NSScrubber *)scrubber didSelectItemAtIndex:(NSInteger)index { - (void)scrubber:(NSScrubber *)scrubber didSelectItemAtIndex:(NSInteger)index {
@ -843,7 +906,7 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
_stickers = std::move(temp); _stickers = std::move(temp);
} }
@end @end // @implementation PickerCustomTouchBarItem
@interface TouchBar()<NSTouchBarDelegate> @interface TouchBar()<NSTouchBarDelegate>
@ -1089,7 +1152,7 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
width += WidthFromString(string) * 1.4; width += WidthFromString(string) * 1.4;
[segment setLabel:string forSegment:count++]; [segment setLabel:string forSegment:count++];
} }
segment.frame = NSMakeRect(0, 0, width, kScrubberHeight); segment.frame = NSMakeRect(0, 0, width, kCircleDiameter);
[scroll setDocumentView:segment]; [scroll setDocumentView:segment];
item.view = scroll; item.view = scroll;
return item; return item;
@ -1139,7 +1202,7 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
} }
[stackView addView:button.view inGravity:NSStackViewGravityTrailing]; [stackView addView:button.view inGravity:NSStackViewGravityTrailing];
} }
const auto space = 30; const auto space = kPinnedButtonsSpace;
[stackView setEdgeInsets:NSEdgeInsetsMake(0, space / 2., 0, space)]; [stackView setEdgeInsets:NSEdgeInsetsMake(0, space / 2., 0, space)];
[stackView setSpacing:space]; [stackView setSpacing:space];
item.view = stackView; item.view = stackView;
@ -1428,4 +1491,4 @@ void AppendEmojiPacks(std::vector<PickerScrubberItem> &to) {
[super dealloc]; [super dealloc];
} }
@end @end // @implementation TouchBar