1 // Copyright (c) 2012 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.
5 #include "ui/views/examples/bubble_example.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/views/bubble/bubble_delegate.h"
9 #include "ui/views/controls/button/label_button.h"
10 #include "ui/views/controls/label.h"
11 #include "ui/views/layout/box_layout.h"
12 #include "ui/views/widget/widget.h"
14 using base::ASCIIToUTF16
;
21 SkColor colors
[] = { SK_ColorWHITE
, SK_ColorGRAY
, SK_ColorCYAN
, 0xFFC1B1E1 };
23 BubbleBorder::Arrow arrows
[] = {
24 BubbleBorder::TOP_LEFT
, BubbleBorder::TOP_CENTER
,
25 BubbleBorder::TOP_RIGHT
, BubbleBorder::RIGHT_TOP
,
26 BubbleBorder::RIGHT_CENTER
, BubbleBorder::RIGHT_BOTTOM
,
27 BubbleBorder::BOTTOM_RIGHT
, BubbleBorder::BOTTOM_CENTER
,
28 BubbleBorder::BOTTOM_LEFT
, BubbleBorder::LEFT_BOTTOM
,
29 BubbleBorder::LEFT_CENTER
, BubbleBorder::LEFT_TOP
};
31 base::string16
GetArrowName(BubbleBorder::Arrow arrow
) {
33 case BubbleBorder::TOP_LEFT
: return ASCIIToUTF16("TOP_LEFT");
34 case BubbleBorder::TOP_RIGHT
: return ASCIIToUTF16("TOP_RIGHT");
35 case BubbleBorder::BOTTOM_LEFT
: return ASCIIToUTF16("BOTTOM_LEFT");
36 case BubbleBorder::BOTTOM_RIGHT
: return ASCIIToUTF16("BOTTOM_RIGHT");
37 case BubbleBorder::LEFT_TOP
: return ASCIIToUTF16("LEFT_TOP");
38 case BubbleBorder::RIGHT_TOP
: return ASCIIToUTF16("RIGHT_TOP");
39 case BubbleBorder::LEFT_BOTTOM
: return ASCIIToUTF16("LEFT_BOTTOM");
40 case BubbleBorder::RIGHT_BOTTOM
: return ASCIIToUTF16("RIGHT_BOTTOM");
41 case BubbleBorder::TOP_CENTER
: return ASCIIToUTF16("TOP_CENTER");
42 case BubbleBorder::BOTTOM_CENTER
: return ASCIIToUTF16("BOTTOM_CENTER");
43 case BubbleBorder::LEFT_CENTER
: return ASCIIToUTF16("LEFT_CENTER");
44 case BubbleBorder::RIGHT_CENTER
: return ASCIIToUTF16("RIGHT_CENTER");
45 case BubbleBorder::NONE
: return ASCIIToUTF16("NONE");
46 case BubbleBorder::FLOAT
: return ASCIIToUTF16("FLOAT");
48 return ASCIIToUTF16("INVALID");
51 class ExampleBubble
: public BubbleDelegateView
{
53 ExampleBubble(View
* anchor
, BubbleBorder::Arrow arrow
)
54 : BubbleDelegateView(anchor
, arrow
) {}
57 void Init() override
{
58 SetLayoutManager(new BoxLayout(BoxLayout::kVertical
, 50, 50, 0));
59 AddChildView(new Label(GetArrowName(arrow())));
63 DISALLOW_COPY_AND_ASSIGN(ExampleBubble
);
68 BubbleExample::BubbleExample() : ExampleBase("Bubble") {}
70 BubbleExample::~BubbleExample() {}
72 void BubbleExample::CreateExampleView(View
* container
) {
73 PrintStatus("Click with optional modifiers: [Ctrl] for set_arrow(NONE), "
74 "[Alt] for set_arrow(FLOAT), or [Shift] to reverse the arrow iteration.");
75 container
->SetLayoutManager(new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 10));
76 no_shadow_
= new LabelButton(this, ASCIIToUTF16("No Shadow"));
77 container
->AddChildView(no_shadow_
);
78 no_shadow_opaque_
= new LabelButton(this, ASCIIToUTF16("Opaque Border"));
79 container
->AddChildView(no_shadow_opaque_
);
80 big_shadow_
= new LabelButton(this, ASCIIToUTF16("Big Shadow"));
81 container
->AddChildView(big_shadow_
);
82 small_shadow_
= new LabelButton(this, ASCIIToUTF16("Small Shadow"));
83 container
->AddChildView(small_shadow_
);
84 no_assets_
= new LabelButton(this, ASCIIToUTF16("No Assets"));
85 container
->AddChildView(no_assets_
);
86 align_to_edge_
= new LabelButton(this, ASCIIToUTF16("Align To Edge"));
87 container
->AddChildView(align_to_edge_
);
88 persistent_
= new LabelButton(this, ASCIIToUTF16("Persistent"));
89 container
->AddChildView(persistent_
);
92 void BubbleExample::ButtonPressed(Button
* sender
, const ui::Event
& event
) {
93 static int arrow_index
= 0, color_index
= 0;
94 static const int count
= arraysize(arrows
);
95 arrow_index
= (arrow_index
+ count
+ (event
.IsShiftDown() ? -1 : 1)) % count
;
96 BubbleBorder::Arrow arrow
= arrows
[arrow_index
];
97 if (event
.IsControlDown())
98 arrow
= BubbleBorder::NONE
;
99 else if (event
.IsAltDown())
100 arrow
= BubbleBorder::FLOAT
;
102 ExampleBubble
* bubble
= new ExampleBubble(sender
, arrow
);
103 bubble
->set_color(colors
[(color_index
++) % arraysize(colors
)]);
105 if (sender
== no_shadow_
)
106 bubble
->set_shadow(BubbleBorder::NO_SHADOW
);
107 else if (sender
== no_shadow_opaque_
)
108 bubble
->set_shadow(BubbleBorder::NO_SHADOW_OPAQUE_BORDER
);
109 else if (sender
== big_shadow_
)
110 bubble
->set_shadow(BubbleBorder::BIG_SHADOW
);
111 else if (sender
== small_shadow_
)
112 bubble
->set_shadow(BubbleBorder::SMALL_SHADOW
);
113 else if (sender
== no_assets_
)
114 bubble
->set_shadow(BubbleBorder::NO_ASSETS
);
116 if (sender
== persistent_
)
117 bubble
->set_close_on_deactivate(false);
119 BubbleDelegateView::CreateBubble(bubble
);
120 if (sender
== align_to_edge_
)
121 bubble
->SetAlignment(BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE
);
123 bubble
->GetWidget()->Show();
126 } // namespace examples