Move top level window to a specific monitor

I have a laptop and a monitor connected to it. some times window is showing up on laptop screen and some times on monitor screen.

is there a programmatic way to ensure that the window always open on a particular screen ?

Here is the sample code I have :

#include<gtk/gtk.h>

GtkWidget *main_window;

static void move_window_to_biggest_monitor()
{
	GdkDisplay *display = gdk_display_get_default();
	GListModel *list = gdk_display_get_monitors( display );
	guint size = g_list_model_get_n_items( list );
	int monitor_width = 0, monitor_height = 0, window_x = 0, window_y = 0;

	GdkMonitor *biggest_monitor = NULL;
	for( guint i=0; i<size; i++ )
	{
		GdkMonitor *temp_monitor = g_list_model_get_item( list, i );
		GdkRectangle rect;
		gdk_monitor_get_geometry( temp_monitor, &rect );
		if( monitor_width < rect.width )
		{
			monitor_width = rect.width;
			monitor_height = rect.height;
			window_x = rect.x;
			window_y = rect.y;
			biggest_monitor = temp_monitor;
		}
	}
	// how to move main_window to biggest_monitor ????
	gtk_widget_set_size_request( main_window, monitor_width, monitor_height );
}

static void activate( GtkApplication *app, gpointer user_data )
{
	main_window = gtk_application_window_new( app );
	move_window_to_biggest_monitor();
	gtk_widget_show( main_window );
}

int main( int argc, char **argv )
{
	GtkApplication *app = gtk_application_new( "move.gtk", G_APPLICATION_FLAGS_NONE );
	g_signal_connect( app, "activate", G_CALLBACK( activate ), NULL );
	int status = g_application_run( G_APPLICATION( app ), argc, argv );
	g_object_unref( app );
	return status;
}

Is there any API in GTK4 to move the window to a specific monitor ?

There is not, it is not up to the application to decide where it should be put but it is up to the compositor to decide so.

Could you please let me know what is this compositor ? Is there a way to achieve the objective ?

A compositor is the system-level component that is responsible for presenting all windows to the user’s display.

No, there is no way to do that from an application’s perspective.

At most, an application can ask the compositor to full screen a window on a specific monitor; but moving windows programmatically is not allowed.

static void move_window_to_biggest_monitor()
{
	GdkDisplay *display = gdk_display_get_default();
	GListModel *list = gdk_display_get_monitors( display );
	guint size = g_list_model_get_n_items( list );
	int monitor_width = 0, monitor_height = 0, window_x = 0, window_y = 0;

	GdkMonitor *biggest_monitor = NULL;
	for( guint i=0; i<size; i++ )
	{
		GdkMonitor *temp_monitor = g_list_model_get_item( list, i );
		GdkRectangle rect;
		gdk_monitor_get_geometry( temp_monitor, &rect );
		if( monitor_width < rect.width )
		{
			monitor_width = rect.width;
			monitor_height = rect.height;
			window_x = rect.x;
			window_y = rect.y;
			biggest_monitor = temp_monitor;
		}
	}
	gtk_window_fullscreen_on_monitor( (GtkWindow*)main_window, biggest_monitor );
	gtk_window_unfullscreen( (GtkWindow*)main_window );
	gtk_widget_set_size_request( main_window, monitor_width, monitor_height );
}

Tried multiple times. All the times, the window is on biggest _monitor. May be this is accidental.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.