Skip to content

Commit

Permalink
Merge pull request flutter#568 from collinjackson/playfair
Browse files Browse the repository at this point in the history
Add support for charts to fitness app
  • Loading branch information
collinjackson committed Aug 12, 2015
2 parents 5099c65 + d4e68ab commit 480ea65
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 14 deletions.
59 changes: 45 additions & 14 deletions examples/fitness/lib/feed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,28 +173,59 @@ class FeedFragment extends StatefulComponent {
});
}

Widget buildChart() {
double startX;
double endX;
double startY;
double endY;
List<Point> dataSet = new List<Point>();
for (FitnessItem item in userData) {
if (item is Measurement) {
double x = item.when.millisecondsSinceEpoch.toDouble();
double y = item.weight;
if (startX == null)
startX = x;
endX = x;
if (startY == null || startY > y)
startY = y;
if (endY == null || endY < y)
endY = y;
dataSet.add(new Point(x, y));
}
}
playfair.ChartData data = new playfair.ChartData(
startX: startX,
startY: startY,
endX: endX,
endY: endY,
dataSet: dataSet
);
return new playfair.Chart(data: data);
}

Widget buildBody() {
TextStyle style = Theme.of(this).text.title;
if (userData.length == 0)
return new Material(
type: MaterialType.canvas,
child: new Flex(
[new Text("No data yet.\nAdd some!", style: style)],
justifyContent: FlexJustifyContent.center
)
);
switch (_fitnessMode) {
case FitnessMode.feed:
if (userData.length > 0)
return new FitnessItemList(
items: userData,
onDismissed: _handleItemDismissed
);
return new Material(
type: MaterialType.canvas,
child: new Flex(
[new Text("No data yet.\nAdd some!", style: style)],
justifyContent: FlexJustifyContent.center
)
return new FitnessItemList(
items: userData,
onDismissed: _handleItemDismissed
);
case FitnessMode.chart:
return new Material(
type: MaterialType.canvas,
child: new Flex([
new Text("Charts are coming soon!", style: style)
], justifyContent: FlexJustifyContent.center)
child: new Container(
padding: const EdgeDims.all(20.0),
child: buildChart()
)
);
}
}
Expand Down
1 change: 1 addition & 0 deletions examples/fitness/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

library fitness;

import 'package:playfair/playfair.dart' as playfair;
import 'package:sky/editing/input.dart';
import 'package:sky/painting/text_style.dart';
import 'package:sky/theme/colors.dart' as colors;
Expand Down
1 change: 1 addition & 0 deletions examples/fitness/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: fitness
dependencies:
sky: any
sky_tools: any
playfair: any
path: "^1.3.6"
dependency_overrides:
material_design_icons:
Expand Down
3 changes: 3 additions & 0 deletions sky/engine/core/core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ sky_core_files = [
"painting/Size.cpp",
"painting/Size.h",
"painting/TransferMode.h",
"painting/Typeface.cpp",
"painting/Typeface.h",
"rendering/BidiRun.h",
"rendering/BidiRunForLine.cpp",
"rendering/BidiRunForLine.h",
Expand Down Expand Up @@ -682,6 +684,7 @@ core_idl_files = get_path_info([
"painting/PictureRecorder.idl",
"painting/RRect.idl",
"painting/Shader.idl",
"painting/Typeface.idl",
"view/EventCallback.idl",
"view/FrameCallback.idl",
"view/View.idl",
Expand Down
8 changes: 8 additions & 0 deletions sky/engine/core/painting/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ void Canvas::drawPaintingNode(PaintingNode* paintingNode, const Point& p)
translate(-p.sk_point.x(), -p.sk_point.y());
}

void Canvas::drawText(const String& text, const Point& p, const Paint& paint)
{
if (!m_canvas)
return;
ASSERT(text);
m_canvas->drawText(text.utf8().data(), text.length(), p.sk_point.x(), p.sk_point.y(), paint.sk_paint);
}

void Canvas::drawAtlas(CanvasImage* atlas,
const Vector<RSTransform>& transforms, const Vector<Rect>& rects,
const Vector<SkColor>& colors, SkXfermode::Mode mode,
Expand Down
1 change: 1 addition & 0 deletions sky/engine/core/painting/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class Canvas : public RefCounted<Canvas>, public DartWrappable {
void drawPicture(Picture* picture);
void drawDrawable(Drawable* drawable);
void drawPaintingNode(PaintingNode* paintingNode, const Point& p);
void drawText(const String& text, const Point& p, const Paint& paint);

void drawAtlas(CanvasImage* atlas,
const Vector<RSTransform>& transforms, const Vector<Rect>& rects,
Expand Down
1 change: 1 addition & 0 deletions sky/engine/core/painting/Canvas.idl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
void drawPicture(Picture picture);
void drawDrawable(Drawable drawable);
void drawPaintingNode(PaintingNode paintingNode, Point p);
void drawText(DOMString text, Point p, Paint paint);

// TODO(eseidel): Paint should be optional, but optional doesn't work.
[RaisesException] void drawAtlas(Image image,
Expand Down
4 changes: 4 additions & 0 deletions sky/engine/core/painting/Paint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "sky/engine/core/painting/PaintingStyle.h"
#include "sky/engine/core/painting/Shader.h"
#include "sky/engine/core/painting/TransferMode.h"
#include "sky/engine/core/painting/Typeface.h"
#include "sky/engine/core/script/dom_dart_state.h"
#include "sky/engine/wtf/text/StringBuilder.h"
#include "third_party/skia/include/core/SkColorFilter.h"
Expand All @@ -35,6 +36,7 @@ enum PaintFields {
kShader,
kStyle,
kTransferMode,
kTypeface,

// kNumberOfPaintFields must be last.
kNumberOfPaintFields,
Expand Down Expand Up @@ -82,6 +84,8 @@ Paint DartConverter<Paint>::FromDart(Dart_Handle dart_paint) {
paint.setStyle(DartConverter<PaintingStyle>::FromDart(values[kStyle]));
if (!Dart_IsNull(values[kTransferMode]))
paint.setXfermodeMode(DartConverter<TransferMode>::FromDart(values[kTransferMode]));
if (!Dart_IsNull(values[kTypeface]))
paint.setTypeface(DartConverter<Typeface*>::FromDart(values[kTypeface])->typeface());

result.is_null = false;
return result;
Expand Down
4 changes: 4 additions & 0 deletions sky/engine/core/painting/Paint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Paint {
Shader _shader;
PaintingStyle _style;
TransferMode _transferMode;
Typeface typeface;

// Must match PaintFields enum in Paint.cpp.
List<dynamic> get _value {
Expand All @@ -57,6 +58,7 @@ class Paint {
_shader,
_style,
_transferMode,
typeface
];
}

Expand All @@ -71,6 +73,8 @@ class Paint {
// TODO(mpcomplete): Figure out how to show a drawLooper.
if (_drawLooper != null)
result += ', drawLooper:true';
if (typeface != null)
result += ', typeface: $_typeface';
result += ')';
return result;
}
Expand Down
17 changes: 17 additions & 0 deletions sky/engine/core/painting/Typeface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 "sky/engine/core/painting/Typeface.h"

namespace blink {

Typeface::Typeface(PassRefPtr<SkTypeface> typeface)
: typeface_(typeface) {
}

Typeface::~Typeface()
{
}

} // namespace blink
32 changes: 32 additions & 0 deletions sky/engine/core/painting/Typeface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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.

#ifndef SKY_ENGINE_CORE_PAINTING_TYPEFACE_H_
#define SKY_ENGINE_CORE_PAINTING_TYPEFACE_H_

#include "sky/engine/tonic/dart_wrappable.h"
#include "sky/engine/wtf/PassRefPtr.h"
#include "sky/engine/wtf/RefCounted.h"
#include "third_party/skia/include/core/SkTypeface.h"

namespace blink {

class Typeface : public RefCounted<Typeface>, public DartWrappable {
DEFINE_WRAPPERTYPEINFO();
public:
~Typeface() override;

SkTypeface* typeface() { return typeface_.get(); }
void set_typeface(PassRefPtr<SkTypeface> typeface) { typeface_ = typeface; }

protected:
Typeface(PassRefPtr<SkTypeface> typeface);

private:
RefPtr<SkTypeface> typeface_;
};

} // namespace blink

#endif // SKY_ENGINE_CORE_PAINTING_TYPEFACE_H_
6 changes: 6 additions & 0 deletions sky/engine/core/painting/Typeface.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// 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.

interface Typeface {
};

0 comments on commit 480ea65

Please sign in to comment.