5#include <unordered_map>
15#include <gempyre_types.h>
30using namespace std::chrono_literals;
39 class GempyreInternal;
40 enum class CloseStatus;
41 enum class WindowType;
52 GEMPYRE_EX std::tuple<int, int, int>
version();
57 class GEMPYRE_EX
HtmlStream :
public std::ostringstream {
62 #ifdef AUTO_UINT8_STREAM
65 if constexpr (std::is_same_v<std::decay_t<T>, int8_t>) {
66 static_cast<std::ostringstream&
>(*this) <<
static_cast<int>(value);
67 }
else if constexpr (std::is_same_v<std::decay_t<T>, uint8_t>) {
68 static_cast<std::ostringstream&
>(*this) <<
static_cast<unsigned int>(value);
70 static_cast<std::ostringstream&
>(*this) << std::forward<T>(value);
77 using FlushFunction = std::function<void (
HtmlStream&)>;
80 const FlushFunction m_flush;
87 using Attributes = std::unordered_map<std::string, std::string>;
89 using Values = std::unordered_map<std::string, std::string>;
127 [[nodiscard]]
const Ui&
ui()
const {
return *m_ui; }
129 [[nodiscard]]
Ui&
ui() {
return *m_ui;}
131 [[nodiscard]] std::string
id()
const {
return m_id;}
172 [[nodiscard]] std::optional<Values>
styles(
const std::vector<std::string>& keys)
const;
174 [[nodiscard]] std::optional<Elements>
children()
const;
176 [[nodiscard]] std::optional<Values>
values()
const;
178 [[nodiscard]] std::optional<std::string>
html()
const;
182 [[nodiscard]] std::optional<std::string>
type()
const;
184 [[nodiscard]] std::optional<Rect>
rect()
const;
186 [[nodiscard]] std::optional<Element>
parent()
const;
189 const GempyreInternal& ref()
const;
190 GempyreInternal& ref();
191 static const std::string generateId(std::string_view prefix);
197 friend class GempyreInternal;
221 static constexpr auto CLICK =
"click";
227 static constexpr auto FOCUS =
"focus";
229 static constexpr auto BLUR =
"blur";
235 static constexpr auto LOAD =
"load";
240 static constexpr auto REMOVED =
"element_removed";
251 static bool has_true(
const std::optional<std::unordered_map<std::string, std::string>>& map, std::string_view key);
256 static bool has_true(
const std::unordered_map<std::string, std::string>& map, std::string_view key);
260 class GEMPYRE_EX
Ui {
277 using FileMap = std::vector<std::pair<std::string, std::string>>;
281 static constexpr unsigned short UseDefaultPort = 0;
282 static constexpr auto UseDefaultRoot =
"";
290 Ui(
const FileMap& filemap, std::string_view indexHtml,
unsigned short port = UseDefaultPort, std::string_view root = UseDefaultRoot);
299 Ui(
const FileMap& filemap, std::string_view indexHtml, std::string_view browser, std::string_view browser_params,
unsigned short port = UseDefaultPort, std::string_view root = UseDefaultRoot);
311 Ui(
const FileMap& filemap, std::string_view indexHtml, std::string_view title,
int width,
int height,
unsigned flags = 0,
312 const std::unordered_map<std::string, std::string>& ui_params = {},
unsigned short port = UseDefaultPort, std::string_view root = UseDefaultRoot);
316 Ui(
const Ui& other) =
delete;
317 Ui(
Ui&& other) =
delete;
322 [[deprecated(
"Prefer exit")]]
void close();
331 using ErrorFunction = std::function<void (
const std::string& element,
const std::string& info)>;
366 void eval(std::string_view eval);
374 void open(std::string_view url, std::string_view name =
"");
380 TimerId
start_periodic(
const std::chrono::milliseconds& ms,
const std::function<
void (TimerId
id)>& timerFunc);
385 TimerId
start_periodic(
const std::chrono::milliseconds& ms,
const std::function<
void ()>& timerFunc);
391 TimerId
after(
const std::chrono::milliseconds& ms,
const std::function<
void (TimerId
id)>& timerFunc);
397 TimerId
after(
const std::chrono::milliseconds& ms,
const std::function<
void ()>& timerFunc);
407 [[nodiscard]] std::string
address_of(std::string_view filepath)
const;
410 [[nodiscard]] std::optional<Element::Elements>
by_class(std::string_view className)
const;
413 [[nodiscard]] std::optional<Element::Elements>
by_name(std::string_view className)
const;
416 [[nodiscard]] std::optional<std::pair<std::chrono::microseconds, std::chrono::microseconds>>
ping()
const;
420 void extension_call(std::string_view callId,
const std::unordered_map<std::string, std::any>& parameters);
422 [[nodiscard]] std::optional<std::any> extension_get(std::string_view callId,
const std::unordered_map<std::string, std::any>& parameters);
428 [[nodiscard]] std::optional<std::vector<uint8_t>>
resource(std::string_view url)
const;
434 bool add_file(std::string_view url, std::string_view file);
439 std::optional<std::string>
add_file(std::string_view file);
445 bool add_data(std::string_view url,
const std::vector<uint8_t>& data);
493 bool ui_available()
const;
497 Ui(
const FileMap& filemap, std::string_view indexHtml,
498 unsigned short port, std::string_view root,
499 const std::unordered_map<std::string, std::string>& parameters, WindowType windowType);
500 const GempyreInternal& ref()
const;
501 GempyreInternal& ref();
504 std::unique_ptr<GempyreInternal> m_ui;
Represents all HTML elements on UI.
Definition gempyre.h:84
std::function< void(const Event &)> SubscribeFunction
Callback function for event subscriptions.
Definition gempyre.h:93
std::optional< Values > values() const
Applies to form elements only - receive values bound to the element.
Element & set_html(std::string_view htmlText)
Set HTML text value of the element.
Element & operator=(Element &&other)
Move operator.
Definition gempyre.h:105
std::optional< Rect > rect() const
Get this element UI rect. I.e area it occupies on screen (if applicable)
Element(Ui &ui, std::string_view id, std::string_view htmlElement, const Element &parent)
Constructor for exiting elements.
virtual ~Element()
Destructor.
Element & set_attribute(std::string_view attr, std::string_view value)
Set HTML a attribute of this element.
std::unordered_map< std::string, std::string > Attributes
Attribute key, value pairs.
Definition gempyre.h:87
std::string id() const
Get id of element.
Definition gempyre.h:131
Element(Ui &ui, std::string_view htmlElement, const Element &parent)
Constructor for exiting elements.
Element & subscribe(std::string_view name, const SubscribeFunction &handler, const std::vector< std::string > &properties={}, const std::chrono::milliseconds &throttle=0ms)
Subscribe UI event.
Element & operator=(const Element &other)
Copy operator.
Definition gempyre.h:103
Element & set_attribute(std::string_view attr)
Set HTML a attribute of this element.
std::optional< Attributes > attributes() const
Get this element attributes.
Ui & ui()
Get Ui.
Definition gempyre.h:129
std::optional< std::string > type() const
Get this element type, mostly a HTML tag.
Element(Element &&other)=default
Move constructor.
std::vector< Element > Elements
Vector of Elements.
Definition gempyre.h:91
std::optional< Element > parent() const
Parent of this element. If query fails, element is root or parent id is not set, nullopt is returned.
void remove()
Remove this element from UI.
const Ui & ui() const
Get Ui.
Definition gempyre.h:127
std::optional< Values > styles(const std::vector< std::string > &keys) const
Get element styles.
std::optional< Elements > children() const
Get element children.
Element(Ui &ui, std::string_view id)
Constructor for existing elements.
Element(const Element &other)=default
Copy constructor.
Element & set_style(std::string_view style, std::string_view value)
Set CSS style of this element.
Element & remove_attribute(std::string_view attr)
Remove attribute.
std::optional< std::string > html() const
Get HTML value bound to this element (does not apply all elements)
std::unordered_map< std::string, std::string > Values
Value key, value pairs.
Definition gempyre.h:89
HtmlStream html_stream()
get a stream that writes to html part of this element
The application UI.
Definition gempyre.h:260
void exit()
Application gracefully finish the event loop and exits.
std::vector< std::pair< std::string, std::string > > FileMap
Resource map type.
Definition gempyre.h:277
void flush()
Write pending requests to UI - e.g. when eventloop thread is blocked.
void set_timer_on_hold(bool on_hold)
Set all timers to hold. Can be used to pause UI actions.
void debug(std::string_view msg)
Send a debug message to UI. Message is get received is set_logging is true.
static Ui::FileMap to_file_map(const std::vector< std::string > &filenames)
Read list files as a maps.
bool add_data(std::string_view url, const std::vector< uint8_t > &data)
Add a data into Gempyre to be accessed via url.
void eval(std::string_view eval)
Executes eval string in UI context.
bool is_timer_on_hold() const
Tells if timers are on hold.
void run()
Starts the event loop.
UiFlags
UI flags for a Window UI.
Definition gempyre.h:263
void resize(int width, int height)
Resize, fail silently if backend wont support.
TimerId start_periodic(const std::chrono::milliseconds &ms, const std::function< void(TimerId id)> &timerFunc)
Start a periodic timer.
std::function< void()> OpenFunction
Function called on open.x.
Definition gempyre.h:329
TimerId after(const std::chrono::milliseconds &ms, const std::function< void()> &timerFunc)
Starts a single shot timer.
Ui(const FileMap &filemap, std::string_view indexHtml, std::string_view title, int width, int height, unsigned flags=0, const std::unordered_map< std::string, std::string > &ui_params={}, unsigned short port=UseDefaultPort, std::string_view root=UseDefaultRoot)
Create a window UI.
std::optional< std::pair< std::chrono::microseconds, std::chrono::microseconds > > ping() const
Test function to measure round trip time.
void open(std::string_view url, std::string_view name="")
Opens an url in the UI view.
void set_logging(bool logging)
Set browser to verbose mode.
void close()
Requires Client window to close (that cause the application to close).
bool cancel_timer(TimerId timerId)
Stop a timer.
std::function< void()> ReloadFunction
Function called on reload. (page reload)
Definition gempyre.h:327
TimerId start_periodic(const std::chrono::milliseconds &ms, const std::function< void()> &timerFunc)
Start a periodic timer.
ExitFunction on_exit(const ExitFunction &onExitFunction)
The callback is called before before the eventloop exit.
bool available(std::string_view id) const
test if Element can be accessed. Note that in false it's may be in HTML, but not available in DOM tre...
std::optional< Element::Elements > by_class(std::string_view className) const
Get elements by class name.
std::optional< Element::Elements > by_name(std::string_view className) const
Get elements by name.
void set_application_icon(const uint8_t *data, size_t dataLen, std::string_view type)
Set application icon, fail silently if backend wont support.
TimerId after(const std::chrono::milliseconds &ms, const std::function< void(TimerId id)> &timerFunc)
Starts a single shot timer.
OpenFunction on_open(const OpenFunction &onOpenFunction)
The callback is called on UI open.
std::string address_of(std::string_view filepath) const
Get a local file path an URL, can be used with open.
void end_batch()
Ends an UI read batch, push all stored messages at once.
void begin_batch()
Starts an UI write batch, no messages are sent to USER until endBatch.
Element root() const
Get a (virtual) root element.
static std::optional< std::string > add_file(FileMap &map, std::string_view filename)
Add file data into map to be added as a map.
std::optional< double > device_pixel_ratio() const
Get an native UI device pixel ratio.
void set_title(std::string_view name)
Set title, fail silently if backend wont support.
bool add_file(std::string_view url, std::string_view file)
Add a file data into Gempyre to be accessed via url.
void alert(std::string_view msg)
Show an alert window.
std::optional< std::string > add_file(std::string_view file)
Add a file data into Gempyre to be accessed via url.
std::function< void(const std::string &element, const std::string &info)> ErrorFunction
Function called on UI error.
Definition gempyre.h:331
ErrorFunction on_error(const ErrorFunction &onErrorFunction)
The callback called on UI error.
Ui(const FileMap &filemap, std::string_view indexHtml, unsigned short port=UseDefaultPort, std::string_view root=UseDefaultRoot)
Create UI using default ui app or gempyre.conf.
std::optional< std::vector< uint8_t > > resource(std::string_view url) const
Read a resource.
Ui(const FileMap &filemap, std::string_view indexHtml, std::string_view browser, std::string_view browser_params, unsigned short port=UseDefaultPort, std::string_view root=UseDefaultRoot)
Create a browser UI using given ui app and command line.
std::function< void()> ExitFunction
Function called on exit.
Definition gempyre.h:325
ReloadFunction on_reload(const ReloadFunction &onReloadFunction)
The callback is called on UI reload.
GEMPYRE_EX void set_debug(bool isDebug=true)
set debugging on/off
GEMPYRE_EX void setJNIENV(void *env, void *obj)
Internal for Android.
GEMPYRE_EX std::tuple< int, int, int > version()
Return current version.
Event received.
Definition gempyre.h:201
static constexpr auto SELECT
Generic select.
Definition gempyre.h:225
static constexpr auto REMOVED
Remove - Element is removed.
Definition gempyre.h:240
static bool has_true(const std::unordered_map< std::string, std::string > &map, std::string_view key)
Utility to find if subsctibed event is true.
std::unordered_map< std::string, std::string > properties
List of requested properties.
Definition gempyre.h:245
static constexpr auto CLICK
Generic click.
Definition gempyre.h:221
static bool has_true(const std::optional< std::unordered_map< std::string, std::string > > &map, std::string_view key)
Utility to find if subsctibed event is true.
static constexpr auto MOUSE_MOVE
Mouse moving.
Definition gempyre.h:203
static constexpr auto SCROLL
Scroll.
Definition gempyre.h:219
static constexpr auto KEY_UP
Key up.
Definition gempyre.h:213
static constexpr auto MOUSE_UP
Mouse button up.
Definition gempyre.h:205
static constexpr auto KEY_DOWN
Key down.
Definition gempyre.h:217
static constexpr auto MOUSE_DBLCLICK
Mouse button double click.
Definition gempyre.h:211
static constexpr auto BLUR
Off focus.
Definition gempyre.h:229
static constexpr auto LOAD
Load.
Definition gempyre.h:235
static constexpr auto RESIZE
Resize - use ui.root().subscribe(Event::RESIZE, [... for window resize.
Definition gempyre.h:237
static constexpr auto FOCUS_OUT
Focus Out.
Definition gempyre.h:233
static constexpr auto CHANGE
Generic change.
Definition gempyre.h:223
static constexpr auto FOCUS
On focus.
Definition gempyre.h:227
static constexpr auto MOUSE_CLICK
Click event.
Definition gempyre.h:209
static constexpr auto MOUSE_DOWN
Mouse button down.
Definition gempyre.h:207
Element element
element that has emitted the event, the same that did subscription.
Definition gempyre.h:243
static constexpr auto FOCUS_IN
In Focus.
Definition gempyre.h:231
static constexpr auto KEY_PRESS
Key pressed.
Definition gempyre.h:215
Rect.
Definition gempyre_types.h:24