Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions samples/demo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ SOFTWARE.

using namespace tinyui;

static constexpr Id RootPanelId = 1;

static constexpr Id NextPanelId = 100;

int quit(Id, void *instance) {
int quit(WidgetHandle, void *instance) {
if (instance == nullptr) {
return ErrorCode;
}
Expand All @@ -46,7 +42,7 @@ static uint32_t LastTick = 0;
static uint32_t Diff = 0;
static constexpr uint32_t TimeDiff = 10;

int updateProgressbar(Id, void *instance) {
int updateProgressbar(WidgetHandle, void *instance) {
if (instance == nullptr) {
return ErrorCode;
}
Expand Down Expand Up @@ -87,29 +83,44 @@ int main(int argc, char *argv[]) {
}

constexpr int32_t ButtonHeight = 25;
Widgets::panel(RootPanelId, 0, "Sample-Dialog", Rect(90, 5, 120, 600), nullptr);
Widgets::label(2, RootPanelId, "Title", Rect(100, 10, 100, 20), Alignment::Center);
Widgets::textButton(3, RootPanelId, "Test 1", Rect(100, 50, 100, ButtonHeight), Alignment::Center, nullptr);
Widgets::textButton(4, RootPanelId, "Test 2", Rect(100, 100, 100, ButtonHeight), Alignment::Center, nullptr);
Widgets::textButton(5, RootPanelId, "Test 3", Rect(100, 150, 100, ButtonHeight), Alignment::Center, nullptr);
Widgets::imageButton(6, RootPanelId, "button_test.png", Rect(100, 200, 100, ButtonHeight), nullptr);
WidgetHandle panel = Widgets::panel(WidgetHandle::getRootHandle(), "Sample-Dialog", Rect(90, 5, 120, 600), nullptr);
if (!panel.isValid()) {
const auto &ctx = TinyUi::getContext();
ctx.mLogger(LogSeverity::Error, "Cannot create panel");
return ErrorCode;
}

Widgets::label(panel, "Title", Rect(100, 10, 100, 20), Alignment::Center);
Widgets::textButton(panel, "Test 1", Rect(100, 50, 100, ButtonHeight), Alignment::Center, nullptr);
Widgets::textButton(panel, "Test 2", Rect(100, 100, 100, ButtonHeight), Alignment::Center, nullptr);
Widgets::textButton(panel, "Test 3", Rect(100, 150, 100, ButtonHeight), Alignment::Center, nullptr);
Widgets::imageButton(panel, "button_test.png", Rect(100, 200, 100, ButtonHeight), nullptr);

auto &ctx = TinyUi::getContext();

// Allocate callbacks dynamically to ensure they persist during event handling
CallbackI *dynamicQuitCallback = new CallbackI(quit, (void*) &ctx);
CallbackI *dynamicUpdateProgressBarCallback = new CallbackI(updateProgressbar, nullptr, Events::UpdateEvent);

Widgets::textButton(7, RootPanelId, "Quit", Rect(100, 250, 100, ButtonHeight), Alignment::Center, dynamicQuitCallback);
Widgets::progressBar(8, RootPanelId, Rect(100, 300, 100, ButtonHeight), 50, dynamicUpdateProgressBarCallback);
Widgets::textButton(panel, "Quit", Rect(100, 250, 100, ButtonHeight), Alignment::Center, dynamicQuitCallback);
Widgets::progressBar(panel, Rect(100, 300, 100, ButtonHeight), 50, dynamicUpdateProgressBarCallback);

Widgets::inputText(9, RootPanelId, Rect(100, 350, 100, ButtonHeight), Alignment::Left, KeyInputType::Character, "");
Widgets::inputText(panel, Rect(100, 350, 100, ButtonHeight), Alignment::Left, KeyInputType::Character, "");

Widgets::treeView(10, RootPanelId, "tree", Rect(100, 400, 100, ButtonHeight));
Widgets::treeItem(11, 10, "Item 1");
//Widgets::treeItem(12, 11, "Item 1.1");
Widgets::treeItem(13, 10, "Item 2");
Widgets::treeItem(14, 13, "Item 2.1");
WidgetHandle tree = Widgets::treeView(panel, "tree", Rect(100, 400, 100, ButtonHeight));
if (!tree.isValid()) {
ctx.mLogger(LogSeverity::Error, "Cannot create tree view");
return ErrorCode;
}

Widgets::treeItem(tree, "Item 1");

WidgetHandle view = Widgets::treeItem(tree, "Item 2");
if (!view.isValid()) {
ctx.mLogger(LogSeverity::Error, "Cannot create tree view item");
return ErrorCode;
}
Widgets::treeItem(view, "Item 2.1");

while (TinyUi::run()) {
TinyUi::render();
Expand Down
15 changes: 9 additions & 6 deletions samples/hello_world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ SOFTWARE.

using namespace tinyui;

static constexpr Id RootPanelId = 1;

int quit(Id, void *instance) {
int quit(WidgetHandle, void *instance) {
if (instance == nullptr) {
return ErrorCode;
}
Expand All @@ -50,12 +48,17 @@ int main(int argc, char *argv[]) {
}

constexpr int32_t ButtonHeight = 18;
Widgets::panel(RootPanelId, 0, "Sample-Dialog", Rect(90, 5, 220, 60), nullptr);
WidgetHandle panel = Widgets::panel(WidgetHandle::getRootHandle(), "Sample-Dialog", Rect(90, 5, 220, 60), nullptr);
if (!panel.isValid()) {
const auto &ctx = TinyUi::getContext();
ctx.mLogger(LogSeverity::Error, "Cannot create panel");
return ErrorCode;
}
auto &ctx = TinyUi::getContext();
auto *dynamicQuitCallback = new CallbackI(quit, (void*) &ctx);

Widgets::label(2, RootPanelId, "Hi, World!", Rect(100, 10, 200, ButtonHeight), Alignment::Center);
Widgets::textButton(3, RootPanelId, "Quit", Rect(100, 30, 200, ButtonHeight), Alignment::Center, dynamicQuitCallback);
Widgets::label(panel, "Hi, World!", Rect(100, 10, 200, ButtonHeight), Alignment::Center);
Widgets::textButton(panel, "Quit", Rect(100, 30, 200, ButtonHeight), Alignment::Center, dynamicQuitCallback);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
while (TinyUi::run()) {
TinyUi::render();
}
Expand Down
3 changes: 2 additions & 1 deletion src/tinyui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ bool TinyUi::run() {
const auto &ctx = getContext();
if (!ctx.mUpdateCallbackList.empty()) {
for (auto it = ctx.mUpdateCallbackList.begin(); it != ctx.mUpdateCallbackList.end(); ++it) {
(*it)->mfuncCallback[Events::UpdateEvent](1, (*it)->mInstance);
WidgetHandle handle{1};
(*it)->mfuncCallback[Events::UpdateEvent](handle, (*it)->mInstance);
Comment thread
kimkulling marked this conversation as resolved.
}
}
return Renderer::update(ctx);
Expand Down
23 changes: 22 additions & 1 deletion src/tinyui.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ using Id = uint64_t;
/// @brief The return code type used in the ui library.
using ret_code = int32_t;

/// @brief The widget handle struct.
struct WidgetHandle {
static constexpr Id RootItem = 0; ///< The root item id.
static constexpr Id InvalidId = 999999; ///< The invalid id of the widget handle.
Id mId{InvalidId}; ///< The unique id of the widget.

/// @brief Check if the widget handle is valid.
/// @return true if the widget handle is valid, false if not.
bool isValid() const {
return mId != InvalidId;
Comment thread
kimkulling marked this conversation as resolved.
}

/// @brief Will return the root item;
/// @return Thhe root item.
static WidgetHandle getRootHandle() {
static WidgetHandle root;
root.mId = RootItem;
return root;
}
};

/// @brief The operation was cancelled.
static constexpr ret_code OpCancelled = -5;
/// @brief The context is invalid.
Expand Down Expand Up @@ -350,7 +371,7 @@ struct EventPayload {
/// @brief This interface is used to store all neede message handlers.
struct CallbackI {
/// The function callback
typedef int (*funcCallback) (Id id, void *data);
typedef int (*funcCallback) (WidgetHandle id, void *data);
/// The function callback array, not handled callbacks are marked as a nullptr.
funcCallback mfuncCallback[Events::NumEvents];
/// The data instance.
Expand Down
Loading
Loading