Add TitleInterface

This commit is contained in:
2023-01-10 17:41:03 +01:00
parent f80f8427da
commit 26c0be33b1
5 changed files with 97 additions and 5 deletions

View File

@ -49,6 +49,7 @@ set(headers
src/tftcolors.h
src/tftespiimpl.h
src/tftinterface.h
src/titleinterface.h
src/visibleinterface.h
src/widgets/graph.h
src/widgets/label.h

View File

@ -11,6 +11,7 @@
namespace espgui {
class TftInterface;
class TextInterface;
class TitleInterface;
class MenuDisplay;
class ChangeValueDisplayInterface;
template<typename Tvalue> class ChangeValueDisplay;
@ -60,6 +61,18 @@ public:
}
};
template <typename T1, typename T2, typename T3, typename ...T4>
class makeComponentArgs12 : public T1, public T2, public T3, public T4...
{
public:
template<typename Targ1, typename Targ2, typename Targ3>
makeComponentArgs12(Targ1&& arg1, Targ2&& arg2, Targ3&& arg3) :
T2{std::forward<Targ1>(arg1)},
T3{std::forward<Targ2>(arg2), std::forward<Targ3>(arg3)}
{
}
};
class Display : public virtual ButtonsInterface
{
public:
@ -83,6 +96,9 @@ public:
virtual TextInterface *asTextInterface() { return nullptr; }
virtual const TextInterface *asTextInterface() const { return nullptr; }
virtual TitleInterface *asTitleInterface() { return nullptr; }
virtual const TitleInterface *asTitleInterface() const { return nullptr; }
virtual MenuDisplay *asMenuDisplay() { return nullptr; }
virtual const MenuDisplay *asMenuDisplay() const { return nullptr; }

View File

@ -18,7 +18,7 @@ void DisplayWithTitle::redraw(TftInterface &tft)
{
Base::redraw(tft);
m_titleLabel.redraw(tft, text(), TFT_YELLOW, TFT_BLACK, 4);
m_titleLabel.redraw(tft, title(), TFT_YELLOW, TFT_BLACK, 4);
}
} // namespace espgui

View File

@ -2,20 +2,20 @@
// local includes
#include "display.h"
#include "textinterface.h"
#include "titleinterface.h"
#include "widgets/label.h"
namespace espgui {
class DisplayWithTitle :
public Display,
public virtual TextInterface
public virtual TitleInterface
{
using Base = Display;
public:
TextInterface *asTextInterface() override { return this; }
const TextInterface *asTextInterface() const override { return this; }
TitleInterface *asTitleInterface() override { return this; }
const TitleInterface *asTitleInterface() const override { return this; }
void initScreen(TftInterface &tft) override;
void redraw(TftInterface &tft) override;

75
src/titleinterface.h Normal file
View File

@ -0,0 +1,75 @@
#pragma once
// system includes
#include <string>
#include <utility>
namespace espgui {
class TitleInterface {
public:
virtual std::string title() const = 0;
};
class EmptyTitle : public virtual TitleInterface
{
public:
std::string title() const override { return {}; }
};
template<const char *Ttitle>
class StaticTitle : public virtual TitleInterface
{
public:
static constexpr const char *STATIC_TITLE = Ttitle;
std::string title() const override { return Ttitle; }
};
class ChangeableTitle : public virtual TitleInterface
{
public:
ChangeableTitle() = default;
ChangeableTitle(std::string &&title) : m_title{std::move(title)} {}
ChangeableTitle(std::string_view title) : m_title{title} {}
std::string title() const override { return m_title; }
void setTitle(std::string &&title) { m_title = std::move(title); }
void setTitle(const std::string &title) { m_title = title; }
private:
std::string m_title;
};
template<typename T>
class CachedTitle : public virtual T
{
public:
std::string title() const override
{
if (!m_loaded)
{
m_title = T::title();
m_loaded = true;
}
return m_title;
}
private:
mutable bool m_loaded{};
mutable std::string m_title;
};
template<typename T>
class StaticallyCachedTitle : public virtual T
{
public:
std::string title() const override
{
static const auto title = T::title();
return title;
}
};
} // namespace espgui