Skip to content

Commit

Permalink
scroll the main window to a newly added torrent (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefantalpalaru authored Jun 3, 2023
1 parent 6afea2c commit 8d9b138
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### GTK+ Client
- Stop eating the last message at each refresh of the Message Log
- UI: better visual torrent separation
- Scroll the main window to a newly added torrent

### Qt Client
- Fix download percentage bar glitch ([#3150](https://github.com/transmission/transmission/issues/3150))
Expand Down
6 changes: 3 additions & 3 deletions gtk/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ static void on_core_busy(TrCore* core UNUSED, gboolean busy, struct cbdata* c)
}

static void on_core_error(TrCore*, guint, char const*, struct cbdata*);
static void on_add_torrent(TrCore*, tr_ctor*, gpointer);
static void on_add_prompt(TrCore*, tr_ctor*, gpointer);
static void on_prefs_changed(TrCore* core, tr_quark const key, gpointer);
static void main_window_setup(struct cbdata* cbdata, GtkWindow* wind);
static gboolean update_model_loop(gpointer gdata);
Expand All @@ -711,7 +711,7 @@ static void app_setup(GtkWindow* wind, struct cbdata* cbdata)
/* set up core handlers */
g_signal_connect(cbdata->core, "busy", G_CALLBACK(on_core_busy), cbdata);
g_signal_connect(cbdata->core, "add-error", G_CALLBACK(on_core_error), cbdata);
g_signal_connect(cbdata->core, "add-prompt", G_CALLBACK(on_add_torrent), cbdata);
g_signal_connect(cbdata->core, "add-prompt", G_CALLBACK(on_add_prompt), cbdata);
g_signal_connect(cbdata->core, "prefs-changed", G_CALLBACK(on_prefs_changed), cbdata);

/* add torrents from command-line and saved state */
Expand Down Expand Up @@ -1084,7 +1084,7 @@ static gboolean on_main_window_focus_in(GtkWidget* widget UNUSED, GdkEventFocus*
return FALSE;
}

static void on_add_torrent(TrCore* core, tr_ctor* ctor, gpointer gdata)
static void on_add_prompt(TrCore* core, tr_ctor* ctor, gpointer gdata)
{
struct cbdata* cbdata = gdata;
GtkWidget* w = gtr_torrent_options_dialog_new(cbdata->wind, core, ctor);
Expand Down
26 changes: 17 additions & 9 deletions gtk/tr-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ enum
BUSY_SIGNAL,
PORT_SIGNAL,
PREFS_SIGNAL,
TORRENT_ADDED_SIGNAL,
LAST_SIGNAL
};

Expand Down Expand Up @@ -138,6 +139,9 @@ static void tr_core_class_init(TrCoreClass* core_class)

signals[PREFS_SIGNAL] = g_signal_new("prefs-changed", core_type, G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET(TrCoreClass,
prefs_changed), NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);

signals[TORRENT_ADDED_SIGNAL] = g_signal_new("torrent-added", core_type, G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET(TrCoreClass,
torrent_added), NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
}

static void tr_core_init(TrCore* core)
Expand Down Expand Up @@ -187,7 +191,7 @@ static void tr_core_init(TrCore* core)
**** EMIT SIGNALS
***/

static inline void core_emit_blocklist_udpated(TrCore* core, int ruleCount)
static inline void core_emit_blocklist_updated(TrCore* core, int ruleCount)
{
g_signal_emit(core, signals[BLOCKLIST_SIGNAL], 0, ruleCount);
}
Expand Down Expand Up @@ -988,7 +992,7 @@ struct metadata_callback_data
int torrent_id;
};

static gboolean find_row_from_torrent_id(GtkTreeModel* model, int id, GtkTreeIter* setme)
gboolean gtr_find_row_from_torrent_id(GtkTreeModel* model, int id, GtkTreeIter* setme)
{
GtkTreeIter iter;
gboolean match = FALSE;
Expand Down Expand Up @@ -1024,7 +1028,7 @@ static gboolean on_torrent_metadata_changed_idle(gpointer gdata)
GtkTreeIter iter;
GtkTreeModel* model = core_raw_model(data->core);

if (find_row_from_torrent_id(model, data->torrent_id, &iter))
if (gtr_find_row_from_torrent_id(model, data->torrent_id, &iter))
{
char const* collated = get_collated_name(data->core, tor);
GtkListStore* store = GTK_LIST_STORE(model);
Expand Down Expand Up @@ -1080,13 +1084,14 @@ void gtr_core_add_torrent(TrCore* core, tr_torrent* tor, gboolean do_notify)
{
if (tor != NULL)
{
GtkTreeIter unused;
GtkTreeIter iter;
tr_stat const* st = tr_torrentStat(tor);
char const* collated = get_collated_name(core, tor);
unsigned int const trackers_hash = build_torrent_trackers_hash(tor);
GtkListStore* store = GTK_LIST_STORE(core_raw_model(core));
GtkTreeModel* model = core_raw_model(core);
GtkListStore* store = GTK_LIST_STORE(model);

gtk_list_store_insert_with_values(store, &unused, 0,
gtk_list_store_insert_with_values(store, &iter, 0,
MC_NAME_COLLATED, collated,
MC_TORRENT, tor,
MC_TORRENT_ID, tr_torrentId(tor),
Expand All @@ -1108,6 +1113,9 @@ void gtr_core_add_torrent(TrCore* core, tr_torrent* tor, gboolean do_notify)
gtr_notify_torrent_added(tr_torrentName(tor));
}

// Get the main window to scroll to the newly added torrent.
g_signal_emit(core, signals[TORRENT_ADDED_SIGNAL], 0, tr_torrentId(tor));

tr_torrentSetMetadataCallback(tor, on_torrent_metadata_changed, core);
tr_torrentSetCompletenessCallback(tor, on_torrent_completeness_changed, core);
}
Expand Down Expand Up @@ -1377,7 +1385,7 @@ void gtr_core_torrent_changed(TrCore* self, int id)
GtkTreeIter iter;
GtkTreeModel* model = core_raw_model(self);

if (find_row_from_torrent_id(model, id, &iter))
if (gtr_find_row_from_torrent_id(model, id, &iter))
{
GtkTreePath* path = gtk_tree_model_get_path(model, &iter);
gtk_tree_model_row_changed(model, path, &iter);
Expand All @@ -1394,7 +1402,7 @@ void gtr_core_remove_torrent(TrCore* core, int id, gboolean delete_local_data)
GtkTreeIter iter;
GtkTreeModel* model = core_raw_model(core);

if (find_row_from_torrent_id(model, id, &iter))
if (gtr_find_row_from_torrent_id(model, id, &iter))
{
gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
}
Expand Down Expand Up @@ -1887,7 +1895,7 @@ static void on_blocklist_response(TrCore* core, tr_variant* response, gpointer d
gtr_pref_int_set(TR_KEY_blocklist_date, tr_time());
}

core_emit_blocklist_udpated(core, ruleCount);
core_emit_blocklist_updated(core, ruleCount);
}

void gtr_core_blocklist_update(TrCore* core)
Expand Down
3 changes: 3 additions & 0 deletions gtk/tr-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ typedef struct _TrCoreClass
void (* busy)(TrCore*, gboolean is_busy);
void (* prefs_changed)(TrCore*, tr_quark const key);
void (* port_tested)(TrCore*, gboolean is_open);
void (* torrent_added)(TrCore*, int);
void (* quit)(TrCore*);
}
TrCoreClass;
Expand All @@ -70,6 +71,8 @@ TrCore* gtr_core_new(tr_session*);

tr_session* gtr_core_close(TrCore*);

gboolean gtr_find_row_from_torrent_id(GtkTreeModel* model, int id, GtkTreeIter* setme);

/* Return the model used without incrementing the reference count */
GtkTreeModel* gtr_core_model(TrCore* self);

Expand Down
17 changes: 17 additions & 0 deletions gtk/tr-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef struct
GtkTreeModel* filter_model;
TrCore* core;
gulong pref_handler_id;
gulong torrent_added_handler_id;
}
PrivateData;

Expand Down Expand Up @@ -205,6 +206,7 @@ static void privateFree(gpointer vprivate)
{
PrivateData* p = vprivate;
g_signal_handler_disconnect(p->core, p->pref_handler_id);
g_signal_handler_disconnect(p->core, p->torrent_added_handler_id);
g_free(p);
}

Expand Down Expand Up @@ -548,6 +550,18 @@ static void onOptionsClicked(GtkButton* button, gpointer vp)
#endif
}

static void on_torrent_added(TrCore* core, int torrent_id, PrivateData* p)
{
GtkTreeModel* model = gtr_core_model(core);

// Find the torrent row.
GtkTreeIter iter;
gtr_find_row_from_torrent_id(model, torrent_id, &iter);

// Scroll to the newly added torrent.
gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(p->view), gtk_tree_model_get_path(model, &iter), NULL, FALSE, 0.0, 0.0);
}

/***
**** PUBLIC
***/
Expand Down Expand Up @@ -741,6 +755,9 @@ GtkWidget* gtr_window_new(GtkApplication* app, GtkUIManager* ui_mgr, TrCore* cor

tr_sessionSetAltSpeedFunc(gtr_core_session(core), onAltSpeedToggled, p);

// Window-related reactions to torrents being added.
p->torrent_added_handler_id = g_signal_connect(core, "torrent-added", G_CALLBACK(on_torrent_added), p);

gtr_window_refresh(GTK_WINDOW(self));
return self;
}
Expand Down

0 comments on commit 8d9b138

Please sign in to comment.