Skip to content

Commit

Permalink
feat(message-manager): return id when show message
Browse files Browse the repository at this point in the history
  • Loading branch information
cEvolve05 committed Sep 1, 2024
1 parent 3b89319 commit 0d4a602
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/Controller/Core/MessageManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ MessageManager::MessageManager(slint::ComponentHandle<UiEntryName> uiEntry, UiBr
toastList = std::make_shared<slint::VectorModel<ToastData>>();

self->on_show_message([this](slint::SharedString content, MessageType type) {
showMessage(std::string(content), type);
return showMessage(std::string(content), type);
});

self->on_get_message([this](int id) -> MessageData { return getMessage(id); });
Expand All @@ -26,9 +26,9 @@ MessageManager::MessageManager(slint::ComponentHandle<UiEntryName> uiEntry, UiBr
self->set_toast_list(toastList);
}

void MessageManager::showMessage(std::string content,
MessageType type,
std::chrono::steady_clock::duration timeout) {
int MessageManager::showMessage(std::string content,
MessageType type,
std::chrono::steady_clock::duration timeout) {
auto& self = *this;
auto id = nextId++;

Expand All @@ -42,6 +42,8 @@ void MessageManager::showMessage(std::string content,
[this, id] { hideMessage(id); },
timeout + animationLength,
AsyncExecutor::Once | AsyncExecutor::Delay);

return id;
}

void MessageManager::hideMessage(int id) {
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/Core/MessageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class MessageManager : private GlobalAgent<MessageManagerBridge> {
MessageManager(slint::ComponentHandle<UiEntryName> uiEntry, UiBridge& bridge);
MessageManager(MessageManager&) = delete;

void showMessage(std::string content,
MessageType type = MessageType::Info,
std::chrono::steady_clock::duration timeout = std::chrono::milliseconds(3000));
// @return message id
int showMessage(std::string content,
MessageType type = MessageType::Info,
std::chrono::steady_clock::duration timeout = std::chrono::milliseconds(3000));

// unnecessary to invoke if set correct timeout
void hideMessage(int id);
Expand Down
6 changes: 3 additions & 3 deletions ui/logic/message_manager.slint
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ export struct ToastData {
export global MessageManagerBridge {
in-out property <[ToastData]> toast-list;
in-out property <bool> refresh;
callback show-message(string, MessageType);
callback show-message(string, MessageType) -> int;
pure callback get-message(int) -> MessageData;
callback hide-message(int);
}

export global MessageManager {
out property <[ToastData]> toast-list: MessageManagerBridge.toast-list;
public function show-message(content: string, type: MessageType) {
MessageManagerBridge.show-message(content, type);
public function show-message(content: string, type: MessageType) -> int {
return MessageManagerBridge.show-message(content, type);
}
pure public function get-message(id: int) -> MessageData {
return MessageManagerBridge.get-message(id);
Expand Down

0 comments on commit 0d4a602

Please sign in to comment.