From adc03c9c0056635baed1e10f4abcc6ac5bfafdcb Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Thu, 15 Jul 2021 05:30:21 -0400 Subject: [PATCH 1/3] Implement Application::get_locale() for Windows backend --- druid-shell/Cargo.toml | 2 +- druid-shell/src/backend/windows/application.rs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/druid-shell/Cargo.toml b/druid-shell/Cargo.toml index 9dd2f15f05..bda6dbecee 100755 --- a/druid-shell/Cargo.toml +++ b/druid-shell/Cargo.toml @@ -65,7 +65,7 @@ version = "0.3.9" features = ["d2d1_1", "dwrite", "winbase", "libloaderapi", "errhandlingapi", "winuser", "shellscalingapi", "shobjidl", "combaseapi", "synchapi", "dxgi1_3", "dcomp", "d3d11", "dwmapi", "wincon", "fileapi", "processenv", "winbase", "handleapi", - "shellapi"] + "shellapi", "winnls"] [target.'cfg(target_os="macos")'.dependencies] block = "0.1.6" diff --git a/druid-shell/src/backend/windows/application.rs b/druid-shell/src/backend/windows/application.rs index 8c8a21e4fb..c0c1391a6f 100644 --- a/druid-shell/src/backend/windows/application.rs +++ b/druid-shell/src/backend/windows/application.rs @@ -27,6 +27,8 @@ use winapi::shared::windef::{DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, HCURSOR use winapi::shared::winerror::HRESULT_FROM_WIN32; use winapi::um::errhandlingapi::GetLastError; use winapi::um::shellscalingapi::PROCESS_PER_MONITOR_DPI_AWARE; +use winapi::um::winnls::GetUserDefaultLocaleName; +use winapi::um::winnt::LOCALE_NAME_MAX_LENGTH; use winapi::um::winuser::{ DispatchMessageW, GetAncestor, GetMessageW, LoadIconW, PeekMessageW, PostMessageW, PostQuitMessage, RegisterClassW, TranslateAcceleratorW, TranslateMessage, GA_ROOT, @@ -40,7 +42,7 @@ use crate::application::AppHandler; use super::accels; use super::clipboard::Clipboard; use super::error::Error; -use super::util::{self, ToWide, CLASS_NAME, OPTIONAL_FUNCTIONS}; +use super::util::{self, FromWide, ToWide, CLASS_NAME, OPTIONAL_FUNCTIONS}; use super::window::{self, DS_REQUEST_DESTROY}; #[derive(Clone)] @@ -194,7 +196,15 @@ impl Application { } pub fn get_locale() -> String { - //TODO ahem - "en-US".into() + unsafe { + let mut buf: [u16; LOCALE_NAME_MAX_LENGTH] = std::mem::zeroed(); + let len_with_null = GetUserDefaultLocaleName(buf.as_mut_ptr(), buf.len() as _) as usize; + let locale = if len_with_null > 1 { + buf.get(..len_with_null - 1).and_then(FromWide::from_wide) + } else { + None + }; + locale.unwrap_or_else(|| "en-US".into()) + } } } From 1e3fc7d13ba861da303326d8e38b475ee79183e8 Mon Sep 17 00:00:00 2001 From: chad Date: Thu, 15 Jul 2021 12:30:02 -0400 Subject: [PATCH 2/3] Cleanup Application::get_locale() in Windows backend * Minimize extent of unsafe block * Be less defensive about locales of zero length * Warn on failure to retrieve locale --- .../src/backend/windows/application.rs | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/druid-shell/src/backend/windows/application.rs b/druid-shell/src/backend/windows/application.rs index c0c1391a6f..fab90751ce 100644 --- a/druid-shell/src/backend/windows/application.rs +++ b/druid-shell/src/backend/windows/application.rs @@ -196,15 +196,18 @@ impl Application { } pub fn get_locale() -> String { - unsafe { - let mut buf: [u16; LOCALE_NAME_MAX_LENGTH] = std::mem::zeroed(); - let len_with_null = GetUserDefaultLocaleName(buf.as_mut_ptr(), buf.len() as _) as usize; - let locale = if len_with_null > 1 { - buf.get(..len_with_null - 1).and_then(FromWide::from_wide) - } else { - None - }; - locale.unwrap_or_else(|| "en-US".into()) - } + let mut buf = [0u16; LOCALE_NAME_MAX_LENGTH]; + let len_with_null = unsafe { + GetUserDefaultLocaleName(buf.as_mut_ptr(), buf.len() as _) as usize + }; + let locale = if len_with_null > 0 { + buf.get(..len_with_null - 1).and_then(FromWide::from_wide) + } else { + None + }; + locale.unwrap_or_else(|| { + tracing::warn!("Failed to get user locale"); + "en-US".into() + }) } } From 1b5ed642c7d578dc0e6fd7251e4256e60b6653cd Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Fri, 16 Jul 2021 02:52:33 -0400 Subject: [PATCH 3/3] Formatting in Application::get_locale() for Windows backend --- druid-shell/src/backend/windows/application.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/druid-shell/src/backend/windows/application.rs b/druid-shell/src/backend/windows/application.rs index fab90751ce..2b15b78a92 100644 --- a/druid-shell/src/backend/windows/application.rs +++ b/druid-shell/src/backend/windows/application.rs @@ -197,9 +197,8 @@ impl Application { pub fn get_locale() -> String { let mut buf = [0u16; LOCALE_NAME_MAX_LENGTH]; - let len_with_null = unsafe { - GetUserDefaultLocaleName(buf.as_mut_ptr(), buf.len() as _) as usize - }; + let len_with_null = + unsafe { GetUserDefaultLocaleName(buf.as_mut_ptr(), buf.len() as _) as usize }; let locale = if len_with_null > 0 { buf.get(..len_with_null - 1).and_then(FromWide::from_wide) } else {