Skip to content

Commit

Permalink
refactor(windows): move to_wstring to util mod (rust-windowing#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Jul 23, 2021
1 parent 1408666 commit 638cb79
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 44 deletions.
16 changes: 3 additions & 13 deletions src/platform_impl/windows/dark_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

/// This is a simple implementation of support for Windows Dark Mode,
/// which is inspired by the solution in https://github.com/ysc3839/win32-darkmode
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;

use winapi::{
shared::{
basetsd::SIZE_T,
Expand All @@ -17,7 +14,7 @@ use winapi::{
um::{libloaderapi, uxtheme, winuser},
};

use crate::window::Theme;
use crate::{platform_impl::platform::util, window::Theme};

lazy_static! {
static ref WIN10_BUILD_VERSION: Option<DWORD> = {
Expand Down Expand Up @@ -71,8 +68,8 @@ lazy_static! {
}
};

static ref DARK_THEME_NAME: Vec<u16> = widestring("DarkMode_Explorer");
static ref LIGHT_THEME_NAME: Vec<u16> = widestring("");
static ref DARK_THEME_NAME: Vec<u16> = util::to_wstring("DarkMode_Explorer");
static ref LIGHT_THEME_NAME: Vec<u16> = util::to_wstring("");
}

/// Attempt to set a theme on a window, if necessary.
Expand Down Expand Up @@ -215,10 +212,3 @@ fn is_high_contrast() -> bool {

ok != FALSE && (HCF_HIGHCONTRASTON & hc.dwFlags) == 1
}

fn widestring(src: &'static str) -> Vec<u16> {
OsStr::new(src)
.encode_wide()
.chain(Some(0).into_iter())
.collect()
}
7 changes: 1 addition & 6 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,8 @@ lazy_static! {
winuser::RegisterWindowMessageA("Tao::SetRetainMaximized\0".as_ptr() as LPCSTR)
};
static ref THREAD_EVENT_TARGET_WINDOW_CLASS: Vec<u16> = unsafe {
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;

let class_name: Vec<_> = OsStr::new("Tao Thread Event Target")
.encode_wide()
.chain(Some(0).into_iter())
.collect();
let class_name= util::to_wstring("Tao Thread Event Target");

let class = winuser::WNDCLASSEXW {
cbSize: mem::size_of::<winuser::WNDCLASSEXW>() as UINT,
Expand Down
11 changes: 2 additions & 9 deletions src/platform_impl/windows/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use raw_window_handle::RawWindowHandle;
use std::{collections::HashMap, ffi::CString, fmt, os::windows::ffi::OsStrExt, sync::Mutex};
use std::{collections::HashMap, ffi::CString, fmt, sync::Mutex};

use winapi::{
shared::{basetsd, minwindef, windef},
Expand All @@ -17,7 +17,7 @@ use crate::{
window::WindowId as RootWindowId,
};

use super::{accelerator::register_accel, keyboard::key_to_vk, WindowId};
use super::{accelerator::register_accel, keyboard::key_to_vk, util::to_wstring, WindowId};

#[derive(Copy, Clone)]
struct AccelWrapper(winuser::ACCEL);
Expand Down Expand Up @@ -361,13 +361,6 @@ pub fn initialize(
}
}

pub(crate) fn to_wstring(str: &str) -> Vec<u16> {
std::ffi::OsStr::new(str)
.encode_wide()
.chain(Some(0).into_iter())
.collect()
}

pub(crate) unsafe extern "system" fn subclass_proc(
hwnd: windef::HWND,
msg: minwindef::UINT,
Expand Down
6 changes: 3 additions & 3 deletions src/platform_impl/windows/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::{
dpi::{dpi_to_scale_factor, hwnd_dpi},
menu::{subclass_proc as menu_subclass_proc, to_wstring, Menu, MenuHandler},
menu::{subclass_proc as menu_subclass_proc, Menu, MenuHandler},
util, OsError,
};
use crate::{
Expand Down Expand Up @@ -56,7 +56,7 @@ impl SystemTrayBuilder {
) -> Result<RootSystemTray, RootOsError> {
let hmenu: Option<HMENU> = self.tray_menu.map(|m| m.hmenu());

let class_name = to_wstring("tao_system_tray_app");
let class_name = util::to_wstring("tao_system_tray_app");
unsafe {
let hinstance = libloaderapi::GetModuleHandleA(std::ptr::null_mut());

Expand All @@ -72,7 +72,7 @@ impl SystemTrayBuilder {
let hwnd = winuser::CreateWindowExW(
0,
class_name.as_ptr(),
to_wstring("tao_system_tray_window").as_ptr(),
util::to_wstring("tao_system_tray_window").as_ptr(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
Expand Down
9 changes: 8 additions & 1 deletion src/platform_impl/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::{
io, mem,
ops::BitAnd,
os::raw::c_void,
os::{raw::c_void, windows::prelude::OsStrExt},
ptr, slice,
sync::atomic::{AtomicBool, Ordering},
};
Expand Down Expand Up @@ -42,6 +42,13 @@ pub fn wchar_ptr_to_string(wchar: *const wchar_t) -> String {
wchar_to_string(wchar_slice)
}

pub fn to_wstring(str: &str) -> Vec<u16> {
std::ffi::OsStr::new(str)
.encode_wide()
.chain(Some(0).into_iter())
.collect()
}

pub unsafe fn status_map<T, F: FnMut(&mut T) -> BOOL>(mut fun: F) -> Option<T> {
let mut data: T = mem::zeroed();
if fun(&mut data) != 0 {
Expand Down
15 changes: 3 additions & 12 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,7 @@ impl Window {
}

pub fn set_title(&self, text: &str) {
let text = OsStr::new(text)
.encode_wide()
.chain(Some(0).into_iter())
.collect::<Vec<_>>();
let text = util::to_wstring(text);
unsafe {
winuser::SetWindowTextW(self.window.0, text.as_ptr() as LPCWSTR);
}
Expand Down Expand Up @@ -811,10 +808,7 @@ unsafe fn init<T: 'static>(
pl_attribs: PlatformSpecificWindowBuilderAttributes,
event_loop: &EventLoopWindowTarget<T>,
) -> Result<Window, RootOsError> {
let title = OsStr::new(&attributes.title)
.encode_wide()
.chain(Some(0).into_iter())
.collect::<Vec<_>>();
let title = util::to_wstring(&attributes.title);

// registering the window class
let class_name = register_window_class(&attributes.window_icon, &pl_attribs.taskbar_icon);
Expand Down Expand Up @@ -971,10 +965,7 @@ unsafe fn register_window_class(
window_icon: &Option<Icon>,
taskbar_icon: &Option<Icon>,
) -> Vec<u16> {
let class_name: Vec<_> = OsStr::new("Window Class")
.encode_wide()
.chain(Some(0).into_iter())
.collect();
let class_name = util::to_wstring("Window Class");

let h_icon = taskbar_icon
.as_ref()
Expand Down

0 comments on commit 638cb79

Please sign in to comment.