Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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/4110.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Row numbers scrolled out of view when columns were scrolled sideways.
4 changes: 4 additions & 0 deletions linux/crates/app/src/test_support/key_injection_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ pub(crate) enum KeyInjectionError {
Connection(#[from] x11rb::errors::ConnectionError),
#[error("the X server refused a request: {0}")]
Reply(#[from] x11rb::errors::ReplyError),
#[error("could not allocate an X resource: {0}")]
Resource(#[from] x11rb::errors::ReplyOrIdError),
#[error("no keycode carries keysym {0:#x}")]
KeysymNotMapped(u32),
#[error("pointer target ({x}, {y}) is outside the {width} by {height} X screen")]
PointerOutsideScreen { x: i16, y: i16, width: u16, height: u16 },
#[error("the widget is not on screen")]
NotShown,
/// XTEST reaches GDK only through an X11 display; set GDK_BACKEND=x11.
Expand Down
131 changes: 118 additions & 13 deletions linux/crates/app/src/test_support/key_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use gtk4::prelude::*;
use gtk4::{gdk, graphene};
use x11rb::connection::Connection;
use x11rb::protocol::xproto::{
BUTTON_PRESS_EVENT, BUTTON_RELEASE_EVENT, ConnectionExt as _, KEY_PRESS_EVENT, KEY_RELEASE_EVENT,
MOTION_NOTIFY_EVENT,
BUTTON_PRESS_EVENT, BUTTON_RELEASE_EVENT, ConnectionExt as _, CreateWindowAux, KEY_PRESS_EVENT, KEY_RELEASE_EVENT,
MOTION_NOTIFY_EVENT, Window, WindowClass,
};
use x11rb::protocol::xtest::ConnectionExt as _;
use x11rb::rust_connection::RustConnection;
Expand Down Expand Up @@ -89,15 +89,101 @@ impl KeyInjector {
Ok(())
}

/// Click the primary button `count` times at `at` in `widget`'s own
/// coordinates, in the window that has the keyboard. A widget not laid
/// out yet sits at the window's corner, so it is refused.
pub(crate) fn click(
&self,
widget: &impl IsA<gtk4::Widget>,
at: (f32, f32),
count: u8,
) -> Result<(), KeyInjectionError> {
self.button(widget, at, 1, count)
}

pub(crate) fn button(
&self,
widget: &impl IsA<gtk4::Widget>,
at: (f32, f32),
button: u8,
count: u8,
) -> Result<(), KeyInjectionError> {
self.move_pointer(widget, at)?;
for _ in 0..count {
self.fake(BUTTON_PRESS_EVENT, button)?;
self.fake(BUTTON_RELEASE_EVENT, button)?;
}
self.connection.flush()?;
self.connection.get_input_focus()?.reply()?;
Ok(())
}

pub(crate) fn drag(
&self,
widget: &impl IsA<gtk4::Widget>,
from: (f32, f32),
to: (f32, f32),
) -> Result<(), KeyInjectionError> {
self.move_pointer(widget, from)?;
self.fake(BUTTON_PRESS_EVENT, 1)?;
self.connection.flush()?;
self.connection.get_input_focus()?.reply()?;
super::drain_main_context();
for step in 1..=10 {
let fraction = step as f32 / 10.0;
let point = (from.0 + (to.0 - from.0) * fraction, from.1 + (to.1 - from.1) * fraction);
self.move_pointer(widget, point)?;
self.connection.flush()?;
self.connection.get_input_focus()?.reply()?;
super::drain_main_context();
}
self.fake(BUTTON_RELEASE_EVENT, 1)?;
self.connection.flush()?;
self.connection.get_input_focus()?.reply()?;
Ok(())
}

pub(crate) fn hover(&self, widget: &impl IsA<gtk4::Widget>, at: (f32, f32)) -> Result<(), KeyInjectionError> {
self.move_pointer(widget, at)?;
self.connection.flush()?;
self.connection.get_input_focus()?.reply()?;
Ok(())
}

pub(crate) fn cover_pointer(&self) -> Result<PointerCover<'_>, KeyInjectionError> {
let root = self
.connection
.setup()
.roots
.first()
.map(|screen| screen.root)
.ok_or(KeyInjectionError::NotShown)?;
let pointer = self.connection.query_pointer(root)?.reply()?;
let window = self.connection.generate_id()?;
// A native cover triggers crossing events without pointer motion or a keyboard grab.
self.connection
.create_window(
0,
window,
root,
pointer.root_x.saturating_sub(8),
pointer.root_y.saturating_sub(8),
16,
16,
0,
WindowClass::INPUT_ONLY,
0,
&CreateWindowAux::new().override_redirect(1),
)?
.check()?;
let cover = PointerCover { keys: self, window };
self.connection.map_window(window)?.check()?;
Ok(cover)
}

fn uncover_pointer(&self, window: Window) -> Result<(), KeyInjectionError> {
self.connection.destroy_window(window)?.check()?;
Ok(())
}

fn move_pointer(&self, widget: &impl IsA<gtk4::Widget>, at: (f32, f32)) -> Result<(), KeyInjectionError> {
let widget = widget.as_ref();
if !widget.is_mapped() || widget.width() <= 0 {
return Err(KeyInjectionError::NotShown);
Expand All @@ -109,25 +195,31 @@ impl KeyInjector {
let (left, top) = native.surface_transform();
let scale = f64::from(native.surface().map_or(1, |surface| surface.scale_factor()));
let window = self.connection.get_input_focus()?.reply()?.focus;
let root = self
let screen = self
.connection
.setup()
.roots
.first()
.map(|screen| screen.root)
.ok_or(KeyInjectionError::NotShown)?;
let root = screen.root;
let origin = self.connection.translate_coordinates(window, root, 0, 0)?.reply()?;
let on_root = |origin: i16, logical: f64| origin.saturating_add((logical * scale).round() as i16);
let x = on_root(origin.dst_x, f64::from(point.x()) + left);
let y = on_root(origin.dst_y, f64::from(point.y()) + top);
if x < 0
|| y < 0
|| i32::from(x) >= i32::from(screen.width_in_pixels)
|| i32::from(y) >= i32::from(screen.height_in_pixels)
{
return Err(KeyInjectionError::PointerOutsideScreen {
x,
y,
width: screen.width_in_pixels,
height: screen.height_in_pixels,
});
}
self.connection
.xtest_fake_input(MOTION_NOTIFY_EVENT, 0, x11rb::CURRENT_TIME, root, x, y, 0)?;
for _ in 0..count {
self.fake(BUTTON_PRESS_EVENT, 1)?;
self.fake(BUTTON_RELEASE_EVENT, 1)?;
}
self.connection.flush()?;
self.connection.get_input_focus()?.reply()?;
Ok(())
}

Expand Down Expand Up @@ -158,6 +250,19 @@ impl KeyInjector {
}
}

pub(crate) struct PointerCover<'a> {
keys: &'a KeyInjector,
window: Window,
}

impl Drop for PointerCover<'_> {
fn drop(&mut self) {
if let Err(error) = self.keys.uncover_pointer(self.window) {
tracing::error!(%error, "a pointer cover was not removed");
}
}
}

pub(crate) struct Held<'a> {
keys: &'a KeyInjector,
keycode: u8,
Expand Down
37 changes: 31 additions & 6 deletions linux/crates/app/src/ui/data_grid/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,12 @@ impl DataGrid {
}
built.row_numbers = Some(row_number_column::build(self));
let names: Vec<&str> = columns.iter().map(|column| column.name.as_str()).collect();
if let Some(column) = &built.row_numbers {
view.append_column(column);
}
for placement in built.layout.arrange(&names) {
let column = match placement {
Placement::RowNumbers => built.row_numbers.clone(),
Placement::RowNumbers => None,
Placement::Column { index, visible, width } => {
built.view_columns.get(index).cloned().inspect(|column| {
column.set_visible(visible);
Expand Down Expand Up @@ -448,14 +451,37 @@ impl DataGrid {
self.imp().columns.borrow_mut().handlers.extend(handlers);
}

/// A column moved, went or came back: the layout keeps it, and a
/// search counts only the columns on screen, in their order.
fn columns_moved(&self) {
self.keep_filler_last();
self.keep_numbers_first();
self.record_layout();
self.find_rows_changed();
}

fn keep_numbers_first(&self) {
let Some(column) = self.row_numbers_column() else {
return;
};
let view = self.column_view();
if view.columns().item(0).as_ref() == Some(column.upcast_ref()) {
return;
}
// GTK finishes moving the header after it announces the column change.
glib::idle_add_local_once(glib::clone!(
#[weak]
view,
#[weak]
column,
move || {
if column.column_view().as_ref() == Some(&view)
&& view.columns().item(0).as_ref() != Some(column.upcast_ref())
{
view.insert_column(0, &column);
}
}
));
}

// GTK drops a header dragged onto the filler after it, and lets the
// filler's own header be dragged.
fn keep_filler_last(&self) {
Expand Down Expand Up @@ -865,9 +891,8 @@ fn same_names(layout: &ColumnLayout, columns: &[GridColumn]) -> bool {
names.len() == columns.len() && names.iter().zip(columns).all(|(name, column)| *name == column.name)
}

/// The font a cell draws its value in: libadwaita's `monospace` class
/// sets only the family and size of the desktop's monospace font.
fn value_font(context: &pango::Context) -> pango::FontDescription {
// Libadwaita's monospace class overrides only family and size.
pub(super) fn value_font(context: &pango::Context) -> pango::FontDescription {
let mut monospace = pango::FontDescription::from_string(&adw::StyleManager::default().monospace_font_name());
monospace.unset_fields(!(pango::FontMask::FAMILY | pango::FontMask::SIZE));
let mut font = context.font_description().unwrap_or_default();
Expand Down
6 changes: 6 additions & 0 deletions linux/crates/app/src/ui/data_grid/edit/cell_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ impl EditController {
}
}

pub(crate) fn queue_editor_allocate(&self) {
if let Some(cell) = self.open_cell() {
cell.queue_allocate();
}
}

pub fn cancel_open_edit(&self) {
let held = self.editor_has_focus();
let cell = self.open_cell();
Expand Down
75 changes: 67 additions & 8 deletions linux/crates/app/src/ui/data_grid/grid_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ mod imp {
type ParentType = gtk::Widget;

fn class_init(klass: &mut Self::Class) {
klass.set_layout_manager_type::<gtk::BinLayout>();
klass.set_css_name("tp-grid-cell");
}
}
Expand All @@ -125,6 +124,42 @@ mod imp {
}

impl WidgetImpl for GridCell {
fn measure(&self, orientation: gtk::Orientation, for_size: i32) -> (i32, i32, i32, i32) {
let mut size = (0, 0, -1, -1);
let mut child = self.obj().first_child();
while let Some(widget) = child {
if widget.should_layout() {
let measured = widget.measure(orientation, for_size);
size = (
size.0.max(measured.0),
size.1.max(measured.1),
size.2.max(measured.2),
size.3.max(measured.3),
);
}
child = widget.next_sibling();
}
size
}

fn size_allocate(&self, width: i32, height: i32, baseline: i32) {
let cell = self.obj();
let mut child = cell.first_child();
while let Some(widget) = child {
if widget.should_layout() {
let (offset, available) = if self.editor.borrow().as_ref() == Some(&widget) {
cell.editor_space(width)
} else {
(0, width)
};
let transform =
gtk::gsk::Transform::new().translate(&gtk::graphene::Point::new(offset as f32, 0.0));
widget.allocate(available, height, baseline, Some(transform));
}
child = widget.next_sibling();
}
}

fn unroot(&self) {
let cell = self.obj();
if let Some(grid) = cell.grid() {
Expand All @@ -147,6 +182,7 @@ glib::wrapper! {
impl GridCell {
pub fn new(paint: Rc<ColumnPaint>) -> Self {
let cell: Self = glib::Object::new();
cell.set_overflow(gtk::Overflow::Hidden);
let display = gtk::Inscription::new(None);
display.set_hexpand(true);
// An ellipsized Inscription draws from its top edge, so it is kept
Expand All @@ -162,6 +198,27 @@ impl GridCell {
cell
}

fn editor_space(&self, width: i32) -> (i32, i32) {
let Some(grid) = self.grid() else {
return (0, width);
};
let Some(edge) = super::pinned_numbers::covered_edge(&grid) else {
return (0, width);
};
let Some(origin) = self.compute_point(&grid.column_view(), &gtk::graphene::Point::new(0.0, 0.0)) else {
return (0, width);
};
// GTK picks descendants before their parent's contains method. Allocate
// the editor in the uncovered part so its native gestures cannot reach the gutter.
match grid.column_view().direction() {
gtk::TextDirection::Rtl => (0, ((edge - origin.x()).floor() as i32).clamp(0, width)),
_ => {
let offset = ((edge - origin.x()).ceil() as i32).clamp(0, width);
(offset, width - offset)
}
}
}

pub fn attach(&self, list_item: &gtk::ColumnViewCell) {
self.imp().list_item.set(Some(list_item));
}
Expand Down Expand Up @@ -287,7 +344,6 @@ impl GridCell {
}
}

/// Lay `editor` over the cell's text until `close_editor`.
pub(crate) fn open_editor(&self, editor: &impl IsA<gtk::Widget>) {
self.close_editor();
if let Some(closed) = self.imp().closed.take() {
Expand All @@ -296,14 +352,17 @@ impl GridCell {
if let Some(display) = self.display() {
display.set_visible(false);
}
editor.as_ref().set_parent(self);
self.imp().editor.replace(Some(editor.as_ref().clone()));
// A viewport clips a fully covered editor without allocating GtkText
// below its minimum size or changing its text and cursor state.
let viewport = gtk::Viewport::new(None::<&gtk::Adjustment>, None::<&gtk::Adjustment>);
viewport.set_child(Some(editor));
viewport.set_scroll_to_focus(true);
viewport.set_parent(self);
self.imp().editor.replace(Some(viewport.upcast()));
}

/// The editor goes and the text shows again. GTK 4.22 loops forever
/// ending a focus change from a widget that lost its parent, so an
/// editor that still holds the focus is only hidden until the next
/// edit.
// GTK 4.22 loops when focus leaves a widget that lost its parent.
// Keep an editor that holds focus parented until the next edit.
pub(crate) fn close_editor(&self) {
if let Some(editor) = self.imp().editor.take() {
match holds_window_focus(&editor) {
Expand Down
Loading
Loading