Skip to content

Commit

Permalink
refactor: destruct tuples to enhance readability (#1438)
Browse files Browse the repository at this point in the history
Signed-off-by: Integral <[email protected]>
  • Loading branch information
Integral-Tech authored Dec 10, 2024
1 parent eb0c816 commit 4740c0b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 36 deletions.
8 changes: 4 additions & 4 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ fn main() -> wry::Result<()> {
let mut webviews = HashMap::new();
let proxy = event_loop.create_proxy();

let new_window = create_new_window(
let (window, webview) = create_new_window(
format!("Window {}", webviews.len() + 1),
&event_loop,
proxy.clone(),
);
webviews.insert(new_window.0.id(), (new_window.0, new_window.1));
webviews.insert(window.id(), (window, webview));

event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
Expand All @@ -43,12 +43,12 @@ fn main() -> wry::Result<()> {
}
}
Event::UserEvent(UserEvent::NewWindow) => {
let new_window = create_new_window(
let (window, webview) = create_new_window(
format!("Window {}", webviews.len() + 1),
event_loop,
proxy.clone(),
);
webviews.insert(new_window.0.id(), (new_window.0, new_window.1));
webviews.insert(window.id(), (window, webview));
}
Event::UserEvent(UserEvent::CloseWindow(id)) => {
webviews.remove(&id);
Expand Down
4 changes: 2 additions & 2 deletions src/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ impl<'a> MainPipe<'a> {
string_class,
self.env.new_string("")?,
)?;
for (i, script) in initialization_scripts.into_iter().enumerate() {
for (i, (script, _)) in initialization_scripts.into_iter().enumerate() {
self.env.set_object_array_element(
&initialization_scripts_array,
i as i32,
self.env.new_string(script.0)?,
self.env.new_string(script)?,
)?;
}

Expand Down
14 changes: 7 additions & 7 deletions src/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ impl InnerWebView {
UnsafeRequestHandler::new(Box::new(
move |webview_id: &str, mut request, is_document_start_script_enabled| {
let uri = request.uri().to_string();
if let Some(custom_protocol) = custom_protocols.iter().find(|(name, _)| {
if let Some((custom_protocol_uri, custom_protocol_closure)) = custom_protocols.iter().find(|(name, _)| {
uri.starts_with(&format!("{scheme}://{}.", name))
}) {
let uri_res = uri
.replace(
&format!("{scheme}://{}.", custom_protocol.0),
&format!("{}://", custom_protocol.0),
&format!("{scheme}://{}.", custom_protocol_uri),
&format!("{}://", custom_protocol_uri),
)
.parse();

Expand Down Expand Up @@ -273,15 +273,15 @@ impl InnerWebView {
let mut hashes = Vec::new();
with_html_head(&mut document, |head| {
// iterate in reverse order since we are prepending each script to the head tag
for script in initialization_scripts.iter().rev() {
for (script, _) in initialization_scripts.iter().rev() {
let script_el = NodeRef::new_element(
QualName::new(None, ns!(html), "script".into()),
None,
);
script_el.append(NodeRef::new_text(script.0.as_str()));
script_el.append(NodeRef::new_text(script.as_str()));
head.prepend(script_el);
if csp.is_some() {
hashes.push(hash_script(script.0.as_str()));
hashes.push(hash_script(script.as_str()));
}
}
});
Expand All @@ -304,7 +304,7 @@ impl InnerWebView {
tx.send(response).unwrap();
});

(custom_protocol.1)(webview_id, request, RequestAsyncResponder { responder });
(custom_protocol_closure)(webview_id, request, RequestAsyncResponder { responder });
return Some(rx.recv_timeout(MAIN_PIPE_TIMEOUT).unwrap());
}
None
Expand Down
14 changes: 4 additions & 10 deletions src/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,9 @@ impl InnerWebView {
webview.set_background_color(&gtk::gdk::RGBA::new(0., 0., 0., 0.));
} else {
// background color
if let Some(background_color) = attributes.background_color {
if let Some((red, green, blue, alpha)) = attributes.background_color {
webview.set_background_color(&gtk::gdk::RGBA::new(
background_color.0 as _,
background_color.1 as _,
background_color.2 as _,
background_color.3 as _,
red as _, green as _, blue as _, alpha as _,
));
}
}
Expand Down Expand Up @@ -660,12 +657,9 @@ impl InnerWebView {
Ok(())
}

pub fn set_background_color(&self, background_color: RGBA) -> Result<()> {
pub fn set_background_color(&self, (red, green, blue, alpha): RGBA) -> Result<()> {
self.webview.set_background_color(&gtk::gdk::RGBA::new(
background_color.0 as _,
background_color.1 as _,
background_color.2 as _,
background_color.3 as _,
red as _, green as _, blue as _, alpha as _,
));
Ok(())
}
Expand Down
13 changes: 4 additions & 9 deletions src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,19 +1667,14 @@ unsafe fn set_background_color(
controller: &ICoreWebView2Controller,
background_color: RGBA,
) -> Result<()> {
let mut color = background_color;
if is_windows_7() || color.3 != 0 {
color.3 = 255;
let (R, G, B, mut A) = background_color;
if is_windows_7() || A != 0 {
A = 255;
}

let controller2: ICoreWebView2Controller2 = controller.cast()?;
controller2
.SetDefaultBackgroundColor(COREWEBVIEW2_COLOR {
R: color.0,
G: color.1,
B: color.2,
A: color.3,
})
.SetDefaultBackgroundColor(COREWEBVIEW2_COLOR { R, G, B, A })
.map_err(Into::into)
}

Expand Down
8 changes: 4 additions & 4 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ impl InnerWebView {
let config = WKWebViewConfiguration::new();

// Incognito mode
let os_version = util::operating_system_version();
let (os_major_version, _, _) = util::operating_system_version();
#[cfg(target_os = "macos")]
let custom_data_store_available = os_version.0 >= 14;
let custom_data_store_available = os_major_version >= 14;
#[cfg(target_os = "ios")]
let custom_data_store_available = os_version.0 >= 17;
let custom_data_store_available = os_major_version >= 17;

let data_store = match (
attributes.incognito,
Expand Down Expand Up @@ -521,7 +521,7 @@ r#"Object.defineProperty(window, 'ipc', {

// make sure the window is always on top when we create a new webview
let app = NSApplication::sharedApplication(mtm);
if os_version.0 >= 14 {
if os_major_version >= 14 {
NSApplication::activate(&app);
} else {
#[allow(deprecated)]
Expand Down

0 comments on commit 4740c0b

Please sign in to comment.