Skip to content

Commit 4973004

Browse files
committed
refactor: improve code quality with macros
1 parent 4cf1c78 commit 4973004

5 files changed

Lines changed: 68 additions & 77 deletions

File tree

crates/gdcef/src/cef_texture/mod.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -662,11 +662,9 @@ impl CefTexture {
662662
0,
663663
);
664664

665-
#[cfg(target_os = "windows")]
666-
let ops = cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(allowed_ops));
667-
#[cfg(not(target_os = "windows"))]
668-
let ops =
669-
cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(allowed_ops as u32));
665+
let ops = cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(
666+
crate::cef_i32_to_raw!(allowed_ops),
667+
));
670668
self.with_app_mut(|app| {
671669
let Some(host) = app.host() else {
672670
return;
@@ -686,11 +684,9 @@ impl CefTexture {
686684
0,
687685
);
688686

689-
#[cfg(target_os = "windows")]
690-
let ops = cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(allowed_ops));
691-
#[cfg(not(target_os = "windows"))]
692-
let ops =
693-
cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(allowed_ops as u32));
687+
let ops = cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(
688+
crate::cef_i32_to_raw!(allowed_ops),
689+
));
694690
self.with_app(|app| {
695691
if let Some(host) = app.host() {
696692
host.drag_target_drag_over(Some(&mouse_event), ops);
@@ -729,11 +725,9 @@ impl CefTexture {
729725

730726
#[func]
731727
pub fn drag_source_ended(&mut self, position: Vector2, operation: i32) {
732-
#[cfg(target_os = "windows")]
733-
let op = cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(operation));
734-
#[cfg(not(target_os = "windows"))]
735-
let op =
736-
cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(operation as u32));
728+
let op = cef::DragOperationsMask::from(cef::sys::cef_drag_operations_mask_t(
729+
crate::cef_i32_to_raw!(operation),
730+
));
737731
self.with_app_mut(|app| {
738732
let Some(host) = app.host() else {
739733
return;

crates/gdcef/src/cef_texture/permission_ops.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,5 @@ pub(crate) fn resolve_permission_request(app: &App, request_id: i64, grant: bool
133133
}
134134

135135
fn media_permission_none_mask() -> u32 {
136-
#[cfg(target_os = "windows")]
137-
{
138-
cef::MediaAccessPermissionTypes::NONE.get_raw() as u32
139-
}
140-
#[cfg(not(target_os = "windows"))]
141-
{
142-
cef::MediaAccessPermissionTypes::NONE.get_raw()
143-
}
136+
crate::cef_raw_to_u32!(cef::MediaAccessPermissionTypes::NONE.get_raw())
144137
}

crates/gdcef/src/cef_texture/signals.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ impl IRefCounted for CookieInfo {
190190

191191
impl CookieInfo {
192192
fn from_data(data: &crate::cookie::CookieData) -> Gd<Self> {
193-
#[cfg(target_os = "windows")]
194193
return Gd::from_init_fn(|base| Self {
195194
base,
196195
name: GString::from(&data.name),
@@ -199,19 +198,7 @@ impl CookieInfo {
199198
path: GString::from(&data.path),
200199
secure: data.secure,
201200
httponly: data.httponly,
202-
same_site: data.same_site.get_raw(),
203-
has_expires: data.has_expires,
204-
});
205-
#[cfg(not(target_os = "windows"))]
206-
return Gd::from_init_fn(|base| Self {
207-
base,
208-
name: GString::from(&data.name),
209-
value: GString::from(&data.value),
210-
domain: GString::from(&data.domain),
211-
path: GString::from(&data.path),
212-
secure: data.secure,
213-
httponly: data.httponly,
214-
same_site: data.same_site.get_raw() as i32,
201+
same_site: crate::cef_raw_to_i32!(data.same_site.get_raw()),
215202
has_expires: data.has_expires,
216203
});
217204
}

crates/gdcef/src/compat.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,63 @@
77
88
use cef::sys::cef_event_flags_t;
99

10+
/// Casts a CEF "raw" integer value to `u32` across platforms.
11+
///
12+
/// Some CEF raw values are `i32` on Windows and `u32` elsewhere.
13+
#[macro_export]
14+
macro_rules! cef_raw_to_u32 {
15+
($value:expr) => {{
16+
#[cfg(target_os = "windows")]
17+
{
18+
$value as u32
19+
}
20+
#[cfg(not(target_os = "windows"))]
21+
{
22+
$value
23+
}
24+
}};
25+
}
26+
27+
/// Casts a CEF "raw" integer value to `i32` across platforms.
28+
///
29+
/// Some CEF raw values are `u32` on non-Windows targets.
30+
#[macro_export]
31+
macro_rules! cef_raw_to_i32 {
32+
($value:expr) => {{
33+
#[cfg(target_os = "windows")]
34+
{
35+
$value
36+
}
37+
#[cfg(not(target_os = "windows"))]
38+
{
39+
$value as i32
40+
}
41+
}};
42+
}
43+
44+
/// Casts an `i32` input to the platform-specific raw CEF integer type.
45+
///
46+
/// Useful when constructing raw CEF tuple structs that differ by target.
47+
#[macro_export]
48+
macro_rules! cef_i32_to_raw {
49+
($value:expr) => {{
50+
#[cfg(target_os = "windows")]
51+
{
52+
$value
53+
}
54+
#[cfg(not(target_os = "windows"))]
55+
{
56+
$value as u32
57+
}
58+
}};
59+
}
60+
1061
/// Converts a `cef_event_flags_t` bitfield to `u32`.
1162
///
1263
/// On Windows the inner type is `i32`; on other platforms it is already `u32`.
1364
#[inline]
1465
pub fn event_flags_to_u32(flags: cef_event_flags_t) -> u32 {
15-
#[cfg(target_os = "windows")]
16-
{
17-
flags.0 as u32
18-
}
19-
#[cfg(not(target_os = "windows"))]
20-
{
21-
flags.0
22-
}
66+
crate::cef_raw_to_u32!(flags.0)
2367
}
2468

2569
#[cfg(test)]

crates/gdcef/src/webrender.rs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -172,38 +172,17 @@ fn handle_popup_size(popup_state: &Arc<Mutex<cef_app::PopupState>>, rect: Option
172172

173173
/// Helper to convert DragOperationsMask to u32 in a cross-platform way.
174174
fn drag_ops_to_u32(ops: DragOperationsMask) -> u32 {
175-
#[cfg(target_os = "windows")]
176-
{
177-
ops.as_ref().0 as u32
178-
}
179-
#[cfg(not(target_os = "windows"))]
180-
{
181-
ops.as_ref().0
182-
}
175+
crate::cef_raw_to_u32!(ops.as_ref().0)
183176
}
184177

185178
/// Helper to convert MediaAccessPermissionTypes bitmask to u32 in a cross-platform way.
186179
fn media_permission_to_u32(permission: cef::MediaAccessPermissionTypes) -> u32 {
187-
#[cfg(target_os = "windows")]
188-
{
189-
permission.get_raw() as u32
190-
}
191-
#[cfg(not(target_os = "windows"))]
192-
{
193-
permission.get_raw()
194-
}
180+
crate::cef_raw_to_u32!(permission.get_raw())
195181
}
196182

197183
/// Helper to convert PermissionRequestTypes bitmask to u32 in a cross-platform way.
198184
fn prompt_permission_to_u32(permission: cef::PermissionRequestTypes) -> u32 {
199-
#[cfg(target_os = "windows")]
200-
{
201-
permission.get_raw() as u32
202-
}
203-
#[cfg(not(target_os = "windows"))]
204-
{
205-
permission.get_raw()
206-
}
185+
crate::cef_raw_to_u32!(permission.get_raw())
207186
}
208187

209188
fn cef_resource_type_to_adblock_request_type(resource_type: ResourceType) -> &'static str {
@@ -632,10 +611,7 @@ wrap_drag_handler! {
632611
if let Some(drag_data) = drag_data {
633612
let drag_info = extract_drag_data_info(drag_data);
634613
if let Ok(mut queues) = self.event_queues.lock() {
635-
#[cfg(target_os = "windows")]
636-
let mask: u32 = mask.as_ref().0 as u32;
637-
#[cfg(not(target_os = "windows"))]
638-
let mask: u32 = mask.as_ref().0;
614+
let mask: u32 = crate::cef_raw_to_u32!(mask.as_ref().0);
639615

640616
queues.drag_events.push_back(DragEvent::Entered {
641617
drag_data: drag_info,
@@ -731,10 +707,7 @@ wrap_display_handler! {
731707
) -> ::std::os::raw::c_int {
732708
let message_str = message.map(|m| m.to_string()).unwrap_or_default();
733709
let source_str = source.map(|s| s.to_string()).unwrap_or_default();
734-
#[cfg(target_os = "windows")]
735-
let level: u32 = level.get_raw() as u32;
736-
#[cfg(not(target_os = "windows"))]
737-
let level: u32 = level.get_raw();
710+
let level: u32 = crate::cef_raw_to_u32!(level.get_raw());
738711

739712
if let Ok(mut queues) = self.event_queues.lock() {
740713
queues.console_messages.push_back(ConsoleMessageEvent {

0 commit comments

Comments
 (0)