From fd4d3864e49a9cf9400a19dc2c9954806b3518b3 Mon Sep 17 00:00:00 2001 From: Huzaifa-code Date: Sun, 14 Jul 2024 17:50:38 +0530 Subject: [PATCH 1/5] FIX: Used GdkMonitor to get screen size and calculate default window size --- packages/app_center/linux/my_application.cc | 22 +++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/app_center/linux/my_application.cc b/packages/app_center/linux/my_application.cc index 6c7224433..2eea40a04 100644 --- a/packages/app_center/linux/my_application.cc +++ b/packages/app_center/linux/my_application.cc @@ -2,6 +2,7 @@ #include #include +#include #include "flutter/generated_plugin_registrant.h" @@ -34,14 +35,27 @@ static void my_application_activate(GApplication* application) { GtkWindow* window = GTK_WINDOW(hdy_application_window_new()); gtk_window_set_application(window, GTK_APPLICATION(application)); + // Get screen size using GdkMonitor + GdkDisplay* display = gdk_display_get_default(); + GdkMonitor* monitor = gdk_display_get_primary_monitor(display); + GdkRectangle monitor_geometry; + gdk_monitor_get_geometry(monitor, &monitor_geometry); + gint screen_width = monitor_geometry.width; + gint screen_height = monitor_geometry.height; + + // Calculate default window size (e.g., 80% of screen size) + gint default_width = screen_width * 0.8; + gint default_height = screen_height * 0.8; + GdkGeometry geometry; - // TODO: find better solution; set default window size based on available space - geometry.min_width = 800 + 52; // account for shadow from libhandy - geometry.min_height = 600 + 52; + // This ensures that the minimum width of the window is at least 800+52 pixels + geometry.min_width = default_width < 800 + 52 ? 800 + 52 : default_width; + geometry.min_height = default_height < 600 + 52 ? 600 + 52 : default_height; + gtk_window_set_geometry_hints(window, nullptr, &geometry, GDK_HINT_MIN_SIZE); - gtk_window_set_default_size(window, 1280 + 52, 800 + 52); + gtk_window_set_default_size(window, default_width, default_height); gtk_widget_show(GTK_WIDGET(window)); g_autoptr(FlDartProject) project = fl_dart_project_new(); From 4b89a84d794bf8aa584c9f9024e90e4d85450b2b Mon Sep 17 00:00:00 2001 From: Huzaifa-code Date: Wed, 17 Jul 2024 13:54:45 +0530 Subject: [PATCH 2/5] fix: window size in multiple monitor setup --- packages/app_center/linux/my_application.cc | 76 +++++++++++++++------ 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/packages/app_center/linux/my_application.cc b/packages/app_center/linux/my_application.cc index 2eea40a04..3123ff5f6 100644 --- a/packages/app_center/linux/my_application.cc +++ b/packages/app_center/linux/my_application.cc @@ -20,6 +20,61 @@ struct _MyApplication { G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) +// Callback function to set the window size based on the monitor it's located on +void on_window_realize(GtkWidget* widget, gpointer user_data) { + GdkWindow* gdk_window = gtk_widget_get_window(widget); + if (gdk_window == nullptr) { + return; + } + + GdkDisplay* display = gdk_window_get_display(gdk_window); + GdkMonitor* monitor = gdk_display_get_monitor_at_window(display, gdk_window); + + if (monitor == nullptr) { + return; + } + + GdkRectangle monitor_geometry; + gdk_monitor_get_geometry(monitor, &monitor_geometry); + gint screen_width = monitor_geometry.width; + gint screen_height = monitor_geometry.height; + + // Get the scale factor for high-DPI displays + gint scale_factor = gdk_monitor_get_scale_factor(monitor); + + // Predefined sizes + const gint min_width = 800 + 52; + const gint min_height = 600 + 52; + const gint max_width = 1280 + 52; + const gint max_height = 800 + 52; + + // Determine default window size based on screen size + gint default_width = screen_width/scale_factor; + gint default_height = screen_height/scale_factor; + + + // Ensure the window size is within the predefined sizes + if (default_width < min_width) { + default_width = min_width; + } else if (default_width > max_width) { + default_width = max_width; + } + + if (default_height < min_height) { + default_height = min_height; + } else if (default_height > max_height) { + default_height = max_height; + } + + GdkGeometry geometry; + geometry.min_width = min_width; + geometry.min_height = min_height; + + gtk_window_set_geometry_hints(GTK_WINDOW(widget), nullptr, &geometry, GDK_HINT_MIN_SIZE); + gtk_window_set_default_size(GTK_WINDOW(widget), default_width, default_height); +} + + // Implements GApplication::activate. static void my_application_activate(GApplication* application) { MyApplication* self = MY_APPLICATION(application); @@ -35,27 +90,10 @@ static void my_application_activate(GApplication* application) { GtkWindow* window = GTK_WINDOW(hdy_application_window_new()); gtk_window_set_application(window, GTK_APPLICATION(application)); - // Get screen size using GdkMonitor - GdkDisplay* display = gdk_display_get_default(); - GdkMonitor* monitor = gdk_display_get_primary_monitor(display); - GdkRectangle monitor_geometry; - gdk_monitor_get_geometry(monitor, &monitor_geometry); - gint screen_width = monitor_geometry.width; - gint screen_height = monitor_geometry.height; - - // Calculate default window size (e.g., 80% of screen size) - gint default_width = screen_width * 0.8; - gint default_height = screen_height * 0.8; - - GdkGeometry geometry; - - // This ensures that the minimum width of the window is at least 800+52 pixels - geometry.min_width = default_width < 800 + 52 ? 800 + 52 : default_width; - geometry.min_height = default_height < 600 + 52 ? 600 + 52 : default_height; + // Connect to the "realize" signal to get the monitor size after the window is realized + g_signal_connect(window, "realize", G_CALLBACK(on_window_realize), nullptr); - gtk_window_set_geometry_hints(window, nullptr, &geometry, GDK_HINT_MIN_SIZE); - gtk_window_set_default_size(window, default_width, default_height); gtk_widget_show(GTK_WIDGET(window)); g_autoptr(FlDartProject) project = fl_dart_project_new(); From 3f62fcee671353af4443284b45f76ec0e8214a9b Mon Sep 17 00:00:00 2001 From: Huzaifa-code Date: Wed, 17 Jul 2024 23:50:33 +0530 Subject: [PATCH 3/5] fix: getting default screen size of current active monitor not primary --- packages/app_center/linux/my_application.cc | 45 ++++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/packages/app_center/linux/my_application.cc b/packages/app_center/linux/my_application.cc index 3123ff5f6..f4b5493bd 100644 --- a/packages/app_center/linux/my_application.cc +++ b/packages/app_center/linux/my_application.cc @@ -22,55 +22,60 @@ G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) // Callback function to set the window size based on the monitor it's located on void on_window_realize(GtkWidget* widget, gpointer user_data) { + GdkRectangle monitor_geometry; GdkWindow* gdk_window = gtk_widget_get_window(widget); if (gdk_window == nullptr) { return; } GdkDisplay* display = gdk_window_get_display(gdk_window); - GdkMonitor* monitor = gdk_display_get_monitor_at_window(display, gdk_window); + GdkSeat* seat = gdk_display_get_default_seat(display); + GdkDevice* pointer = gdk_seat_get_pointer(seat); + + // Get the current cursor position + int x, y; + gdk_device_get_position(pointer, nullptr, &x, &y); + + // Get the monitor at the cursor position + GdkMonitor* monitor = gdk_display_get_monitor_at_point(display, x, y); if (monitor == nullptr) { return; } - GdkRectangle monitor_geometry; gdk_monitor_get_geometry(monitor, &monitor_geometry); gint screen_width = monitor_geometry.width; gint screen_height = monitor_geometry.height; - // Get the scale factor for high-DPI displays - gint scale_factor = gdk_monitor_get_scale_factor(monitor); - // Predefined sizes - const gint min_width = 800 + 52; - const gint min_height = 600 + 52; - const gint max_width = 1280 + 52; - const gint max_height = 800 + 52; + const gint min_width = 800; + const gint min_height = 600; + const gint max_width = 1280; + const gint max_height = 800; // Determine default window size based on screen size - gint default_width = screen_width/scale_factor; - gint default_height = screen_height/scale_factor; + gint default_width = screen_width; + gint default_height = screen_height; + g_print("Default width: %d, Default height: %d\n", default_width, default_height); - // Ensure the window size is within the predefined sizes - if (default_width < min_width) { - default_width = min_width; - } else if (default_width > max_width) { - default_width = max_width; - } - if (default_height < min_height) { + if (screen_width <= max_width || screen_height <= max_height) { + default_width = min_width; default_height = min_height; - } else if (default_height > max_height) { + } else { + default_width = max_width; default_height = max_height; } + g_print("Default width: %d, Default height: %d\n", default_width, default_height); + + GdkGeometry geometry; geometry.min_width = min_width; geometry.min_height = min_height; - gtk_window_set_geometry_hints(GTK_WINDOW(widget), nullptr, &geometry, GDK_HINT_MIN_SIZE); + // gtk_window_set_geometry_hints(GTK_WINDOW(widget), nullptr, &geometry, GDK_HINT_MIN_SIZE); gtk_window_set_default_size(GTK_WINDOW(widget), default_width, default_height); } From dc604cc77f6425abd8754af421dd2fa546463925 Mon Sep 17 00:00:00 2001 From: Huzaifa-code Date: Thu, 18 Jul 2024 13:19:33 +0530 Subject: [PATCH 4/5] fix: fullscreen issue in external monitor --- packages/app_center/linux/my_application.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/app_center/linux/my_application.cc b/packages/app_center/linux/my_application.cc index f4b5493bd..5cbe62be0 100644 --- a/packages/app_center/linux/my_application.cc +++ b/packages/app_center/linux/my_application.cc @@ -50,8 +50,8 @@ void on_window_realize(GtkWidget* widget, gpointer user_data) { // Predefined sizes const gint min_width = 800; const gint min_height = 600; - const gint max_width = 1280; - const gint max_height = 800; + const gint max_width = 1080; + const gint max_height = 700; // Determine default window size based on screen size gint default_width = screen_width; From dd1e152f3ea1a1b445e69a7ff0355b3e675ee84f Mon Sep 17 00:00:00 2001 From: Huzaifa-code Date: Thu, 18 Jul 2024 13:54:39 +0530 Subject: [PATCH 5/5] fix: fullscreen issue in external monitor --- packages/app_center/linux/my_application.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/app_center/linux/my_application.cc b/packages/app_center/linux/my_application.cc index 5cbe62be0..5f5a13b5c 100644 --- a/packages/app_center/linux/my_application.cc +++ b/packages/app_center/linux/my_application.cc @@ -60,7 +60,7 @@ void on_window_realize(GtkWidget* widget, gpointer user_data) { g_print("Default width: %d, Default height: %d\n", default_width, default_height); - if (screen_width <= max_width || screen_height <= max_height) { + if (screen_width <= 1440 ) { default_width = min_width; default_height = min_height; } else {