mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Add setInitialRoute on FlutterView. (flutter/engine#3727)
This commit is contained in:
parent
d4248e2e6c
commit
0dd69f7b07
@ -40,25 +40,7 @@ void _updateSemanticsEnabled(bool enabled) {
|
||||
window.onSemanticsEnabledChanged();
|
||||
}
|
||||
|
||||
void _handleNavigationMessage(ByteData data) {
|
||||
if (window._defaultRouteName != null)
|
||||
return;
|
||||
try {
|
||||
final dynamic message = _decodeJSON(_decodeUTF8(data));
|
||||
final dynamic method = message['method'];
|
||||
if (method != 'pushRoute')
|
||||
return;
|
||||
final dynamic args = message['args'];
|
||||
window._defaultRouteName = args[0];
|
||||
} catch (e) {
|
||||
// We ignore any exception and just let the message be dispatched as usual.
|
||||
}
|
||||
}
|
||||
|
||||
void _dispatchPlatformMessage(String name, ByteData data, int responseId) {
|
||||
if (name == 'flutter/navigation')
|
||||
_handleNavigationMessage(data);
|
||||
|
||||
if (window.onPlatformMessage != null) {
|
||||
window.onPlatformMessage(name, data, (ByteData responseData) {
|
||||
window._respondToPlatformMessage(responseId, responseData);
|
||||
|
||||
@ -275,8 +275,8 @@ class Window {
|
||||
|
||||
/// The route or path that the operating system requested when the application
|
||||
/// was launched.
|
||||
String get defaultRouteName => _defaultRouteName;
|
||||
String _defaultRouteName;
|
||||
String get defaultRouteName => _defaultRouteName();
|
||||
String _defaultRouteName() native "Window_defaultRouteName";
|
||||
|
||||
/// Requests that, at the next appropriate opportunity, the [onBeginFrame]
|
||||
/// and [onDrawFrame] callbacks be invoked.
|
||||
|
||||
@ -39,6 +39,11 @@ Dart_Handle ToByteData(const std::vector<uint8_t>& buffer) {
|
||||
return data_handle;
|
||||
}
|
||||
|
||||
void DefaultRouteName(Dart_NativeArguments args) {
|
||||
std::string routeName = UIDartState::Current()->window()->client()->DefaultRouteName();
|
||||
Dart_SetReturnValue(args, StdStringToDart(routeName));
|
||||
}
|
||||
|
||||
void ScheduleFrame(Dart_NativeArguments args) {
|
||||
UIDartState::Current()->window()->client()->ScheduleFrame();
|
||||
}
|
||||
@ -253,6 +258,7 @@ void Window::CompletePlatformMessageResponse(int response_id,
|
||||
|
||||
void Window::RegisterNatives(tonic::DartLibraryNatives* natives) {
|
||||
natives->Register({
|
||||
{"Window_defaultRouteName", DefaultRouteName, 1, true},
|
||||
{"Window_scheduleFrame", ScheduleFrame, 1, true},
|
||||
{"Window_sendPlatformMessage", _SendPlatformMessage, 4, true},
|
||||
{"Window_respondToPlatformMessage", _RespondToPlatformMessage, 3, true},
|
||||
|
||||
@ -23,6 +23,7 @@ class Scene;
|
||||
|
||||
class WindowClient {
|
||||
public:
|
||||
virtual std::string DefaultRouteName() = 0;
|
||||
virtual void ScheduleFrame() = 0;
|
||||
virtual void Render(Scene* scene) = 0;
|
||||
virtual void UpdateSemantics(SemanticsUpdate* update) = 0;
|
||||
|
||||
@ -98,6 +98,10 @@ Window* RuntimeController::GetWindow() {
|
||||
return dart_controller_->dart_state()->window();
|
||||
}
|
||||
|
||||
std::string RuntimeController::DefaultRouteName() {
|
||||
return client_->DefaultRouteName();
|
||||
}
|
||||
|
||||
void RuntimeController::ScheduleFrame() {
|
||||
client_->ScheduleFrame();
|
||||
}
|
||||
|
||||
@ -52,6 +52,7 @@ class RuntimeController : public WindowClient, public IsolateClient {
|
||||
|
||||
Window* GetWindow();
|
||||
|
||||
std::string DefaultRouteName() override;
|
||||
void ScheduleFrame() override;
|
||||
void Render(Scene* scene) override;
|
||||
void UpdateSemantics(SemanticsUpdate* update) override;
|
||||
|
||||
@ -17,6 +17,7 @@ namespace blink {
|
||||
|
||||
class RuntimeDelegate {
|
||||
public:
|
||||
virtual std::string DefaultRouteName() = 0;
|
||||
virtual void ScheduleFrame() = 0;
|
||||
virtual void Render(std::unique_ptr<flow::LayerTree> layer_tree) = 0;
|
||||
virtual void UpdateSemantics(std::vector<SemanticsNode> update) = 0;
|
||||
|
||||
@ -310,7 +310,7 @@ void Engine::DispatchPlatformMessage(
|
||||
return;
|
||||
}
|
||||
|
||||
// If there's no runtime_, we need to buffer some navigation messages.
|
||||
// If there's no runtime_, we may still need to set the initial route.
|
||||
if (message->channel() == kNavigationChannel)
|
||||
HandleNavigationPlatformMessage(std::move(message));
|
||||
}
|
||||
@ -341,10 +341,10 @@ bool Engine::HandleNavigationPlatformMessage(
|
||||
return false;
|
||||
auto root = document.GetObject();
|
||||
auto method = root.FindMember("method");
|
||||
if (method == root.MemberEnd() || method->value != "pushRoute")
|
||||
if (method->value != "setInitialRoute")
|
||||
return false;
|
||||
|
||||
pending_push_route_message_ = std::move(message);
|
||||
auto route = root.FindMember("args");
|
||||
initial_route_ = std::move(route->value.GetString());
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -427,8 +427,6 @@ void Engine::ConfigureRuntime(const std::string& script_uri) {
|
||||
runtime_->SetViewportMetrics(viewport_metrics_);
|
||||
runtime_->SetLocale(language_code_, country_code_);
|
||||
runtime_->SetSemanticsEnabled(semantics_enabled_);
|
||||
if (pending_push_route_message_)
|
||||
runtime_->DispatchPlatformMessage(std::move(pending_push_route_message_));
|
||||
}
|
||||
|
||||
void Engine::DidCreateMainIsolate(Dart_Isolate isolate) {
|
||||
@ -450,6 +448,13 @@ void Engine::StartAnimatorIfPossible() {
|
||||
animator_->Start();
|
||||
}
|
||||
|
||||
std::string Engine::DefaultRouteName() {
|
||||
if (!initial_route_.empty()) {
|
||||
return initial_route_;
|
||||
}
|
||||
return "/";
|
||||
}
|
||||
|
||||
void Engine::ScheduleFrame() {
|
||||
animator_->RequestFrame();
|
||||
}
|
||||
|
||||
@ -71,6 +71,7 @@ class Engine : public blink::RuntimeDelegate {
|
||||
|
||||
private:
|
||||
// RuntimeDelegate methods:
|
||||
std::string DefaultRouteName() override;
|
||||
void ScheduleFrame() override;
|
||||
void Render(std::unique_ptr<flow::LayerTree> layer_tree) override;
|
||||
void UpdateSemantics(std::vector<blink::SemanticsNode> update) override;
|
||||
@ -98,7 +99,7 @@ class Engine : public blink::RuntimeDelegate {
|
||||
std::unique_ptr<Animator> animator_;
|
||||
std::unique_ptr<blink::RuntimeController> runtime_;
|
||||
tonic::DartErrorHandleType load_script_error_;
|
||||
ftl::RefPtr<blink::PlatformMessage> pending_push_route_message_;
|
||||
std::string initial_route_;
|
||||
blink::ViewportMetrics viewport_metrics_;
|
||||
std::string language_code_;
|
||||
std::string country_code_;
|
||||
|
||||
@ -241,6 +241,10 @@ public class FlutterView extends SurfaceView
|
||||
mFlutterSystemChannel.send(message);
|
||||
}
|
||||
|
||||
public void setInitialRoute(String route) {
|
||||
mFlutterNavigationChannel.invokeMethod("setInitialRoute", route);
|
||||
}
|
||||
|
||||
public void pushRoute(String route) {
|
||||
mFlutterNavigationChannel.invokeMethod("pushRoute", route);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user