2019-04-05 14:41:06 +04:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
2019-09-13 19:45:48 +03:00
|
|
|
#include "base/base_integration.h"
|
2019-04-05 14:41:06 +04:00
|
|
|
|
2019-09-13 19:45:48 +03:00
|
|
|
namespace base {
|
2019-04-05 14:41:06 +04:00
|
|
|
|
|
|
|
// This method allows to create an rpl::producer from a Qt object
|
|
|
|
// and a signal with none or one reported value.
|
|
|
|
//
|
|
|
|
// QtSignalProducer(qtWindow, &QWindow::activeChanged) | rpl::start_
|
|
|
|
//
|
|
|
|
// This producer values construct a custom event loop leave point.
|
|
|
|
// This means that all postponeCall's will be invoked right after
|
|
|
|
// the value processing by the current consumer finishes.
|
|
|
|
template <typename Object, typename Signal>
|
2019-09-13 19:45:48 +03:00
|
|
|
auto qt_signal_producer(Object *object, Signal signal);
|
2019-04-05 14:41:06 +04:00
|
|
|
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
template <typename Signal>
|
2019-09-13 19:45:48 +03:00
|
|
|
struct qt_signal_argument;
|
2019-04-05 14:41:06 +04:00
|
|
|
|
|
|
|
template <typename Class, typename Return, typename Value>
|
2019-09-13 19:45:48 +03:00
|
|
|
struct qt_signal_argument<Return(Class::*)(Value)> {
|
2019-04-05 14:41:06 +04:00
|
|
|
using type = Value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Class, typename Return>
|
2019-09-13 19:45:48 +03:00
|
|
|
struct qt_signal_argument<Return(Class::*)()> {
|
2019-04-05 14:41:06 +04:00
|
|
|
using type = void;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|
|
|
|
template <typename Object, typename Signal>
|
2019-09-13 19:45:48 +03:00
|
|
|
auto qt_signal_producer(Object *object, Signal signal) {
|
|
|
|
using Value = typename details::qt_signal_argument<Signal>::type;
|
2019-04-05 14:41:06 +04:00
|
|
|
static constexpr auto NoArgument = std::is_same_v<Value, void>;
|
|
|
|
using Produced = std::conditional_t<
|
|
|
|
NoArgument,
|
|
|
|
rpl::empty_value,
|
|
|
|
std::remove_const_t<std::decay_t<Value>>>;
|
2019-09-13 15:22:54 +03:00
|
|
|
const auto guarded = QPointer<Object>(object);
|
2019-04-05 14:41:06 +04:00
|
|
|
return rpl::make_producer<Produced>([=](auto consumer) {
|
|
|
|
if (!guarded) {
|
|
|
|
return rpl::lifetime();
|
|
|
|
}
|
|
|
|
const auto connect = [&](auto &&handler) {
|
|
|
|
const auto listener = new QObject(guarded.data());
|
|
|
|
QObject::connect(
|
|
|
|
guarded,
|
|
|
|
signal,
|
|
|
|
listener,
|
|
|
|
std::forward<decltype(handler)>(handler));
|
2019-09-13 15:22:54 +03:00
|
|
|
const auto weak = QPointer<QObject>(listener);
|
2019-04-05 14:41:06 +04:00
|
|
|
return rpl::lifetime([=] {
|
|
|
|
if (weak) {
|
|
|
|
delete weak;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
auto put = [=](const Produced &value) {
|
2019-09-13 19:45:48 +03:00
|
|
|
EnterFromEventLoop([&] {
|
2019-04-05 14:41:06 +04:00
|
|
|
consumer.put_next_copy(value);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
if constexpr (NoArgument) {
|
|
|
|
return connect([put = std::move(put)] { put({}); });
|
|
|
|
} else {
|
|
|
|
return connect(std::move(put));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:45:48 +03:00
|
|
|
} // namespace base
|