aboutsummaryrefslogtreecommitdiff
path: root/src/ui
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-12-21 15:36:00 +0800
committercrupest <crupest@outlook.com>2021-12-21 15:36:00 +0800
commit63b39863b9d567068480f9c1d59177c92a8c6169 (patch)
tree06485cd94ca299bcc0d09fdc23fc9a73cad38eab /src/ui
parent061f9ad04131febcc66975fd3d95b332789da52f (diff)
downloadcru-63b39863b9d567068480f9c1d59177c92a8c6169.tar.gz
cru-63b39863b9d567068480f9c1d59177c92a8c6169.tar.bz2
cru-63b39863b9d567068480f9c1d59177c92a8c6169.zip
...
Diffstat (limited to 'src/ui')
-rw-r--r--src/ui/controls/TextHostControlService.cpp44
-rw-r--r--src/ui/events/MouseEventArgs.cpp14
2 files changed, 58 insertions, 0 deletions
diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp
index 26484ae6..61e6aef6 100644
--- a/src/ui/controls/TextHostControlService.cpp
+++ b/src/ui/controls/TextHostControlService.cpp
@@ -461,8 +461,50 @@ void TextHostControlService::MouseDownHandler(
args.GetPointToContent(text_render_object));
const auto position = result.position + (result.trailing ? 1 : 0);
SetSelection(position);
+ args.SetHandled(true);
} else if (args.GetButton() == mouse_buttons::right) {
// TODO: Finish context menu logic here.
+
+ const Point p = args.GetPointToContent(GetTextRenderObject());
+ if (GetSelection().count != 0) {
+ auto selected_area =
+ GetTextRenderObject()->TextRangeRect(GetSelection());
+
+ bool inside = false;
+
+ for (const auto& rect : selected_area) {
+ if (rect.IsPointInside(p)) {
+ inside = true;
+ break;
+ }
+ }
+
+ if (inside) {
+ ContextMenuItem items = ContextMenuItem(kSelectAll | kCopy);
+ if (IsEditable()) {
+ items = ContextMenuItem(items | kCut | kPaste);
+ }
+
+ this->OpenContextMenu(args.GetPointOfScreen(), items);
+ args.SetHandled(true);
+ return;
+ }
+ }
+
+ const auto text_render_object = this->GetTextRenderObject();
+ const auto result = text_render_object->TextHitTest(
+ args.GetPointToContent(text_render_object));
+ const auto position = result.position + (result.trailing ? 1 : 0);
+ SetSelection(position);
+
+ ContextMenuItem items = ContextMenuItem::kSelectAll;
+ if (IsEditable()) {
+ items = ContextMenuItem(items | ContextMenuItem::kPaste);
+ }
+
+ OpenContextMenu(args.GetPointOfScreen(), items);
+
+ args.SetHandled(true);
}
}
}
@@ -472,6 +514,7 @@ void TextHostControlService::MouseUpHandler(
if (args.GetButton() == mouse_buttons::left && mouse_move_selecting_) {
this->control_->ReleaseMouse();
this->mouse_move_selecting_ = false;
+ args.SetHandled();
}
}
@@ -482,6 +525,7 @@ void TextHostControlService::MouseMoveHandler(events::MouseEventArgs& args) {
args.GetPointToContent(text_render_object));
const auto position = result.position + (result.trailing ? 1 : 0);
ChangeSelectionEnd(position);
+ args.SetHandled();
}
}
diff --git a/src/ui/events/MouseEventArgs.cpp b/src/ui/events/MouseEventArgs.cpp
index b27bb9d4..39424c65 100644
--- a/src/ui/events/MouseEventArgs.cpp
+++ b/src/ui/events/MouseEventArgs.cpp
@@ -1,5 +1,7 @@
#include "cru/ui/events/MouseEventArgs.hpp"
+#include "cru/ui/controls/Control.hpp"
+#include "cru/ui/host/WindowHost.hpp"
#include "cru/ui/render/RenderObject.hpp"
namespace cru::ui::events {
@@ -11,4 +13,16 @@ Point MouseEventArgs::GetPointToContent(
render::RenderObject* render_object) const {
return render_object->FromRootToContent(GetPoint());
}
+
+Point MouseEventArgs::GetPointOfScreen() const {
+ auto sender = GetSender();
+ if (auto control = dynamic_cast<controls::Control*>(sender)) {
+ if (auto host = control->GetWindowHost())
+ return GetPoint() + control->GetWindowHost()
+ ->GetNativeWindow()
+ ->GetClientRect()
+ .GetLeftTop();
+ }
+ return GetPoint();
+}
} // namespace cru::ui::events