RADLib
RADical C++ application framework
RADUi.h
Go to the documentation of this file.
1 #ifndef RADUI_H
2 #define RADUI_H
3 
10 #pragma once
11 
12 #include <RADCore/RADCore.h>
13 
14 #include <chrono>
15 #include <filesystem>
16 #include <functional>
17 #include <memory>
18 #include <mutex>
19 #include <set>
20 #include <stdexcept>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 
25 namespace RADUi {
26 
28  class Binding {
29  public:
31  Binding() = default;
33  explicit Binding(std::shared_ptr<RADCore::RADObject> owner);
34 
36  void disconnect();
38  bool isConnected() const;
39 
40  private:
41  std::shared_ptr<RADCore::RADObject> owner_;
42  };
43 
45  class Backend {
46  public:
48  virtual ~Backend() = default;
49 
51  virtual void run() = 0;
53  virtual void quit() = 0;
55  virtual void postToUi(std::function<void()> task) = 0;
57  virtual bool isUiThread() const = 0;
58  };
59 
61  void setBackend(std::shared_ptr<Backend> backend);
63  bool hasBackend();
66 
68  void run();
70  void quit();
72  void postToUi(std::function<void()> task);
74  bool isUiThread();
75 
78  public:
80  explicit FrameScheduler(double framesPerSecond = 60.0);
83 
85  FrameScheduler(const FrameScheduler&) = delete;
88 
90  void requestUpdate(std::function<void()> task);
91 
92  private:
93  struct Impl;
94  std::shared_ptr<Impl> pImpl_;
95  };
96 
100  Parent,
102  Folder,
104  Project,
106  Midi,
108  Audio,
110  Application,
112  File
113  };
114 
118  std::filesystem::path path;
120  std::string name;
122  std::string subtitle;
124  std::string icon;
126  int depth = 0;
128  bool isDirectory = false;
130  bool expandable = false;
132  bool expanded = false;
134  bool isApplication = false;
137  };
138 
142  bool includeFiles = true;
144  bool includeHidden = false;
146  bool includeParentEntry = true;
148  int maxDepth = 8;
150  std::vector<std::string> allowedExtensions;
151  };
152 
155  public:
157  static std::filesystem::path stablePath(const std::filesystem::path& path);
159  static bool extensionAllowed(const std::filesystem::path& path, const DirectoryBrowserOptions& options);
161  static DirectoryBrowserEntryKind classify(const std::filesystem::directory_entry& entry);
163  static std::string iconForKind(DirectoryBrowserEntryKind kind);
165  static std::vector<DirectoryBrowserEntry> snapshot(const std::filesystem::path& root,
166  const std::set<std::filesystem::path>& expandedDirectories,
167  const DirectoryBrowserOptions& options = {});
168  };
169 
171  template<typename F>
172  void postToRad(RADCore::EventLoop& loop, F&& task) {
173  loop.post(std::forward<F>(task));
174  }
175 
176 } // namespace RADUi
177 
178 #endif
RADCore task event loop with locked and low-latency scheduling strategies.
Definition: RADCore.h:956
void post(Priority priority, Task task)
Posts task with explicit priority.
Abstract UI backend used to post tasks to a toolkit UI thread.
Definition: RADUi.h:45
virtual void run()=0
Runs the toolkit event loop.
virtual ~Backend()=default
Destroys the backend through the interface.
virtual void postToUi(std::function< void()> task)=0
Posts task for execution on the UI thread.
virtual void quit()=0
Requests that the toolkit event loop exit.
virtual bool isUiThread() const =0
Returns true when called from the UI thread.
RAII-style binding handle that keeps a bridge object alive until disconnected.
Definition: RADUi.h:28
bool isConnected() const
Returns true when this binding still owns a bridge object.
void disconnect()
Releases the owned bridge object and disconnects future delivery.
Binding()=default
Creates an empty, disconnected binding.
Binding(std::shared_ptr< RADCore::RADObject > owner)
Creates a binding that owns owner for the lifetime of the binding.
Backend-neutral expandable directory browser model.
Definition: RADUi.h:154
static std::string iconForKind(DirectoryBrowserEntryKind kind)
Returns the toolkit-neutral icon key for kind.
static std::filesystem::path stablePath(const std::filesystem::path &path)
Returns a stable absolute path when possible.
static std::vector< DirectoryBrowserEntry > snapshot(const std::filesystem::path &root, const std::set< std::filesystem::path > &expandedDirectories, const DirectoryBrowserOptions &options={})
Builds a flattened tree snapshot from root and expanded directory paths.
static bool extensionAllowed(const std::filesystem::path &path, const DirectoryBrowserOptions &options)
Returns true when extension matches options.allowedExtensions, or the list is empty.
static DirectoryBrowserEntryKind classify(const std::filesystem::directory_entry &entry)
Classifies a filesystem entry for display and behavior.
Coalesces UI updates so expensive redraws occur at a target frame rate.
Definition: RADUi.h:77
FrameScheduler & operator=(const FrameScheduler &)=delete
Schedulers are owned by one bridge/model and are not copied.
FrameScheduler(double framesPerSecond=60.0)
Creates a scheduler capped at framesPerSecond.
~FrameScheduler()
Stops the scheduler worker.
void requestUpdate(std::function< void()> task)
Requests that task run on the UI thread at the next scheduled frame.
FrameScheduler(const FrameScheduler &)=delete
Schedulers are owned by one bridge/model and are not copied.
Definition: RADAppKit.h:20
void postToUi(std::function< void()> task)
Posts task to the installed UI backend.
bool hasBackend()
Returns true when a UI backend is installed.
void setBackend(std::shared_ptr< Backend > backend)
Installs the process-wide UI backend used by RADUi helpers.
bool isUiThread()
Returns true when the current thread is the installed UI backend thread.
void postToRad(RADCore::EventLoop &loop, F &&task)
Posts task to a RADCore event loop from UI-side code.
Definition: RADUi.h:172
Backend & backend()
Returns the installed backend or throws when none is installed.
void run()
Runs the installed backend's UI event loop.
DirectoryBrowserEntryKind
File type category for DirectoryBrowserEntry.
Definition: RADUi.h:98
@ Parent
Synthetic parent-folder row.
@ Project
RADBard or RADLib-style project file.
@ Folder
Filesystem directory.
@ Application
Executable or desktop application entry.
void quit()
Requests the installed backend to quit.
One flattened row in a native-style expandable directory browser.
Definition: RADUi.h:116
int depth
Tree depth relative to the browser root.
Definition: RADUi.h:126
bool isDirectory
True for filesystem directories and the synthetic parent row.
Definition: RADUi.h:128
std::filesystem::path path
Stable absolute path for the row.
Definition: RADUi.h:118
std::string subtitle
Secondary display text.
Definition: RADUi.h:122
bool expanded
True when this directory is expanded.
Definition: RADUi.h:132
std::string name
Display name.
Definition: RADUi.h:120
bool isApplication
True when this row represents an executable/app-like file.
Definition: RADUi.h:134
std::string icon
Toolkit-neutral icon key: up, folder, score, midi, audio, app, or file.
Definition: RADUi.h:124
bool expandable
True when this directory currently has displayable children.
Definition: RADUi.h:130
DirectoryBrowserEntryKind kind
Classified file type.
Definition: RADUi.h:136
Options for DirectoryBrowserModel snapshots.
Definition: RADUi.h:140
std::vector< std::string > allowedExtensions
Optional lowercase extension allow-list. Empty means all files.
Definition: RADUi.h:150
bool includeFiles
Include files in addition to folders.
Definition: RADUi.h:142
bool includeHidden
Include dot-prefixed hidden files and folders.
Definition: RADUi.h:144
int maxDepth
Maximum expanded tree depth.
Definition: RADUi.h:148
bool includeParentEntry
Include synthetic parent row when the root has a parent.
Definition: RADUi.h:146