Skip to content

Commit 6dce173

Browse files
committed
🎨 Android
1 parent 2a7c680 commit 6dce173

File tree

11 files changed

+66
-21
lines changed

11 files changed

+66
-21
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git submodule update --init --recursive

src/Avalonia.WebView2.Sample.Mobile.Android/MainActivity.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,9 @@ namespace Avalonia.WebView2.Sample;
2222
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize | ConfigChanges.UiMode)]
2323
sealed class MainActivity : AvaloniaMainActivity<App>
2424
{
25-
/// <summary>
26-
/// 获取状态栏的高度
27-
/// </summary>
28-
/// <param name="ctx"></param>
29-
/// <returns></returns>
30-
static int GetStatusBarHeight(Context ctx)
25+
static int GetDimensionPixelSizeByAndroidDimen(Context ctx, string identifierName)
3126
{
32-
var resId = ctx.Resources!.GetIdentifier("status_bar_height", "dimen", "android");
27+
var resId = ctx.Resources!.GetIdentifier(identifierName, "dimen", "android");
3328
if (resId > 0)
3429
{
3530
var h = ctx.Resources.GetDimensionPixelSize(resId);
@@ -38,6 +33,20 @@ static int GetStatusBarHeight(Context ctx)
3833
return default;
3934
}
4035

36+
/// <summary>
37+
/// 获取顶部状态栏的高度
38+
/// </summary>
39+
/// <param name="ctx"></param>
40+
/// <returns></returns>
41+
static int GetStatusBarHeight(Context ctx) => GetDimensionPixelSizeByAndroidDimen(ctx, "status_bar_height");
42+
43+
/// <summary>
44+
/// 获取底部导航栏的高度
45+
/// </summary>
46+
/// <param name="ctx"></param>
47+
/// <returns></returns>
48+
static int GetNavigationBarHeight(Context ctx) => GetDimensionPixelSizeByAndroidDimen(ctx, "navigation_bar_height");
49+
4150
/// <summary>
4251
/// 获取 DPI 缩放比例
4352
/// </summary>
@@ -60,6 +69,23 @@ protected override void OnCreate(Bundle? savedInstanceState)
6069
MainViewModel.SetStatusBarMargin(top: statusBarHeight, scaling: scaling);
6170

6271
base.OnCreate(savedInstanceState);
72+
73+
Window!.DecorView.ApplyWindowInsets += OnApplyWindowInsets;
74+
}
75+
76+
WindowInsets OnApplyWindowInsets(View v, WindowInsets insets)
77+
{
78+
var insetsCompat = WindowInsetsCompat.ToWindowInsetsCompat(insets, v)!; // https://developer.android.google.cn/reference/androidx/core/view/WindowInsetsCompat#toWindowInsetsCompat(android.view.WindowInsets,android.view.View)
79+
// https://developer.android.google.cn/reference/androidx/core/view/WindowInsetsCompat#getInsetsIgnoringVisibility(int)
80+
// https://developer.android.google.cn/reference/androidx/core/view/WindowInsetsCompat.Type
81+
var navigationBars = insetsCompat.GetInsetsIgnoringVisibility(WindowInsetsCompat.Type.NavigationBars())!;
82+
var statusBars = insetsCompat.GetInsetsIgnoringVisibility(WindowInsetsCompat.Type.StatusBars())!;
83+
global::Android.Util.Log.Warn("WebView2",
84+
$"""
85+
NavigationBars: l={navigationBars.Left}, r={navigationBars.Right}, t={navigationBars.Top}, b={navigationBars.Bottom}
86+
StatusBars: l={statusBars.Left}, r={statusBars.Right}, t={statusBars.Top}, b={statusBars.Bottom}
87+
""");
88+
return insets;
6389
}
6490

6591
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)

src/Avalonia.WebView2.Sample.Mobile/Views/MainView.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public MainView()
1717
UrlTextBox.KeyDown += UrlTextBox_KeyDown;
1818

1919
WV.StorageService = this;
20-
WV.Fill = new SolidColorBrush(Colors.Purple);
20+
//WV.Fill = new SolidColorBrush(Colors.Purple);
2121
}
2222

2323
IEnumerable<KeyValuePair<(StorageItemType type, string key), StorageItemValue>>? IStorageService.GetStorages(string requestUri)

src/Avalonia.WebView2.Sample/WebView2Compat.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public WebView2Compat()
1818
#if WINDOWS
1919
WebView2.IsVisible = false;
2020
// 设置背景色透明
21-
WebView2.Fill = new SolidColorBrush(Colors.Transparent);
21+
//WebView2.Fill = new SolidColorBrush(Colors.Transparent);
2222
if (!IsSupported)
2323
{
2424
TextBlock.Text = "Couldn't find a compatible Webview2 Runtime installation to host WebViews.";

src/Avalonia.WebView2/Controls/WebView2.Properties/DefaultBackgroundColor.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public Color DefaultBackgroundColor
4343
SetDefaultBackgroundColor(this, value);
4444
#endif
4545

46-
var avaColor =
47-
global::Avalonia.Media.Color.FromArgb(value.A, value.R, value.G, value.B);
48-
if ((object?)this is Shape shape)
49-
{
50-
shape.Fill = new ImmutableSolidColorBrush(avaColor);
51-
}
46+
// var avaColor =
47+
//global::Avalonia.Media.Color.FromArgb(value.A, value.R, value.G, value.B);
48+
// if ((object?)this is Shape shape)
49+
// {
50+
// shape.Fill = new ImmutableSolidColorBrush(avaColor);
51+
// }
5252
}
5353
}
5454

src/Avalonia.WebView2/Controls/WebView2.SizeChanged.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#if !DISABLE_WEBVIEW2_CORE && (WINDOWS || NETFRAMEWORK)
2+
// 仅 Windows Edge WebView2 处理 OnSizeChanged 事件
3+
14
#if !DISABLE_WEBVIEW2_CORE && (WINDOWS || NETFRAMEWORK)
25
using Avalonia.Controls.Platforms.Windows.Interop;
36
#endif
@@ -213,4 +216,5 @@ protected virtual void OnWindowPositionChanged(Rectangle rectangle)
213216
nwv?.Frame = new CGRect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
214217
#endif
215218
}
216-
}
219+
}
220+
#endif

src/Avalonia.WebView2/Controls/WebView2.VisualTree.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
7474
{
7575
viewHandler = CreateViewHandler(this);
7676
var view = viewHandler.CreatePlatformView();
77-
SetValue(this);
77+
SetInitValue(view);
7878
Child = viewHandler;
7979
}
8080
#elif IOS || MACCATALYST || (MACOS && !USE_DEPRECATED_WEBVIEW)

src/Avalonia.WebView2/Controls/WebView2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ protected virtual void SetValue(IWebView2 webView)
132132

133133
}
134134

135-
partial class WebView2 : global::Avalonia.Controls.Shapes.Rectangle
135+
partial class WebView2 : global::Avalonia.Controls.Control
136136
{
137137
// 使用矩形控件作为占位符,监听布局矩阵坐标传递值给本机控件
138138
// 处理显示隐藏属性

src/Avalonia.WebView2/Platforms/Droid/Controls/WebView2.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,21 @@ public AWebView? AWebView
156156
}
157157
}
158158

159+
protected virtual void SetInitValue(AWebView webView)
160+
{
161+
var visibility = IsVisible ? ViewStates.Visible : ViewStates.Gone;
162+
if (webView.Visibility != visibility)
163+
{
164+
webView.Visibility = visibility;
165+
}
159166

167+
if (_source != null)
168+
{
169+
webView.SetSource(_source);
170+
}
171+
172+
// TODO: other properties
173+
}
160174

161175
/// <summary>
162176
/// 指示应用是否打算使用明文网络流量,如明文 HTTP

src/Avalonia.WebView2/Platforms/EdgeWebView2/Controls/WebView2.CoreWebView2.EventHandlers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void CoreWebView2_NewWindowRequested(object? sender, CoreWebView2NewWindowReques
169169
{
170170
Source = new Uri(args.Uri, UriKind.Absolute),
171171
//EnabledDevTools = coreWebView2.Settings.AreDevToolsEnabled,
172-
Fill = Fill,
172+
//Fill = Fill,
173173
//UserDataFolder = coreWebView2.Environment.UserDataFolder,
174174
},
175175
};

0 commit comments

Comments
 (0)