Lumiera  0.pre.03
»edit your freedom«
expander-revealer.hpp
Go to the documentation of this file.
1 /*
2  EXPANDER-REVEALER.hpp - functor components for standard UI element actions
3 
4  Copyright (C)
5  2018, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
53 #ifndef STAGE_MODEL_EXPANDER_REVEALER_H
54 #define STAGE_MODEL_EXPANDER_REVEALER_H
55 
56 
57 #include "lib/error.hpp"
58 
59 #include <functional>
60 #include <utility>
61 
62 
63 namespace stage {
64 namespace model {
65 
66  using std::move;
67 
68 
76  class Expander
77  {
78  public:
79  using ProbeFun = std::function<bool(void)>;
80  using ChangeFun = std::function<void(bool)>;
81 
82  private:
83  ProbeFun probeState_;
84  ChangeFun changeState_;
85 
86  public:
87  Expander() { }
88  Expander(ProbeFun detectCurrExpansionState, ChangeFun expand_collapse)
89  : probeState_{move (detectCurrExpansionState)}
90  , changeState_{move (expand_collapse)}
91  {
92  ENSURE (canExpand());
93  }
94 
95  bool
96  canExpand() const
97  {
98  return bool{probeState_}
99  and bool{changeState_};
100  }
101 
102  explicit
103  operator bool() const
104  {
105  REQUIRE (canExpand());
106  return probeState_();
107  }
108 
109  bool
110  operator() (bool shallExpand)
111  {
112  REQUIRE (canExpand());
113  bool currState = probeState_();
114  if (currState != shallExpand)
115  changeState_(shallExpand);
116  return currState;
117  }
118 
119  /* === alternate "expressive" API === */
120  bool
121  expand (bool yes =true)
122  {
123  return this->operator() (yes);
124  }
125 
126  bool
127  collapse()
128  {
129  return expand (false);
130  }
131 
132  bool
133  isExpanded() const
134  {
135  return bool{*this};
136  }
137  };
138 
139 
140 
147  class Revealer
148  {
149  public:
150  using RevealeItFun = std::function<void()>;
151 
152  private:
153  RevealeItFun revealIt_;
154 
155  public:
156  Revealer() { }
157  Revealer(RevealeItFun how_to_uncover_the_element)
158  : revealIt_{move (how_to_uncover_the_element)}
159  {
160  ENSURE (canReveal());
161  }
162 
163  bool
164  canReveal() const
165  {
166  return bool{revealIt_};
167  }
168 
169  void
170  operator()()
171  {
172  REQUIRE (canReveal());
173  revealIt_();
174  }
175  };
176 
177 
178 
179 }} // namespace stage::model
180 #endif /*STAGE_MODEL_EXPANDER_REVEALER_H*/
Functor component to support the default implementation of revealing an UI-Element.
Lumiera GTK UI implementation root.
Definition: guifacade.cpp:37
Lumiera error handling (C++ interface).
Functor component to support the default implementation of expanding/collapsing.