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

Unified Diff: examples/ui/shapes/shapes_view.cc

Issue 1559723002: Update the UI examples. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-14
Patch Set: address feedback Created 4 years, 11 months 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
« no previous file with comments | « examples/ui/shapes/shapes_view.h ('k') | examples/ui/spinning_cube/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: examples/ui/shapes/shapes_view.cc
diff --git a/examples/ui/shapes/shapes_view.cc b/examples/ui/shapes/shapes_view.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8bea9c332dfd6179e8069ea67516efe07191b197
--- /dev/null
+++ b/examples/ui/shapes/shapes_view.cc
@@ -0,0 +1,86 @@
+// Copyright 2015 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 "examples/ui/shapes/shapes_view.h"
+
+#include "base/bind.h"
+#include "base/logging.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkColor.h"
+#include "third_party/skia/include/core/SkSurface.h"
+
+namespace examples {
+
+namespace {
+constexpr uint32_t kContentImageResourceId = 1;
+constexpr uint32_t kRootNodeId = mojo::gfx::composition::kSceneRootNodeId;
+} // namespace
+
+ShapesView::ShapesView(
+ mojo::ApplicationImpl* app_impl,
+ const mojo::ui::ViewProvider::CreateViewCallback& create_view_callback)
+ : GaneshView(app_impl, "Shapes", create_view_callback) {}
+
+ShapesView::~ShapesView() {}
+
+void ShapesView::OnLayout(mojo::ui::ViewLayoutParamsPtr layout_params,
+ mojo::Array<uint32_t> children_needing_layout,
+ const OnLayoutCallback& callback) {
+ size_.width = layout_params->constraints->max_width;
+ size_.height = layout_params->constraints->max_height;
+
+ // Submit the new layout information.
+ auto info = mojo::ui::ViewLayoutResult::New();
+ info->size = size_.Clone();
+ callback.Run(info.Pass());
+
+ // Draw!
+ UpdateScene();
+}
+
+void ShapesView::UpdateScene() {
+ mojo::Rect bounds;
+ bounds.width = size_.width;
+ bounds.height = size_.height;
+
+ auto update = mojo::gfx::composition::SceneUpdate::New();
+
+ // Draw the content of the view to a texture and include it as an
+ // image resource in the scene.
+ mojo::gfx::composition::ResourcePtr content_resource =
+ ganesh_renderer()->DrawCanvas(
+ size_, base::Bind(&ShapesView::DrawContent, base::Unretained(this)));
+ DCHECK(content_resource);
+ update->resources.insert(kContentImageResourceId, content_resource.Pass());
+
+ // Add a root node to the scene graph to draw the image resource to
+ // the screen such that it fills the entire view.
+ auto root_node = mojo::gfx::composition::Node::New();
+ root_node->op = mojo::gfx::composition::NodeOp::New();
+ root_node->op->set_image(mojo::gfx::composition::ImageNodeOp::New());
+ root_node->op->get_image()->content_rect = bounds.Clone();
+ root_node->op->get_image()->image_resource_id = kContentImageResourceId;
+ update->nodes.insert(kRootNodeId, root_node.Pass());
+
+ // Submit the scene update then publish it to cause the changes to be
+ // applied.
+ scene()->Update(update.Pass());
+ scene()->Publish(nullptr);
+}
+
+void ShapesView::DrawContent(SkCanvas* canvas) {
+ canvas->clear(SK_ColorCYAN);
+
+ SkPaint paint;
+ paint.setColor(SK_ColorGREEN);
+ SkRect rect = SkRect::MakeWH(size_.width, size_.height);
+ rect.inset(10, 10);
+ canvas->drawRect(rect, paint);
+
+ paint.setColor(SK_ColorRED);
+ paint.setFlags(SkPaint::kAntiAlias_Flag);
+ canvas->drawCircle(50, 100, 100, paint);
+}
+
+} // namespace examples
« no previous file with comments | « examples/ui/shapes/shapes_view.h ('k') | examples/ui/spinning_cube/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698