libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
tuisubsystem.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <chrono>
11#include <promeki/namespace.h>
12#include <promeki/application.h>
13#include <promeki/atomic.h>
14#include <promeki/terminal.h>
15#include <promeki/point.h>
16#include <promeki/eventloop.h>
17#include <promeki/tui/screen.h>
18#include <promeki/tui/palette.h>
20
21PROMEKI_NAMESPACE_BEGIN
22
23class TuiWidget;
24
58 public:
68
71
72 TuiSubsystem(const TuiSubsystem &) = delete;
73 TuiSubsystem &operator=(const TuiSubsystem &) = delete;
74
78 static TuiSubsystem *instance() { return _instance; }
79
84 void setRootWidget(TuiWidget *widget);
85
87 TuiWidget *rootWidget() const { return _rootWidget; }
88
90 TuiScreen &screen() { return _screen; }
91
93 Terminal &terminal() { return _terminal; }
94
96 const TuiPalette &palette() const { return _palette; }
97
99 TuiPalette &palette() { return _palette; }
100
102 void setPalette(const TuiPalette &palette) { _palette = palette; }
103
115 void setColorMode(Terminal::ColorSupport mode) {
116 _screen.setColorMode(mode);
117 updateAll();
118 }
119
124 Terminal::ColorSupport colorMode() const { return _screen.colorMode(); }
125
129 void updateAll();
130
136
138 TuiWidget *focusWidget() const { return _focusWidget; }
139
144 void focusNext(bool reverse = false);
145
156
163 void grabMouse(TuiWidget *widget) { _mouseGrab = widget; }
164
168 void releaseMouse() { _mouseGrab = nullptr; }
169
170 private:
171 static TuiSubsystem *_instance;
172
173 EventLoop *_eventLoop = nullptr;
174 Terminal _terminal;
175 TuiScreen _screen;
176 TuiPalette _palette;
177 TuiInputParser _inputParser;
178 AnsiStream _ansiStream;
179 TuiWidget *_rootWidget = nullptr;
180 TuiWidget *_focusWidget = nullptr;
181 TuiWidget *_mouseGrab = nullptr;
182 int _lastCols = 0;
183 int _lastRows = 0;
184
185 // Event-loop-driven input / resize / repaint state.
186 // - _stdinSourceHandle: IoSource handle for STDIN_FILENO.
187 // - _winchSubscription: SignalHandler subscription handle
188 // for SIGWINCH. The SignalHandler watcher thread
189 // invokes the subscriber callback, which in turn
190 // posts handleResize() onto the main EventLoop.
191 // - _repaintQueued: coalesces markNeedsRepaint() calls
192 // into a single pending postCallable — if a repaint
193 // is already queued we skip reposting.
194 int _stdinSourceHandle = -1;
195 int _winchSubscription = -1;
196 Atomic<bool> _repaintQueued;
197
198 // Double-click detection state
199 using Clock = std::chrono::steady_clock;
200 using TimePoint = Clock::time_point;
201 static constexpr int DoubleClickIntervalMs = 400;
202 TimePoint _lastClickTime{};
203 Point2Di32 _lastClickPos{-1, -1};
204 MouseEvent::Button _lastClickButton = MouseEvent::NoButton;
205
206 void setupEventSources();
207 void teardownEventSources();
208 void doPaint();
209 void processInput();
210 void paintWidgets();
211 void paintWidget(TuiWidget *widget);
212 void handleResize();
213 void dispatchKeyEvent(const TuiInputParser::ParsedEvent &ev);
214 void dispatchMouseEvent(const TuiInputParser::ParsedEvent &ev);
215 void collectFocusable(TuiWidget *widget, List<TuiWidget *> &list);
216 TuiWidget *widgetAt(TuiWidget *widget, const Point2Di32 &globalPos);
217};
218
219PROMEKI_NAMESPACE_END
State machine for parsing terminal escape sequences into events.
Definition inputparser.h:29
Style palette for TUI widgets.
Definition palette.h:41
Double-buffered character cell grid for TUI rendering.
Definition screen.h:68
TUI subsystem installed alongside an Application.
Definition tuisubsystem.h:57
void releaseMouse()
Releases the mouse grab.
Definition tuisubsystem.h:168
const TuiPalette & palette() const
Returns the palette.
Definition tuisubsystem.h:96
void setColorMode(Terminal::ColorSupport mode)
Sets the color mode for the TUI screen.
Definition tuisubsystem.h:115
void setRootWidget(TuiWidget *widget)
Sets the top-level (root) widget.
void setFocusWidget(TuiWidget *widget)
Sets the focused widget.
void grabMouse(TuiWidget *widget)
Grabs mouse events for a widget.
Definition tuisubsystem.h:163
TuiScreen & screen()
Returns the screen.
Definition tuisubsystem.h:90
TuiSubsystem()
Installs the TUI on the current Application.
void updateAll()
Forces a full screen repaint.
void setPalette(const TuiPalette &palette)
Sets the palette.
Definition tuisubsystem.h:102
static TuiSubsystem * instance()
Returns the active TuiSubsystem instance.
Definition tuisubsystem.h:78
TuiWidget * focusWidget() const
Returns the currently focused widget.
Definition tuisubsystem.h:138
void focusNext(bool reverse=false)
Cycles focus to the next focusable widget.
~TuiSubsystem()
Destructor. Restores terminal state and releases I/O sources.
void markNeedsRepaint()
Marks the screen as needing a repaint.
Terminal::ColorSupport colorMode() const
Returns the current color mode.
Definition tuisubsystem.h:124
TuiPalette & palette()
Returns the palette for modification.
Definition tuisubsystem.h:99
Terminal & terminal()
Returns the terminal.
Definition tuisubsystem.h:93
TuiWidget * rootWidget() const
Returns the root widget.
Definition tuisubsystem.h:87
TUI-specific widget base class.
Definition widget.h:33