OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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 #include "mojo/examples/wm_flow/wm/frame_controller.h" | |
6 | |
7 #include "mojo/services/public/cpp/view_manager/view.h" | |
8 #include "mojo/views/native_widget_view_manager.h" | |
9 #include "ui/views/background.h" | |
10 #include "ui/views/controls/button/label_button.h" | |
11 #include "ui/views/layout/layout_manager.h" | |
12 #include "ui/views/widget/widget.h" | |
13 | |
14 class FrameController::LayoutManager : public views::LayoutManager, | |
15 public views::ButtonListener { | |
16 public: | |
17 explicit LayoutManager(FrameController* controller) | |
18 : controller_(controller), | |
19 close_button_(new views::LabelButton(this, L"Begone")), | |
20 maximize_button_(new views::LabelButton(this, L"Embiggen")) {} | |
21 virtual ~LayoutManager() {} | |
22 | |
23 private: | |
24 const int kButtonFrameMargin = 5; | |
sky
2014/09/11 19:32:22
nit: make these static, otherwise they should be n
| |
25 const int kButtonFrameSpacing = 2; | |
26 const int kFrameSize = 10; | |
27 | |
28 // Overridden from views::LayoutManager: | |
29 virtual void Installed(views::View* host) { | |
30 host->AddChildView(close_button_); | |
31 host->AddChildView(maximize_button_); | |
32 } | |
33 virtual void Layout(views::View* host) OVERRIDE { | |
34 gfx::Size ps = close_button_->GetPreferredSize(); | |
35 gfx::Rect bounds = host->GetLocalBounds(); | |
36 close_button_->SetBounds(bounds.right() - kButtonFrameMargin - ps.width(), | |
37 kButtonFrameMargin, ps.width(), ps.height()); | |
38 | |
39 ps = maximize_button_->GetPreferredSize(); | |
40 maximize_button_->SetBounds( | |
41 close_button_->x() - kButtonFrameSpacing - ps.width(), | |
42 kButtonFrameMargin, ps.width(), ps.height()); | |
43 | |
44 bounds.Inset(kFrameSize, | |
45 close_button_->bounds().bottom() + kButtonFrameMargin, | |
46 kFrameSize, kFrameSize); | |
47 controller_->app_view_->SetBounds(bounds); | |
48 } | |
49 virtual gfx::Size GetPreferredSize(const views::View* host) const OVERRIDE { | |
50 return gfx::Size(); | |
51 } | |
52 | |
53 // Overridden from views::ButtonListener: | |
54 virtual void ButtonPressed(views::Button* sender, | |
55 const ui::Event& event) OVERRIDE { | |
56 if (sender == close_button_) { | |
sky
2014/09/11 19:32:22
nit: no {}
| |
57 controller_->CloseWindow(); | |
58 } else if (sender == maximize_button_) { | |
59 controller_->ToggleMaximize(); | |
60 } | |
61 } | |
62 | |
63 FrameController* controller_; | |
64 views::Button* close_button_; | |
65 views::Button* maximize_button_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(LayoutManager); | |
68 }; | |
69 | |
70 //////////////////////////////////////////////////////////////////////////////// | |
71 // FrameController, public: | |
72 | |
73 FrameController::FrameController(mojo::View* view, mojo::View** app_view) | |
74 : view_(view), | |
75 app_view_(mojo::View::Create(view->view_manager())), | |
76 frame_view_(new views::View), | |
77 frame_view_layout_manager_(new LayoutManager(this)), | |
78 widget_(new views::Widget), | |
79 maximized_(false) { | |
80 view_->AddChild(app_view_); | |
81 *app_view = app_view_; | |
82 frame_view_->set_background( | |
83 views::Background::CreateSolidBackground(SK_ColorBLUE)); | |
84 frame_view_->SetLayoutManager(frame_view_layout_manager_); | |
85 views::Widget::InitParams params( | |
86 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
87 params.native_widget = new mojo::NativeWidgetViewManager(widget_, view_); | |
88 params.bounds = gfx::Rect(view_->bounds().size()); | |
89 widget_->Init(params); | |
90 widget_->SetContentsView(frame_view_); | |
91 widget_->Show(); | |
92 } | |
93 | |
94 FrameController::~FrameController() {} | |
95 | |
96 void FrameController::CloseWindow() { | |
97 mojo::View* first_child = view_->children().front(); | |
sky
2014/09/11 19:32:22
It's worth documenting why you need to delete only
| |
98 first_child->Destroy(); | |
99 view_->Destroy(); | |
100 } | |
101 | |
102 void FrameController::ToggleMaximize() { | |
103 if (!maximized_) | |
104 restored_bounds_ = view_->bounds(); | |
105 maximized_ = !maximized_; | |
106 if (maximized_) { | |
107 view_->SetBounds(view_->parent()->bounds()); | |
108 } else { | |
109 view_->SetBounds(restored_bounds_); | |
110 } | |
111 } | |
OLD | NEW |