Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
KeyboardInsetsService.cs
1using System;
2using Microsoft.Maui.ApplicationModel;
5
7{
11 public sealed class KeyboardInsetsService : IKeyboardInsetsService, IDisposable
12 {
13 private readonly IPlatformSpecific platformSpecific;
14 private double keyboardHeight;
15 private bool isKeyboardVisible;
16 private bool isDisposed;
17
23 {
24 this.platformSpecific = platformSpecific ?? throw new ArgumentNullException(nameof(platformSpecific));
25 this.platformSpecific.KeyboardSizeChanged += this.OnKeyboardSizeChanged;
26 this.platformSpecific.KeyboardHidden += this.OnKeyboardHidden;
27 }
28
30 public event EventHandler<KeyboardInsetChangedEventArgs>? KeyboardInsetChanged;
31
33 public double KeyboardHeight => this.keyboardHeight;
34
36 public bool IsKeyboardVisible => this.isKeyboardVisible;
37
41 public void Dispose()
42 {
43 if (this.isDisposed)
44 return;
45
46 this.platformSpecific.KeyboardSizeChanged -= this.OnKeyboardSizeChanged;
47 this.platformSpecific.KeyboardHidden -= this.OnKeyboardHidden;
48 this.isDisposed = true;
49 }
50
51 private void OnKeyboardSizeChanged(object? sender, KeyboardSizeMessage message)
52 {
53 double newHeight = Math.Max(0, message.KeyboardSize);
54 bool newVisibility = newHeight > 0.5;
55
56 if (Math.Abs(newHeight - this.keyboardHeight) < 0.5 && newVisibility == this.isKeyboardVisible)
57 return;
58
59 this.keyboardHeight = newHeight;
60 this.isKeyboardVisible = newVisibility;
61 this.RaiseChanged();
62 }
63
64 private void OnKeyboardHidden(object? sender, KeyboardSizeMessage message)
65 {
66 if (this.keyboardHeight <= 0 && !this.isKeyboardVisible)
67 return;
68
69 this.keyboardHeight = 0;
70 this.isKeyboardVisible = false;
71 this.RaiseChanged();
72 }
73
74 private void RaiseChanged()
75 {
76 void Raise()
77 {
78 this.KeyboardInsetChanged?.Invoke(this, new KeyboardInsetChangedEventArgs(this.keyboardHeight, this.isKeyboardVisible));
79 }
80
81 if (MainThread.IsMainThread)
82 Raise();
83 else
84 MainThread.BeginInvokeOnMainThread(Raise);
85 }
86 }
87}
Default implementation of IKeyboardInsetsService that normalizes platform keyboard measurements.
bool IsKeyboardVisible
Gets a value indicating whether the software keyboard is currently visible.
KeyboardInsetsService(IPlatformSpecific platformSpecific)
Initializes a new instance of the KeyboardInsetsService class.
EventHandler< KeyboardInsetChangedEventArgs >? KeyboardInsetChanged
double KeyboardHeight
Gets the current keyboard height in device-independent units.
void Dispose()
Disposes the instance and detaches event handlers.
Interface for platform-specific functions.
Provides normalized keyboard inset information for UI components.
class KeyboardSizeMessage(float KeyboardSize)
Keyboard size change message
Definition: Messages.cs:32