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/4126.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The header has Save Changes for table edits, table structure and editable query results.
2 changes: 2 additions & 0 deletions linux/crates/app/src/ui/browse_tab/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct BrowseTab {
/// The tab's one grid, reachable while an input is being handled
/// and the model is borrowed.
pub(super) grid: OnceCell<DataGrid>,
pub(super) save_action: OnceCell<gio::SimpleAction>,
pub(super) browse_actions: OnceCell<gtk::gio::SimpleActionGroup>,
pub(super) structure_requested: Cell<bool>,
pub(super) model: RefCell<Option<BrowseModel>>,
Expand Down Expand Up @@ -124,6 +125,7 @@ impl ObjectImpl for BrowseTab {
.build(),
Signal::builder("save-requested").build(),
Signal::builder("structure-requested").build(),
Signal::builder("save-action-changed").build(),
Signal::builder("loading-changed")
.param_types([bool::static_type()])
.build(),
Expand Down
26 changes: 20 additions & 6 deletions linux/crates/app/src/ui/browse_tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl BrowseTab {
filter_strip.set_revealed(true);
}
let actions = browse_actions::install_browse_actions(&tab, init.page_size);
let _ = tab.imp().save_action.set(actions.save.clone());
// A sort saved with the tab cannot reach an engine that reads
// in its own order.
let initial_sort = init.initial_sort.filter(|_| sorts_rows(dialect.as_deref()));
Expand Down Expand Up @@ -209,6 +210,13 @@ impl BrowseTab {
tab
}

pub(crate) fn shown_save_action(&self) -> Option<gio::SimpleAction> {
if self.shows_structure() {
return self.structure_tab()?.save_action();
}
self.imp().save_action.get().cloned()
}

/// Follow reconnects for as long as the tab is open, and read the
/// table once the tab is first shown, or at once.
fn start(&self, link_changes: tokio::sync::watch::Receiver<LinkGeneration>, first_load: FirstLoad) {
Expand Down Expand Up @@ -400,6 +408,14 @@ impl BrowseTab {
)
}

pub fn connect_save_action_changed<F: Fn(&Self) + 'static>(&self, f: F) -> glib::SignalHandlerId {
self.connect_closure(
"save-action-changed",
false,
glib::closure_local!(move |tab: &Self| f(tab)),
)
}

pub fn offer_structure(&self) {
let imp = self.imp();
imp.view_toggle.set_visible(true);
Expand All @@ -413,6 +429,7 @@ impl BrowseTab {

pub fn set_structure(&self, structure: &crate::ui::structure_tab::StructureTab) {
libadwaita::prelude::BinExt::set_child(&*self.imp().structure_host, Some(structure));
self.emit_by_name::<()>("save-action-changed", &[]);
}

pub fn structure_tab(&self) -> Option<crate::ui::structure_tab::StructureTab> {
Expand Down Expand Up @@ -442,15 +459,12 @@ impl BrowseTab {
if let Some(model) = imp.model.borrow_mut().as_mut() {
model.show_grid_actions();
}
self.emit_by_name::<()>("save-action-changed", &[]);
}

fn save_selected_view(&self) {
if self.shows_structure() {
if let Some(structure) = self.structure_tab() {
let _ = structure.activate_action("structure.save", None);
}
} else {
let _ = self.activate_action("browse.save", None);
if let Some(action) = self.shown_save_action() {
action.activate(None);
}
}

Expand Down
17 changes: 17 additions & 0 deletions linux/crates/app/src/ui/editor/result_editing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(super) struct EditTarget {
#[derive(Debug)]
struct ResultActions {
save: gio::SimpleAction,
save_changes: gio::SimpleAction,
undo: gio::SimpleAction,
redo: gio::SimpleAction,
}
Expand All @@ -75,6 +76,14 @@ pub struct ResultEditing {
}

impl SqlEditor {
pub(crate) fn save_changes_action(&self) -> Option<gio::SimpleAction> {
self.imp()
.editing
.actions
.get()
.map(|actions| actions.save_changes.clone())
}

pub(crate) fn attach_row_tracker(&self, tracker: TabTrackerHandle) {
if self.imp().editing.tracker.set(tracker).is_err() {
return;
Expand All @@ -95,6 +104,7 @@ impl SqlEditor {
};
let actions = ResultActions {
save: action("save", SqlEditor::save_rows),
save_changes: action("save-changes", SqlEditor::save_rows),
undo: action("undo", |editor| {
if let Some(grid) = editor.owner_grid() {
grid.undo();
Expand Down Expand Up @@ -421,6 +431,13 @@ impl SqlEditor {
&& tracker
.as_ref()
.is_some_and(|tracker| tracker.pending_state() != PendingState::Saving);
actions.save_changes.set_enabled(
!self.imp().editing.saving.get()
&& tracker.as_ref().is_some_and(|tracker| {
matches!(tracker.pending_state(), PendingState::Clean | PendingState::Unsaved)
&& (tracker.pending_count() > 0 || grid.as_ref().is_some_and(DataGrid::is_editing))
}),
);
actions
.undo
.set_enabled(idle && tracker.as_ref().is_some_and(TabTrackerHandle::can_undo));
Expand Down
39 changes: 39 additions & 0 deletions linux/crates/app/src/ui/editor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3276,6 +3276,45 @@ mod result_edits {
assert_eq!(names, ["id", "a"], "the renamed column writes its own column");
}

#[gtk4::test]
fn header_save_action_commits_result_inline_edits_and_follows_save_state() {
let (harness, tracker) = editing(
rows_of(vec![traced("id", "id"), traced("x", "a")], TransactionState::Idle),
"SELECT id, a AS x FROM t",
);
let _window = shown(&harness);
run_and_wait(&harness);
assert_eq!(settled(&harness), None);
let grid = shown_grid(&harness);
let action = harness.editor.save_changes_action().expect("the data save action");
assert!(!action.is_enabled(), "clean rows have nothing to save");
let mut cell = None;
wait_until(WAIT, || {
cell = grid.focus_cell(0, 1);
cell.is_some()
})
.expect("the cell");
let cell = cell.expect("the cell");
assert!(grid.edit().edit_cell(&cell));
grid.edit().open_text().expect("inline field").set_text("changed");
assert_eq!(tracker.pending_count(), 0);
assert!(action.is_enabled(), "the first inline edit enables Save Changes");
let saves = SignalLog::connect(&harness.editor, "save-rows", |_| ());

action.activate(None);

assert_eq!(tracker.pending_count(), 1);
assert_eq!(saves.count(), 1);
assert!(!cell.is_editing());
harness.editor.rows_save_started();
assert!(!action.is_enabled());
harness.editor.rows_save_failed(None);
assert!(action.is_enabled());
let _ = tracker.discard();
harness.editor.rows_discarded(false);
assert!(!action.is_enabled());
}

#[gtk4::test]
fn a_join_stays_read_only_and_says_why() {
let (harness, _) = editing(
Expand Down
3 changes: 2 additions & 1 deletion linux/crates/app/src/ui/structure_tab/imp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::{Cell, RefCell};
use std::cell::{Cell, OnceCell, RefCell};
use std::collections::VecDeque;
use std::sync::OnceLock;

Expand Down Expand Up @@ -83,6 +83,7 @@ pub struct StructureTab {
#[template_child]
pub stop_button: TemplateChild<gtk::Button>,

pub(super) save_action: OnceCell<gio::SimpleAction>,
pub(super) model: RefCell<Option<StructureModel>>,
pub(super) queue: RefCell<VecDeque<StructureTabInput>>,
pub(super) dispatching: Cell<bool>,
Expand Down
6 changes: 5 additions & 1 deletion linux/crates/app/src/ui/structure_tab/structure_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use super::{SaveInput, StructureTab};
use crate::application::{ShortcutOwner, local_shortcut_controller};

impl StructureTab {
/// Ctrl+S saves as the Save button does, and only while it could.
pub(crate) fn save_action(&self) -> Option<gio::SimpleAction> {
self.imp().save_action.get().cloned()
}

pub(super) fn install_actions(&self) {
let imp = self.imp();
let save = gio::SimpleAction::new("save", None);
Expand All @@ -18,6 +21,7 @@ impl StructureTab {
.build();
let group = gio::SimpleActionGroup::new();
group.add_action(&save);
let _ = imp.save_action.set(save);
self.insert_action_group("structure", Some(&group));
self.add_controller(local_shortcut_controller(ShortcutOwner::StructureTab));
crate::ui::a11y::announce_key_shortcuts(&imp.save_button.get(), "structure.save");
Expand Down
Loading
Loading