Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions linux/changelog.d/4149.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A long right-click menu, such as a table's in the sidebar, was cut short with a scrollbar in the middle of the screen.
83 changes: 83 additions & 0 deletions linux/crates/app/src/ui/context_menu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use gtk4 as gtk;
use gtk4::prelude::*;
use gtk4::{gdk, gio};

// GtkPopover slides a menu along the screen edge only when it opens to a side. Opened above or
// below, a menu taller than the room on either side of the pointer is shrunk and scrolls.
pub(crate) fn context_menu(model: Option<&impl IsA<gio::MenuModel>>) -> gtk::PopoverMenu {
let menu = gtk::PopoverMenu::from_model(model);
menu.set_has_arrow(false);
menu.set_position(trailing_side(menu.direction()));
menu.set_valign(gtk::Align::Start);
menu
}

// A key points the menu at a whole row or cell, and the menu opens beside its trailing edge,
// which a cell wider than the view has out of sight.
pub(crate) fn popup_at(menu: &gtk::PopoverMenu, pointing: Option<gdk::Rectangle>) {
let shown = menu
.parent()
.map(|parent| gdk::Rectangle::new(0, 0, parent.width(), parent.height()));
let pointing = pointing.map(|rect| shown.map_or(rect, |shown| in_view(rect, shown)));
menu.set_pointing_to(pointing.as_ref());
menu.popup();
}

fn in_view(rect: gdk::Rectangle, shown: gdk::Rectangle) -> gdk::Rectangle {
rect.intersect(&shown).unwrap_or(rect)
}

// GTK does not mirror left and right for a right-to-left locale.
fn trailing_side(direction: gtk::TextDirection) -> gtk::PositionType {
match direction {
gtk::TextDirection::Rtl => gtk::PositionType::Left,
_ => gtk::PositionType::Right,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn the_menu_opens_on_the_side_text_runs_toward() {
assert_eq!(trailing_side(gtk::TextDirection::Ltr), gtk::PositionType::Right);
assert_eq!(trailing_side(gtk::TextDirection::None), gtk::PositionType::Right);
assert_eq!(trailing_side(gtk::TextDirection::Rtl), gtk::PositionType::Left);
}

#[gtk4::test]
fn a_context_menu_opens_beside_the_pointer_from_its_top_corner_with_no_arrow() {
let menu = context_menu(None::<&gio::MenuModel>);

assert!(!menu.has_arrow());
assert_eq!(menu.position(), trailing_side(menu.direction()));
assert_eq!(menu.valign(), gtk::Align::Start);
}

#[gtk4::test]
fn a_menu_points_at_the_part_of_its_cell_in_view() {
let shown = gdk::Rectangle::new(0, 0, 400, 300);

assert_eq!(
in_view(gdk::Rectangle::new(250, 40, 600, 24), shown),
gdk::Rectangle::new(250, 40, 150, 24),
"a cell past the trailing edge"
);
assert_eq!(
in_view(gdk::Rectangle::new(-200, 40, 300, 24), shown),
gdk::Rectangle::new(0, 40, 100, 24),
"a cell scrolled past the leading edge"
);
assert_eq!(
in_view(gdk::Rectangle::new(40, 60, 1, 1), shown),
gdk::Rectangle::new(40, 60, 1, 1),
"a pointer"
);
assert_eq!(
in_view(gdk::Rectangle::new(500, 40, 100, 24), shown),
gdk::Rectangle::new(500, 40, 100, 24),
"a cell out of view keeps its place"
);
}
}
7 changes: 2 additions & 5 deletions linux/crates/app/src/ui/data_grid/grid_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ impl GridMenus {
}

fn popover(grid: &DataGrid, model: &gio::Menu) -> gtk::PopoverMenu {
let popover = gtk::PopoverMenu::from_model(Some(model));
popover.set_has_arrow(false);
popover.set_halign(gtk::Align::Start);
let popover = crate::ui::context_menu::context_menu(Some(model));
popover.set_parent(grid);
let weak = grid.downgrade();
popover.connect_closed(move |_| {
Expand Down Expand Up @@ -343,8 +341,7 @@ impl DataGrid {
rect.height(),
))
});
popover.set_pointing_to(at.as_ref());
popover.popup();
crate::ui::context_menu::popup_at(popover, at);
}

pub(crate) fn bounds_in_view(&self, widget: &gtk::Widget) -> Option<gdk::Rectangle> {
Expand Down
22 changes: 22 additions & 0 deletions linux/crates/app/src/ui/data_grid/tests/menus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,28 @@ fn shift_f10_opens_menu_anchored_to_focused_cell() {
);
}

#[gtk4::test]
fn the_keyboard_menu_of_a_cell_wider_than_the_grid_opens_beside_its_part_in_view() {
let grid = two_text_rows();
let wide = grid.grid.width() * 3;
grid.grid
.data_view_column(1)
.expect("the name column")
.set_fixed_width(wide);
let cell = grid.focus(1, 1);
wait_until(WAIT, || cell.width() > grid.grid.width()).expect("the cell is wider than the grid");

assert!(press(&grid, gdk::Key::F10, gdk::ModifierType::SHIFT_MASK));

let (pointed, rect) = shown_menu(&grid).pointing_to();
assert!(pointed);
assert!(
rect.x() >= 0 && rect.x() + rect.width() <= grid.grid.width(),
"the menu points past the grid at {rect:?}"
);
close_menus(&grid);
}

#[gtk4::test]
fn a_result_grid_opens_its_read_only_menu_from_the_keyboard() {
let grid = Grid::build(
Expand Down
6 changes: 2 additions & 4 deletions linux/crates/app/src/ui/diagram/canvas_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,12 @@ pub(super) fn popup_menu(canvas: &DiagramCanvas, at: gdk::Rectangle) {
.popover
.borrow_mut()
.get_or_insert_with(|| {
let popover = gtk::PopoverMenu::from_model(Some(&table_menu()));
popover.set_has_arrow(true);
let popover = crate::ui::context_menu::context_menu(Some(&table_menu()));
popover.set_parent(canvas);
popover
})
.clone();
popover.set_pointing_to(Some(&at));
popover.popup();
crate::ui::context_menu::popup_at(&popover, Some(at));
}

fn table_menu() -> gio::Menu {
Expand Down
6 changes: 2 additions & 4 deletions linux/crates/app/src/ui/history_dialog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl HistoryDialog {
let entries = gio::ListStore::new::<HistoryEntryObject>();
let sorted = gtk::SortListModel::new(Some(entries.clone()), None::<gtk::Sorter>);
sorted.set_section_sorter(Some(&pinned_first()));
let menu = gtk::PopoverMenu::from_model(None::<&gio::MenuModel>);
menu.set_has_arrow(true);
let menu = crate::ui::context_menu::context_menu(None::<&gio::MenuModel>);
// Parented now: a popover menu finds its actions through the
// parent it had when it was set up.
menu.set_parent(&imp.list_view.get());
Expand Down Expand Up @@ -329,8 +328,7 @@ impl HistoryDialog {
}),
};
parts.menu.set_menu_model(Some(&row_menu(&entry)));
parts.menu.set_pointing_to(pointing.as_ref());
parts.menu.popup();
crate::ui::context_menu::popup_at(&parts.menu, pointing);
}

fn popup_focused_row_menu(&self) {
Expand Down
1 change: 1 addition & 0 deletions linux/crates/app/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod combo_value;
mod connect_dialog;
mod connection_appearance;
mod connection_list_banner;
mod context_menu;
pub(crate) mod credential_prompt;
pub(crate) mod data_grid;
mod definition_dialog;
Expand Down
6 changes: 2 additions & 4 deletions linux/crates/app/src/ui/welcome_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ impl Parts {
let finding = view.downgrade();
let found_filter = gtk::CustomFilter::new(move |item| finding.upgrade().is_some_and(|view| view.finds(item)));
let found_rows = gtk::FilterListModel::new(Some(found.clone()), Some(found_filter.clone()));
let menu = gtk::PopoverMenu::from_model(Some(&library_menu()));
menu.set_has_arrow(true);
let menu = crate::ui::context_menu::context_menu(Some(&library_menu()));
Self {
list,
tasks,
Expand Down Expand Up @@ -518,8 +517,7 @@ impl WelcomeView {
let stack = self.imp().stack.get();
let at = list_view.compute_point(&stack, &graphene::Point::new(x as f32, y as f32));
let pointing = at.map(|point| gdk::Rectangle::new(point.x() as i32, point.y() as i32, 1, 1));
parts.menu.set_pointing_to(pointing.as_ref());
parts.menu.popup();
crate::ui::context_menu::popup_at(&parts.menu, pointing);
}

fn activate(&self, which: Which, position: u32) {
Expand Down
9 changes: 3 additions & 6 deletions linux/crates/app/src/ui/workspace/sidebar/connection_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ impl ConnectionTree {
let tree_pane = Pane::new(&rows, &crate::i18n::gettext("Connections"));
let found_pane = Pane::new(&results, &crate::i18n::gettext("Search Results"));
let root = pages(&tree_pane, &found_pane);
let popover = gtk::PopoverMenu::from_model(None::<&gio::MenuModel>);
popover.set_has_arrow(true);
let popover = crate::ui::context_menu::context_menu(None::<&gio::MenuModel>);
popover.set_parent(&root);
TreeInner {
root,
Expand Down Expand Up @@ -1178,8 +1177,7 @@ impl TreeInner {
return;
};
self.popover.set_menu_model(Some(&menu));
self.popover.set_pointing_to(pointing.as_ref());
self.popover.popup();
crate::ui::context_menu::popup_at(&self.popover, pointing);
}

/// The Menu key and Shift+F10: the menu of the row the keyboard is
Expand Down Expand Up @@ -1240,8 +1238,7 @@ impl TreeInner {

fn popup_library_menu(&self, pointing: Option<gdk::Rectangle>) {
self.popover.set_menu_model(Some(&crate::ui::picker::library_menu()));
self.popover.set_pointing_to(pointing.as_ref());
self.popover.popup();
crate::ui::context_menu::popup_at(&self.popover, pointing);
}

// Only a library item moves: a group, or a saved connection's own row while the list has it.
Expand Down
29 changes: 29 additions & 0 deletions linux/crates/app/src/ui/workspace/sidebar/favorites_pane_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,35 @@ fn a_secondary_click_targets_the_clicked_favorite() {
assert_eq!(requested(&actions), [("delete", 2, "Two".to_owned())]);
}

#[gtk4::test]
fn a_favorites_menu_opens_beside_the_pointer_like_every_sidebar_menu() {
let (pane, _window, _) = shown();
let list = list(&pane);
let row = find_row(&list, Key::Query(1)).expect("first row");
let bounds = row.compute_bounds(&list).expect("row bounds");
let controllers = list.observe_controllers();
let click = (0..controllers.n_items())
.filter_map(|position| controllers.item(position).and_downcast::<gtk::GestureClick>())
.find(|click| click.button() == gtk::gdk::BUTTON_SECONDARY)
.expect("secondary click");
let (x, y) = (bounds.x() + 4.0, bounds.y() + 4.0);

click.emit_by_name::<()>("pressed", &[&1i32, &f64::from(x), &f64::from(y)]);

assert!(pane.menu.is_visible());
assert!(matches!(
pane.menu.position(),
gtk::PositionType::Left | gtk::PositionType::Right
));
assert_eq!(pane.menu.valign(), gtk::Align::Start);
assert!(!pane.menu.has_arrow());
assert_eq!(
pane.menu.pointing_to(),
(true, gtk::gdk::Rectangle::new(x as i32, y as i32, 1, 1))
);
pane.menu.popdown();
}

#[gtk4::test]
fn reordered_menu_targets_keep_their_id_and_removed_targets_do_nothing() {
let (pane, _window, actions) = shown();
Expand Down
9 changes: 3 additions & 6 deletions linux/crates/app/src/ui/workspace/sidebar/favorites_rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ pub(super) fn install_menu(
actions.add_action(&action);
}
list.insert_action_group("favorite", Some(&actions));
let menu = gtk::PopoverMenu::from_model(None::<&gio::MenuModel>);
menu.set_has_arrow(false);
let menu = crate::ui::context_menu::context_menu(None::<&gio::MenuModel>);
menu.set_parent(list);
let click = gtk::GestureClick::builder().button(gtk::gdk::BUTTON_SECONDARY).build();
click.connect_pressed({
Expand All @@ -203,8 +202,7 @@ pub(super) fn install_menu(
};
gesture.set_state(gtk::EventSequenceState::Claimed);
menu.set_menu_model(Some(&menu_of(key)));
menu.set_pointing_to(Some(&gtk::gdk::Rectangle::new(x as i32, y as i32, 1, 1)));
menu.popup();
crate::ui::context_menu::popup_at(&menu, Some(gtk::gdk::Rectangle::new(x as i32, y as i32, 1, 1)));
}
});
list.add_controller(click);
Expand Down Expand Up @@ -273,8 +271,7 @@ fn install_keys(
)
});
menu.set_menu_model(Some(&menu_of(row_key)));
menu.set_pointing_to(anchor.as_ref());
menu.popup();
crate::ui::context_menu::popup_at(&menu, anchor);
return glib::Propagation::Stop;
}
if !modifiers.is_empty() {
Expand Down
32 changes: 32 additions & 0 deletions linux/crates/app/src/ui/workspace/sidebar/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,38 @@ fn the_selection_follows_the_tab_and_survives_a_new_list() {
assert_eq!(shown.selected(), None, "no tab, yet a row selected");
}

#[gtk4::test]
fn a_rows_menu_opens_beside_the_pointer_so_a_long_one_slides_instead_of_shrinking() {
let alpha = connection("Alpha", None);
let shown = Shown::new(std::slice::from_ref(&alpha));
let (view, _) = session(&[object("public", "a")], every_affordance());
shown.open(&alpha, &view);
let pointer = gdk::Rectangle::new(40, 60, 1, 1);

shown.inner().pressed(
Which::Tree,
shown.position("a"),
gdk::BUTTON_SECONDARY,
gdk::ModifierType::empty(),
Some(pointer),
);

let popover = shown.popover();
assert!(popover.is_visible(), "the menu did not open");
assert!(
matches!(popover.position(), gtk::PositionType::Left | gtk::PositionType::Right),
"opened above or below, a long menu cannot slide"
);
assert_eq!(
popover.valign(),
gtk::Align::Start,
"the menu does not start at the pointer"
);
assert!(!popover.has_arrow(), "a context menu has no arrow");
assert_eq!(popover.pointing_to(), (true, pointer));
popover.popdown();
}

#[gtk4::test]
fn secondary_press_selects_the_row_and_opens_its_menu() {
let alpha = connection("Alpha", None);
Expand Down
1 change: 1 addition & 0 deletions linux/po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ crates/app/src/ui/connect_dialog/tests.rs
crates/app/src/ui/connect_dialog/tls_section.rs
crates/app/src/ui/connection_appearance.rs
crates/app/src/ui/connection_list_banner.rs
crates/app/src/ui/context_menu.rs
crates/app/src/ui/credential_prompt/mod.rs
crates/app/src/ui/credential_prompt/presenter.rs
crates/app/src/ui/credential_prompt/prompt_form.rs
Expand Down
Loading