aboutsummaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-12-04 18:06:43 +0800
committercrupest <crupest@outlook.com>2021-12-04 18:06:43 +0800
commit942f1d34c48e61a853db745cd68e46db13266a5c (patch)
treefdb859933dbb302404391ed686bae8c7126c96ee /src/ui
parent2317061516b0bd0002467acbead3120cbe49a6c5 (diff)
downloadcru-942f1d34c48e61a853db745cd68e46db13266a5c.tar.gz
cru-942f1d34c48e61a853db745cd68e46db13266a5c.tar.bz2
cru-942f1d34c48e61a853db745cd68e46db13266a5c.zip
...
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/components/Menu.cpp21
-rw-r--r--src/ui/controls/LayoutControl.cpp6
-rw-r--r--src/ui/controls/TextHostControlService.cpp36
3 files changed, 62 insertions, 1 deletions
diff --git a/src/ui/components/Menu.cpp b/src/ui/components/Menu.cpp
index c3359cf9..60364f13 100644
--- a/src/ui/components/Menu.cpp
+++ b/src/ui/components/Menu.cpp
@@ -1,10 +1,12 @@
#include "cru/ui/components/Menu.hpp"
+#include <functional>
#include "cru/platform/gui/Window.hpp"
#include "cru/ui/UiManager.hpp"
#include "cru/ui/controls/Button.hpp"
#include "cru/ui/controls/Control.hpp"
#include "cru/ui/controls/FlexLayout.hpp"
#include "cru/ui/controls/TextBlock.hpp"
+#include "cru/ui/helper/ClickDetector.hpp"
#include "cru/ui/host/WindowHost.hpp"
#include "cru/ui/style/StyleRuleSet.hpp"
@@ -15,6 +17,9 @@ MenuItem::MenuItem() {
container_->SetChild(text_);
container_->GetStyleRuleSet()->SetParent(
&UiManager::GetInstance()->GetThemeResources()->menu_item_style);
+ container_->ClickEvent()->AddHandler([this](const helper::ClickEventArgs&) {
+ if (this->on_click_) this->on_click_();
+ });
}
MenuItem::MenuItem(String text) : MenuItem() { SetText(std::move(text)); }
@@ -61,8 +66,20 @@ Component* Menu::RemoveItem(gsl::index index) {
return item;
}
-void Menu::AddTextItem(String text, gsl::index index) {
+void Menu::ClearItems() {
+ for (auto item : items_) {
+ delete item;
+ }
+
+ items_.clear();
+
+ container_->ClearChildren();
+}
+
+void Menu::AddTextItem(String text, gsl::index index,
+ std::function<void()> on_click) {
MenuItem* item = new MenuItem(std::move(text));
+ item->SetOnClick(std::move(on_click));
AddItem(item, index);
}
@@ -94,4 +111,6 @@ void PopupMenu::Show() {
popup_->GetWindowHost()->GetNativeWindow()->SetVisibility(
platform::gui::WindowVisibilityType::Show);
}
+
+void PopupMenu::Close() { popup_->GetWindowHost()->GetNativeWindow()->Close(); }
} // namespace cru::ui::components
diff --git a/src/ui/controls/LayoutControl.cpp b/src/ui/controls/LayoutControl.cpp
index 5954853e..e5a38445 100644
--- a/src/ui/controls/LayoutControl.cpp
+++ b/src/ui/controls/LayoutControl.cpp
@@ -3,6 +3,12 @@
#include "cru/ui/render/RenderObject.hpp"
namespace cru::ui::controls {
+void LayoutControl::ClearChildren() {
+ while (GetChildren().size() > 0) {
+ RemoveChild(0);
+ }
+}
+
void LayoutControl::OnAddChild(Control* child, Index position) {
if (container_render_object_ != nullptr) {
container_render_object_->AddChild(child->GetRenderObject(), position);
diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp
index b250ccae..90896632 100644
--- a/src/ui/controls/TextHostControlService.cpp
+++ b/src/ui/controls/TextHostControlService.cpp
@@ -14,12 +14,15 @@
#include "cru/platform/gui/Window.hpp"
#include "cru/ui/Base.hpp"
#include "cru/ui/DebugFlags.hpp"
+#include "cru/ui/components/Menu.hpp"
#include "cru/ui/events/UiEvent.hpp"
#include "cru/ui/helper/ShortcutHub.hpp"
#include "cru/ui/host/WindowHost.hpp"
#include "cru/ui/render/ScrollRenderObject.hpp"
#include "cru/ui/render/TextRenderObject.hpp"
+#include <memory>
+
namespace cru::ui::controls {
TextControlMovePattern TextControlMovePattern::kLeft(
helper::ShortcutKeyBind(platform::gui::KeyCode::Left),
@@ -119,6 +122,8 @@ std::vector<TextControlMovePattern> TextControlMovePattern::kDefaultPatterns = {
TextHostControlService::TextHostControlService(gsl::not_null<Control*> control)
: control_(control),
text_host_control_(dynamic_cast<ITextHostControl*>(control.get())) {
+ context_menu_ = std::make_unique<components::PopupMenu>();
+
SetUpShortcuts();
SetupOneHandler(&Control::MouseMoveEvent,
@@ -152,6 +157,16 @@ void TextHostControlService::SetEnabled(bool enable) {
}
}
+void TextHostControlService::SetContextMenuEnabled(bool enabled) {
+ if (context_menu_enabled_ == enabled) return;
+
+ context_menu_enabled_ = enabled;
+
+ if (!context_menu_enabled_ && context_menu_) {
+ context_menu_->Close();
+ }
+}
+
void TextHostControlService::SetEditable(bool editable) {
this->editable_ = editable;
if (!editable) CancelComposition();
@@ -594,4 +609,25 @@ void TextHostControlService::SetUpShortcuts() {
});
}
}
+
+void TextHostControlService::OpenContextMenu(const Point& position,
+ ContextMenuItem items) {
+ context_menu_ = std::make_unique<components::PopupMenu>();
+ auto menu = context_menu_->GetMenu();
+ if (items & ContextMenuItem::kSelectAll) {
+ menu->AddTextItem(u"Select All", [this] { this->SelectAll(); });
+ }
+ if (items & ContextMenuItem::kCopy) {
+ menu->AddTextItem(u"Copy", [this] { this->Copy(); });
+ }
+ if (items & ContextMenuItem::kCut) {
+ menu->AddTextItem(u"Cut", [this] { this->Cut(); });
+ }
+ if (items & ContextMenuItem::kPaste) {
+ menu->AddTextItem(u"Paste", [this] { this->Paste(); });
+ }
+ context_menu_->SetPosition(position);
+ context_menu_->Show();
+}
+
} // namespace cru::ui::controls