Skip to content

Commit

Permalink
Cut off name / date correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Jul 20, 2023
1 parent c35556b commit 2cd08b8
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,11 @@ PauseState Controller::pauseState() const {
const auto playing = !inactive && !_paused;
return playing
? PauseState::Playing
: inactive
? PauseState::Inactive
: PauseState::Paused;
: !inactive
? PauseState::Paused
: _paused
? PauseState::InactivePaused
: PauseState::Inactive;
}

float64 Controller::currentVolume() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ enum class PauseState {
Playing,
Paused,
Inactive,
InactivePaused,
};

struct SiblingLayout {
Expand Down
133 changes: 91 additions & 42 deletions Telegram/SourceFiles/media/stories/media_stories_header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,12 @@ void UserpicBadge::updateGeometry() {
return { Ui::FormatDateTime(whenFull) };
}

[[nodiscard]] TextWithEntities ComposeName(HeaderData data) {
auto result = Ui::Text::Bold(data.user->isSelf()
? tr::lng_stories_my_name(tr::now)
: data.user->shortName());
if (data.fullCount) {
result.append(QString::fromUtf8(" \xE2\x80\xA2 %1/%2"
).arg(data.fullIndex + 1
).arg(data.fullCount));
}
return result;
[[nodiscard]] QString ComposeCounter(HeaderData data) {
const auto index = data.fullIndex + 1;
const auto count = data.fullCount;
return count
? QString::fromUtf8(" \xE2\x80\xA2 %1/%2").arg(index).arg(count)
: QString();
}

[[nodiscard]] Timestamp ComposeDetails(HeaderData data, TimeId now) {
Expand All @@ -269,17 +265,23 @@ void Header::show(HeaderData data) {
if (_data == data) {
return;
}
const auto userChanged = !_data
|| (_data->user != data.user);
const auto nameDataChanged = userChanged
|| !_name
|| (_data->fullCount != data.fullCount)
|| (data.fullCount && _data->fullIndex != data.fullIndex);
const auto userChanged = !_data || (_data->user != data.user);
_data = data;
const auto updateInfoGeometry = [=] {
if (_name && _date) {
const auto namex = st::storiesHeaderNamePosition.x();
const auto namer = namex + _name->width();
const auto datex = st::storiesHeaderDatePosition.x();
const auto dater = datex + _date->width();
const auto r = std::max(namer, dater);
_info->setGeometry({ 0, 0, r, _widget->height() });
}
};
if (userChanged) {
_volume = nullptr;
_date = nullptr;
_name = nullptr;
_counter = nullptr;
_userpic = nullptr;
_info = nullptr;
_privacy = nullptr;
Expand All @@ -288,10 +290,12 @@ void Header::show(HeaderData data) {
const auto parent = _controller->wrap();
auto widget = std::make_unique<Ui::RpWidget>(parent);
const auto raw = widget.get();

_info = std::make_unique<Ui::AbstractButton>(raw);
_info->setClickedCallback([=] {
_controller->uiShow()->show(PrepareShortInfoBox(_data->user));
});

_userpic = std::make_unique<Ui::UserpicButton>(
raw,
data.user,
Expand All @@ -301,38 +305,30 @@ void Header::show(HeaderData data) {
_userpic->move(
st::storiesHeaderMargin.left(),
st::storiesHeaderMargin.top());
raw->show();
_widget = std::move(widget);

_controller->layoutValue(
) | rpl::start_with_next([=](const Layout &layout) {
raw->setGeometry(layout.header);
}, raw->lifetime());
}
const auto updateInfoGeometry = [=] {
if (_name && _date) {
const auto namex = st::storiesHeaderNamePosition.x();
const auto namer = namex + _name->width();
const auto datex = st::storiesHeaderDatePosition.x();
const auto dater = datex + _date->width();
const auto r = std::max(namer, dater);
_info->setGeometry({ 0, 0, r, _widget->height() });
}
};
if (nameDataChanged) {
_name = std::make_unique<Ui::FlatLabel>(
_widget.get(),
rpl::single(ComposeName(data)),
raw,
rpl::single(data.user->isSelf()
? tr::lng_stories_my_name(tr::now)
: data.user->shortName()),
st::storiesHeaderName);
_name->setAttribute(Qt::WA_TransparentForMouseEvents);
_name->setOpacity(kNameOpacity);
_name->move(st::storiesHeaderNamePosition);
_name->show();
_name->move(st::storiesHeaderNamePosition);

rpl::combine(
_name->widthValue(),
_widget->heightValue()
raw->heightValue()
) | rpl::start_with_next(updateInfoGeometry, _name->lifetime());

raw->show();
_widget = std::move(widget);

_controller->layoutValue(
) | rpl::start_with_next([=](const Layout &layout) {
raw->setGeometry(layout.header);
}, raw->lifetime());
}
auto timestamp = ComposeDetails(data, base::unixtime::now());
_date = std::make_unique<Ui::FlatLabel>(
Expand All @@ -347,8 +343,21 @@ void Header::show(HeaderData data) {
_date->widthValue(
) | rpl::start_with_next(updateInfoGeometry, _date->lifetime());

_privacy = MakePrivacyBadge(_userpic.get(), data.privacy, [=] {
auto counter = ComposeCounter(data);
if (!counter.isEmpty()) {
_counter = std::make_unique<Ui::FlatLabel>(
_widget.get(),
std::move(counter),
st::storiesHeaderDate);
_counter->resizeToNaturalWidth(_counter->naturalWidth());
_counter->setAttribute(Qt::WA_TransparentForMouseEvents);
_counter->setOpacity(kNameOpacity);
_counter->show();
} else {
_counter = nullptr;
}

_privacy = MakePrivacyBadge(_userpic.get(), data.privacy, [=] {
});

if (data.video) {
Expand All @@ -370,6 +379,41 @@ void Header::show(HeaderData data) {
_volumeToggle = nullptr;
}

rpl::combine(
_widget->widthValue(),
_counter ? _counter->widthValue() : rpl::single(0),
_dateUpdated.events_starting_with_copy(rpl::empty)
) | rpl::start_with_next([=](int outer, int counter, auto) {
const auto right = _playPause
? _playPause->x()
: (outer - st::storiesHeaderMargin.right());
const auto nameLeft = st::storiesHeaderNamePosition.x();
const auto nameNatural = _name->naturalWidth();
if (counter) {
counter += st::normalFont->spacew;
}
const auto nameAvailable = right - nameLeft - counter;
auto counterLeft = nameLeft;
if (nameAvailable <= 0) {
_name->hide();
} else {
_name->show();
_name->resizeToNaturalWidth(nameAvailable);
counterLeft += _name->width() + st::normalFont->spacew;
}
if (_counter) {
_counter->move(counterLeft, _name->y());
}
const auto dateLeft = st::storiesHeaderDatePosition.x();
const auto dateAvailable = right - dateLeft;
if (dateAvailable <= 0) {
_date->hide();
} else {
_date->show();
_date->resizeToNaturalWidth(dateAvailable);
}
}, _date->lifetime());

if (timestamp.changes > 0) {
_dateUpdateTimer.callOnce(timestamp.changes * crl::time(1000));
}
Expand Down Expand Up @@ -403,14 +447,17 @@ void Header::createPlayPause() {
} else if (type == QEvent::MouseButtonRelease) {
const auto down = base::take(state->down);
if (down && state->over) {
_controller->togglePaused(_pauseState != PauseState::Paused);
const auto paused = (_pauseState == PauseState::Paused)
|| (_pauseState == PauseState::InactivePaused);
_controller->togglePaused(!paused);
}
}
}, lifetime);

_playPause->paintRequest() | rpl::start_with_next([=] {
auto p = QPainter(_playPause.get());
const auto paused = (_pauseState == PauseState::Paused);
const auto paused = (_pauseState == PauseState::Paused)
|| (_pauseState == PauseState::InactivePaused);
const auto icon = paused
? &st::storiesPlayIcon
: &st::storiesPauseIcon;
Expand Down Expand Up @@ -620,7 +667,8 @@ void Header::updateVolumeIcon() {
void Header::applyPauseState() {
Expects(_playPause != nullptr);

const auto inactive = (_pauseState == PauseState::Inactive);
const auto inactive = (_pauseState == PauseState::Inactive)
|| (_pauseState == PauseState::InactivePaused);
_playPause->setAttribute(Qt::WA_TransparentForMouseEvents, inactive);
if (inactive) {
QEvent e(QEvent::Leave);
Expand All @@ -646,6 +694,7 @@ void Header::updateDateText() {
}
auto timestamp = ComposeDetails(*_data, base::unixtime::now());
_date->setText(timestamp.text);
_dateUpdated.fire({});
if (timestamp.changes > 0) {
_dateUpdateTimer.callOnce(timestamp.changes * crl::time(1000));
}
Expand Down
4 changes: 3 additions & 1 deletion Telegram/SourceFiles/media/stories/media_stories_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Header final {
void createPlayPause();
void createVolumeToggle();
void rebuildVolumeControls(
not_null<Ui::RpWidget*> dropdown,
not_null<Ui::RpWidget*> dropdown,
bool horizontal);

const not_null<Controller*> _controller;
Expand All @@ -73,7 +73,9 @@ class Header final {
std::unique_ptr<Ui::AbstractButton> _info;
std::unique_ptr<Ui::UserpicButton> _userpic;
std::unique_ptr<Ui::FlatLabel> _name;
std::unique_ptr<Ui::FlatLabel> _counter;
std::unique_ptr<Ui::FlatLabel> _date;
rpl::event_stream<> _dateUpdated;
std::unique_ptr<Ui::RpWidget> _playPause;
std::unique_ptr<Ui::RpWidget> _volumeToggle;
std::unique_ptr<Ui::FadeWrap<Ui::RpWidget>> _volume;
Expand Down
6 changes: 5 additions & 1 deletion Telegram/SourceFiles/media/view/media_view.style
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,15 @@ storiesHeaderPhoto: UserpicButton(defaultUserpicButton) {
}
storiesHeaderName: FlatLabel(defaultFlatLabel) {
textFg: mediaviewControlFg;
style: defaultTextStyle;
style: semiboldTextStyle;
minWidth: 10px;
maxHeight: 20px;
}
storiesHeaderNamePosition: point(50px, 0px);
storiesHeaderDate: FlatLabel(defaultFlatLabel) {
textFg: mediaviewControlFg;
minWidth: 10px;
maxHeight: 20px;
}
storiesHeaderDatePosition: point(50px, 17px);
storiesShadowTop: icon{{ "mediaview/shadow_bottom-flip_vertical", windowShadowFg }};
Expand Down

0 comments on commit 2cd08b8

Please sign in to comment.