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) Lumiera.org
5  2018, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
62 #ifndef STAGE_MODEL_EXPANDER_REVEALER_H
63 #define STAGE_MODEL_EXPANDER_REVEALER_H
64 
65 
66 #include "lib/error.hpp"
67 
68 #include <functional>
69 #include <utility>
70 
71 
72 namespace stage {
73 namespace model {
74 
75  using std::move;
76 
77 
85  class Expander
86  {
87  public:
88  using ProbeFun = std::function<bool(void)>;
89  using ChangeFun = std::function<void(bool)>;
90 
91  private:
92  ProbeFun probeState_;
93  ChangeFun changeState_;
94 
95  public:
96  Expander() { }
97  Expander(ProbeFun detectCurrExpansionState, ChangeFun expand_collapse)
98  : probeState_{move (detectCurrExpansionState)}
99  , changeState_{move (expand_collapse)}
100  {
101  ENSURE (canExpand());
102  }
103 
104  bool
105  canExpand() const
106  {
107  return bool{probeState_}
108  and bool{changeState_};
109  }
110 
111  explicit
112  operator bool() const
113  {
114  REQUIRE (canExpand());
115  return probeState_();
116  }
117 
118  bool
119  operator() (bool shallExpand)
120  {
121  REQUIRE (canExpand());
122  bool currState = probeState_();
123  if (currState != shallExpand)
124  changeState_(shallExpand);
125  return currState;
126  }
127 
128  /* === alternate "expressive" API === */
129  bool
130  expand (bool yes =true)
131  {
132  return this->operator() (yes);
133  }
134 
135  bool
136  collapse()
137  {
138  return expand (false);
139  }
140 
141  bool
142  isExpanded() const
143  {
144  return bool{*this};
145  }
146  };
147 
148 
149 
156  class Revealer
157  {
158  public:
159  using RevealeItFun = std::function<void()>;
160 
161  private:
162  RevealeItFun revealIt_;
163 
164  public:
165  Revealer() { }
166  Revealer(RevealeItFun how_to_uncover_the_element)
167  : revealIt_{move (how_to_uncover_the_element)}
168  {
169  ENSURE (canReveal());
170  }
171 
172  bool
173  canReveal() const
174  {
175  return bool{revealIt_};
176  }
177 
178  void
179  operator()()
180  {
181  REQUIRE (canReveal());
182  revealIt_();
183  }
184  };
185 
186 
187 
188 }} // namespace stage::model
189 #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:46
Lumiera error handling (C++ interface).
Functor component to support the default implementation of expanding/collapsing.