1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ASH_WM_DESKS_DESK_NAME_VIEW_H_
6 #define ASH_WM_DESKS_DESK_NAME_VIEW_H_
7 
8 #include "ash/ash_export.h"
9 #include "ash/wm/overview/overview_highlight_controller.h"
10 #include "ash/wm/wm_highlight_item_border.h"
11 #include "ui/views/controls/textfield/textfield.h"
12 
13 namespace ash {
14 
15 // Defines a special textfield that allows modifying the name of its
16 // corresponding desk. When it's not focused, it looks like a normal label. It
17 // can be highlighted and activated by the OverviewHighlightController, and it
18 // provides an API to elide long desk names.
19 class ASH_EXPORT DeskNameView
20     : public views::Textfield,
21       public OverviewHighlightController::OverviewHighlightableView {
22  public:
23   DeskNameView();
24   DeskNameView(const DeskNameView&) = delete;
25   DeskNameView& operator=(const DeskNameView&) = delete;
26   ~DeskNameView() override;
27 
28   // The max number of characters (UTF-16) allowed for desks' names.
29   static constexpr size_t kMaxLength = 300;
30 
31   // Commits an on-going desk name change (if any) by bluring the focus away
32   // from any view on |widget|, where |widget| should be the desks bar widget.
33   static void CommitChanges(views::Widget* widget);
34 
35   void SetTextAndElideIfNeeded(const base::string16& text);
36 
37   // If this view has focus, make the view's border visible and change
38   // background to its active color. If it doesn't have focus, hide the view's
39   // border and change background to its default color.
40   void UpdateViewAppearance();
41 
42   // views::View:
43   const char* GetClassName() const override;
44   gfx::Size CalculatePreferredSize() const override;
45   bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& event) override;
46   void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
47   void OnMouseEntered(const ui::MouseEvent& event) override;
48   void OnMouseExited(const ui::MouseEvent& event) override;
49   void OnThemeChanged() override;
50   gfx::NativeCursor GetCursor(const ui::MouseEvent& event) override;
51 
52   // OverviewHighlightController::OverviewHighlightableView:
53   views::View* GetView() override;
54   void MaybeActivateHighlightedView() override;
55   void MaybeCloseHighlightedView() override;
56   void OnViewHighlighted() override;
57   void OnViewUnhighlighted() override;
58 
59  private:
60   void UpdateBorderState();
61 
62   // Returns the background color for this view based on whether it has focus
63   // and if the mouse is entering/exiting the view.
64   SkColor GetBackgroundColor() const;
65 
66   // Owned by this View via `View::border_`. This is just a convenient pointer
67   // to it.
68   WmHighlightItemBorder* border_ptr_;
69 
70   // Full text without being elided.
71   base::string16 full_text_;
72 };
73 
74 }  // namespace ash
75 
76 #endif  // ASH_WM_DESKS_DESK_NAME_VIEW_H_
77