summaryrefslogtreecommitdiffstats
path: root/ui/aura/desktop.cc
blob: 50b38029dc5fec1cbe958f1d79de9f53c8bf9df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2011 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/aura/desktop.h"

#include "base/logging.h"
#include "base/message_loop.h"
#include "ui/aura/desktop_host.h"
#include "ui/aura/root_window.h"
#include "ui/aura/toplevel_window_container.h"
#include "ui/aura/window.h"
#include "ui/gfx/compositor/compositor.h"
#include "ui/gfx/compositor/layer.h"

namespace aura {

// static
Desktop* Desktop::instance_ = NULL;

Desktop::Desktop()
    : toplevel_window_container_(new aura::internal::ToplevelWindowContainer),
      host_(aura::DesktopHost::Create(gfx::Rect(200, 200, 1280, 1024))),
      ALLOW_THIS_IN_INITIALIZER_LIST(schedule_paint_(this)) {
  DCHECK(MessageLoopForUI::current())
      << "The UI message loop must be initialized first.";
  compositor_ = ui::Compositor::Create(this, host_->GetAcceleratedWidget(),
                                       host_->GetSize());
  host_->SetDesktop(this);
  DCHECK(compositor_.get());
  window_.reset(new internal::RootWindow);
}

Desktop::~Desktop() {
  if (instance_ == this)
    instance_ = NULL;
}

void Desktop::Init() {
  window_->Init();
  compositor()->set_root_layer(window_->layer());
  toplevel_window_container_->Init();
  toplevel_window_container_->SetBounds(gfx::Rect(0, 0, 1280, 1024), 0);
  toplevel_window_container_->SetVisibility(aura::Window::VISIBILITY_SHOWN);
  toplevel_window_container_->SetParent(window_.get());
#if defined(USE_X11)
  // TODO(oshima): Implement configure notify and remove this.
  OnHostResized(host_->GetSize());
#endif
}

void Desktop::Show() {
  host_->Show();
}

void Desktop::SetSize(const gfx::Size& size) {
  host_->SetSize(size);
}

void Desktop::Run() {
  Show();
  MessageLoopForUI::current()->Run(host_.get());
}

void Desktop::Draw() {
  compositor_->Draw(false);
}

bool Desktop::OnMouseEvent(const MouseEvent& event) {
  return window_->HandleMouseEvent(event);
}

bool Desktop::OnKeyEvent(const KeyEvent& event) {
  return window_->HandleKeyEvent(event);
}

void Desktop::OnHostResized(const gfx::Size& size) {
  gfx::Rect bounds(window_->bounds().origin(), size);
  window_->SetBounds(bounds, 0);
  compositor_->WidgetSizeChanged(size);
}

void Desktop::ScheduleCompositorPaint() {
  if (schedule_paint_.empty()) {
    MessageLoop::current()->PostTask(FROM_HERE,
        schedule_paint_.NewRunnableMethod(&Desktop::Draw));
  }
}

// static
Desktop* Desktop::GetInstance() {
  if (!instance_) {
    instance_ = new Desktop;
    instance_->Init();
  }
  return instance_;
}

}  // namespace aura