"Fossies" - the Fresh Open Source Software Archive

Member "tdesktop-2.6.1/Telegram/SourceFiles/platform/mac/touchbar/mac_touchbar_audio.mm" (24 Feb 2021, 5227 Bytes) of package /linux/misc/tdesktop-2.6.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "mac_touchbar_audio.mm" see the Fossies "Dox" file reference documentation.

    1 /*
    2 This file is part of Telegram Desktop,
    3 the official desktop application for the Telegram messaging service.
    4 
    5 For license and copyright information please follow this link:
    6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
    7 */
    8 #include "platform/mac/touchbar/mac_touchbar_audio.h"
    9 
   10 #ifndef OS_OSX
   11 
   12 #include "media/audio/media_audio.h"
   13 #include "media/player/media_player_instance.h"
   14 #include "platform/mac/touchbar/mac_touchbar_common.h"
   15 #include "platform/mac/touchbar/mac_touchbar_controls.h"
   16 #include "styles/style_media_player.h"
   17 
   18 #import <AppKit/NSButton.h>
   19 #import <AppKit/NSCustomTouchBarItem.h>
   20 #import <AppKit/NSSlider.h>
   21 #import <AppKit/NSSliderTouchBarItem.h>
   22 
   23 using TouchBar::kCircleDiameter;
   24 using TouchBar::CreateNSImageFromStyleIcon;
   25 
   26 namespace {
   27 
   28 constexpr auto kSongType = AudioMsgId::Type::Song;
   29 
   30 const auto *kCustomizationIdPlayer = @"telegram.touchbar";
   31 
   32 inline NSTouchBarItemIdentifier Format(NSString *s) {
   33 	return [NSString stringWithFormat:@"%@.%@", kCustomizationIdPlayer, s];
   34 }
   35 const auto kSeekBarItemIdentifier = Format(@"seekbar");
   36 const auto kPlayItemIdentifier = Format(@"play");
   37 const auto kNextItemIdentifier = Format(@"nextItem");
   38 const auto kPreviousItemIdentifier = Format(@"previousItem");
   39 const auto kClosePlayerItemIdentifier = Format(@"closePlayer");
   40 const auto kCurrentPositionItemIdentifier = Format(@"currentPosition");
   41 
   42 } // namespace
   43 
   44 #pragma mark - TouchBarAudioPlayer
   45 
   46 @interface TouchBarAudioPlayer()
   47 @end // @interface TouchBarAudioPlayer
   48 
   49 @implementation TouchBarAudioPlayer {
   50 	rpl::event_stream<> _closeRequests;
   51 	rpl::producer< Media::Player::TrackState> _trackState;
   52 
   53 	rpl::lifetime _lifetime;
   54 }
   55 
   56 - (id)init {
   57 	self = [super init];
   58 	if (!self) {
   59 		return self;
   60 	}
   61 	self.delegate = self;
   62 	self.customizationIdentifier = kCustomizationIdPlayer.lowercaseString;
   63 	self.defaultItemIdentifiers = @[
   64 		kPlayItemIdentifier,
   65 		kPreviousItemIdentifier,
   66 		kNextItemIdentifier,
   67 		kSeekBarItemIdentifier,
   68 		kClosePlayerItemIdentifier];
   69 	self.customizationAllowedItemIdentifiers = @[
   70 		kPlayItemIdentifier,
   71 		kPreviousItemIdentifier,
   72 		kNextItemIdentifier,
   73 		kCurrentPositionItemIdentifier,
   74 		kSeekBarItemIdentifier,
   75 		kClosePlayerItemIdentifier];
   76 
   77 	_trackState = Media::Player::instance()->updatedNotifier(
   78 	) | rpl::filter([=](const Media::Player::TrackState &state) {
   79 		return state.id.type() == kSongType;
   80 	});
   81 
   82 	return self;
   83 }
   84 
   85 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
   86 		makeItemForIdentifier:(NSTouchBarItemIdentifier)itemId {
   87 	if (!touchBar) {
   88 		return nil;
   89 	}
   90 	const auto mediaPlayer = Media::Player::instance();
   91 	const auto isEqual = [&](NSString *string) {
   92 		return [itemId isEqualToString:string];
   93 	};
   94 
   95 	if (isEqual(kSeekBarItemIdentifier)) {
   96 		auto *item = TouchBar::CreateTouchBarSlider(
   97 			itemId,
   98 			_lifetime,
   99 			[=](bool touchUp, double value, double duration) {
  100 				if (touchUp) {
  101 					mediaPlayer->finishSeeking(kSongType, value);
  102 				} else {
  103 					mediaPlayer->startSeeking(kSongType);
  104 				}
  105 			},
  106 			rpl::duplicate(_trackState));
  107 		return [item autorelease];
  108 	} else if (isEqual(kNextItemIdentifier)
  109 			|| isEqual(kPreviousItemIdentifier)) {
  110 		const auto isNext = isEqual(kNextItemIdentifier);
  111 		auto *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:itemId];
  112 
  113 		auto *button = TouchBar::CreateTouchBarButton(
  114 			isNext
  115 				? st::touchBarIconPlayerNext
  116 				: st::touchBarIconPlayerPrevious,
  117 			_lifetime,
  118 			[=] { isNext // TODO
  119 				? mediaPlayer->next(kSongType)
  120 				: mediaPlayer->previous(kSongType); });
  121 		rpl::duplicate(
  122 			_trackState
  123 		) | rpl::start_with_next([=] {
  124 			const auto newValue = isNext
  125 				? mediaPlayer->nextAvailable(kSongType)
  126 				: mediaPlayer->previousAvailable(kSongType);
  127 			if (button.enabled != newValue) {
  128 				button.enabled = newValue;
  129 			}
  130 		}, _lifetime);
  131 
  132 		item.view = button;
  133 		item.customizationLabel = [NSString
  134 			stringWithFormat:@"%@ Playlist Item",
  135 			isNext ? @"Next" : @"Previous"];
  136 		return [item autorelease];
  137 	} else if (isEqual(kPlayItemIdentifier)) {
  138 		auto *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:itemId];
  139 
  140 		auto *button = TouchBar::CreateTouchBarButtonWithTwoStates(
  141 			st::touchBarIconPlayerPause,
  142 			st::touchBarIconPlayerPlay,
  143 			_lifetime,
  144 			[=](bool value) { mediaPlayer->playPause(kSongType); },
  145 			false,
  146 			rpl::duplicate(
  147 				_trackState
  148 			) | rpl::map([](const auto &state) {
  149 				return (state.state == Media::Player::State::Playing);
  150 			}) | rpl::distinct_until_changed());
  151 
  152 		item.view = button;
  153 		item.customizationLabel = @"Play/Pause";
  154 		return [item autorelease];
  155 	} else if (isEqual(kClosePlayerItemIdentifier)) {
  156 		auto *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:itemId];
  157 		auto *button = TouchBar::CreateTouchBarButton(
  158 			st::touchBarIconPlayerClose,
  159 			_lifetime,
  160 			[=] { _closeRequests.fire({}); });
  161 
  162 		item.view = button;
  163 		item.customizationLabel = @"Close Player";
  164 		return [item autorelease];
  165 	} else if (isEqual(kCurrentPositionItemIdentifier)) {
  166 		auto *item = TouchBar::CreateTouchBarTrackPosition(
  167 			itemId,
  168 			rpl::duplicate(_trackState));
  169 		return [item autorelease];
  170 	}
  171 	return nil;
  172 }
  173 
  174 - (rpl::producer<>)closeRequests {
  175 	return _closeRequests.events();
  176 }
  177 
  178 @end // @implementation TouchBarAudioPlayer
  179 
  180 #endif // OS_OSX