Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:support api visibilityState, hidden #350

Merged
merged 8 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bridge/core/binding_call_methods.json5
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,7 @@
"domain",
"compatMode",
"readyState",
"visibilityState",
"hidden",
]
}
5 changes: 5 additions & 0 deletions bridge/core/dom/document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ AtomicString Document::readyState() {
return NativeValueConverter<NativeTypeString>::FromNativeValue(ctx(), std::move(dart_result));
}

bool Document::hidden() {
NativeValue dart_result = GetBindingProperty(binding_call_methods::khidden, ASSERT_NO_EXCEPTION());
return NativeValueConverter<NativeTypeBool>::FromNativeValue(dart_result);
}

template <typename CharType>
static inline bool IsValidNameASCII(const CharType* characters, unsigned length) {
CharType c = characters[0];
Expand Down
2 changes: 2 additions & 0 deletions bridge/core/dom/document.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ interface Document extends Node {
readonly location: any;
readonly compatMode: string;
readonly readyState: string;
readonly visibilityState: DartImpl<string>;
readonly hidden: boolean;

createElement(tagName: string, options?: any): Element;
createElementNS(uri: string | null, tagName: string, options?: any): Element;
Expand Down
10 changes: 6 additions & 4 deletions bridge/core/dom/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "bindings/qjs/cppgc/local_handle.h"
#include "container_node.h"
#include "event_type_names.h"
#include "scripted_animation_controller.h"
#include "tree_scope.h"
#include "event_type_names.h"

namespace webf {

Expand Down Expand Up @@ -70,13 +70,15 @@ class Document : public ContainerNode, public TreeScope {
std::vector<Element*> getElementsByTagName(const AtomicString& tag_name, ExceptionState& exception_state);
std::vector<Element*> getElementsByName(const AtomicString& name, ExceptionState& exception_state);

AtomicString domain();
AtomicString domain();
void setDomain(const AtomicString& value, ExceptionState& exception_state);
AtomicString compatMode();
AtomicString compatMode();

AtomicString readyState();
AtomicString readyState();
DEFINE_DOCUMENT_ATTRIBUTE_EVENT_LISTENER(readystatechange, kreadystatechange);

bool hidden();

// The following implements the rule from HTML 4 for what valid names are.
static bool IsValidName(const AtomicString& name);

Expand Down
12 changes: 6 additions & 6 deletions bridge/core/dom/events/event_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,12 @@ class EventTargetWithInlineData : public EventTarget {
GetDocument().SetWindowAttributeEventListener(event_type_names::symbol_name, listener, exception_state); \
}

#define DEFINE_DOCUMENT_ATTRIBUTE_EVENT_LISTENER(lower_name, symbol_name) \
std::shared_ptr<EventListener> on##lower_name() { \
return GetWindowAttributeEventListener(event_type_names::symbol_name); \
} \
void setOn##lower_name(const std::shared_ptr<EventListener>& listener, ExceptionState& exception_state) { \
SetWindowAttributeEventListener(event_type_names::symbol_name, listener, exception_state); \
#define DEFINE_DOCUMENT_ATTRIBUTE_EVENT_LISTENER(lower_name, symbol_name) \
std::shared_ptr<EventListener> on##lower_name() { \
return GetWindowAttributeEventListener(event_type_names::symbol_name); \
} \
void setOn##lower_name(const std::shared_ptr<EventListener>& listener, ExceptionState& exception_state) { \
SetWindowAttributeEventListener(event_type_names::symbol_name, listener, exception_state); \
}

#define DEFINE_STATIC_WINDOW_ATTRIBUTE_EVENT_LISTENER(lower_name, symbol_name) \
Expand Down
8 changes: 8 additions & 0 deletions integration_tests/specs/dom/nodes/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ describe('Document api', () => {
it('document.readyState', () => {
expect(document.readyState).not.toBeUndefined();
});

it('document.visibilityState', () => {
expect(document.visibilityState).not.toBeUndefined();
});

it('document.hidden', () => {
expect(document.hidden).not.toBeUndefined();
});
});
4 changes: 2 additions & 2 deletions webf/lib/src/dom/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ abstract class ElementsBindingObserver {
/// settings.
///
/// This method exposes notifications from [Window.onLocaleChanged].
void didChangeLocales(List<Locale> locale) {}
void didChangeLocales(List<Locale>? locale) {}

/// Called when the system puts the app in the background or returns
/// the app to the foreground.
Expand Down Expand Up @@ -192,7 +192,7 @@ mixin ElementsBinding
/// notification is received.
@protected
@mustCallSuper
void dispatchLocalesChanged(List<Locale> locales) {
void dispatchLocalesChanged(List<Locale>? locales) {
for (ElementsBindingObserver observer in _observers) observer.didChangeLocales(locales);
}

Expand Down
21 changes: 20 additions & 1 deletion webf/lib/src/dom/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class _InactiveRenderObjects {
_renderObjects.clear();
}
}
enum DocumentReadyState { loading, interactive, complete}
enum DocumentReadyState { loading, interactive, complete }
enum VisibilityState { visible, hidden }

class Document extends ContainerNode {
final WebFController controller;
Expand All @@ -82,6 +83,7 @@ class Document extends ContainerNode {
final String _compatMode = 'CSS1Compat';

String? _readyState;
VisibilityState _visibilityState = VisibilityState.hidden;

@override
bool get isConnected => true;
Expand Down Expand Up @@ -180,6 +182,8 @@ class Document extends ContainerNode {
properties['compatMode'] = BindingObjectProperty(getter: () => compatMode,);
properties['domain'] = BindingObjectProperty(getter: () => domain, setter: (value) => domain = value);
properties['readyState'] = BindingObjectProperty(getter: () => readyState,);
properties['visibilityState'] = BindingObjectProperty(getter: () => visibilityState,);
properties['hidden'] = BindingObjectProperty(getter: () => hidden,);
}

@override
Expand Down Expand Up @@ -211,6 +215,19 @@ class Document extends ContainerNode {
}
}

get visibilityState {
return _visibilityState.name;
}

get hidden {
return _visibilityState == VisibilityState.visible;
}

void visibilityChange(VisibilityState state) {
_visibilityState = state;
ownerDocument.dispatchEvent(Event('visibilitychange'));
}

void _dispatchReadyStateChangeEvent() {
Event event = Event(EVENT_READY_STATE_CHANGE);
defaultView.dispatchEvent(event);
Expand Down Expand Up @@ -313,6 +330,7 @@ class Document extends ContainerNode {
// Init with viewport size.
element.renderStyle.width = CSSLengthValue(viewport.viewportSize.width, CSSLengthType.PX);
element.renderStyle.height = CSSLengthValue(viewport.viewportSize.height, CSSLengthType.PX);
_visibilityState = VisibilityState.visible;
} else {
// Detach document element.
viewport.removeAll();
Expand Down Expand Up @@ -459,4 +477,5 @@ class Document extends ContainerNode {
cookie.clearCookie();
super.dispose();
}

}
22 changes: 13 additions & 9 deletions webf/lib/src/launcher/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -617,17 +617,26 @@ class WebFViewController implements WidgetsBindingObserver, ElementsBindingObser

@override
void didChangeAccessibilityFeatures() {
// TODO: implement didChangeAccessibilityFeatures
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// TODO: implement didChangeAppLifecycleState
switch (state) {
case AppLifecycleState.resumed:
document.visibilityChange(VisibilityState.visible);
break;
case AppLifecycleState.paused:
document.visibilityChange(VisibilityState.hidden);
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.detached:
break;
}
}

@override
void didChangeLocales(List<Locale>? locales) {
// TODO: implement didChangeLocales
}

ui.WindowPadding _prevViewInsets = ui.window.viewInsets;
Expand Down Expand Up @@ -666,34 +675,29 @@ class WebFViewController implements WidgetsBindingObserver, ElementsBindingObser

@override
void didChangePlatformBrightness() {
// TODO: implement didChangePlatformBrightness
}

@override
void didChangeTextScaleFactor() {
// TODO: implement didChangeTextScaleFactor
}

@override
void didHaveMemoryPressure() {
// TODO: implement didHaveMemoryPressure
}

@override
Future<bool> didPopRoute() async {
// TODO: implement didPopRoute

return false;
}

@override
Future<bool> didPushRoute(String route) async {
// TODO: implement didPushRoute
return false;
}

@override
Future<bool> didPushRouteInformation(RouteInformation routeInformation) async {
// TODO: implement didPushRouteInformation
return false;
}
}
Expand Down
Loading