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

Unified Diff: ui/views/selection_controller_delegate.cc

Issue 2345183002: Views: Draw Textfield selected text in gray when top-level Widget loses focus.
Patch Set: Refactor to use SelectionController(Delegate). Unfinished! Created 4 years, 1 month 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/selection_controller_delegate.cc
diff --git a/ui/views/selection_controller_delegate.cc b/ui/views/selection_controller_delegate.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7f1aed4a7dbd0415b742ca12fb91c28cb33f2436
--- /dev/null
+++ b/ui/views/selection_controller_delegate.cc
@@ -0,0 +1,67 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/views/selection_controller_delegate.h"
+
+#include "base/logging.h"
+#include "ui/gfx/render_text.h"
+
+namespace views {
+
+TextSelectionDrawState
+SelectionControllerDelegate::UpdateTextSelectionDrawState(
+ const View* this_view,
+ const View* focused_before,
+ const View* focused_now) {
+ // Selection should not be drawn if:
+ // - Nothing is focused now and |this_view| was not the last focused View.
+ // - The currently focused View is a different View (not |this_view|).
+ if ((focused_now == nullptr && focused_before != this_view) ||
+ (focused_now != this_view && focused_now != nullptr)) {
+ GetRenderTextForSelectionController()->set_draw_text_selection(false);
+ return TextSelectionDrawState::kDrawAsUnfocused;
+ }
+
+ SkColor selection_bg_color;
+ TextSelectionDrawState text_selection_draw_state;
+ if (focused_now) {
+ selection_bg_color = GetSelectionBackgroundColor();
+ text_selection_draw_state = TextSelectionDrawState::kDrawAsFocused;
+ } else {
+ selection_bg_color = GetSelectionBackgroundUnfocusedColor();
+ text_selection_draw_state = TextSelectionDrawState::kDrawAsLastFocused;
+ }
+ GetRenderTextForSelectionController()->set_selection_color(
+ GetSelectionTextColor());
+ GetRenderTextForSelectionController()->set_selection_background_color(
+ selection_bg_color);
+ GetRenderTextForSelectionController()->set_draw_text_selection(true);
+ return text_selection_draw_state;
+}
+
+void SelectionControllerDelegate::ObserveWidgetFocusChanges(
+ FocusManager* new_focus_manager,
+ bool add_as_listener) {
+ if (focus_manager_) {
+ focus_manager_->RemoveFocusChangeListener(this);
+ focus_manager_ = nullptr;
+ }
+
+ if (add_as_listener) {
+ focus_manager_ = new_focus_manager;
+ if (!focus_manager_) {
+ return;
+ }
+ focus_manager_->AddFocusChangeListener(this);
+ }
+}
+
+void SelectionControllerDelegate::OnDidChangeFocus(View* focused_before,
+ View* focused_now) {}
+
+SelectionControllerDelegate::~SelectionControllerDelegate() {
+ DCHECK(!focus_manager_);
+}
+
+} // namespace views

Powered by Google App Engine
This is Rietveld 408576698