Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Unified Diff: ui/views/cocoa/bridged_content_view.mm

Issue 1531213002: Mac: Implement firstRectForCharacterRange:actualRange in BridgedContentView. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed reduntant statement. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/cocoa/bridged_content_view.mm
diff --git a/ui/views/cocoa/bridged_content_view.mm b/ui/views/cocoa/bridged_content_view.mm
index 2167561368c38f1b8a637d99c1b1daa022faae15..2a4eee483a1fa2f7806261a3c4974e9a3ac91dc9 100644
--- a/ui/views/cocoa/bridged_content_view.mm
+++ b/ui/views/cocoa/bridged_content_view.mm
@@ -56,6 +56,72 @@ bool DispatchEventToMenu(views::Widget* widget, ui::KeyboardCode key_code) {
return false;
}
+gfx::Rect GetFirstRectForRangeHelper(
+ const ui::TextInputClient* textInputClient_,
tapted 2015/12/17 08:42:20 no trailing underscores on function arguments (als
karandeepb 2015/12/18 09:15:06 Done.
+ const gfx::Range& range,
tapted 2015/12/17 08:42:20 range -> requested_range? (for clarity)
karandeepb 2015/12/18 09:15:06 Done.
+ gfx::Range& actual_range) {
tapted 2015/12/17 08:42:20 chromium style doesn't allow non-const references,
karandeepb 2015/12/18 09:15:06 Done.
+ if (!textInputClient_)
+ return gfx::Rect(0, 0, 0, 0);
tapted 2015/12/17 08:42:20 return gfx::Rect();
karandeepb 2015/12/18 09:15:06 Done.
+
+ // Set up default return values, to be returned in case of unusual cases.
+ gfx::Rect caret_bounds = textInputClient_->GetCaretBounds();
+ caret_bounds.set_width(0);
+ actual_range.set_start(range.start());
tapted 2015/12/17 08:42:19 *actual_range = gfx::Range(requested_rangerange.st
karandeepb 2015/12/18 09:15:06 actual_range should correspond to the returned rec
+ actual_range.set_end(range.start());
+
+ if (!textInputClient_->HasCompositionText())
+ return caret_bounds;
+
+ if (range.is_reversed())
tapted 2015/12/17 08:42:19 I'm pretty sure it's impossible for requested_rang
karandeepb 2015/12/18 09:15:06 Done.
+ return caret_bounds;
+
+ gfx::Range composition_range;
+ if (!textInputClient_->GetCompositionTextRange(&composition_range))
+ return caret_bounds;
+
+ if (!composition_range.Contains(gfx::Range(range.start())) ||
tapted 2015/12/17 08:42:20 why not just `if (!composition_range.Contains(requ
karandeepb 2015/12/18 09:15:06 Done.
+ !composition_range.Contains(gfx::Range(range.end())))
+ return caret_bounds;
+
+ // If range is empty, return a zero width rectangle corresponding to the
+ // queried range.
+ if (range.is_empty()) {
+ size_t end_index = composition_range.end();
+ gfx::Rect current_rect;
+ if (range.start() == end_index) {
+ // Return a rectangle corresponding to the top right corner of the last
+ // compositioned character.
+ if (!textInputClient_->GetCompositionCharacterBounds(
+ end_index - 1 - composition_range.start(), &current_rect))
tapted 2015/12/17 08:42:19 would RenderWidgetHostViewMac::ConvertCharacterRan
karandeepb 2015/12/18 09:15:06 Done.
+ return caret_bounds;
+ current_rect.set_origin(current_rect.top_right());
+ } else if (!textInputClient_->GetCompositionCharacterBounds(
+ range.start() - composition_range.start(), &current_rect))
tapted 2015/12/17 08:42:20 this `else if` needs curlies, since it's attached
karandeepb 2015/12/18 09:15:06 Done.
+ return caret_bounds;
+ current_rect.set_width(0);
+ return current_rect;
+ }
+
+ // In browser multi-line textfields are not supported by Chrome currently.
tapted 2015/12/17 08:42:19 Perhaps something like // Toolkit-views textfie
karandeepb 2015/12/18 09:15:06 Done.
+ // Hence we don't check for a line break.
+
+ // Return the union rectangle of the character bounds within the query range.
+ gfx::Rect union_rect, current_rect;
+ for (size_t i = range.start(); i < range.end(); i++) {
+ if (textInputClient_->GetCompositionCharacterBounds(
+ i - composition_range.start(), &current_rect))
+ union_rect.Union(current_rect);
+ else if (i == range.start())
+ return caret_bounds;
tapted 2015/12/17 08:42:19 can this be dealt with outside the loop? (maybe st
karandeepb 2015/12/18 09:15:06 Done.
+ else {
tapted 2015/12/17 08:42:19 if any bit of an if/else chain has a curly, it all
karandeepb 2015/12/18 09:15:06 Done.
+ actual_range.set_end(i);
+ return union_rect;
+ }
+ }
+ actual_range.set_end(range.end());
+ return union_rect;
+}
+
} // namespace
@interface BridgedContentView ()
@@ -592,8 +658,20 @@ bool DispatchEventToMenu(views::Widget* widget, ui::KeyboardCode key_code) {
- (NSRect)firstRectForCharacterRange:(NSRange)range
actualRange:(NSRangePointer)actualRange {
- NOTIMPLEMENTED();
- return NSZeroRect;
+ gfx::Range requested_range(range);
tapted 2015/12/17 08:42:20 since this is between @implementation and @end (i.
karandeepb 2015/12/18 09:15:06 Done.
+ gfx::Range actual_range;
+ gfx::Rect rect = GetFirstRectForRangeHelper(textInputClient_, requested_range,
+ actual_range);
+ NSRect ns_rect = NSRectFromCGRect(rect.ToCGRect());
+ if (NSEqualRects(ns_rect, NSZeroRect)) {
tapted 2015/12/17 08:42:19 nit: move this up a line and do if (rect.IsEmpty(
karandeepb 2015/12/18 09:15:06 Removed the early exit. Also, its unlikely for tex
+ return NSZeroRect;
+ }
+ *actualRange = actual_range.ToNSRange();
+ // ns_rect is in screen coordinates with origin at top left. Convert to Apple
tapted 2015/12/17 08:42:20 return gfx::ScreenRectToNSRect(screen_rect);
karandeepb 2015/12/18 09:15:06 Done.
+ // coordinate system with origin at bottom left
+ NSRect screen_rect = [[NSScreen mainScreen] frame];
+ ns_rect.origin.y = screen_rect.size.height - NSMaxY(ns_rect);
+ return ns_rect;
}
- (BOOL)hasMarkedText {
« no previous file with comments | « no previous file | ui/views/cocoa/bridged_native_widget_unittest.mm » ('j') | ui/views/cocoa/bridged_native_widget_unittest.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698