Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Window.h
Go to the documentation of this file.
1/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
8#ifndef Window_DEFINED
9#define Window_DEFINED
10
11#include "include/core/SkRect.h"
15#include "tools/skui/Key.h"
18
19#include <functional>
20
21class GrDirectContext;
22class SkCanvas;
23class SkSurface;
24class SkSurfaceProps;
25class SkString;
26
27namespace skgpu::graphite {
28class Context;
29class Recorder;
30}
31
33
34namespace skwindow {
35class WindowContext;
36}
37
38namespace sk_app {
39
40class Window {
41public:
42 static Window* CreateNativeWindow(void* platformData);
43
44 virtual ~Window();
45
46 virtual void setTitle(const char*) = 0;
47 virtual void show() = 0;
48
49 // JSON-formatted UI state for Android. Do nothing by default
50 virtual void setUIState(const char*) {}
51
52 // Interface to the system clipboard. Only implemented on UNIX.
53 virtual const char* getClipboardText() { return nullptr; }
54 virtual void setClipboardText(const char*) {}
55
56 // Schedules an invalidation event for window if one is not currently pending.
57 // Make sure that either onPaint or markInvalReceived is called when the client window consumes
58 // the the inval event. They unset fIsContentInvalided which allow future onInval.
59 void inval();
60
61 virtual bool scaleContentToFit() const { return false; }
62
64#ifdef SK_GL
65 kNativeGL_BackendType,
66#endif
67#if SK_ANGLE && (defined(SK_BUILD_FOR_WIN) || defined(SK_BUILD_FOR_MAC))
68 kANGLE_BackendType,
69#endif
70#ifdef SK_DAWN
71#if defined(SK_GRAPHITE)
72 kGraphiteDawn_BackendType,
73#endif
74#endif
75#ifdef SK_VULKAN
76 kVulkan_BackendType,
77#if defined(SK_GRAPHITE)
78 kGraphiteVulkan_BackendType,
79#endif
80#endif
81#ifdef SK_METAL
82 kMetal_BackendType,
83#if defined(SK_GRAPHITE)
84 kGraphiteMetal_BackendType,
85#endif
86#endif
87#ifdef SK_DIRECT3D
88 kDirect3D_BackendType,
89#endif
91
93 };
94 enum {
96 };
97
98 virtual bool attach(BackendType) = 0;
99 void detach();
100
101 // input handling
102
103 class Layer {
104 public:
105 Layer() : fActive(true) {}
106 virtual ~Layer() = default;
107
108 bool getActive() { return fActive; }
109 void setActive(bool active) { fActive = active; }
110
111 // return value of 'true' means 'I have handled this event'
112 virtual void onBackendCreated() {}
113 virtual void onAttach(Window* window) {}
114 virtual bool onChar(SkUnichar c, skui::ModifierKey) { return false; }
115 virtual bool onKey(skui::Key, skui::InputState, skui::ModifierKey) { return false; }
116 virtual bool onMouse(int x, int y, skui::InputState, skui::ModifierKey) { return false; }
117 virtual bool onMouseWheel(float delta, int x, int y, skui::ModifierKey) { return false; }
118 virtual bool onTouch(intptr_t owner, skui::InputState, float x, float y) { return false; }
119 // Platform-detected gesture events
120 virtual bool onFling(skui::InputState state) { return false; }
121 virtual bool onPinch(skui::InputState state, float scale, float x, float y) { return false; }
122 virtual void onUIStateChanged(const SkString& stateName, const SkString& stateValue) {}
123 virtual void onPrePaint() {}
124 virtual void onPaint(SkSurface*) {}
125 virtual void onResize(int width, int height) {}
126
127 private:
128 friend class Window;
129 bool fActive;
130 };
131
132 void pushLayer(Layer* layer) {
133 layer->onAttach(this);
134 fLayers.push_back(layer);
135 }
136
137 void onBackendCreated();
138 bool onChar(SkUnichar c, skui::ModifierKey modifiers);
140 bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers);
141 bool onMouseWheel(float delta, int x, int y, skui::ModifierKey modifiers);
142 bool onTouch(intptr_t owner, skui::InputState state, float x, float y); // multi-owner = multi-touch
143 // Platform-detected gesture events
145 bool onPinch(skui::InputState state, float scale, float x, float y);
146 void onUIStateChanged(const SkString& stateName, const SkString& stateValue);
147 void onPaint();
148 void onResize(int width, int height);
149 void onActivate(bool isActive);
150
151 int width() const;
152 int height() const;
153 virtual float scaleFactor() const { return 1.0f; }
154
156 virtual void setRequestedDisplayParams(const DisplayParams&, bool allowReattach = true);
157
158 // Actual parameters in effect, obtained from the native window.
159 int sampleCount() const;
160 int stencilBits() const;
161
162 // Returns null if there is not a GPU backend or if the backend is not yet created.
166
167protected:
168 Window();
169
172 bool fIsActive = true;
173
174 std::unique_ptr<skwindow::WindowContext> fWindowContext;
175
176 virtual void onInval() = 0;
177
178 // Uncheck fIsContentInvalided to allow future inval/onInval.
179 void markInvalProcessed();
180
181 bool fIsContentInvalidated = false; // use this to avoid duplicate invalidate events
182
183 void visitLayers(const std::function<void(Layer*)>& visitor);
184 bool signalLayers(const std::function<bool(Layer*)>& visitor);
185};
186
187} // namespace sk_app
188#endif
int32_t SkUnichar
Definition SkTypes.h:175
virtual void onUIStateChanged(const SkString &stateName, const SkString &stateValue)
Definition Window.h:122
virtual bool onChar(SkUnichar c, skui::ModifierKey)
Definition Window.h:114
virtual bool onMouseWheel(float delta, int x, int y, skui::ModifierKey)
Definition Window.h:117
void setActive(bool active)
Definition Window.h:109
virtual bool onPinch(skui::InputState state, float scale, float x, float y)
Definition Window.h:121
virtual bool onFling(skui::InputState state)
Definition Window.h:120
virtual bool onTouch(intptr_t owner, skui::InputState, float x, float y)
Definition Window.h:118
virtual bool onKey(skui::Key, skui::InputState, skui::ModifierKey)
Definition Window.h:115
virtual void onAttach(Window *window)
Definition Window.h:113
virtual void onResize(int width, int height)
Definition Window.h:125
virtual ~Layer()=default
virtual bool onMouse(int x, int y, skui::InputState, skui::ModifierKey)
Definition Window.h:116
virtual void onBackendCreated()
Definition Window.h:112
virtual void onPrePaint()
Definition Window.h:123
virtual void onPaint(SkSurface *)
Definition Window.h:124
virtual const DisplayParams & getRequestedDisplayParams()
Definition Window.h:155
virtual bool scaleContentToFit() const
Definition Window.h:61
bool onKey(skui::Key key, skui::InputState state, skui::ModifierKey modifiers)
Definition Window.cpp:52
void visitLayers(const std::function< void(Layer *)> &visitor)
Definition Window.cpp:27
bool onTouch(intptr_t owner, skui::InputState state, float x, float y)
Definition Window.cpp:65
virtual ~Window()
Definition Window.cpp:23
skgpu::graphite::Context * graphiteContext() const
Definition Window.cpp:165
int stencilBits() const
Definition Window.cpp:151
virtual void setUIState(const char *)
Definition Window.h:50
bool onChar(SkUnichar c, skui::ModifierKey modifiers)
Definition Window.cpp:48
void onUIStateChanged(const SkString &stateName, const SkString &stateValue)
Definition Window.cpp:77
virtual void setClipboardText(const char *)
Definition Window.h:54
@ kBackendTypeCount
Definition Window.h:95
static Window * CreateNativeWindow(void *platformData)
void detach()
Definition Window.cpp:25
void pushLayer(Layer *layer)
Definition Window.h:132
@ kRaster_BackendType
Definition Window.h:90
@ kLast_BackendType
Definition Window.h:92
virtual void setRequestedDisplayParams(const DisplayParams &, bool allowReattach=true)
Definition Window.cpp:137
int height() const
Definition Window.cpp:130
bool onMouseWheel(float delta, int x, int y, skui::ModifierKey modifiers)
Definition Window.cpp:60
virtual bool attach(BackendType)=0
bool onFling(skui::InputState state)
Definition Window.cpp:69
virtual const char * getClipboardText()
Definition Window.h:53
bool signalLayers(const std::function< bool(Layer *)> &visitor)
Definition Window.cpp:35
void markInvalProcessed()
Definition Window.cpp:197
GrDirectContext * directContext() const
Definition Window.cpp:158
void onActivate(bool isActive)
Definition Window.cpp:116
bool fIsContentInvalidated
Definition Window.h:181
DisplayParams fRequestedDisplayParams
Definition Window.h:171
bool onPinch(skui::InputState state, float scale, float x, float y)
Definition Window.cpp:73
void onResize(int width, int height)
Definition Window.cpp:108
std::unique_ptr< skwindow::WindowContext > fWindowContext
Definition Window.h:174
bool onMouse(int x, int y, skui::InputState state, skui::ModifierKey modifiers)
Definition Window.cpp:56
int sampleCount() const
Definition Window.cpp:144
virtual void show()=0
bool fIsActive
Definition Window.h:172
virtual void onInval()=0
virtual void setTitle(const char *)=0
SkTDArray< Layer * > fLayers
Definition Window.h:170
virtual float scaleFactor() const
Definition Window.h:153
int width() const
Definition Window.cpp:123
void onPaint()
Definition Window.cpp:81
skgpu::graphite::Recorder * graphiteRecorder() const
Definition Window.cpp:176
void onBackendCreated()
Definition Window.cpp:44
GLFWwindow * window
Definition main.cc:45
AtkStateType state
double y
double x
InputState
Definition InputState.h:6
ModifierKey
Definition ModifierKey.h:9
Key
Definition Key.h:6
const Scalar scale