Skip to content

Commit

Permalink
Update click check method
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Yuwei authored and Wu Yuwei committed Nov 22, 2024
1 parent 478341d commit 8d8f29a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 77 deletions.
21 changes: 1 addition & 20 deletions src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,8 +1333,7 @@ impl IOCompositor {
}
}

/// TODO: doc
pub fn hit_test_at_point(&self, point: DevicePoint) -> Option<CompositorHitTestResult> {
fn hit_test_at_point(&self, point: DevicePoint) -> Option<CompositorHitTestResult> {
return self
.hit_test_at_point_with_flags_and_pipeline(point, HitTestFlags::empty(), None)
.first()
Expand Down Expand Up @@ -2163,24 +2162,6 @@ impl IOCompositor {
self.webrender_api
.send_transaction(self.webrender_document, transaction);
}

/// Get webview id on the position
pub fn webview_id_on_position(
&self,
position: DevicePoint,
) -> Option<TopLevelBrowsingContextId> {
let hit_result: Option<CompositorHitTestResult> = self.hit_test_at_point(position);
if let Some(result) = hit_result {
let pipeline_id = result.pipeline_id;
for (w_id, p_id) in &self.webviews {
if *p_id == pipeline_id {
return Some(*w_id);
}
}
}

None
}
}

#[derive(Debug, PartialEq)]
Expand Down
6 changes: 4 additions & 2 deletions src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Window {
log::trace!("Verso WebView {webview_id:?} ignores this message: {message:?}")
}
EmbedderMsg::WebViewFocused(w) => {
self.close_context_menu(sender);
log::debug!(
"Verso Window {:?}'s webview {} has loaded completely.",
self.id(),
Expand Down Expand Up @@ -170,6 +171,7 @@ impl Window {
log::trace!("Verso Panel ignores this message: {message:?}")
}
EmbedderMsg::WebViewFocused(w) => {
self.close_context_menu(sender);
log::debug!(
"Verso Window {:?}'s panel {} has loaded completely.",
self.id(),
Expand Down Expand Up @@ -317,10 +319,10 @@ impl Window {
self.handle_context_menu_event(sender, result);
}
}
_ => log::trace!("Verso Panel isn't supporting this prompt yet"),
_ => log::trace!("Verso context menu isn't supporting this prompt yet"),
},
e => {
log::trace!("Verso Panel isn't supporting this message yet: {e:?}")
log::trace!("Verso context menu isn't supporting this message yet: {e:?}")
}
}
false
Expand Down
67 changes: 12 additions & 55 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,37 +292,19 @@ impl Window {

#[cfg(linux)]
{
let is_click_on_context_menu =
self.is_position_on_context_menu(compositor, position);

if !is_click_on_context_menu {
if *state == ElementState::Pressed {
match *button {
winit::event::MouseButton::Left => {
if self.close_context_menu(sender) {
// return here to bypass following mouse event for underlying element
return;
}
}
winit::event::MouseButton::Right => {
// Close old context menu
self.close_context_menu(sender);
// Create new context menu
self.context_menu = Some(self.show_context_menu(sender));
return;
}
_ => {}
match (state, button) {
(ElementState::Pressed, winit::event::MouseButton::Right) => {
if self.context_menu.is_none() {
self.context_menu = Some(self.show_context_menu(sender));
return;
}
} else if *state == ElementState::Released {
match *button {
winit::event::MouseButton::Right => {
if self.context_menu.is_some() {
return;
}
}
_ => {}
}
(ElementState::Released, winit::event::MouseButton::Right) => {
if self.context_menu.is_some() {
return;
}
}
_ => {}
}
// TODO(context-menu): ignore first release event after context menu open or close to prevent click on background element
}
Expand Down Expand Up @@ -699,40 +681,15 @@ impl Window {
context_menu
}

/// Close the context menu
///
/// If context menu exists, return true.
/// Close window's context menu
#[cfg(linux)]
pub(crate) fn close_context_menu(&self, sender: &Sender<ConstellationMsg>) -> bool {
pub(crate) fn close_context_menu(&self, sender: &Sender<ConstellationMsg>) {
if let Some(context_menu) = &self.context_menu {
send_to_constellation(
sender,
ConstellationMsg::CloseWebView(context_menu.webview().webview_id),
);
return true;
}
false
}

#[cfg(linux)]
fn is_position_on_context_menu(
&self,
compositor: &mut IOCompositor,
position: DevicePoint,
) -> bool {
if let Some(webview_id) = compositor.webview_id_on_position(position) {
return self
.context_menu
.as_ref()
.and_then(|context_menu| {
if context_menu.webview().webview_id == webview_id {
return Some(true);
}
None
})
.unwrap_or(false);
}
false
}

/// Handle linux context menu event
Expand Down

0 comments on commit 8d8f29a

Please sign in to comment.