Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 25634c6

Browse files
committed
Merge Changes
1 parent 282e7ae commit 25634c6

File tree

6 files changed

+117
-137
lines changed

6 files changed

+117
-137
lines changed

lib/ui/platform_dispatcher.dart

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,19 +593,34 @@ class PlatformDispatcher {
593593
_invoke(onLocaleChanged, _onLocaleChangedZone);
594594
}
595595

596-
597-
/// The locale that the platform's native locale resolution system resolves
598-
/// to.
596+
/// Performs the platform-native locale resolution.
599597
///
600-
/// This value may differ between platforms and is meant to allow Flutter's
601-
/// locale resolution algorithms access to a locale that is consistent with
602-
/// other apps on the device. Using this property is optional.
598+
/// Each platform may return different results.
603599
///
604-
/// This value may be used in a custom [localeListResolutionCallback] or used
605-
/// directly in order to arrive at the most appropriate locale for the app.
600+
/// If the platform fails to resolve a locale, then this will return null.
606601
///
607-
/// See [locales], which is the list of locales the user/device prefers.
608-
Locale? get platformResolvedLocale => configuration.platformResolvedLocale;
602+
/// This method returns synchronously and is a direct call to
603+
/// platform specific APIs without invoking method channels.
604+
Locale? computePlatformResolvedLocale(List<Locale> supportedLocales) {
605+
final List<String?> supportedLocalesData = <String?>[];
606+
for (Locale locale in supportedLocales) {
607+
supportedLocalesData.add(locale.languageCode);
608+
supportedLocalesData.add(locale.countryCode);
609+
supportedLocalesData.add(locale.scriptCode);
610+
}
611+
612+
final List<String> result = _computePlatformResolvedLocale(supportedLocalesData);
613+
614+
if (result.isNotEmpty) {
615+
return Locale.fromSubtags(
616+
languageCode: result[0],
617+
countryCode: result[1] == '' ? null : result[1],
618+
scriptCode: result[2] == '' ? null : result[2]);
619+
}
620+
return null;
621+
}
622+
List<String> _computePlatformResolvedLocale(List<String?> supportedLocalesData) native 'Window_computePlatformResolvedLocale';
623+
609624

610625
/// A callback that is invoked whenever [locale] changes value.
611626
///

lib/ui/window.dart

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ enum FramePhase {
7474
/// [SchedulerBinding.addTimingsCallback] to get this. It's preferred over using
7575
/// [PlatformDispatcher.onReportTimings] directly because
7676
/// [SchedulerBinding.addTimingsCallback] allows multiple callbacks. If
77-
/// [SchedulerBinding] is unavailable, then see [PlatformDispatcher.onReportTimings]
78-
/// for how to get this.
77+
/// [SchedulerBinding] is unavailable, then see
78+
/// [PlatformDispatcher.onReportTimings] for how to get this.
7979
///
8080
/// The metrics in debug mode (`flutter run` without any flags) may be very
8181
/// different from those in profile and release modes due to the debug overhead.
@@ -88,7 +88,8 @@ class FrameTiming {
8888
/// [FramePhase.values].
8989
///
9090
/// This constructor is usually only called by the Flutter engine, or a test.
91-
/// To get the [FrameTiming] of your app, see [PlatformDispatcher.onReportTimings].
91+
/// To get the [FrameTiming] of your app, see
92+
/// [PlatformDispatcher.onReportTimings].
9293
FrameTiming(List<int> timestamps)
9394
: assert(timestamps.length == FramePhase.values.length),
9495
_timestamps = timestamps;
@@ -101,9 +102,10 @@ class FrameTiming {
101102

102103
/// The duration to build the frame on the UI thread.
103104
///
104-
/// The build starts approximately when [PlatformDispatcher.onBeginFrame] is called. The
105-
/// [Duration] in the [PlatformDispatcher.onBeginFrame] callback is exactly the
106-
/// `Duration(microseconds: timestampInMicroseconds(FramePhase.buildStart))`.
105+
/// The build starts approximately when [PlatformDispatcher.onBeginFrame] is
106+
/// called. The [Duration] in the [PlatformDispatcher.onBeginFrame] callback
107+
/// is exactly the `Duration(microseconds:
108+
/// timestampInMicroseconds(FramePhase.buildStart))`.
107109
///
108110
/// The build finishes when [FlutterView.render] is called.
109111
///
@@ -674,9 +676,8 @@ abstract class FlutterView {
674676
///
675677
/// When this property changes, [onMetricsChanged] is called.
676678
///
677-
/// The relationship between this [viewInsets],
678-
/// [viewPadding], and [padding] are described in
679-
/// more detail in the documentation for [FlutterView].
679+
/// The relationship between this [viewInsets], [viewPadding], and [padding]
680+
/// are described in more detail in the documentation for [FlutterView].
680681
///
681682
/// See also:
682683
///
@@ -693,16 +694,14 @@ abstract class FlutterView {
693694
/// the display (e.g. overscan regions on television screens or phone sensor
694695
/// housings).
695696
///
696-
/// Unlike [padding], this value does not change relative to
697-
/// [viewInsets]. For example, on an iPhone X, it will not
698-
/// change in response to the soft keyboard being visible or hidden, whereas
699-
/// [padding] will.
697+
/// Unlike [padding], this value does not change relative to [viewInsets]. For
698+
/// example, on an iPhone X, it will not change in response to the soft
699+
/// keyboard being visible or hidden, whereas [padding] will.
700700
///
701701
/// When this property changes, [onMetricsChanged] is called.
702702
///
703-
/// The relationship between this [viewInsets],
704-
/// [viewPadding], and [padding] are described in
705-
/// more detail in the documentation for [FlutterView].
703+
/// The relationship between this [viewInsets], [viewPadding], and [padding]
704+
/// are described in more detail in the documentation for [FlutterView].
706705
///
707706
/// See also:
708707
///
@@ -756,35 +755,6 @@ abstract class FlutterView {
756755
/// * [Scaffold], which automatically applies the padding in material design
757756
/// applications.
758757
WindowPadding get padding => viewConfiguration.padding;
759-
760-
/// Updates the view's rendering on the GPU with the newly provided
761-
/// [Scene].
762-
///
763-
/// This function must be called within the scope of the
764-
/// [PlatformDispatcher.onBeginFrame] or [PlatformDispatcher.onDrawFrame]
765-
/// callbacks being invoked. If this function is called a second time during a
766-
/// single [PlatformDispatcher.onBeginFrame]/[PlatformDispatcher.onDrawFrame]
767-
/// callback sequence or called outside the scope of those callbacks, the call
768-
/// will be ignored.
769-
///
770-
/// To record graphical operations, first create a [PictureRecorder], then
771-
/// construct a [Canvas], passing that [PictureRecorder] to its constructor.
772-
/// After issuing all the graphical operations, call the
773-
/// [PictureRecorder.endRecording] function on the [PictureRecorder] to obtain
774-
/// the final [Picture] that represents the issued graphical operations.
775-
///
776-
/// Next, create a [SceneBuilder], and add the [Picture] to it using
777-
/// [SceneBuilder.addPicture]. With the [SceneBuilder.build] method you can
778-
/// then obtain a [Scene] object, which you can display to the user via this
779-
/// [render] function.
780-
///
781-
/// See also:
782-
///
783-
/// * [SchedulerBinding], the Flutter framework class which manages the
784-
/// scheduling of frames.
785-
/// * [RendererBinding], the Flutter framework class which manages layout and
786-
/// painting.
787-
void render(Scene scene) => platformDispatcher.render(scene, this);
788758
}
789759

790760
/// A top-level platform window displaying a Flutter layer tree drawn from a
@@ -896,24 +866,8 @@ class SingletonFlutterWindow extends FlutterWindow {
896866
/// This method returns synchronously and is a direct call to
897867
/// platform specific APIs without invoking method channels.
898868
Locale? computePlatformResolvedLocale(List<Locale> supportedLocales) {
899-
final List<String?> supportedLocalesData = <String?>[];
900-
for (Locale locale in supportedLocales) {
901-
supportedLocalesData.add(locale.languageCode);
902-
supportedLocalesData.add(locale.countryCode);
903-
supportedLocalesData.add(locale.scriptCode);
904-
}
905-
906-
final List<String> result = _computePlatformResolvedLocale(supportedLocalesData);
907-
908-
if (result.isNotEmpty) {
909-
return Locale.fromSubtags(
910-
languageCode: result[0],
911-
countryCode: result[1] == '' ? null : result[1],
912-
scriptCode: result[2] == '' ? null : result[2]);
913-
}
914-
return null;
869+
platformDispatcher.computePlatformResolvedLocale(supportedLocales);
915870
}
916-
List<String> _computePlatformResolvedLocale(List<String?> supportedLocalesData) native 'Window_computePlatformResolvedLocale';
917871

918872
/// A callback that is invoked whenever [locale] changes value.
919873
///
@@ -1149,6 +1103,34 @@ class SingletonFlutterWindow extends FlutterWindow {
11491103
/// scheduling of frames.
11501104
void scheduleFrame() => platformDispatcher.scheduleFrame();
11511105

1106+
/// Updates the view's rendering on the GPU with the newly provided [Scene].
1107+
///
1108+
/// This function must be called within the scope of the
1109+
/// [PlatformDispatcher.onBeginFrame] or [PlatformDispatcher.onDrawFrame]
1110+
/// callbacks being invoked. If this function is called a second time during a
1111+
/// single [PlatformDispatcher.onBeginFrame]/[PlatformDispatcher.onDrawFrame]
1112+
/// callback sequence or called outside the scope of those callbacks, the call
1113+
/// will be ignored.
1114+
///
1115+
/// To record graphical operations, first create a [PictureRecorder], then
1116+
/// construct a [Canvas], passing that [PictureRecorder] to its constructor.
1117+
/// After issuing all the graphical operations, call the
1118+
/// [PictureRecorder.endRecording] function on the [PictureRecorder] to obtain
1119+
/// the final [Picture] that represents the issued graphical operations.
1120+
///
1121+
/// Next, create a [SceneBuilder], and add the [Picture] to it using
1122+
/// [SceneBuilder.addPicture]. With the [SceneBuilder.build] method you can
1123+
/// then obtain a [Scene] object, which you can display to the user via this
1124+
/// [render] function.
1125+
///
1126+
/// See also:
1127+
///
1128+
/// * [SchedulerBinding], the Flutter framework class which manages the
1129+
/// scheduling of frames.
1130+
/// * [RendererBinding], the Flutter framework class which manages layout and
1131+
/// painting.
1132+
void render(Scene scene) => platformDispatcher.render(scene, this);
1133+
11521134
/// Whether the user has requested that [updateSemantics] be called when
11531135
/// the semantic contents of window changes.
11541136
///

lib/ui/window/platform_configuration.cc

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
#include "third_party/tonic/converter/dart_converter.h"
1313
#include "third_party/tonic/dart_args.h"
1414
#include "third_party/tonic/dart_library_natives.h"
15+
#include "third_party/tonic/dart_microtask_queue.h"
1516
#include "third_party/tonic/logging/dart_invoke.h"
1617
#include "third_party/tonic/typed_data/dart_byte_data.h"
1718

1819
namespace flutter {
19-
2020
namespace {
2121

2222
void InitialRouteName(Dart_NativeArguments args) {
@@ -38,7 +38,6 @@ void Render(Dart_NativeArguments args) {
3838
Dart_Handle exception = nullptr;
3939
Scene* scene =
4040
tonic::DartConverter<Scene*>::FromArguments(args, 1, exception);
41-
// The view argument is currently ignored.
4241
if (exception) {
4342
Dart_ThrowException(exception);
4443
return;
@@ -180,25 +179,12 @@ void GetPersistentIsolateData(Dart_NativeArguments args) {
180179
persistent_isolate_data->GetSize()));
181180
}
182181

183-
} // namespace
184-
185182
Dart_Handle ToByteData(const std::vector<uint8_t>& buffer) {
186-
Dart_Handle data_handle =
187-
Dart_NewTypedData(Dart_TypedData_kByteData, buffer.size());
188-
if (Dart_IsError(data_handle))
189-
return data_handle;
190-
191-
Dart_TypedData_Type type;
192-
void* data = nullptr;
193-
intptr_t num_bytes = 0;
194-
FML_CHECK(!Dart_IsError(
195-
Dart_TypedDataAcquireData(data_handle, &type, &data, &num_bytes)));
196-
197-
memcpy(data, buffer.data(), num_bytes);
198-
Dart_TypedDataReleaseData(data_handle);
199-
return data_handle;
183+
return tonic::DartByteData::Create(buffer.data(), buffer.size());
200184
}
201185

186+
} // namespace
187+
202188
WindowClient::~WindowClient() {}
203189

204190
PlatformConfiguration::PlatformConfiguration(WindowClient* client)
@@ -234,19 +220,6 @@ void PlatformConfiguration::UpdateLocales(
234220
}));
235221
}
236222

237-
void PlatformConfiguration::UpdatePlatformResolvedLocale(
238-
const std::vector<std::string>& locale) {
239-
std::shared_ptr<tonic::DartState> dart_state = library_.dart_state().lock();
240-
if (!dart_state)
241-
return;
242-
tonic::DartState::Scope scope(dart_state);
243-
tonic::LogIfError(tonic::DartInvokeField(
244-
library_.value(), "_updatePlatformResolvedLocale",
245-
{
246-
tonic::ToDart<std::vector<std::string>>(locale),
247-
}));
248-
}
249-
250223
void PlatformConfiguration::UpdateUserSettingsData(const std::string& data) {
251224
std::shared_ptr<tonic::DartState> dart_state = library_.dart_state().lock();
252225
if (!dart_state)
@@ -426,6 +399,27 @@ void PlatformConfiguration::CompletePlatformMessageResponse(
426399
response->Complete(std::make_unique<fml::DataMapping>(std::move(data)));
427400
}
428401

402+
Dart_Handle ComputePlatformResolvedLocale(Dart_Handle supportedLocalesHandle) {
403+
std::vector<std::string> supportedLocales =
404+
tonic::DartConverter<std::vector<std::string>>::FromDart(
405+
supportedLocalesHandle);
406+
407+
std::vector<std::string> results =
408+
*UIDartState::Current()
409+
->window()
410+
->client()
411+
->ComputePlatformResolvedLocale(supportedLocales);
412+
413+
return tonic::DartConverter<std::vector<std::string>>::ToDart(results);
414+
}
415+
416+
static void _ComputePlatformResolvedLocale(Dart_NativeArguments args) {
417+
UIDartState::ThrowIfUIOperationsProhibited();
418+
Dart_Handle result =
419+
ComputePlatformResolvedLocale(Dart_GetNativeArgument(args, 1));
420+
Dart_SetReturnValue(args, result);
421+
}
422+
429423
void PlatformConfiguration::RegisterNatives(
430424
tonic::DartLibraryNatives* natives) {
431425
natives->Register({
@@ -445,6 +439,8 @@ void PlatformConfiguration::RegisterNatives(
445439
true},
446440
{"PlatformConfiguration_getPersistentIsolateData",
447441
GetPersistentIsolateData, 1, true},
442+
{"PlatformConfiguration_computePlatformResolvedLocale", _ComputePlatformResolvedLocale,
443+
2, true},
448444
});
449445
}
450446

lib/ui/window/platform_configuration.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "flutter/fml/time/time_point.h"
1414
#include "flutter/lib/ui/semantics/semantics_update.h"
15+
#include "flutter/lib/ui/window/platform_message.h"
1516
#include "flutter/lib/ui/window/pointer_data_packet.h"
1617
#include "flutter/lib/ui/window/screen.h"
1718
#include "flutter/lib/ui/window/viewport_metrics.h"
@@ -37,8 +38,6 @@ class FontCollection;
3738
class PlatformMessage;
3839
class Scene;
3940

40-
Dart_Handle ToByteData(const std::vector<uint8_t>& buffer);
41-
4241
// Must match the AccessibilityFeatureFlag enum in framework.
4342
enum class AccessibilityFeatureFlag : int32_t {
4443
kAccessibleNavigation = 1 << 0,
@@ -63,6 +62,9 @@ class WindowClient {
6362
int64_t isolate_port) = 0;
6463
virtual void SetNeedsReportTimings(bool value) = 0;
6564
virtual std::shared_ptr<const fml::Mapping> GetPersistentIsolateData() = 0;
65+
virtual std::unique_ptr<std::vector<std::string>>
66+
ComputePlatformResolvedLocale(
67+
const std::vector<std::string>& supported_locale_data) = 0;
6668

6769
protected:
6870
virtual ~WindowClient();
@@ -78,7 +80,6 @@ class PlatformConfiguration final {
7880

7981
void DidCreateIsolate();
8082
void UpdateLocales(const std::vector<std::string>& locales);
81-
void UpdatePlatformResolvedLocale(const std::vector<std::string>& locale);
8283
void UpdateUserSettingsData(const std::string& data);
8384
void UpdateLifecycleState(const std::string& data);
8485
void UpdateSemanticsEnabled(bool enabled);
@@ -101,9 +102,10 @@ class PlatformConfiguration final {
101102
Screen* get_screen(int screen_id) { return screens_[screen_id].get(); }
102103

103104
private:
105+
WindowClient* client_;
104106
tonic::DartPersistentValue library_;
105107
ViewportMetrics viewport_metrics_;
106-
WindowClient* client_;
108+
107109
std::unordered_map<int, std::unique_ptr<Window>> windows_;
108110
std::unordered_map<int, std::unique_ptr<Screen>> screens_;
109111

0 commit comments

Comments
 (0)