int documentMaxStatusWidth(DocumentData *document) {
	auto result = st::normalFont->width(formatDownloadText(document->size, document->size));
	const auto duration = document->getDuration();
	if (const auto song = document->song()) {
		accumulate_max(result, st::normalFont->width(formatPlayedText(duration, duration)));
		accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(duration, document->size)));
	} else if (const auto voice = document->voice()) {
		accumulate_max(result, st::normalFont->width(formatPlayedText(duration, duration)));
		accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(duration, document->size)));
	} else if (document->isVideoFile()) {
		accumulate_max(result, st::normalFont->width(formatDurationAndSizeText(duration, document->size)));
	} else {
		accumulate_max(result, st::normalFont->width(formatSizeText(document->size)));
	}
	return result;
}
Exemplo n.º 2
0
void PlayerWidget::updateState(SongMsgId playing, AudioPlayerState playingState, int64 playingPosition, int64 playingDuration, int32 playingFrequency) {
	if (!playing) {
		audioPlayer()->currentState(&playing, &playingState, &playingPosition, &playingDuration, &playingFrequency);
	}

	bool songChanged = false;
	if (playing && _song != playing) {
		songChanged = true;
		_song = playing;
		if (HistoryItem *item = App::histItemById(_song.msgId)) {
			_history = item->history();
			if (_history->peer->migrateFrom()) {
				_migrated = App::history(_history->peer->migrateFrom()->id);
				_msgmigrated = false;
			} else if (_history->peer->migrateTo()) {
				_migrated = _history;
				_history = App::history(_migrated->peer->migrateTo()->id);
				_msgmigrated = true;
			}
			findCurrent();
		} else {
			_history = 0;
			_msgmigrated = false;
			_index = -1;
		}
		SongData *song = _song.song->song();
		if (song->performer.isEmpty()) {
			_name.setText(st::linkFont, song->title.isEmpty() ? (_song.song->name.isEmpty() ? qsl("Unknown Track") : _song.song->name) : song->title, _textNameOptions);
		} else {
			TextCustomTagsMap custom;
			custom.insert(QChar('c'), qMakePair(textcmdStartLink(1), textcmdStopLink()));
			_name.setRichText(st::linkFont, QString::fromUtf8("[c]%1[/c] \xe2\x80\x93 %2").arg(textRichPrepare(song->performer)).arg(song->title.isEmpty() ? qsl("Unknown Track") : textRichPrepare(song->title)), _textNameOptions, custom);
		}
		updateControls();
	}

	qint64 position = 0, duration = 0, display = 0;
	if (playing == _song) {
		if (!(playingState & AudioPlayerStoppedMask) && playingState != AudioPlayerFinishing) {
			display = position = playingPosition;
			duration = playingDuration;
		} else {
			display = playingDuration;
		}
		display = display / (playingFrequency ? playingFrequency : AudioVoiceMsgFrequency);
	} else if (_song) {
		display = _song.song->song()->duration;
	}
	bool showPause = false, stopped = ((playingState & AudioPlayerStoppedMask) || playingState == AudioPlayerFinishing);
	bool wasPlaying = !!_duration;
	if (!stopped) {
		showPause = (playingState == AudioPlayerPlaying || playingState == AudioPlayerResuming || playingState == AudioPlayerStarting);
	}
	QString time;
	float64 progress = 0.;
	int32 loaded;
	float64 loadProgress = 1.;
	if (duration || !_song || !_song.song || !_song.song->loading()) {
		time = (_down == OverPlayback) ? _time : formatDurationText(display);
		progress = duration ? snap(float64(position) / duration, 0., 1.) : 0.;
		loaded = duration ? _song.song->size : 0;
	} else {
		loaded = _song.song->loading() ? _song.song->loadOffset() : 0;
		time = formatDownloadText(loaded, _song.song->size);
		loadProgress = snap(float64(loaded) / qMax(_song.song->size, 1), 0., 1.);
	}
	if (time != _time || showPause != _showPause) {
		if (_time != time) {
			_time = time;
			_timeWidth = st::linkFont->width(_time);
		}
		_showPause = showPause;
		if (duration != _duration || position != _position || loaded != _loaded) {
			if (!songChanged && ((!stopped && duration && _duration) || (!duration && _loaded != loaded))) {
				a_progress.start(progress);
				a_loadProgress.start(loadProgress);
				_a_progress.start();
			} else {
				a_progress = anim::fvalue(progress, progress);
				a_loadProgress = anim::fvalue(loadProgress, loadProgress);
				_a_progress.stop();
			}
			_position = position;
			_duration = duration;
			_loaded = loaded;
		}
		update();
	} else if (duration != _duration || position != _position || loaded != _loaded) {
		if (!songChanged && ((!stopped && duration && _duration) || (!duration && _loaded != loaded))) {
			a_progress.start(progress);
			a_loadProgress.start(loadProgress);
			_a_progress.start();
		} else {
			a_progress = anim::fvalue(progress, progress);
			a_loadProgress = anim::fvalue(loadProgress, loadProgress);
			_a_progress.stop();
		}
		_position = position;
		_duration = duration;
		_loaded = loaded;
	}

	if (wasPlaying && playingState == AudioPlayerStoppedAtEnd) {
		if (_repeat) {
			startPlay(_song.msgId);
		} else {
			nextPressed();
		}
	}

	if (songChanged) {
		emit playerSongChanged(_song.msgId);
	}
}