-
-
Notifications
You must be signed in to change notification settings - Fork 148
fix(ui-windows): AdBanner was an unresolved external - add the placeholder widget #6640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| //! `AdBanner` widget (#867) — Win32 layout placeholder. | ||
| //! | ||
| //! Google Mobile Ads ships no first-party Windows SDK, so — exactly like | ||
| //! the macOS backend (`perry-ui-macos/src/widgets/adbanner.rs`) — the | ||
| //! banner is a **layout placeholder**: an empty STATIC child sized to the | ||
| //! requested banner slot, so a `perry/ui` layout developed or previewed | ||
| //! on Windows reserves exactly the space the real ad occupies on | ||
| //! iOS/Android. The `unitId` is accepted for cross-platform API parity | ||
| //! but unused since nothing loads. | ||
| //! | ||
| //! Before this existed, `perry_ui_adbanner_create` was simply absent from | ||
| //! the Windows staticlib: any TS program using `AdBanner(...)` failed at | ||
| //! link time with an unresolved external (the symbol is a required | ||
| //! dispatch row in `perry-dispatch/src/ui_table/part_a.rs`). | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| use windows::Win32::Foundation::*; | ||
| #[cfg(target_os = "windows")] | ||
| use windows::Win32::System::LibraryLoader::GetModuleHandleW; | ||
| #[cfg(target_os = "windows")] | ||
| use windows::Win32::UI::WindowsAndMessaging::*; | ||
|
|
||
| use super::{alloc_control_id, register_widget, WidgetKind}; | ||
|
|
||
| fn str_from_header(ptr: *const u8) -> &'static str { | ||
| if ptr.is_null() { | ||
| return ""; | ||
| } | ||
| unsafe { | ||
| let header = ptr as *const perry_runtime::string::StringHeader; | ||
| let len = (*header).byte_len as usize; | ||
| let data = ptr.add(std::mem::size_of::<perry_runtime::string::StringHeader>()); | ||
| std::str::from_utf8_unchecked(std::slice::from_raw_parts(data, len)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| fn to_wide(s: &str) -> Vec<u16> { | ||
| s.encode_utf16().chain(std::iter::once(0)).collect() | ||
| } | ||
|
|
||
| /// Banner dimensions in logical pixels for each size key the d.ts exposes. | ||
| /// Values match Google Mobile Ads' standard `AdSize` constants (and the | ||
| /// macOS placeholder) so the reserved slot lines up with the real | ||
| /// iOS/Android banner. | ||
| fn banner_size(size_key: &str) -> (f64, f64) { | ||
| match size_key { | ||
| "large-banner" => (320.0, 100.0), | ||
| "medium-rectangle" => (300.0, 250.0), | ||
| "full-banner" => (468.0, 60.0), | ||
| "leaderboard" => (728.0, 90.0), | ||
| // "banner" / "adaptive" / empty / unknown → standard 320×50. | ||
| _ => (320.0, 50.0), | ||
| } | ||
| } | ||
|
|
||
| /// Create the banner placeholder sized per `size_ptr`. Returns the widget | ||
| /// handle. The slot dimensions are pinned via fixed width/height so the | ||
| /// stack layout reserves them exactly (matching the macOS NSView frame). | ||
| pub fn create(unit_id_ptr: *const u8, size_ptr: *const u8) -> i64 { | ||
| let _unit_id = str_from_header(unit_id_ptr); | ||
| let size_key = str_from_header(size_ptr); | ||
| let (w, h) = banner_size(size_key); | ||
| let scale = crate::app::get_dpi_scale(); | ||
| let w = (w * scale).round() as i32; | ||
| let h = (h * scale).round() as i32; | ||
| let control_id = alloc_control_id(); | ||
|
|
||
| #[cfg(target_os = "windows")] | ||
| { | ||
| let class_name = to_wide("STATIC"); | ||
| let window_text = to_wide(""); | ||
| unsafe { | ||
| let hinstance = GetModuleHandleW(None).unwrap(); | ||
| let hwnd = CreateWindowExW( | ||
| WINDOW_EX_STYLE::default(), | ||
| windows::core::PCWSTR(class_name.as_ptr()), | ||
| windows::core::PCWSTR(window_text.as_ptr()), | ||
| WINDOW_STYLE(WS_CHILD.0 | WS_VISIBLE.0), | ||
| 0, | ||
| 0, | ||
| w, | ||
| h, | ||
| Some(super::get_parking_hwnd()), | ||
| Some(HMENU(control_id as *mut _)), | ||
| Some(HINSTANCE::from(hinstance)), | ||
| None, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let handle = register_widget(hwnd, WidgetKind::Image, control_id); | ||
| super::set_fixed_width(handle, w); | ||
| super::set_fixed_height(handle, h); | ||
| handle | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(target_os = "windows"))] | ||
| { | ||
| let handle = register_widget(0, WidgetKind::Image, control_id); | ||
| super::set_fixed_width(handle, w); | ||
| super::set_fixed_height(handle, h); | ||
| handle | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win
Prevent Undefined Behavior by safely parsing potentially invalid UTF-8 strings.
The
perry_runtime::string::StringHeaderexplicitly documents that strings may contain lone surrogates (WTF-8). Callingstd::str::from_utf8_uncheckedon such strings invokes Undefined Behavior in Rust. Usestd::str::from_utf8and safely fall back (e.g., to an empty string) if the bytes are invalid.Additionally, returning
&'static strfor a runtime-allocated string is semantically incorrect because the string's lifetime is bound to the runtime's memory, not the life of the program. It should use a bounded lifetime parameter.🔒️ Proposed fix
🤖 Prompt for AI Agents