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 #include <RADUi/RADDesktopShell.h>
26 
27 namespace RADUi {
28 
30  class Binding {
31  public:
33  Binding() = default;
35  explicit Binding(std::shared_ptr<RADCore::RADObject> owner);
36 
38  void disconnect();
40  bool isConnected() const;
41 
42  private:
43  std::shared_ptr<RADCore::RADObject> owner_;
44  };
45 
47  class Backend {
48  public:
50  virtual ~Backend() = default;
51 
53  virtual void run() = 0;
55  virtual void quit() = 0;
57  virtual void postToUi(std::function<void()> task) = 0;
59  virtual bool isUiThread() const = 0;
60  };
61 
63  void setBackend(std::shared_ptr<Backend> backend);
65  bool hasBackend();
68 
70  void run();
72  void quit();
74  void postToUi(std::function<void()> task);
76  bool isUiThread();
77 
80  public:
82  explicit FrameScheduler(double framesPerSecond = 60.0);
85 
87  FrameScheduler(const FrameScheduler&) = delete;
90 
92  void requestUpdate(std::function<void()> task);
93 
94  private:
95  struct Impl;
96  std::shared_ptr<Impl> pImpl_;
97  };
98 
102  Parent,
104  Folder,
106  Project,
108  Midi,
110  Audio,
112  Application,
114  File
115  };
116 
120  std::filesystem::path path;
122  std::string name;
124  std::string subtitle;
126  std::string icon;
128  int depth = 0;
130  bool isDirectory = false;
132  bool expandable = false;
134  bool expanded = false;
136  bool isApplication = false;
139  };
140 
144  bool includeFiles = true;
146  bool includeHidden = false;
148  bool includeParentEntry = true;
150  int maxDepth = 8;
152  std::vector<std::string> allowedExtensions;
153  };
154 
157  public:
159  static std::filesystem::path stablePath(const std::filesystem::path& path);
161  static bool extensionAllowed(const std::filesystem::path& path, const DirectoryBrowserOptions& options);
163  static DirectoryBrowserEntryKind classify(const std::filesystem::directory_entry& entry);
165  static std::string iconForKind(DirectoryBrowserEntryKind kind);
167  static std::vector<DirectoryBrowserEntry> snapshot(const std::filesystem::path& root,
168  const std::set<std::filesystem::path>& expandedDirectories,
169  const DirectoryBrowserOptions& options = {});
170  };
171 
173  template<typename F>
174  void postToRad(RADCore::EventLoop& loop, F&& task) {
175  loop.post(std::forward<F>(task));
176  }
177 
178 } // namespace RADUi
179 
180 #endif
RADCore task event loop with locked and low-latency scheduling strategies.
Definition: RADCore.h:988
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:47
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:30
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:156
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:79
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:174
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:100
@ 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:118
int depth
Tree depth relative to the browser root.
Definition: RADUi.h:128
bool isDirectory
True for filesystem directories and the synthetic parent row.
Definition: RADUi.h:130
std::filesystem::path path
Stable absolute path for the row.
Definition: RADUi.h:120
std::string subtitle
Secondary display text.
Definition: RADUi.h:124
bool expanded
True when this directory is expanded.
Definition: RADUi.h:134
std::string name
Display name.
Definition: RADUi.h:122
bool isApplication
True when this row represents an executable/app-like file.
Definition: RADUi.h:136
std::string icon
Toolkit-neutral icon key: up, folder, score, midi, audio, app, or file.
Definition: RADUi.h:126
bool expandable
True when this directory currently has displayable children.
Definition: RADUi.h:132
DirectoryBrowserEntryKind kind
Classified file type.
Definition: RADUi.h:138
Options for DirectoryBrowserModel snapshots.
Definition: RADUi.h:142
std::vector< std::string > allowedExtensions
Optional lowercase extension allow-list. Empty means all files.
Definition: RADUi.h:152
bool includeFiles
Include files in addition to folders.
Definition: RADUi.h:144
bool includeHidden
Include dot-prefixed hidden files and folders.
Definition: RADUi.h:146
int maxDepth
Maximum expanded tree depth.
Definition: RADUi.h:150
bool includeParentEntry
Include synthetic parent row when the root has a parent.
Definition: RADUi.h:148