Skip to content

Commit

Permalink
chore: add context menu to table view, remove popover arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
ppvan committed Mar 19, 2024
1 parent 2d5add6 commit cc4532c
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 18 deletions.
1 change: 1 addition & 0 deletions res/gtk/connection-row.blp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ template $PsequelConnectionRow: Gtk.Box {

PopoverMenu popover {
menu-model: menu;
has-arrow: false;
}

GestureClick {
Expand Down
1 change: 1 addition & 0 deletions res/gtk/datacell.blp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ template $PsequelDataCell: Adw.Bin {

PopoverMenu popover {
menu-model: menu;
has-arrow: false;
}
}

Expand Down
2 changes: 2 additions & 0 deletions res/gtk/schema-view.blp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ template $PsequelSchemaView: Adw.Bin {
autoselect: true;
};

activate => $on_tablelist_activate();

factory: BuilderListItemFactory {
resource: "/me/ppvan/psequel/gtk/table-listitem.ui";
};
Expand Down
19 changes: 4 additions & 15 deletions res/gtk/table-listitem.blp
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
using Gtk 4.0;

template ListItem {
child: Box {
orientation: horizontal;
spacing: 12;

Image {
icon-name: "table-symbolic";
}

Label {
label: bind template.item as <$PsequelTable>.name;
halign: start;
ellipsize: end;
single-line-mode: true;
}
};
child: $PsequelTableRow {
icon-name: "table-symbolic";
content: bind template.item as <$PsequelTable>.name;
};
}
48 changes: 48 additions & 0 deletions res/gtk/table-row.blp
Original file line number Diff line number Diff line change
@@ -1 +1,49 @@
using Gtk 4.0;
using Gtk 4.0;
using Adw 1;

menu menu {
section {
item {
icon: "category-search-symbolic";
label: _("_Copy");
action: "sidebar.copy";
}
}

section {
item {
label: _("_Refresh");
action: "sidebar.refresh";
}
}
}

template $PsequelTableRow: Box {
orientation: horizontal;
spacing: 12;

Image icon_image {
icon-name: bind template.icon-name;
}

Box {
Label label {
label: bind template.content;
halign: start;
ellipsize: end;
single-line-mode: true;
extra-menu: menu;
}

PopoverMenu popover {
menu-model: menu;
has-arrow: false;
}
}

GestureClick {
button: 3; // right clicked
released => $on_right_clicked();
}
}
1 change: 1 addition & 0 deletions res/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ blueprints = custom_target('blueprints',

'gtk/table-cols.blp',
'gtk/table-listitem.blp',
'gtk/table-row.blp',
'gtk/query-listitem.blp',
'gtk/view-listitem.blp',
'gtk/table-fk.blp',
Expand Down
1 change: 1 addition & 0 deletions res/psequel.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<file preprocess="xml-stripblanks">gtk/table-index.ui</file>
<file preprocess="xml-stripblanks">gtk/table-structure-view.ui</file>
<file preprocess="xml-stripblanks">gtk/view-structure-view.ui</file>
<file preprocess="xml-stripblanks">gtk/table-row.ui</file>
<file preprocess="xml-stripblanks">gtk/table-listitem.ui</file>
<file preprocess="xml-stripblanks">gtk/view-listitem.ui</file>
<file preprocess="xml-stripblanks">gtk/query-listitem.ui</file>
Expand Down
1 change: 1 addition & 0 deletions src/application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public class Application : Adw.Application {
/* register needed types, allow me to ref a template inside a template */
private static void ensure_types() {
typeof(Psequel.StyleSwitcher).ensure();
typeof(Psequel.TableRow).ensure();
typeof(Psequel.DataCell).ensure();
typeof(Psequel.ConnectionViewModel).ensure();
typeof(Psequel.SchemaView).ensure();
Expand Down
7 changes: 4 additions & 3 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ ui_sources = files([
'ui/schema/TableIndexInfo.vala',
'ui/schema/TableDataView.vala',
'ui/schema/ViewDataView.vala',
'ui/widgets/DataCell.vala',

'ui/editor/QueryEditor.vala',

'ui/widgets/DataCell.vala',
'ui/widgets/TableRow.vala',
'ui/widgets/StyleSwitcher.vala',

'ui/editor/QueryEditor.vala',
'ui/Window.vala',
'ui/PreferencesWindow.vala',

Expand Down
6 changes: 6 additions & 0 deletions src/ui/views/SchemaView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public class SchemaView : Adw.Bin {
});
}

[GtkCallback]
private void on_tablelist_activate(Gtk.ListView view, uint pos) {
stack.visible_child_name = "data-view";
// reload_btn_clicked(reload);
}

[GtkCallback]
private void logout_btn_clicked(Gtk.Button btn) {
schema_viewmodel.logout.begin();
Expand Down
84 changes: 84 additions & 0 deletions src/ui/widgets/TableRow.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Gtk;

namespace Psequel {
[GtkTemplate(ui = "/me/ppvan/psequel/gtk/table-row.ui")]
class TableRow : Gtk.Box {
public string content { get; set; default = ""; }
public string icon_name { get; set; default = ""; }

private TableDataViewModel tabledata_viewmodel { get; set; }


const ActionEntry[] ACTION_ENTRIES = {
{ "copy", on_row_copy },
{ "refresh", on_row_refresh },
};

public TableRow() {
Object();
}

construct {
var action_group = new SimpleActionGroup();
action_group.add_action_entries(ACTION_ENTRIES, this);
this.insert_action_group("sidebar", action_group);
tabledata_viewmodel = autowire <TableDataViewModel> ();
}


[GtkCallback]
public void on_right_clicked() {
popover.popup();
}

// [GtkAction]
private void on_row_copy() {
clipboard_push(this.content);

var window = get_parrent_window(this);
Adw.Toast toast = new Adw.Toast("Table name copied") {
timeout = 1,
};
window.add_toast(toast);
}

private void on_row_refresh() {
// General idea: Loop throught the single selection model
// Assume the content of the label item is the same as the model-item.name
// and the list view is a single selection.

var listview_type = GLib.Type.from_name("GtkListView");
Gtk.ListView view = this.get_ancestor(listview_type) as Gtk.ListView;
Gtk.SingleSelection model = view.model as Gtk.SingleSelection;

uint n = model.get_n_items();

for (uint i = 0; i < n; i++) {
Object obj = model.model.get_item(i);
Value v = {};
obj.get_property("name", ref v);
string key = v.get_string();

if (key == this.content) {
model.select_item(i, true);
view.activate(i);
break;
}
}



// view.activate();
}

private void clipboard_push(string text) {
var primary = Gdk.Display.get_default();
var clipboard = primary.get_clipboard();

clipboard.set_text(text);
}

[GtkChild]
private unowned Gtk.Popover popover;
}
}

0 comments on commit cc4532c

Please sign in to comment.