Skip to content

Commit

Permalink
Tags can be removed (tkashkin#266)
Browse files Browse the repository at this point in the history
Fix tags toggling (tkashkin#268)


Former-commit-id: fa3012a
  • Loading branch information
tkashkin committed Jun 28, 2019
1 parent 5f22c35 commit c8befb3
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
49 changes: 44 additions & 5 deletions src/data/db/tables/Tags.vala
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ namespace GameHub.Data.DB.Tables
return true;
}

public static bool remove(Tag tag)
{
unowned Sqlite.Database? db = Database.instance.db;
if(db == null) return false;

Statement s;
int res = db.prepare_v2("DELETE FROM `tags` WHERE `id` = ?", -1, out s);

if(res != Sqlite.OK)
{
warning("[Database.Tags.remove] Can't prepare DELETE query (%d): %s", db.errcode(), db.errmsg());
return false;
}

res = s.bind_text(1, tag.id);

res = s.step();

if(res != Sqlite.DONE)
{
warning("[Database.Tags.remove] Error (%d): %s", db.errcode(), db.errmsg());
return false;
}

if(TAGS.contains(tag))
{
TAGS.remove(tag);
instance.tags_updated();
}

return true;
}

public class Tag: Object
{
public const string BUILTIN_PREFIX = "builtin:";
Expand Down Expand Up @@ -210,11 +243,12 @@ namespace GameHub.Data.DB.Tables
}
}

public string? id { get; construct set; }
public string? name { get; construct set; }
public string icon { get; construct set; }
public bool selected { get; construct set; default = true; }
public bool enabled { get; construct set; default = true; }
public string? id { get; construct set; }
public string? name { get; construct set; }
public string icon { get; construct set; }
public bool selected { get; construct set; default = true; }
public bool enabled { get; construct set; default = true; }
public bool removable { get { return id != null && id.has_prefix(USER_PREFIX); } }

public Tag(string? id, string? name, string icon="gh-tag-symbolic", bool selected=true)
{
Expand All @@ -233,6 +267,11 @@ namespace GameHub.Data.DB.Tables
this(USER_PREFIX + Utils.md5(name), name);
}

public bool remove()
{
return Tags.remove(this);
}

public static bool is_equal(Tag first, Tag second)
{
return first == second || first.id == second.id;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/widgets/GameTagsList.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ namespace GameHub.UI.Widgets
{
_games.add(game);
}
update();
}

construct
Expand Down Expand Up @@ -118,7 +119,6 @@ namespace GameHub.UI.Widgets
add(new_entry);

Tables.Tags.instance.tags_updated.connect(update);
update();

show_all();
}
Expand Down
40 changes: 36 additions & 4 deletions src/ui/widgets/TagRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ namespace GameHub.UI.Widgets
box.add(name);
box.add(icon);

ebox.add_events(EventMask.BUTTON_RELEASE_MASK);
ebox.button_release_event.connect(e => {
if(e.button == 1)
ebox.add_events(EventMask.BUTTON_PRESS_MASK);
ebox.button_press_event.connect(e => {
switch(e.button)
{
toggle();
case 1:
toggle();
break;

case 3:
show_context_menu(e, true);
break;
}
return true;
});
Expand Down Expand Up @@ -132,5 +138,31 @@ namespace GameHub.UI.Widgets
tag.selected = check.active;
}
}

private void show_context_menu(Event e, bool at_pointer=true)
{
var menu = new Gtk.Menu();

var remove = new Gtk.MenuItem.with_label(_("Remove"));
remove.sensitive = tag.removable;
remove.activate.connect(() => tag.remove());

menu.add(remove);

menu.show_all();

#if GTK_3_22
if(at_pointer)
{
menu.popup_at_pointer(e);
}
else
{
menu.popup_at_widget(this, Gravity.SOUTH, Gravity.NORTH, e);
}
#else
menu.popup(null, null, null, 0, ((EventButton) e).time);
#endif
}
}
}

0 comments on commit c8befb3

Please sign in to comment.