2019-04-24 17:56:16 +00:00
|
|
|
// Copyright 2019 Google LLC.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// [Work In Progress] Proof of principle of a text editor written with Skia & SkShaper.
|
|
|
|
// https://bugs.skia.org/9020
|
|
|
|
|
2019-04-28 18:40:45 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-04-24 17:56:16 +00:00
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkTime.h"
|
2019-04-28 18:40:45 +00:00
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
#include "tools/ModifierKey.h"
|
2019-04-24 17:56:16 +00:00
|
|
|
#include "tools/sk_app/Application.h"
|
|
|
|
#include "tools/sk_app/Window.h"
|
|
|
|
|
2019-05-14 19:01:39 +00:00
|
|
|
#include "experimental/editor/editor.h"
|
2019-04-28 18:40:45 +00:00
|
|
|
|
2019-07-30 13:58:45 +00:00
|
|
|
#include "third_party/icu/SkLoadICU.h"
|
|
|
|
|
2019-06-20 15:29:10 +00:00
|
|
|
#include <fstream>
|
2019-08-01 19:06:09 +00:00
|
|
|
#include <memory>
|
2019-04-28 18:40:45 +00:00
|
|
|
|
2019-06-20 15:29:10 +00:00
|
|
|
#ifdef SK_EDITOR_DEBUG_OUT
|
2019-04-28 18:40:45 +00:00
|
|
|
static const char* key_name(sk_app::Window::Key k) {
|
|
|
|
switch (k) {
|
|
|
|
#define M(X) case sk_app::Window::Key::k ## X: return #X
|
|
|
|
M(NONE); M(LeftSoftKey); M(RightSoftKey); M(Home); M(Back); M(Send); M(End); M(0); M(1);
|
|
|
|
M(2); M(3); M(4); M(5); M(6); M(7); M(8); M(9); M(Star); M(Hash); M(Up); M(Down); M(Left);
|
|
|
|
M(Right); M(Tab); M(PageUp); M(PageDown); M(Delete); M(Escape); M(Shift); M(Ctrl);
|
|
|
|
M(Option); M(A); M(C); M(V); M(X); M(Y); M(Z); M(OK); M(VolUp); M(VolDown); M(Power);
|
|
|
|
M(Camera);
|
|
|
|
#undef M
|
|
|
|
default: return "?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
static SkString modifiers_desc(ModifierKey m) {
|
2019-04-28 18:40:45 +00:00
|
|
|
SkString s;
|
2019-07-08 20:07:57 +00:00
|
|
|
#define M(X) if (m & ModifierKey::k ## X ##) { s.append(" {" #X "}"); }
|
2019-04-28 18:40:45 +00:00
|
|
|
M(Shift) M(Control) M(Option) M(Command) M(FirstPress)
|
|
|
|
#undef M
|
|
|
|
return s;
|
|
|
|
}
|
2019-04-24 17:56:16 +00:00
|
|
|
|
2019-07-08 20:07:57 +00:00
|
|
|
static void debug_on_char(SkUnichar c, ModifierKey modifiers) {
|
2019-05-03 21:14:21 +00:00
|
|
|
SkString m = modifiers_desc(modifiers);
|
|
|
|
if ((unsigned)c < 0x100) {
|
|
|
|
SkDebugf("char: %c (0x%02X)%s\n", (char)(c & 0xFF), (unsigned)c, m.c_str());
|
|
|
|
} else {
|
|
|
|
SkDebugf("char: 0x%08X%s\n", (unsigned)c, m.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 13:58:43 +00:00
|
|
|
static void debug_on_key(sk_app::Window::Key key, InputState, ModifierKey modi) {
|
2019-07-08 20:07:57 +00:00
|
|
|
SkDebugf("key: %s%s\n", key_name(key), modifiers_desc(modi).c_str());
|
2019-05-03 21:14:21 +00:00
|
|
|
}
|
2019-06-20 15:29:10 +00:00
|
|
|
#endif // SK_EDITOR_DEBUG_OUT
|
2019-05-03 21:14:21 +00:00
|
|
|
|
|
|
|
static editor::Editor::Movement convert(sk_app::Window::Key key) {
|
|
|
|
switch (key) {
|
|
|
|
case sk_app::Window::Key::kLeft: return editor::Editor::Movement::kLeft;
|
|
|
|
case sk_app::Window::Key::kRight: return editor::Editor::Movement::kRight;
|
|
|
|
case sk_app::Window::Key::kUp: return editor::Editor::Movement::kUp;
|
|
|
|
case sk_app::Window::Key::kDown: return editor::Editor::Movement::kDown;
|
|
|
|
case sk_app::Window::Key::kHome: return editor::Editor::Movement::kHome;
|
|
|
|
case sk_app::Window::Key::kEnd: return editor::Editor::Movement::kEnd;
|
|
|
|
default: return editor::Editor::Movement::kNowhere;
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 17:56:16 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct Timer {
|
|
|
|
double fTime;
|
|
|
|
const char* fDesc;
|
|
|
|
Timer(const char* desc = "") : fTime(SkTime::GetNSecs()), fDesc(desc) {}
|
2019-04-30 16:38:28 +00:00
|
|
|
~Timer() { SkDebugf("%s: %5d μs\n", fDesc, (int)((SkTime::GetNSecs() - fTime) * 1e-3)); }
|
2019-04-24 17:56:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct EditorLayer : public sk_app::Window::Layer {
|
2019-06-20 15:29:10 +00:00
|
|
|
SkString fPath;
|
2019-04-24 17:56:16 +00:00
|
|
|
sk_app::Window* fParent = nullptr;
|
2019-08-08 14:25:12 +00:00
|
|
|
// TODO(halcanary): implement a cross-platform clipboard interface.
|
2019-08-01 19:06:09 +00:00
|
|
|
std::vector<char> fClipboard;
|
2019-04-28 18:40:45 +00:00
|
|
|
editor::Editor fEditor;
|
2019-05-03 21:14:21 +00:00
|
|
|
editor::Editor::TextPosition fTextPos{0, 0};
|
|
|
|
editor::Editor::TextPosition fMarkPos;
|
2019-04-28 18:40:45 +00:00
|
|
|
int fPos = 0; // window pixel position in file
|
|
|
|
int fWidth = 0; // window width
|
|
|
|
int fHeight = 0; // window height
|
2019-08-05 18:54:04 +00:00
|
|
|
int fMargin = 10;
|
2019-05-03 21:14:21 +00:00
|
|
|
bool fShiftDown = false;
|
2019-08-01 15:12:01 +00:00
|
|
|
bool fBlink = false;
|
2019-08-08 14:25:12 +00:00
|
|
|
bool fMouseDown = false;
|
2019-05-03 21:14:21 +00:00
|
|
|
|
2019-04-28 18:40:45 +00:00
|
|
|
void loadFile(const char* path) {
|
|
|
|
if (sk_sp<SkData> data = SkData::MakeFromFileName(path)) {
|
2019-06-20 15:29:10 +00:00
|
|
|
fPath = path;
|
2019-08-01 15:06:06 +00:00
|
|
|
fEditor.insert(editor::Editor::TextPosition{0, 0},
|
|
|
|
(const char*)data->data(), data->size());
|
2019-06-20 15:29:10 +00:00
|
|
|
} else {
|
|
|
|
fPath = "output.txt";
|
2019-04-28 18:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 17:56:16 +00:00
|
|
|
|
|
|
|
void onPaint(SkSurface* surface) override {
|
2019-05-03 21:14:21 +00:00
|
|
|
SkCanvas* canvas = surface->getCanvas();
|
|
|
|
SkAutoCanvasRestore acr(canvas, true);
|
|
|
|
canvas->clipRect({0, 0, (float)fWidth, (float)fHeight});
|
2019-08-05 18:54:04 +00:00
|
|
|
canvas->translate(fMargin, (float)(fMargin - fPos));
|
2019-05-03 21:14:21 +00:00
|
|
|
editor::Editor::PaintOpts options;
|
|
|
|
options.fCursor = fTextPos;
|
2019-08-01 15:06:06 +00:00
|
|
|
options.fCursorColor = {1, 0, 0, fBlink ? 0.0f : 1.0f};
|
2019-05-03 21:14:21 +00:00
|
|
|
options.fBackgroundColor = SkColor4f{0.8f, 0.8f, 0.8f, 1};
|
2019-08-01 15:12:01 +00:00
|
|
|
options.fCursorColor = {1, 0, 0, fBlink ? 0.0f : 1.0f};
|
2019-05-03 21:14:21 +00:00
|
|
|
if (fMarkPos != editor::Editor::TextPosition()) {
|
|
|
|
options.fSelectionBegin = fMarkPos;
|
|
|
|
options.fSelectionEnd = fTextPos;
|
|
|
|
}
|
2019-07-30 13:58:45 +00:00
|
|
|
#ifdef SK_EDITOR_DEBUG_OUT
|
2019-06-20 15:29:10 +00:00
|
|
|
{
|
|
|
|
Timer timer("shaping");
|
|
|
|
fEditor.paint(nullptr, options);
|
|
|
|
}
|
|
|
|
Timer timer("painting");
|
|
|
|
#endif // SK_EDITOR_DEBUG_OUT
|
2019-05-03 21:14:21 +00:00
|
|
|
fEditor.paint(canvas, options);
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onResize(int width, int height) override {
|
|
|
|
if (SkISize{fWidth, fHeight} != SkISize{width, height}) {
|
|
|
|
fHeight = height;
|
|
|
|
if (width != fWidth) {
|
|
|
|
fWidth = width;
|
2019-08-05 18:54:04 +00:00
|
|
|
fEditor.setWidth(fWidth - 2 * fMargin);
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
2019-05-03 21:14:21 +00:00
|
|
|
this->inval();
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onAttach(sk_app::Window* w) override { fParent = w; }
|
|
|
|
|
2019-05-03 21:14:21 +00:00
|
|
|
bool scroll(int delta) {
|
2019-08-05 18:54:04 +00:00
|
|
|
int maxPos = std::max(0, fEditor.getHeight() + 2 * fMargin - fHeight / 2);
|
2019-04-28 18:40:45 +00:00
|
|
|
int newpos = std::max(0, std::min(fPos + delta, maxPos));
|
2019-04-24 17:56:16 +00:00
|
|
|
if (newpos != fPos) {
|
|
|
|
fPos = newpos;
|
2019-05-03 21:14:21 +00:00
|
|
|
this->inval();
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
2019-05-03 21:14:21 +00:00
|
|
|
return true;
|
2019-04-28 18:40:45 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:14:21 +00:00
|
|
|
void inval() { if (fParent) { fParent->inval(); } }
|
|
|
|
|
2019-08-07 14:59:27 +00:00
|
|
|
bool onMouseWheel(float delta, ModifierKey) override {
|
2019-04-28 18:40:45 +00:00
|
|
|
this->scroll(-(int)(delta * fEditor.font().getSpacing()));
|
|
|
|
return true;
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 13:58:43 +00:00
|
|
|
bool onMouse(int x, int y, InputState state, ModifierKey modifiers) override {
|
2019-08-07 14:59:27 +00:00
|
|
|
bool mouseDown = InputState::kDown == state;
|
|
|
|
if (mouseDown) {
|
|
|
|
fMouseDown = true;
|
|
|
|
} else if (InputState::kUp == state) {
|
|
|
|
fMouseDown = false;
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
2019-08-07 14:59:27 +00:00
|
|
|
bool shiftOrDrag = skstd::Any(modifiers & ModifierKey::kShift) || !mouseDown;
|
|
|
|
if (fMouseDown) {
|
|
|
|
return this->move(fEditor.getPosition({x - fMargin, y + fPos - fMargin}), shiftOrDrag);
|
|
|
|
}
|
|
|
|
return false;
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
2019-04-28 18:40:45 +00:00
|
|
|
|
2019-08-07 14:59:27 +00:00
|
|
|
bool onChar(SkUnichar c, ModifierKey modi) override {
|
|
|
|
using skstd::Any;
|
|
|
|
modi &= ~ModifierKey::kFirstPress;
|
|
|
|
if (!Any(modi & (ModifierKey::kControl |
|
|
|
|
ModifierKey::kOption |
|
|
|
|
ModifierKey::kCommand))) {
|
2019-06-20 15:29:10 +00:00
|
|
|
if (((unsigned)c < 0x7F && (unsigned)c >= 0x20) || c == '\n') {
|
2019-05-03 21:14:21 +00:00
|
|
|
char ch = (char)c;
|
|
|
|
fEditor.insert(fTextPos, &ch, 1);
|
2019-06-20 15:29:10 +00:00
|
|
|
#ifdef SK_EDITOR_DEBUG_OUT
|
2019-05-03 21:14:21 +00:00
|
|
|
SkDebugf("insert: %X'%c'\n", (unsigned)c, ch);
|
2019-06-20 15:29:10 +00:00
|
|
|
#endif // SK_EDITOR_DEBUG_OUT
|
2019-05-03 21:14:21 +00:00
|
|
|
return this->moveCursor(editor::Editor::Movement::kRight);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 14:59:27 +00:00
|
|
|
static constexpr ModifierKey kCommandOrControl = ModifierKey::kCommand |
|
|
|
|
ModifierKey::kControl;
|
|
|
|
if (Any(modi & kCommandOrControl) && !Any(modi & ~kCommandOrControl)) {
|
2019-06-20 15:29:10 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
2019-08-01 19:06:09 +00:00
|
|
|
for (editor::StringView str : fEditor.text()) {
|
|
|
|
SkDebugf(">> '%.*s'\n", str.size, str.data);
|
2019-06-20 15:29:10 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
std::ofstream out(fPath.c_str());
|
2019-08-01 19:06:09 +00:00
|
|
|
for (editor::StringView str : fEditor.text()) {
|
|
|
|
out.write(str.data, str.size) << '\n';
|
2019-06-20 15:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case 'c':
|
|
|
|
if (fMarkPos != editor::Editor::TextPosition()) {
|
2019-08-01 19:06:09 +00:00
|
|
|
fClipboard.resize(fEditor.copy(fMarkPos, fTextPos, nullptr));
|
|
|
|
fEditor.copy(fMarkPos, fTextPos, fClipboard.data());
|
2019-06-20 15:29:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case 'x':
|
|
|
|
if (fMarkPos != editor::Editor::TextPosition()) {
|
2019-08-01 19:06:09 +00:00
|
|
|
fClipboard.resize(fEditor.copy(fMarkPos, fTextPos, nullptr));
|
|
|
|
fEditor.copy(fMarkPos, fTextPos, fClipboard.data());
|
2019-08-08 14:25:12 +00:00
|
|
|
(void)this->move(fEditor.remove(fMarkPos, fTextPos), false);
|
2019-06-20 15:29:10 +00:00
|
|
|
this->inval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case 'v':
|
|
|
|
if (fClipboard.size()) {
|
2019-08-01 19:06:09 +00:00
|
|
|
fEditor.insert(fTextPos, fClipboard.data(), fClipboard.size());
|
2019-06-20 15:29:10 +00:00
|
|
|
this->inval();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef SK_EDITOR_DEBUG_OUT
|
2019-05-03 21:14:21 +00:00
|
|
|
debug_on_char(c, modifiers);
|
2019-06-20 15:29:10 +00:00
|
|
|
#endif // SK_EDITOR_DEBUG_OUT
|
2019-05-03 21:14:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-08-07 14:59:27 +00:00
|
|
|
|
2019-05-03 21:14:21 +00:00
|
|
|
bool moveCursor(editor::Editor::Movement m, bool shift = false) {
|
2019-08-07 14:59:27 +00:00
|
|
|
return this->move(fEditor.move(m, fTextPos), shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool move(editor::Editor::TextPosition pos, bool shift) {
|
|
|
|
if (pos == fTextPos || pos == editor::Editor::TextPosition()) {
|
2019-08-08 14:25:12 +00:00
|
|
|
if (!shift) {
|
|
|
|
fMarkPos = editor::Editor::TextPosition();
|
|
|
|
}
|
2019-08-07 14:59:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-03 21:14:21 +00:00
|
|
|
if (shift != fShiftDown) {
|
2019-08-07 14:59:27 +00:00
|
|
|
fMarkPos = shift ? fTextPos : editor::Editor::TextPosition();
|
2019-05-03 21:14:21 +00:00
|
|
|
fShiftDown = shift;
|
|
|
|
}
|
2019-08-07 14:59:27 +00:00
|
|
|
fTextPos = pos;
|
2019-07-29 18:44:04 +00:00
|
|
|
|
|
|
|
// scroll if needed.
|
|
|
|
SkIRect cursor = fEditor.getLocation(fTextPos).roundOut();
|
2019-08-05 18:54:04 +00:00
|
|
|
if (fPos < cursor.bottom() - fHeight + 2 * fMargin) {
|
|
|
|
fPos = cursor.bottom() - fHeight + 2 * fMargin;
|
2019-07-29 18:44:04 +00:00
|
|
|
} else if (cursor.top() < fPos) {
|
2019-08-05 18:54:04 +00:00
|
|
|
fPos = cursor.top();
|
2019-07-29 18:44:04 +00:00
|
|
|
}
|
2019-05-03 21:14:21 +00:00
|
|
|
this->inval();
|
2019-04-28 18:40:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-07-29 18:44:04 +00:00
|
|
|
|
2019-05-03 21:14:21 +00:00
|
|
|
bool onKey(sk_app::Window::Key key,
|
2019-07-16 13:58:43 +00:00
|
|
|
InputState state,
|
2019-07-08 20:07:57 +00:00
|
|
|
ModifierKey modifiers) override {
|
2019-07-30 13:58:45 +00:00
|
|
|
if (state != InputState::kDown) {
|
|
|
|
return false; // ignore keyup
|
|
|
|
}
|
|
|
|
// ignore other modifiers.
|
2019-08-07 14:59:27 +00:00
|
|
|
using skstd::Any;
|
2019-07-30 13:58:45 +00:00
|
|
|
ModifierKey ctrlAltCmd = modifiers & (ModifierKey::kControl |
|
|
|
|
ModifierKey::kOption |
|
|
|
|
ModifierKey::kCommand);
|
2019-08-07 14:59:27 +00:00
|
|
|
bool shift = Any(modifiers & (ModifierKey::kShift));
|
|
|
|
if (!Any(ctrlAltCmd)) {
|
2019-07-30 13:58:45 +00:00
|
|
|
// no modifiers
|
2019-05-03 21:14:21 +00:00
|
|
|
switch (key) {
|
|
|
|
case sk_app::Window::Key::kPageDown:
|
|
|
|
return this->scroll(fHeight * 4 / 5);
|
|
|
|
case sk_app::Window::Key::kPageUp:
|
|
|
|
return this->scroll(-fHeight * 4 / 5);
|
|
|
|
case sk_app::Window::Key::kLeft:
|
|
|
|
case sk_app::Window::Key::kRight:
|
|
|
|
case sk_app::Window::Key::kUp:
|
|
|
|
case sk_app::Window::Key::kDown:
|
|
|
|
case sk_app::Window::Key::kHome:
|
|
|
|
case sk_app::Window::Key::kEnd:
|
2019-08-07 14:59:27 +00:00
|
|
|
return this->moveCursor(convert(key), shift);
|
2019-05-03 21:14:21 +00:00
|
|
|
case sk_app::Window::Key::kDelete:
|
|
|
|
if (fMarkPos != editor::Editor::TextPosition()) {
|
2019-08-08 14:25:12 +00:00
|
|
|
(void)this->move(fEditor.remove(fMarkPos, fTextPos), false);
|
2019-05-03 21:14:21 +00:00
|
|
|
} else {
|
2019-08-08 14:25:12 +00:00
|
|
|
auto pos = fEditor.move(editor::Editor::Movement::kRight, fTextPos);
|
|
|
|
(void)this->move(fEditor.remove(fTextPos, pos), false);
|
2019-05-03 21:14:21 +00:00
|
|
|
}
|
|
|
|
this->inval();
|
|
|
|
return true;
|
|
|
|
case sk_app::Window::Key::kBack:
|
|
|
|
if (fMarkPos != editor::Editor::TextPosition()) {
|
2019-08-08 14:25:12 +00:00
|
|
|
(void)this->move(fEditor.remove(fMarkPos, fTextPos), false);
|
2019-05-03 21:14:21 +00:00
|
|
|
} else {
|
2019-08-08 14:25:12 +00:00
|
|
|
auto pos = fEditor.move(editor::Editor::Movement::kLeft, fTextPos);
|
|
|
|
(void)this->move(fEditor.remove(fTextPos, pos), false);
|
2019-05-03 21:14:21 +00:00
|
|
|
}
|
|
|
|
this->inval();
|
|
|
|
return true;
|
2019-06-20 15:29:10 +00:00
|
|
|
case sk_app::Window::Key::kOK:
|
|
|
|
return this->onChar('\n', modifiers);
|
2019-05-03 21:14:21 +00:00
|
|
|
default:
|
|
|
|
break;
|
2019-04-28 18:40:45 +00:00
|
|
|
}
|
2019-08-07 14:59:27 +00:00
|
|
|
} else if (skstd::Any(ctrlAltCmd & (ModifierKey::kControl | ModifierKey::kCommand))) {
|
2019-07-30 13:58:45 +00:00
|
|
|
switch (key) {
|
|
|
|
case sk_app::Window::Key::kLeft:
|
2019-08-07 14:59:27 +00:00
|
|
|
return this->moveCursor(editor::Editor::Movement::kWordLeft, shift);
|
2019-07-30 13:58:45 +00:00
|
|
|
case sk_app::Window::Key::kRight:
|
2019-08-07 14:59:27 +00:00
|
|
|
return this->moveCursor(editor::Editor::Movement::kWordRight, shift);
|
2019-07-30 13:58:45 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-04-28 18:40:45 +00:00
|
|
|
}
|
2019-07-30 13:58:45 +00:00
|
|
|
#ifdef SK_EDITOR_DEBUG_OUT
|
|
|
|
debug_on_key(key, state, modifiers);
|
|
|
|
#endif // SK_EDITOR_DEBUG_OUT
|
|
|
|
return false;
|
2019-04-28 18:40:45 +00:00
|
|
|
}
|
2019-04-24 17:56:16 +00:00
|
|
|
};
|
|
|
|
|
2019-08-01 15:06:06 +00:00
|
|
|
static constexpr float kFontSize = 18;
|
|
|
|
// static constexpr char kTypefaceName[] = "monospace";
|
|
|
|
static constexpr char kTypefaceName[] = "sans-serif";
|
|
|
|
static constexpr SkFontStyle::Weight kFontWeight = SkFontStyle::kNormal_Weight;
|
|
|
|
static constexpr SkFontStyle::Width kFontWidth = SkFontStyle::kNormal_Width;
|
|
|
|
static constexpr SkFontStyle::Slant kFontSlant = SkFontStyle::kUpright_Slant;
|
|
|
|
|
|
|
|
//static constexpr sk_app::Window::BackendType kBackendType = sk_app::Window::kRaster_BackendType;
|
|
|
|
static constexpr sk_app::Window::BackendType kBackendType = sk_app::Window::kNativeGL_BackendType;
|
|
|
|
|
2019-04-24 17:56:16 +00:00
|
|
|
struct EditorApplication : public sk_app::Application {
|
|
|
|
std::unique_ptr<sk_app::Window> fWindow;
|
2019-04-28 18:40:45 +00:00
|
|
|
EditorLayer fLayer;
|
2019-08-01 15:12:01 +00:00
|
|
|
double fNextTime = -DBL_MAX;
|
2019-04-24 17:56:16 +00:00
|
|
|
|
2019-08-01 15:06:06 +00:00
|
|
|
EditorApplication(void* platformData)
|
|
|
|
: fWindow(sk_app::Window::CreateNativeWindow(platformData)) {}
|
|
|
|
|
|
|
|
bool init(const char* path) {
|
|
|
|
fWindow->attach(kBackendType);
|
2019-05-03 21:14:21 +00:00
|
|
|
fLayer.inval();
|
2019-08-01 15:06:06 +00:00
|
|
|
|
|
|
|
fLayer.fEditor.setFont(SkFont(SkTypeface::MakeFromName(kTypefaceName,
|
|
|
|
SkFontStyle(kFontWeight, kFontWidth, kFontSlant)), kFontSize));
|
2019-04-28 18:40:45 +00:00
|
|
|
fLayer.loadFile(path);
|
2019-08-01 15:06:06 +00:00
|
|
|
|
2019-04-28 18:40:45 +00:00
|
|
|
fWindow->pushLayer(&fLayer);
|
2019-06-26 16:11:31 +00:00
|
|
|
fWindow->setTitle(SkStringPrintf("Editor: \"%s\"", fLayer.fPath.c_str()).c_str());
|
2019-04-24 17:56:16 +00:00
|
|
|
fWindow->show();
|
2019-04-28 18:40:45 +00:00
|
|
|
fLayer.onResize(fWindow->width(), fWindow->height());
|
2019-08-01 15:06:06 +00:00
|
|
|
return true;
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|
|
|
|
~EditorApplication() override { fWindow->detach(); }
|
|
|
|
|
2019-08-01 15:12:01 +00:00
|
|
|
void onIdle() override {
|
|
|
|
double now = SkTime::GetNSecs();
|
|
|
|
if (now >= fNextTime) {
|
|
|
|
constexpr double kHalfPeriodNanoSeconds = 0.5 * 1e9;
|
|
|
|
fNextTime = now + kHalfPeriodNanoSeconds;
|
|
|
|
fLayer.fBlink = !fLayer.fBlink;
|
|
|
|
fWindow->inval();
|
|
|
|
}
|
|
|
|
}
|
2019-04-24 17:56:16 +00:00
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
sk_app::Application* sk_app::Application::Create(int argc, char** argv, void* dat) {
|
2019-07-30 13:58:45 +00:00
|
|
|
if (!SkLoadICU()) {
|
|
|
|
SK_ABORT("SkLoadICU failed.");
|
|
|
|
}
|
2019-08-01 15:06:06 +00:00
|
|
|
EditorApplication* app = new EditorApplication(dat);
|
|
|
|
SkASSERT(app->init(argc > 1 ? argv[1] : nullptr));
|
|
|
|
return app;
|
2019-04-24 17:56:16 +00:00
|
|
|
}
|