use super::*;
pub struct NavigationCompletedArgs(pub(crate) ICoreWebView2NavigationCompletedEventArgs);
impl NavigationCompletedArgs {
pub fn is_success(&self) -> bool {
unsafe { self.0.IsSuccess() }.is_ok_and(|value| value.as_bool())
}
pub fn navigation_id(&self) -> u64 {
unsafe { self.0.NavigationId() }.unwrap_or(0)
}
}
pub struct NavigationStartingArgs(pub(crate) ICoreWebView2NavigationStartingEventArgs);
impl NavigationStartingArgs {
pub fn uri(&self) -> String {
unsafe { string::take_result(self.0.Uri()) }
}
pub fn is_user_initiated(&self) -> bool {
unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
}
pub fn is_redirected(&self) -> bool {
unsafe { self.0.IsRedirected() }.is_ok_and(|value| value.as_bool())
}
pub fn navigation_id(&self) -> u64 {
unsafe { self.0.NavigationId() }.unwrap_or(0)
}
pub fn is_cancelled(&self) -> bool {
unsafe { self.0.Cancel() }.is_ok_and(|value| value.as_bool())
}
pub fn set_cancel(&self, cancel: bool) -> Result<()> {
unsafe { self.0.SetCancel(cancel) }.ok()
}
}
pub struct WebMessageReceivedArgs(pub(crate) ICoreWebView2WebMessageReceivedEventArgs);
impl WebMessageReceivedArgs {
pub fn source(&self) -> String {
unsafe { string::take_result(self.0.Source()) }
}
pub fn web_message_as_json(&self) -> String {
unsafe { string::take_result(self.0.WebMessageAsJson()) }
}
pub fn try_web_message_as_string(&self) -> Result<String> {
let value = unsafe { self.0.TryGetWebMessageAsString()? };
Ok(unsafe { string::take(value) })
}
}
pub struct ContentLoadingArgs(pub(crate) ICoreWebView2ContentLoadingEventArgs);
impl ContentLoadingArgs {
pub fn is_error_page(&self) -> bool {
unsafe { self.0.IsErrorPage() }.is_ok_and(|value| value.as_bool())
}
pub fn navigation_id(&self) -> u64 {
unsafe { self.0.NavigationId() }.unwrap_or(0)
}
}
pub struct NewWindowRequestedArgs(pub(crate) ICoreWebView2NewWindowRequestedEventArgs);
impl NewWindowRequestedArgs {
pub fn uri(&self) -> String {
unsafe { string::take_result(self.0.Uri()) }
}
pub fn is_user_initiated(&self) -> bool {
unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
}
pub fn is_handled(&self) -> bool {
unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
}
pub fn set_handled(&self, handled: bool) -> Result<()> {
unsafe { self.0.SetHandled(handled) }.ok()
}
pub fn set_new_window(&self, webview: &WebView) -> Result<()> {
unsafe { self.0.SetNewWindow(&webview.0) }.ok()
}
pub fn defer(&self) -> Result<Deferral> {
Ok(Deferral::new(unsafe { self.0.GetDeferral()? }))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PermissionKind {
Unknown,
Microphone,
Camera,
Geolocation,
Notifications,
OtherSensors,
ClipboardRead,
MultipleAutomaticDownloads,
FileReadWrite,
Autoplay,
LocalFonts,
MidiSystemExclusiveMessages,
WindowManagement,
}
impl PermissionKind {
fn from_raw(value: COREWEBVIEW2_PERMISSION_KIND) -> Self {
match value {
1 => Self::Microphone,
2 => Self::Camera,
3 => Self::Geolocation,
4 => Self::Notifications,
5 => Self::OtherSensors,
6 => Self::ClipboardRead,
7 => Self::MultipleAutomaticDownloads,
8 => Self::FileReadWrite,
9 => Self::Autoplay,
10 => Self::LocalFonts,
11 => Self::MidiSystemExclusiveMessages,
12 => Self::WindowManagement,
_ => Self::Unknown,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PermissionState {
Default,
Allow,
Deny,
}
impl PermissionState {
fn from_raw(value: COREWEBVIEW2_PERMISSION_STATE) -> Self {
match value {
1 => Self::Allow,
2 => Self::Deny,
_ => Self::Default,
}
}
fn to_raw(self) -> COREWEBVIEW2_PERMISSION_STATE {
match self {
Self::Default => 0,
Self::Allow => 1,
Self::Deny => 2,
}
}
}
pub struct PermissionRequestedArgs(pub(crate) ICoreWebView2PermissionRequestedEventArgs);
impl PermissionRequestedArgs {
pub fn uri(&self) -> String {
unsafe { string::take_result(self.0.Uri()) }
}
pub fn kind(&self) -> PermissionKind {
unsafe { self.0.PermissionKind() }.map_or(PermissionKind::Unknown, PermissionKind::from_raw)
}
pub fn is_user_initiated(&self) -> bool {
unsafe { self.0.IsUserInitiated() }.is_ok_and(|value| value.as_bool())
}
pub fn state(&self) -> PermissionState {
unsafe { self.0.State() }.map_or(PermissionState::Default, PermissionState::from_raw)
}
pub fn set_state(&self, state: PermissionState) -> Result<()> {
unsafe { self.0.SetState(state.to_raw()) }.ok()
}
pub fn defer(&self) -> Result<Deferral> {
Ok(Deferral::new(unsafe { self.0.GetDeferral()? }))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ProcessFailedKind {
Unknown,
BrowserProcessExited,
RenderProcessExited,
RenderProcessUnresponsive,
FrameRenderProcessExited,
UtilityProcessExited,
SandboxHelperProcessExited,
GpuProcessExited,
PpapiPluginProcessExited,
PpapiBrokerProcessExited,
UnknownProcessExited,
}
impl ProcessFailedKind {
fn from_raw(value: COREWEBVIEW2_PROCESS_FAILED_KIND) -> Self {
match value {
0 => Self::BrowserProcessExited,
1 => Self::RenderProcessExited,
2 => Self::RenderProcessUnresponsive,
3 => Self::FrameRenderProcessExited,
4 => Self::UtilityProcessExited,
5 => Self::SandboxHelperProcessExited,
6 => Self::GpuProcessExited,
7 => Self::PpapiPluginProcessExited,
8 => Self::PpapiBrokerProcessExited,
9 => Self::UnknownProcessExited,
_ => Self::Unknown,
}
}
}
pub struct ProcessFailedArgs(pub(crate) ICoreWebView2ProcessFailedEventArgs);
impl ProcessFailedArgs {
pub fn kind(&self) -> ProcessFailedKind {
unsafe { self.0.ProcessFailedKind() }
.map_or(ProcessFailedKind::Unknown, ProcessFailedKind::from_raw)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MoveFocusReason {
Programmatic,
Next,
Previous,
}
impl MoveFocusReason {
fn from_raw(value: COREWEBVIEW2_MOVE_FOCUS_REASON) -> Self {
match value {
1 => Self::Next,
2 => Self::Previous,
_ => Self::Programmatic,
}
}
pub(crate) fn to_raw(self) -> COREWEBVIEW2_MOVE_FOCUS_REASON {
match self {
Self::Programmatic => 0,
Self::Next => 1,
Self::Previous => 2,
}
}
}
pub struct MoveFocusRequestedArgs(pub(crate) ICoreWebView2MoveFocusRequestedEventArgs);
impl MoveFocusRequestedArgs {
pub fn reason(&self) -> MoveFocusReason {
unsafe { self.0.Reason() }.map_or(MoveFocusReason::Programmatic, MoveFocusReason::from_raw)
}
pub fn is_handled(&self) -> bool {
unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
}
pub fn set_handled(&self, handled: bool) -> Result<()> {
unsafe { self.0.SetHandled(handled) }.ok()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyEventKind {
KeyDown,
KeyUp,
SystemKeyDown,
SystemKeyUp,
}
impl KeyEventKind {
fn from_raw(value: COREWEBVIEW2_KEY_EVENT_KIND) -> Self {
match value {
1 => Self::KeyUp,
2 => Self::SystemKeyDown,
3 => Self::SystemKeyUp,
_ => Self::KeyDown,
}
}
}
pub struct AcceleratorKeyPressedArgs(pub(crate) ICoreWebView2AcceleratorKeyPressedEventArgs);
impl AcceleratorKeyPressedArgs {
pub fn key_event_kind(&self) -> KeyEventKind {
unsafe { self.0.KeyEventKind() }.map_or(KeyEventKind::KeyDown, KeyEventKind::from_raw)
}
pub fn virtual_key(&self) -> u32 {
unsafe { self.0.VirtualKey() }.unwrap_or(0)
}
pub fn is_handled(&self) -> bool {
unsafe { self.0.Handled() }.is_ok_and(|value| value.as_bool())
}
pub fn set_handled(&self, handled: bool) -> Result<()> {
unsafe { self.0.SetHandled(handled) }.ok()
}
}
pub struct DevToolsProtocolEventReceivedArgs(
pub(crate) ICoreWebView2DevToolsProtocolEventReceivedEventArgs,
);
impl DevToolsProtocolEventReceivedArgs {
pub fn parameter_object_as_json(&self) -> String {
unsafe { string::take_result(self.0.ParameterObjectAsJson()) }
}
}
#[must_use]
pub struct EventRegistration(Option<Box<dyn FnOnce()>>);
impl EventRegistration {
pub(crate) fn new<F: FnOnce() + 'static>(remove: F) -> Self {
Self(Some(Box::new(remove)))
}
pub fn remove(mut self) {
if let Some(remove) = self.0.take() {
remove();
}
}
}
impl Drop for EventRegistration {
fn drop(&mut self) {
if let Some(remove) = self.0.take() {
remove();
}
}
}