diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 42314f6cd..262de3852 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -891,6 +891,8 @@ PRIVATE platform/mac/specific_mac_p.h platform/mac/window_title_mac.mm platform/mac/window_title_mac.h + platform/mac/touchbar/items/mac_formatter_item.h + platform/mac/touchbar/items/mac_formatter_item.mm platform/mac/touchbar/items/mac_pinned_chats_item.h platform/mac/touchbar/items/mac_pinned_chats_item.mm platform/mac/touchbar/items/mac_scrubber_item.h diff --git a/Telegram/SourceFiles/platform/mac/touchbar/items/mac_formatter_item.h b/Telegram/SourceFiles/platform/mac/touchbar/items/mac_formatter_item.h new file mode 100644 index 000000000..a537929c8 --- /dev/null +++ b/Telegram/SourceFiles/platform/mac/touchbar/items/mac_formatter_item.h @@ -0,0 +1,16 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +#import +#import + +API_AVAILABLE(macos(10.12.2)) +@interface TextFormatPopover : NSPopoverTouchBarItem +- (id)init:(NSTouchBarItemIdentifier)identifier; +@end diff --git a/Telegram/SourceFiles/platform/mac/touchbar/items/mac_formatter_item.mm b/Telegram/SourceFiles/platform/mac/touchbar/items/mac_formatter_item.mm new file mode 100644 index 000000000..7f68bbc55 --- /dev/null +++ b/Telegram/SourceFiles/platform/mac/touchbar/items/mac_formatter_item.mm @@ -0,0 +1,142 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "platform/mac/touchbar/items/mac_formatter_item.h" + +#ifndef OS_OSX + +#include "base/platform/mac/base_utilities_mac.h" +#include "lang/lang_keys.h" +#include "platform/mac/touchbar/mac_touchbar_common.h" + +#import +#import +#import + +#include +#include + +namespace { + +constexpr auto kCommandBold = 0x010; +constexpr auto kCommandItalic = 0x011; +constexpr auto kCommandUnderline = 0x012; +constexpr auto kCommandStrikeOut = 0x013; +constexpr auto kCommandMonospace = 0x014; +constexpr auto kCommandClear = 0x015; +constexpr auto kCommandLink = 0x016; + +const auto kPopoverFormatter = @"popoverInputFormatter"; + +void SendKeyEvent(int command) { + auto *focused = qobject_cast(QApplication::focusWidget()); + if (!focused) { + return; + } + auto key = 0; + auto modifier = Qt::KeyboardModifiers(0) | Qt::ControlModifier; + switch (command) { + case kCommandBold: + key = Qt::Key_B; + break; + case kCommandItalic: + key = Qt::Key_I; + break; + case kCommandMonospace: + key = Qt::Key_M; + modifier |= Qt::ShiftModifier; + break; + case kCommandClear: + key = Qt::Key_N; + modifier |= Qt::ShiftModifier; + break; + case kCommandLink: + key = Qt::Key_K; + break; + case kCommandUnderline: + key = Qt::Key_U; + break; + case kCommandStrikeOut: + key = Qt::Key_X; + modifier |= Qt::ShiftModifier; + break; + } + QApplication::postEvent( + focused, + new QKeyEvent(QEvent::KeyPress, key, modifier)); + QApplication::postEvent( + focused, + new QKeyEvent(QEvent::KeyRelease, key, modifier)); +} + +} // namespace + +#pragma mark - TextFormatPopover + +@implementation TextFormatPopover { + rpl::lifetime _lifetime; +} + +- (id)init:(NSTouchBarItemIdentifier)identifier { + self = [super initWithIdentifier:identifier]; + if (!self) { + return nil; + } + + self.collapsedRepresentationImage = [NSImage + imageNamed:NSImageNameTouchBarTextItalicTemplate]; // autorelease]; + auto *secondaryTouchBar = [[[NSTouchBar alloc] init] autorelease]; + + auto *popover = [[[NSCustomTouchBarItem alloc] + initWithIdentifier:kPopoverFormatter] autorelease]; + { + auto *scroll = [[[NSScrollView alloc] init] autorelease]; + auto *segment = [[[NSSegmentedControl alloc] init] autorelease]; + segment.segmentStyle = NSSegmentStyleRounded; + segment.target = self; + segment.action = @selector(segmentClicked:); + + static const auto strings = { + tr::lng_menu_formatting_bold, + tr::lng_menu_formatting_italic, + tr::lng_menu_formatting_underline, + tr::lng_menu_formatting_strike_out, + tr::lng_menu_formatting_monospace, + tr::lng_menu_formatting_clear, + tr::lng_info_link_label, + }; + segment.segmentCount = strings.size(); + auto width = 0; + auto count = 0; + for (const auto &s : strings) { + const auto string = Platform::Q2NSString(s(tr::now)); + width += TouchBar::WidthFromString(string) * 1.4; + [segment setLabel:string forSegment:count++]; + } + segment.frame = NSMakeRect(0, 0, width, TouchBar::kCircleDiameter); + [scroll setDocumentView:segment]; + popover.view = scroll; + } + + secondaryTouchBar.templateItems = [NSSet setWithArray:@[popover]]; + secondaryTouchBar.defaultItemIdentifiers = @[kPopoverFormatter]; + + self.popoverTouchBar = secondaryTouchBar; + return self; +} + +- (void)segmentClicked:(NSSegmentedControl*)sender { + const auto command = int(sender.selectedSegment) + kCommandBold; + sender.selectedSegment = -1; + SendKeyEvent(command); + + [self dismissPopover:nil]; +} + +@end // @implementation TextFormatPopover + +#endif // OS_OSX