Skip to content
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

fix(windows): set physical values in bounds method #1299

Merged

Conversation

kanatapple
Copy link
Contributor

@kanatapple kanatapple commented Jun 21, 2024

wry/src/webview2/mod.rs

Lines 1193 to 1198 in f56ae29

bounds.position = LogicalPosition::new(position_point[0].x, position_point[0].y).into();
bounds.size = LogicalSize::new(
(rect.right - rect.left) as u32,
(rect.bottom - rect.top) as u32,
)
.into();

bounds.position/size expects Logical position/size, but position_point and rect are Physical values.
So conversion is necessary.

closes tauri-apps/tauri#10053

@kanatapple kanatapple requested a review from a team as a code owner June 21, 2024 08:22
pewsheen
pewsheen previously approved these changes Jun 21, 2024
@wusyong
Copy link
Member

wusyong commented Jun 22, 2024

Thanks for helping fix the bug! Could you a change file before I can merge it?

@kanatapple
Copy link
Contributor Author

@wusyong
I added a change file.

Comment on lines 1181 to 1182
let dpi = unsafe { util::hwnd_dpi(self.hwnd) };
let scale_factor = util::dpi_to_scale_factor(dpi);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need these, since we can just use Physical position without converting to logical

)
.into();
bounds.position = PhysicalPosition::new(position_point[0].x, position_point[0].y)
.to_logical::<f64>(scale_factor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to convert to logical

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amrbashir
tauri-apps/tauri#10053

Have you seen this issue?
This is caused by not converting to logical.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the field position and size in Rect are of type Position and Size which can be constructed directly from Physical or Logical type, it is the responsibility of the user to convert the position/size later to their preferred variant.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do

Rect {
	position: PhysicalPositon::new().into(),
}

as well as:

Rect {
	position: LogicalPosition::new().into(),
}

and the user of .bounds() function can convert to their needed types:

let bounds = webview.bounds()?;
let position = bounds.position.to_logical(scale_factor);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this what you mean?

bounds.position = PhysicalPosition::new(position_point[0].x, position_point[0].y).into();

The original code is wrong because it uses logical, right?

bounds.position = LogicalPosition::new(position_point[0].x, position_point[0].y).into();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is what I mean

Copy link
Member

@amrbashir amrbashir Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still haven't tested this so I am not sure yet if the correct value should be LogicalPosition or PhysicalPosition or maybe the bug could in set_bounds not bounds where it should be passing logical values to win32 APIs instead of physical.

Copy link
Contributor

@pewsheen pewsheen Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a bit confused by the definition of Logical lol

- (set) SetWindowPos in set_bound should be Logical, meaning it should have DPI awareness.

  • (set) SetWindowPos in set_bound should be Physical, since our app is DPI unawareness.
  • (get) The position we get from GetClientRect should be Physical since our app is DPI unawareness. (not quite sure)

- So the LogicalPosition for SetWindowPos should be the PhysicalPosition from MapWindowPoints * dpi_scale_factor, which gets a larger value if the DPI is higher.

It looks different from macOS's implementation because macOS will get a smaller coordinate if the DPI is higher.

Copy link
Contributor

@pewsheen pewsheen Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what we get from WIN32 API is already scaled now 😢 , but I think we should use Logical in set_bounds

Update: oh, I forgot we are DPI unawareness, so it should be Physical, Windows will scale it by the DPI factor

}

bounds.size = PhysicalSize::new(rect.right - rect.left, rect.bottom - rect.top)
.to_logical::<f64>(scale_factor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to convert to logical

@kanatapple kanatapple force-pushed the fix/convert-from-physical-to-logical branch from 549648d to ff569a9 Compare June 24, 2024 08:00
@kanatapple
Copy link
Contributor Author

@amrbashir
I just set physical values without converting.

@kanatapple kanatapple changed the title fix: Convert from physical position/size to logical fix: Set physical values Jun 24, 2024
@kanatapple kanatapple force-pushed the fix/convert-from-physical-to-logical branch from 2d472d8 to 49acdd9 Compare June 24, 2024 09:32
@kanatapple
Copy link
Contributor Author

Sorry I don't understand the process, is there anything else I should do?

@kanatapple kanatapple changed the title fix: Set physical values fix: Set physical values to bounds Jul 4, 2024
@kanatapple
Copy link
Contributor Author

@amrbashir
Is there anything else I should do? (except fixing covector)
Sorry to bother you, but please check.

@amrbashir
Copy link
Member

Apologies, I got busy with some other stuff, I will review and test today.

Copy link
Member

@amrbashir amrbashir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

Copy link
Contributor

Package Changes Through 566b53b

There are 1 changes which include wry with patch

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
wry 0.41.0 0.41.1

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@amrbashir amrbashir changed the title fix: Set physical values to bounds fix(windows): set physical values in bounds method Jul 10, 2024
@amrbashir amrbashir merged commit d1f1e7e into tauri-apps:dev Jul 10, 2024
13 checks passed
@kanatapple
Copy link
Contributor Author

@amrbashir
Thank you for the review and merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] Manual WebView resizing does not work correctly
4 participants