• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 
4 // Proof of principle of a text editor written with Skia & SkShaper.
5 // https://bugs.skia.org/9020
6 
7 #include "include/core/SkCanvas.h"
8 #include "include/core/SkSurface.h"
9 #include "include/core/SkTime.h"
10 
11 #include "tools/sk_app/Application.h"
12 #include "tools/sk_app/Window.h"
13 #include "tools/skui/ModifierKey.h"
14 
15 #include "experimental/sktext/editor/Editor.h"
16 
17 #include "third_party/icu/SkLoadICU.h"
18 
19 #include <fstream>
20 #include <memory>
21 
22 using namespace skia::text;
23 namespace skia {
24 namespace editor {
25 
26 struct EditorApplication : public sk_app::Application {
27     std::unique_ptr<sk_app::Window> fWindow;
28     std::unique_ptr<Editor> fLayer;
29     double fNextTime = -DBL_MAX;
30 
EditorApplicationskia::editor::EditorApplication31     EditorApplication(std::unique_ptr<sk_app::Window> win) : fWindow(std::move(win)) {}
32 
initskia::editor::EditorApplication33     bool init(const char* path) {
34         fWindow->attach(sk_app::Window::kRaster_BackendType);
35 
36         fLayer = Editor::MakeDemo(fWindow->width(), fWindow->height());
37 
38         fWindow->pushLayer(fLayer.get());
39         fWindow->setTitle("Editor");
40 
41         fLayer->onResize(fWindow->width(), fWindow->height());
42         fWindow->show();
43         return true;
44     }
~EditorApplicationskia::editor::EditorApplication45     ~EditorApplication() override { fWindow->detach(); }
46 
onIdleskia::editor::EditorApplication47     void onIdle() override {
48         double now = SkTime::GetNSecs();
49         if (now >= fNextTime) {
50             constexpr double kHalfPeriodNanoSeconds = 0.5 * 1e9;
51             fNextTime = now + kHalfPeriodNanoSeconds;
52             fLayer->blink();
53             fWindow->inval();
54         }
55     }
56 };
57 } // namespace text
58 } // namespace skia
59 
Create(int argc,char ** argv,void * dat)60 sk_app::Application* sk_app::Application::Create(int argc, char** argv, void* dat) {
61     if (!SkLoadICU()) {
62         SK_ABORT("SkLoadICU failed.");
63     }
64     std::unique_ptr<sk_app::Window> win(sk_app::Window::CreateNativeWindow(dat));
65     if (!win) {
66         SK_ABORT("CreateNativeWindow failed.");
67     }
68     std::unique_ptr<skia::editor::EditorApplication> app(new skia::editor::EditorApplication(std::move(win)));
69     (void)app->init(argc > 1 ? argv[1] : nullptr);
70     return app.release();
71 }
72