use super::*;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PreferredColorScheme {
Auto,
Light,
Dark,
}
impl PreferredColorScheme {
fn from_raw(value: COREWEBVIEW2_PREFERRED_COLOR_SCHEME) -> Self {
match value {
1 => Self::Light,
2 => Self::Dark,
_ => Self::Auto,
}
}
fn to_raw(self) -> COREWEBVIEW2_PREFERRED_COLOR_SCHEME {
match self {
Self::Auto => 0,
Self::Light => 1,
Self::Dark => 2,
}
}
}
pub struct Profile(pub(crate) ICoreWebView2Profile);
impl Profile {
pub fn name(&self) -> String {
unsafe { string::take_result(self.0.ProfileName()) }
}
pub fn is_in_private_mode(&self) -> bool {
unsafe { self.0.IsInPrivateModeEnabled() }.is_ok_and(|value| value.as_bool())
}
pub fn path(&self) -> String {
unsafe { string::take_result(self.0.ProfilePath()) }
}
pub fn default_download_folder_path(&self) -> String {
unsafe { string::take_result(self.0.DefaultDownloadFolderPath()) }
}
pub fn set_default_download_folder_path(&self, path: &str) -> Result<()> {
let path = HSTRING::from(path);
unsafe { self.0.SetDefaultDownloadFolderPath(&path) }.ok()
}
pub fn preferred_color_scheme(&self) -> PreferredColorScheme {
unsafe { self.0.PreferredColorScheme() }
.map_or(PreferredColorScheme::Auto, PreferredColorScheme::from_raw)
}
pub fn set_preferred_color_scheme(&self, scheme: PreferredColorScheme) -> Result<()> {
unsafe { self.0.SetPreferredColorScheme(scheme.to_raw()) }.ok()
}
pub fn clear_browsing_data_all<F: FnOnce(Result<()>) + 'static>(
&self,
handler: F,
) -> Result<()> {
let source: ICoreWebView2Profile2 = self.0.cast()?;
let handler = handler::ClearBrowsingDataCompleted::create(handler);
unsafe { source.ClearBrowsingDataAll(&handler) }.ok()
}
}