Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,13 @@ impl<T> fmt::Display for EventLoopClosed<T> {
impl<T: fmt::Debug> error::Error for EventLoopClosed<T> {}

/// Fiter controlling the propagation of device events.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub enum DeviceEventFilter {
/// Always filter out device events.
Always,
/// Filter out device events while the window is not focused.
#[default]
Unfocused,
/// Report all device events regardless of window focus.
Never,
}

impl Default for DeviceEventFilter {
fn default() -> Self {
Self::Unfocused
}
}
48 changes: 14 additions & 34 deletions src/platform_impl/windows/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

use std::{fmt, io, iter::once, mem, os::windows::ffi::OsStrExt, path::Path, sync::Arc};
use std::{fmt, mem, path::Path, sync::Arc};

use windows::{
core::PCWSTR,
Expand All @@ -12,8 +12,9 @@ use windows::{
UI::WindowsAndMessaging::*,
},
};
use windows_core::Owned;

use crate::{dpi::PhysicalSize, icon::*};
use crate::{dpi::PhysicalSize, icon::*, platform_impl::platform::util};

impl Pixel {
fn to_bgra(&mut self) {
Expand Down Expand Up @@ -43,10 +44,9 @@ impl RgbaIcon {
and_mask.as_ptr(),
rgba.as_ptr(),
)
};
Ok(WinIcon::from_handle(
handle.map_err(|_| BadIcon::OsError(io::Error::last_os_error()))?,
))
}
.map_err(|error| BadIcon::OsError(error.into()))?;
Ok(WinIcon::from_handle(HICON(handle.0)))
}
}

Expand All @@ -57,33 +57,23 @@ pub enum IconType {
Big = ICON_BIG as isize,
}

#[derive(Debug)]
struct RaiiIcon {
handle: HICON,
}

#[derive(Clone)]
pub struct WinIcon {
inner: Arc<RaiiIcon>,
inner: Arc<Owned<HICON>>,
}

unsafe impl Send for WinIcon {}

impl WinIcon {
pub fn as_raw_handle(&self) -> HICON {
self.inner.handle
**self.inner
}

pub fn from_path<P: AsRef<Path>>(
path: P,
size: Option<PhysicalSize<u32>>,
) -> Result<Self, BadIcon> {
let wide_path: Vec<u16> = path
.as_ref()
.as_os_str()
.encode_wide()
.chain(once(0))
.collect();
let wide_path = util::encode_wide(path.as_ref());

// width / height of 0 along with LR_DEFAULTSIZE tells windows to load the default icon size
let (width, height) = size.map(Into::into).unwrap_or((0, 0));
Expand All @@ -98,10 +88,8 @@ impl WinIcon {
LR_DEFAULTSIZE | LR_LOADFROMFILE,
)
}
.map(|handle| HICON(handle.0));
Ok(WinIcon::from_handle(
handle.map_err(|_| BadIcon::OsError(io::Error::last_os_error()))?,
))
.map_err(|error| BadIcon::OsError(error.into()))?;
Ok(WinIcon::from_handle(HICON(handle.0)))
}

pub fn from_resource(resource_id: u16, size: Option<PhysicalSize<u32>>) -> Result<Self, BadIcon> {
Expand All @@ -117,10 +105,8 @@ impl WinIcon {
LR_DEFAULTSIZE,
)
}
.map(|handle| HICON(handle.0));
Ok(WinIcon::from_handle(
handle.map_err(|_| BadIcon::OsError(io::Error::last_os_error()))?,
))
.map_err(|error| BadIcon::OsError(error.into()))?;
Ok(WinIcon::from_handle(HICON(handle.0)))
}

pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
Expand All @@ -141,17 +127,11 @@ impl WinIcon {

fn from_handle(handle: HICON) -> Self {
Self {
inner: Arc::new(RaiiIcon { handle }),
inner: Arc::new(unsafe { Owned::new(handle) }),
}
}
}

impl Drop for RaiiIcon {
fn drop(&mut self) {
let _ = unsafe { DestroyIcon(self.handle) };
}
}

impl fmt::Debug for WinIcon {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
(*self.inner).fmt(formatter)
Expand Down
10 changes: 1 addition & 9 deletions src/platform_impl/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ pub fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
string.as_ref().encode_wide().chain(once(0)).collect()
}

fn win_to_err<F: FnOnce() -> BOOL>(f: F) -> Result<(), io::Error> {
if f().as_bool() {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}

pub unsafe fn get_window_rect(hwnd: HWND) -> Option<RECT> {
let mut rect = std::mem::zeroed();
GetWindowRect(hwnd, &mut rect).ok().map(|_| rect)
Expand All @@ -72,7 +64,7 @@ pub fn get_client_rect(hwnd: HWND) -> Result<RECT, io::Error> {
let mut top_left = POINT::default();

unsafe {
win_to_err(|| ClientToScreen(hwnd, &mut top_left))?;
ClientToScreen(hwnd, &mut top_left).ok()?;
GetClientRect(hwnd, &mut rect)?;
}

Expand Down
Loading